Help Needed converting python code to Makecode

HI

I have a device that is controlled via commands sent over serial,

I am trying to convert the existing micro python code to makecode for Microbit but am getting stuck on converting the following code…

uart.write(bytes([0x7E, 0x03, 0x00, 0x02, 0x00, track_number, 0xEF]))

any help or suggestions gratefully received

1 Like

you could ask chatgpt to do it, other than that idk any other ideas

Hi @dsssssssss9 ,

I haven’t tried this, but I think the JavaScript equivalent would be something like:

    let track_number = 5;
    let l = pins.createBuffer(8)
    l.setUint8(0, 0x7E);
    l.setUint8(1, 0x03);
    l.setUint8(2, 0x00);
    l.setUint8(3, 0x02);
    l.setUint8(4, 0x00);
    l.setUint8(5, 0x02);
    l.setUint8(6, track_number);
    l.setUint8(7, 0xEF);

    serial.writeBuffer(l)

See https://makecode.microbit.org/S73730-18716-20158-85140 .

And in Python:

track_number = 5
    l = bytearray(8)
    l.set_uint8(0, 0x7E)
    l.set_uint8(1, 0x03)
    l.set_uint8(2, 0x00)
    l.set_uint8(3, 0x02)
    l.set_uint8(4, 0x00)
    l.set_uint8(5, 0x02)
    l.set_uint8(6, track_number)
    l.set_uint8(7, 0xEF)
    serial.write_buffer(l)

See https://makecode.microbit.org/S99388-10508-07062-49540 .

Hi

Thanks for the advice …

Will try chatgpt & see what it comes up with

Seasons greetings

Hi

works fine - many thanks

had to alter the bytearray slightly due to typo in my original question

Do you think this is possible in purely blocks ??

Also is there a good guide on how to write an extension for MakeCode as would love to let others use this

Many Thanls

Hi @dsssssssss9 ,

Glad you were able to get it to work. I don’t think blocks include all the parts needed, but writing an extension would allow you to provide code in JavaScript and use it from blocks.

I’m not aware of any truly ideal, up-to-date tutorials on extensions, but some resources I’ve used in the past include:

I hope that helps a bit!