Page 1 of 1

Converting struct.pack to MakeCode Javascript blocks

Posted: Mon Dec 17, 2018 9:08 pm
by BruinBear
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 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);  
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.

Re: Converting struct.pack to MakeCode Javascript blocks

Posted: Tue Dec 18, 2018 2:26 am
by shaoziyang
There is no easy way as struct.pack in makecode.