Converting struct.pack to MakeCode Javascript blocks
Posted: Mon Dec 17, 2018 9:08 pm
I have some working MicroPython code that uses struct to send data from the micro:bit to another microcontroller board using I2C.
I have converted the Python code to send buffered data 'one byte at a time' using the Javascript/Typescript window in https://makecode.microbit.org/. For example:
becomes:
Obviously, this is not very efficient code but it works. I realise that this is a MicroPython forum, but I was wondering if anybody can suggest how to send those 5 unsigned integer bytes with one TypeScript statement? I don't know enough about TypeScript.
I have converted the Python code to send buffered data 'one byte at a time' using the Javascript/Typescript window in https://makecode.microbit.org/. For example:
Code: Select all
i2c.write(42, struct.pack(‘<BBBBB’,0,0,0,255,0))
Code: Select all
let buf = pins.createBuffer(5);
buf.setNumber(NumberFormat.Int8LE, 0, 0);
buf.setNumber(NumberFormat.Int8LE, 1, 0);
buf.setNumber(NumberFormat.Int8LE, 2, 0);
buf.setNumber(NumberFormat.Int8LE, 3, 255);
buf.setNumber(NumberFormat.Int8LE, 4, 0);
pins.i2cWriteBuffer(42, buf, false);