pxt-calliope/docs/reference/radio/send-buffer.md
Peli de Halleux 07a578831c
support for buffer packet type ()
* support for buffer packet type

* updated description

* updated signature

* added note on json encoding

* adding sendNumbers

* cleanup

* removing 'sendnumbers'
2018-01-30 10:11:12 -08:00

1.2 KiB

Send Buffer

Sends a buffer to other @boardname@s in the area connected by radio. The maximum buffer length is 19 bytes.

radio.sendBuffer(pins.createBuffer(1))

Parameters

  • msg is a buffer to send by radio.

Example: Remote level

If you load this program onto two @boardname@s, each board will send the level information to the other board.

let ax = 0;
let ay = 0;
basic.forever(() => {
    ax = input.acceleration(Dimension.X);
    ay = input.acceleration(Dimension.Y);

    // encode data in buffer
    let buf = pins.createBuffer(4)
    buf.setNumber(NumberFormat.Int16LE, 0, ax)
    buf.setNumber(NumberFormat.Int16LE, 2, ay)
    radio.sendBuffer(buf)
})

radio.onDataPacketReceived(({ receivedBuffer }) => {
    // decode data from buffer
    ax = receivedBuffer.getNumber(NumberFormat.Int16LE, 0);
    ay = receivedBuffer.getNumber(NumberFormat.Int16LE, 2);

    // display
    basic.clearScreen()
    led.plot(
        pins.map(ax, -1023, 1023, 0, 4),
        pins.map(ay, -1023, 1023, 0, 4)
    )
});

~hint

A radio that can both transmit and receive is called a transceiver.

~

See also

on data packet received

radio