pxt-calliope/docs/reference/radio/on-received-buffer.md
Peli de Halleux 2b504d863d
Radiodocsupdate (#1430)
* a few updates

* more updates

* reorder radio blocks

* more shuffling of new radio apis

* fixing hot or ocold

* more doc fixes

* more updates

* fixing docs issues

* more doc fixes

* restore docs errors

* missing packate

* renamed argument of callback

* mssing radio

* more odcs fixes

* lock turtle

* ignore docs for now
2018-10-15 15:32:09 -07:00

1.3 KiB

on Received Buffer

Run part of a program when the @boardname@ receives a buffer over radio.

radio.onReceivedBuffer(function (receivedBuffer) {})

Parameters

  • receivedBuffer: The buffer that was sent in this packet or the empty string if this packet did not contain a string. See send buffer

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.onReceivedBuffer(function (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

send buffer

radio