Moved to separate repos
This commit is contained in:
		@@ -1,30 +0,0 @@
 | 
			
		||||
# I2C FRAM driver
 | 
			
		||||
 | 
			
		||||
This library provides a driver for this FRAM part: https://www.adafruit.com/products/1895
 | 
			
		||||
 | 
			
		||||
The memory is accessed one byte at a time. The library provides a utility functions
 | 
			
		||||
to write an entire buffer.
 | 
			
		||||
 | 
			
		||||
## Reading/writing byte
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
let addr = 100
 | 
			
		||||
i2c_fram.writeByte(addr, 42)
 | 
			
		||||
let val = i2c_fram.readByte(addr)
 | 
			
		||||
console.log(`${addr}: ${val}`)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Reading/writing a buffer
 | 
			
		||||
 | 
			
		||||
This code will log current time and acceleration in X axis every second.
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
let bufSz = 8
 | 
			
		||||
for (let addr = 0; addr < 0x8000; addr += bufSz) {
 | 
			
		||||
    let buf = pins.createBuffer(bufSz)
 | 
			
		||||
    buf.setNumber(NumberFormat.Int32LE, 0, input.runningTime())
 | 
			
		||||
    buf.setNumber(NumberFormat.Int32LE, 4, input.acceleration(Dimension.X))
 | 
			
		||||
    i2c_fram.writeBuffer(addr, buf)
 | 
			
		||||
    basic.pause(1000)
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
@@ -1,55 +0,0 @@
 | 
			
		||||
namespace i2c_fram {
 | 
			
		||||
    const devaddr = 0x50;
 | 
			
		||||
    const memend = 0x7fff;
 | 
			
		||||
 | 
			
		||||
    //% shim=pxtrt::panic
 | 
			
		||||
    function panic(code: number) { }
 | 
			
		||||
 | 
			
		||||
    function die() { panic(142) }
 | 
			
		||||
 | 
			
		||||
    export function readByte(addr: number) {
 | 
			
		||||
        if (addr < 0 || addr > memend)
 | 
			
		||||
            die();
 | 
			
		||||
 | 
			
		||||
        let buf = pins.createBuffer(2)
 | 
			
		||||
        buf[0] = (addr >> 8) & 0xff;
 | 
			
		||||
        buf[1] = addr & 0xff;
 | 
			
		||||
 | 
			
		||||
        pins.i2cWriteBuffer(devaddr, buf);
 | 
			
		||||
        buf = pins.i2cReadBuffer(devaddr, 1);
 | 
			
		||||
 | 
			
		||||
        return buf[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    export function writeByte(addr: number, val: number) {
 | 
			
		||||
        if (addr < 0 || addr > memend)
 | 
			
		||||
            die();
 | 
			
		||||
 | 
			
		||||
        if (val < 0 || val > 0xff)
 | 
			
		||||
            die();
 | 
			
		||||
 | 
			
		||||
        let buf = pins.createBuffer(3)
 | 
			
		||||
 | 
			
		||||
        buf[0] = (addr >> 8) & 0xff;
 | 
			
		||||
        buf[1] = addr & 0xff;
 | 
			
		||||
        buf[2] = val;
 | 
			
		||||
 | 
			
		||||
        pins.i2cWriteBuffer(devaddr, buf)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    export function readBuffer(addr: number, length: number) {
 | 
			
		||||
        if (addr < 0 || length < 0 || (addr + length) > memend)
 | 
			
		||||
            die();
 | 
			
		||||
        let buf = pins.createBuffer(length)
 | 
			
		||||
        for (let i = 0; i < length; ++i)
 | 
			
		||||
            buf[i] = readByte(addr + i)
 | 
			
		||||
        return buf
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    export function writeBuffer(addr: number, buf: Buffer) {
 | 
			
		||||
        if (addr < 0 || (addr + buf.length) > memend)
 | 
			
		||||
            die();
 | 
			
		||||
        for (let i = 0; i < buf.length; ++i)
 | 
			
		||||
            writeByte(addr + i, buf[i])
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,16 +0,0 @@
 | 
			
		||||
i2c_fram.writeByte(100, 42)
 | 
			
		||||
i2c_fram.writeByte(101, 108)
 | 
			
		||||
 | 
			
		||||
function toBuf(arr: number[]) {
 | 
			
		||||
    let buf = pins.createBuffer(arr.length)
 | 
			
		||||
    for (let i = 0; i < arr.length; ++i)
 | 
			
		||||
        buf[i] = arr[i]
 | 
			
		||||
    return buf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
i2c_fram.writeBuffer(98, toBuf([1, 2, 3, 4, 5, 6, 7]))
 | 
			
		||||
 | 
			
		||||
console.log("100:" + i2c_fram.readByte(100))
 | 
			
		||||
console.log("101:" + i2c_fram.readByte(101))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -1,16 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
    "name": "i2c-fram",
 | 
			
		||||
    "description": "AdaFruit I2C FRAM driver for micro:bit",
 | 
			
		||||
    "files": [
 | 
			
		||||
        "README.md",
 | 
			
		||||
        "fram.ts"
 | 
			
		||||
    ],
 | 
			
		||||
    "testFiles": [
 | 
			
		||||
        "ftest.ts"
 | 
			
		||||
    ],
 | 
			
		||||
    "public": true,
 | 
			
		||||
    "dependencies": {
 | 
			
		||||
        "microbit": "file:../microbit"
 | 
			
		||||
    },
 | 
			
		||||
    "installedVersion": "hhneqa"
 | 
			
		||||
}
 | 
			
		||||
@@ -1,45 +0,0 @@
 | 
			
		||||
# NeoPixel driver
 | 
			
		||||
 | 
			
		||||
This library provides a driver for various Neo Pixel LED strips, 
 | 
			
		||||
see https://www.adafruit.com/category/168
 | 
			
		||||
 | 
			
		||||
NeoPixels consist of a number of RGB LEDs, every one of them controlled
 | 
			
		||||
separately.  
 | 
			
		||||
 | 
			
		||||
## Basic usage
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
// Create a NeoPixel driver - specify the number of LEDs:
 | 
			
		||||
let strip = neopixel.create(DigitalPin.P0, 24)
 | 
			
		||||
 | 
			
		||||
// set pixel colors
 | 
			
		||||
strip.setPixelColor(0, 255, 255, 255) // white
 | 
			
		||||
strip.setPixelColor(1, 255, 0, 0)     // red
 | 
			
		||||
strip.setPixelColor(2, 0, 255, 0)     // green
 | 
			
		||||
strip.setPixelColor(3, 0, 0, 255)     // blue
 | 
			
		||||
 | 
			
		||||
// send the data to the strip
 | 
			
		||||
strip.show()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Use `strip.setBrigthness()` to lower the brightness (it's maxed out by default).
 | 
			
		||||
 | 
			
		||||
Use `strip.shift()` or `strip.rotate()` to shift the lights around.
 | 
			
		||||
 | 
			
		||||
## Example: Using accelerometer to control colors
 | 
			
		||||
 | 
			
		||||
This little program will let the position of the microbit control the color of the first LED.
 | 
			
		||||
This first LED will then get shifted further away every 100ms.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
let strip = neopixel.create(DigitalPin.P0, 24)
 | 
			
		||||
while (true) {
 | 
			
		||||
    let x = input.acceleration(Dimension.X) / 2
 | 
			
		||||
    let y = input.acceleration(Dimension.Y) / 2
 | 
			
		||||
    let z = input.acceleration(Dimension.Z) / 2
 | 
			
		||||
    strip.setPixelColor(0, x, y, -z);
 | 
			
		||||
    strip.shift(1);
 | 
			
		||||
    strip.show();
 | 
			
		||||
    basic.pause(100);
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
@@ -1,119 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Functions to operate NeoPixel strips.
 | 
			
		||||
 */
 | 
			
		||||
//% weight=5 color=#2699BF
 | 
			
		||||
namespace neopixel {
 | 
			
		||||
 | 
			
		||||
    //% shim=sendBufferAsm
 | 
			
		||||
    function sendBuffer(buf: Buffer, pin: DigitalPin) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A NeoPixel strip
 | 
			
		||||
     */
 | 
			
		||||
    export class Strip {
 | 
			
		||||
        buf: Buffer;
 | 
			
		||||
        pin: DigitalPin;
 | 
			
		||||
        brightness: number;
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Set give LED to a given color (range 0-255 for r, g, b)
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_set_pixel_color" block="%strip|set pixel color at %ledoff|red: %red|green: %green|blue: %blue" blockGap=8
 | 
			
		||||
        //% weight=80
 | 
			
		||||
        setPixelColor(ledoff: number, red: number, green: number, blue: number): void {
 | 
			
		||||
            ledoff = ledoff * 3;
 | 
			
		||||
            let br = this.brightness;
 | 
			
		||||
            if (br < 255) {
 | 
			
		||||
                red = (Math.clamp(0, 255, red) * br) >> 8;
 | 
			
		||||
                green = (Math.clamp(0, 255, blue) * br) >> 8;
 | 
			
		||||
                blue = (Math.clamp(0, 255, blue) * br) >> 8;
 | 
			
		||||
            }
 | 
			
		||||
            let buf = this.buf;
 | 
			
		||||
            buf[ledoff + 0] = Math.clamp(0, 255, green);
 | 
			
		||||
            buf[ledoff + 1] = Math.clamp(0, 255, red);
 | 
			
		||||
            buf[ledoff + 2] = Math.clamp(0, 255, blue);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Send all the changes to the strip.
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_show" block="%strip|show" blockGap=8
 | 
			
		||||
        //% weight=79
 | 
			
		||||
        show() {
 | 
			
		||||
            basic.pause(1)
 | 
			
		||||
            sendBuffer(this.buf, this.pin);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Turn off all LEDs.
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_clear" block="%strip|clear"
 | 
			
		||||
        //% weight=76
 | 
			
		||||
        clear(): void {
 | 
			
		||||
            this.buf.fill(0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Gets the number of pixels declared on the strip
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_length" block="%strip|length" blockGap=8
 | 
			
		||||
        //% weight=60
 | 
			
		||||
        length() {
 | 
			
		||||
            return this.buf.length / 3
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Set the brightness of the strip, 0-255. eg: 255
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness" blockGap=8
 | 
			
		||||
        //% weight=59
 | 
			
		||||
        setBrigthness(brightness: number): void {
 | 
			
		||||
            this.brightness = brightness;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /**
 | 
			
		||||
         * Shift LEDs forward and clear with zeros.
 | 
			
		||||
         * @param off number of pixels to shift forward, eg: 1
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_shift" block="%strip|shift pixels forward by %off" blockGap=8
 | 
			
		||||
        //% weight=40
 | 
			
		||||
        shift(off: number = 1): void {
 | 
			
		||||
            this.buf.shift(-off * 3)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Rotate LEDs forward.
 | 
			
		||||
         * @param off number of pixels to rotate forward, eg: 1
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId="neopixel_rotate" block="%strip|rotate pixels forward by %off" blockGap=8
 | 
			
		||||
        //% weight=39
 | 
			
		||||
        rotate(off:number = 1): void {
 | 
			
		||||
            this.buf.rotate(-off * 3)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Set the pin where the neopixel is connected, defaults to P0.
 | 
			
		||||
         */        
 | 
			
		||||
        setPin(pin: DigitalPin): void {
 | 
			
		||||
            this.pin = pin;
 | 
			
		||||
            pins.digitalWritePin(this.pin, 0)
 | 
			
		||||
            basic.pause(50)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new NeoPixel driver for `numleds` LEDs.
 | 
			
		||||
     * @param pin the pin where the neopixel is connected.
 | 
			
		||||
     * @param numleds number of leds in the strip, eg: 24,30,60,64
 | 
			
		||||
     */
 | 
			
		||||
    //% blockId="neopixel_create" block="neopixel create|at pin %pin|with %numleds leds"
 | 
			
		||||
    //% weight=90
 | 
			
		||||
    export function create(pin: DigitalPin, numleds: number): Strip {
 | 
			
		||||
        let strip = new Strip();
 | 
			
		||||
        strip.buf = pins.createBuffer(numleds * 3);
 | 
			
		||||
        strip.setBrigthness(255)
 | 
			
		||||
        strip.setPin(pin)
 | 
			
		||||
        return strip;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,47 +0,0 @@
 | 
			
		||||
let strip = neopixel.create(DigitalPin.P0, 24);
 | 
			
		||||
let br = 100;
 | 
			
		||||
strip.setBrigthness(100);
 | 
			
		||||
input.onButtonPressed(Button.B, () => {
 | 
			
		||||
    br = br + 20;
 | 
			
		||||
    if (br > 255) {
 | 
			
		||||
        br = 5;
 | 
			
		||||
    }
 | 
			
		||||
    strip.setBrigthness(br);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
let rotationMode = false;
 | 
			
		||||
input.onButtonPressed(Button.A, () => {
 | 
			
		||||
    rotationMode = !rotationMode;
 | 
			
		||||
    if (rotationMode) {
 | 
			
		||||
        basic.showLeds(`
 | 
			
		||||
            . # # # .
 | 
			
		||||
            # . . . #
 | 
			
		||||
            # . . . #
 | 
			
		||||
            # . . . #
 | 
			
		||||
            . # # # .
 | 
			
		||||
            `);
 | 
			
		||||
    } else {
 | 
			
		||||
        basic.showLeds(`
 | 
			
		||||
            . . # . .
 | 
			
		||||
            . . . # .
 | 
			
		||||
            # # # # #
 | 
			
		||||
            . . . # .
 | 
			
		||||
            . . # . .
 | 
			
		||||
            `);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
while (true) {
 | 
			
		||||
    let x = input.acceleration(Dimension.X) / 2
 | 
			
		||||
    let y = input.acceleration(Dimension.Y) / 2
 | 
			
		||||
    let z = input.acceleration(Dimension.Z) / 2
 | 
			
		||||
    if (rotationMode) {
 | 
			
		||||
        strip.rotate();
 | 
			
		||||
    } else {
 | 
			
		||||
        strip.setPixelColor(0, x, y, -z);
 | 
			
		||||
        strip.shift(1);
 | 
			
		||||
    }
 | 
			
		||||
    strip.show();
 | 
			
		||||
    basic.pause(100);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,26 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
    "name": "neopixel",
 | 
			
		||||
    "description": "AdaFruit NeoPixel driver for micro:bit",
 | 
			
		||||
    "files": [
 | 
			
		||||
        "README.md",
 | 
			
		||||
        "neopixel.ts",
 | 
			
		||||
        "sendbuffer.asm"
 | 
			
		||||
    ],
 | 
			
		||||
    "testFiles": [
 | 
			
		||||
        "neotest.ts"
 | 
			
		||||
    ],
 | 
			
		||||
    "yotta": {
 | 
			
		||||
        "config": {
 | 
			
		||||
            "microbit-dal": {
 | 
			
		||||
                "bluetooth": {
 | 
			
		||||
                    "enabled": 0
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    "public": true,
 | 
			
		||||
    "dependencies": {
 | 
			
		||||
        "microbit": "file:../microbit"
 | 
			
		||||
    },
 | 
			
		||||
    "installedVersion": "fgluxh"
 | 
			
		||||
}
 | 
			
		||||
@@ -1,67 +0,0 @@
 | 
			
		||||
sendBufferAsm:
 | 
			
		||||
 | 
			
		||||
    push {r4,r5,r6,r7,lr}
 | 
			
		||||
    
 | 
			
		||||
    mov r4, r0 ; save buff
 | 
			
		||||
    mov r6, r1 ; save pin
 | 
			
		||||
    
 | 
			
		||||
    mov r0, r4
 | 
			
		||||
    bl BufferMethods::length
 | 
			
		||||
    mov r5, r0
 | 
			
		||||
    
 | 
			
		||||
    mov r0, r4
 | 
			
		||||
    bl BufferMethods::getBytes
 | 
			
		||||
    mov r4, r0
 | 
			
		||||
    
 | 
			
		||||
    ; setup pin as digital
 | 
			
		||||
    mov r0, r6
 | 
			
		||||
    movs r1, #0
 | 
			
		||||
    bl pins::digitalWritePin
 | 
			
		||||
    
 | 
			
		||||
    ; load pin address
 | 
			
		||||
    mov r0, r6
 | 
			
		||||
    bl pins::getPinAddress
 | 
			
		||||
 | 
			
		||||
    ldr r0, [r0, #8] ; get mbed DigitalOut from MicroBitPin
 | 
			
		||||
    ldr r1, [r0, #4] ; r1-mask for this pin
 | 
			
		||||
    ldr r2, [r0, #16] ; r2-clraddr
 | 
			
		||||
    ldr r3, [r0, #12] ; r3-setaddr
 | 
			
		||||
    
 | 
			
		||||
    cpsid i ; disable irq
 | 
			
		||||
    
 | 
			
		||||
    b .start
 | 
			
		||||
    
 | 
			
		||||
.nextbit:               ;            C0
 | 
			
		||||
    str r1, [r3, #0]    ; pin := hi  C2
 | 
			
		||||
    tst r6, r0          ;            C3
 | 
			
		||||
    bne .islate         ;            C4
 | 
			
		||||
    str r1, [r2, #0]    ; pin := lo  C6
 | 
			
		||||
.islate:
 | 
			
		||||
    lsrs r6, r6, #1     ; r6 >>= 1   C7
 | 
			
		||||
    bne .justbit        ;            C8
 | 
			
		||||
    
 | 
			
		||||
    ; not just a bit - need new byte
 | 
			
		||||
    adds r4, #1         ; r4++       C9
 | 
			
		||||
    subs r5, #1         ; r5--       C10
 | 
			
		||||
    bcc .stop           ; if (r5<0) goto .stop  C11
 | 
			
		||||
.start:
 | 
			
		||||
    movs r6, #0x80      ; reset mask C12
 | 
			
		||||
    nop                 ;            C13
 | 
			
		||||
 | 
			
		||||
.common:               ;             C13
 | 
			
		||||
    str r1, [r2, #0]   ; pin := lo   C15
 | 
			
		||||
    ; always re-load byte - it just fits with the cycles better this way
 | 
			
		||||
    ldrb r0, [r4, #0]  ; r0 := *r4   C17
 | 
			
		||||
    b .nextbit         ;             C20
 | 
			
		||||
 | 
			
		||||
.justbit: ; C10
 | 
			
		||||
    ; no nops, branch taken is already 3 cycles
 | 
			
		||||
    b .common ; C13
 | 
			
		||||
 | 
			
		||||
.stop:    
 | 
			
		||||
    str r1, [r2, #0]   ; pin := lo
 | 
			
		||||
    cpsie i            ; enable irq
 | 
			
		||||
 | 
			
		||||
    pop {r4,r5,r6,r7,pc}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -7,8 +7,7 @@
 | 
			
		||||
        "libs/microbit",
 | 
			
		||||
        "libs/microbit-radio",
 | 
			
		||||
        "libs/microbit-devices",
 | 
			
		||||
        "libs/microbit-bluetooth",
 | 
			
		||||
        "libs/neopixel"
 | 
			
		||||
        "libs/microbit-bluetooth"
 | 
			
		||||
    ],
 | 
			
		||||
    "cloud": {
 | 
			
		||||
        "workspace": false,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user