pxt-calliope/docs/examples/radio-dashboard.md
Abhijith Chatra be7858cbed
Cherry picked fixes from v0 to v1 (#1476)
* Fix example and do a few edits (#850)

* Small fixes to the Karel project text (#862)

* fix missing shadow type

* add calibration info (#897)

microbit-support:6348 User reported bug, but didn't realie micro:bit compass had to be calibrated on first run of the program.

* Cherry pick adding various cross-editor compat APIs (#863)

* fix nudge

* Fix a typo ("screent") in the documentation (#1012)

* Fix a typo
"screent" -> "the LED screen"

* Update showArrow doc string for consistency with other methods

* Doc bugs found by xlators (#899)

* Update rotation.md (#998)

reflects actual behaviour of the board

* fixed layout corruption (#1073)

* Updating the new bug filer with the right link

* Update challenges.md (#1325)

For what I'm taught in school, 2 follows 1 😉😉.

* Update simulator.md (#1209)

* Update plant watering.md (#1264)

the connection to servo are male not female

* support for MIDI simulator in v0 (#1331)

* Doc improvements (#1337)

* update grammar in radio-dashboard sample code

* Chanllenge 2 follows 1

I open all challenges.md without "Challenge 2" and review the file
`grep -r "Challenge 2" -L | grep challenge | xargs -I@ code @`

Changes to be committed:
	modified:   docs/lessons/counter/challenges.md
	modified:   docs/lessons/game-counter/challenges.md
	modified:   docs/lessons/night-light/challenges.md
	modified:   docs/lessons/snowflake-fall/challenges.md

* Revert "support for MIDI simulator in v0 (#1331)"

This reverts commit 30a9c411fb80762656e7a636feff8e77b7fd9e67.

* Revert "Cherry pick adding various cross-editor compat APIs (#863)"

This reverts commit 7308dbef23e9ee402bebb7e721d7014d8252c9e5.

* Fixed pin needed to replace button A (#1385)

* Cherry pick part of the signature update in in2cWriteBuffer (#863): 7308dbe
2018-10-22 10:00:57 -07:00

2.3 KiB

Radio Dashboard

/**
 * Radio monitoring dashboard
 * 
 * Each radio client is represented by a dot on the screen.
 * Once a client is registered, it will stay at the same pixel location
 * forever.
 * 
 * Radio clients can simply send a number (between 0..255) on group 4.
 * They must transmit the serial number using ``radio.setTransmitSerialNumber(true)``
 * 
 * The received number is used to set the LED brightness for that client.
 * 
 * If the radio packet is not received for 10sec, the LED starts blinking.
 */
const deadPing = 20000;
const lostPing = 10000;

interface Client {
    // client serial id
    id: number;
    // sprite on screen
    sprite: game.LedSprite;
    // last ping received
    ping: number;
}

const clients: Client[] = [];

/* lazy allocate sprite */
function getClient(id: number): Client {
    // needs an id to track radio client's identity
    if (!id)
        return undefined;

    // look for cached clients
    for (const client of clients)
        if (client.id == id)
            return client;
    const n = clients.length;
    if (n == 24) // out of pixels
        return undefined;
    const client: Client = {
        id: id,
        sprite: game.createSprite(n % 5, n / 5),
        ping: input.runningTime()
    }
    clients.push(client);
    return client;
}

// store data received by clients
radio.onReceivedNumber(function (receivedNumber) {
    const serialNumber = radio.receivedPacket(RadioPacketProperty.SerialNumber)
    const client = getClient(serialNumber);
    if (!client)
        return;

    client.ping = input.runningTime()
    client.sprite.setBrightness(Math.max(1, receivedNumber & 0xff));
})

// monitor the sprites and start blinking when no packet is received
basic.forever(() => {
    const now = input.runningTime()
    for (const client of clients) {
        // lost signal starts blinking
        const lastPing = now - client.ping;
        if (lastPing > deadPing) {
            client.sprite.setBlink(0)
            client.sprite.setBrightness(0)
        }
        else if (lastPing > lostPing)
            client.sprite.setBlink(500);
        else
            client.sprite.setBlink(0);
    }
    basic.pause(500)
})

// setup the radio and start!
radio.setGroup(4)
game.addScore(1)
radio