Converting struct.pack to MakeCode Javascript blocks

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Converting struct.pack to MakeCode Javascript blocks

Post by BruinBear » 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 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.

Posts: 347
Joined: Sun Apr 17, 2016 1:55 pm

Re: Converting struct.pack to MakeCode Javascript blocks

Post by shaoziyang » Tue Dec 18, 2018 2:26 am

There is no easy way as struct.pack in makecode.

Post Reply