surface repeated start functionality in i2c

(cherry picked from commit 213c52d)
This commit is contained in:
Matthias L. Jugel 2017-04-03 18:44:25 +02:00
parent 28aa7d451f
commit 4d9dbb8a66
4 changed files with 20 additions and 11 deletions

View File

@ -1,3 +1,4 @@
import rgbw = basic.rgbw;
serial.writeLine("I2C"); serial.writeLine("I2C");
// send to 0x44, register 0x00, value 0x46 (RESET ISL29125) // send to 0x44, register 0x00, value 0x46 (RESET ISL29125)
pins.i2cWriteNumber(0x44, 0x0046, NumberFormat.UInt16BE); pins.i2cWriteNumber(0x44, 0x0046, NumberFormat.UInt16BE);
@ -6,13 +7,17 @@ pins.i2cWriteNumber(0x44, 0x0105, NumberFormat.UInt16BE);
basic.forever(() => { basic.forever(() => {
serial.writeString("["); serial.writeString("[");
pins.i2cWriteNumber(0x44, 0x0A, NumberFormat.Int8BE); pins.i2cWriteNumber(0x44, 0x0A, NumberFormat.Int8BE);
serial.writeNumber(pins.i2cReadNumber(0x44, NumberFormat.UInt8BE)); let g = pins.i2cReadNumber(0x44, NumberFormat.UInt8BE);
serial.writeNumber(r);
serial.writeString(","); serial.writeString(",");
pins.i2cWriteNumber(0x44, 0x0C, NumberFormat.UInt8BE); pins.i2cWriteNumber(0x44, 0x0C, NumberFormat.UInt8BE);
serial.writeNumber(pins.i2cReadNumber(0x44, NumberFormat.UInt8BE)); let r = pins.i2cReadNumber(0x44, NumberFormat.UInt8BE);
serial.writeNumber(g);
serial.writeString(","); serial.writeString(",");
pins.i2cWriteNumber(0x44, 0x0E, NumberFormat.UInt8BE); pins.i2cWriteNumber(0x44, 0x0E, NumberFormat.UInt8BE);
serial.writeNumber(pins.i2cReadNumber(0x44, NumberFormat.UInt8LE)); let b = pins.i2cReadNumber(0x44, NumberFormat.UInt8LE);
serial.writeNumber(b);
serial.writeLine("]"); serial.writeLine("]");
basic.setLedColor(basic.rgbw(r,g,b, 0));
basic.pause(1000); basic.pause(1000);
}); });

View File

@ -174,8 +174,8 @@
"pins.analogWritePin|block": "analog write|pin %name|to %value", "pins.analogWritePin|block": "analog write|pin %name|to %value",
"pins.digitalReadPin|block": "digital read|pin %name", "pins.digitalReadPin|block": "digital read|pin %name",
"pins.digitalWritePin|block": "digital write|pin %name|to %value", "pins.digitalWritePin|block": "digital write|pin %name|to %value",
"pins.i2cReadNumber|block": "i2c read number|at address %address|of format %format=i2c_sizeof", "pins.i2cReadNumber|block": "i2c read number|at address %address|of format %format=i2c_sizeof|repeat %repeat",
"pins.i2cWriteNumber|block": "i2c write number|at address %address|with value %value|of format %format=i2c_sizeof", "pins.i2cWriteNumber|block": "i2c write number|at address %address|with value %value|of format %format=i2c_sizeof|repeat %repeat",
"pins.map|block": "map %value|from low %fromLow|from high %fromHigh|to low %toLow|to high %toHigh", "pins.map|block": "map %value|from low %fromLow|from high %fromHigh|to low %toLow|to high %toHigh",
"pins.onPulsed|block": "on|pin %pin|pulsed %pulse", "pins.onPulsed|block": "on|pin %pin|pulsed %pulse",
"pins.pulseDuration|block": "pulse duration (µs)", "pins.pulseDuration|block": "pulse duration (µs)",

View File

@ -22,9 +22,9 @@ namespace pins {
* Read one number from 7-bit I2C address. * Read one number from 7-bit I2C address.
*/ */
//% help=pins/i2c-read-number blockGap=8 //% help=pins/i2c-read-number blockGap=8
//% blockId=pins_i2c_readnumber block="i2c read number|at address %address|of format %format=i2c_sizeof" weight=7 //% blockId=pins_i2c_readnumber block="i2c read number|at address %address|of format %format=i2c_sizeof|repeat %repeat" weight=7
export function i2cReadNumber(address: number, format: NumberFormat): number { export function i2cReadNumber(address: number, format: NumberFormat, repeat?: boolean): number {
let buf = pins.i2cReadBuffer(address, pins.sizeOf(format)) let buf = pins.i2cReadBuffer(address, pins.sizeOf(format), repeat)
return buf.getNumber(format, 0) return buf.getNumber(format, 0)
} }
@ -32,11 +32,11 @@ namespace pins {
* Write one number to a 7-bit I2C address. * Write one number to a 7-bit I2C address.
*/ */
//% help=pins/i2c-write-number blockGap=8 //% help=pins/i2c-write-number blockGap=8
//% blockId=i2c_writenumber block="i2c write number|at address %address|with value %value|of format %format=i2c_sizeof" weight=6 //% blockId=i2c_writenumber block="i2c write number|at address %address|with value %value|of format %format=i2c_sizeof|repeat %repeat" weight=6
export function i2cWriteNumber(address: number, value: number, format: NumberFormat): void { export function i2cWriteNumber(address: number, value: number, format: NumberFormat, repeat?: boolean): void {
let buf = createBuffer(pins.sizeOf(format)) let buf = createBuffer(pins.sizeOf(format))
buf.setNumber(format, 0, value) buf.setNumber(format, 0, value)
pins.i2cWriteBuffer(address, buf) pins.i2cWriteBuffer(address, buf, repeat)
} }
/** /**

4
tests/i2c.ts Normal file
View File

@ -0,0 +1,4 @@
let item = pins.i2cReadNumber(123, NumberFormat.Int8LE)
pins.i2cWriteNumber(123, 0, NumberFormat.Int8LE)
let item = pins.i2cReadNumber(123, NumberFormat.Int8LE, true)
pins.i2cWriteNumber(123, 0, NumberFormat.Int8LE, true)