Skip to content

Instantly share code, notes, and snippets.

@bacalj
Last active March 30, 2023 16:39
Show Gist options
  • Save bacalj/1684511d9eb6c185e53aca02a1ab6289 to your computer and use it in GitHub Desktop.
Save bacalj/1684511d9eb6c185e53aca02a1ab6289 to your computer and use it in GitHub Desktop.
most basic microbit to dataflow
/**
* micro:bit + Dataflow: "Listener Program"
*
* This is the basic program for the micro:bit physically attached to the computer.
* A companion program "Broadcaster Program" runs on the micro:bits not physically attached to the computer.
*
* An alternative approach would use the same program on all microbits, with lots of mode-checking.
* This is here as a proof-of-concept
*
* [ A ] - mode 1 - "awake" : pass `name:value` rxed from from radio to serial (dataflow)
* [ B ] - mode 0 - "asleep": ignore incoming radio broadcasts
*/
let mode = 0
function startUp() {
radio.setGroup(1)
serial.redirect(
SerialPin.USB_TX,
SerialPin.USB_RX,
BaudRate.BaudRate9600
)
mode = 0
basic.showIcon(IconNames.Asleep)
}
function wakeAndListen () {
mode = 1
basic.showIcon(IconNames.Happy)
}
function sleepAndIgnore () {
mode = 0
basic.showIcon(IconNames.Asleep)
}
function handleRadioSignal (name: string, value: number) {
if (mode == 1) {
serial.writeValue(name, value)
}
}
input.onButtonPressed(Button.A, function () {
wakeAndListen()
})
input.onButtonPressed(Button.B, function () {
sleepAndIgnore()
})
radio.onReceivedValue(function (name, value) {
handleRadioSignal(name, value)
})
startUp()
/**
* micro:bit + Dataflow: "Broadcaster Program"
* This is the basic program for the micro:bit not attached to the computer.
* A companion program "Listener Program" runs on the micro:bit attached to the computer.
*
* An alternative approach would use the same program on all microbits, with lots of mode-checking.
* This is here as a proof-of-concept
*
* [ AB ] - toggle stream output on and off
* [ A ] - increment the number being sent out
* [ B ] - decrement the number being sent out
*
*/
let mode = 0
let streamingOut = 0
radio.setGroup(1)
serial.redirect(
SerialPin.USB_TX,
SerialPin.USB_RX,
BaudRate.BaudRate9600
)
input.onButtonPressed(Button.A, function () {
streamingOut = streamingOut - 1
})
input.onButtonPressed(Button.AB, function () {
if (mode == 0) {
mode = 1
} else {
mode = 0
}
})
input.onButtonPressed(Button.B, function () {
streamingOut = streamingOut + 1
})
basic.forever(function () {
if (mode == 1) {
radio.sendValue("ax", streamingOut)
basic.showNumber(streamingOut)
} else {
basic.showIcon(IconNames.Asleep)
}
})
/**
* micro:bit + Dataflow: "Mock Communicator"
*
* This is a program for the micro:bit physically attached to the computer, used for development on the dataflow side.
* This is designed to mock output from the communicator micro:bit in a world of radio networked sensor/actuator hubs
*
* [ A ] - mode 1 - "on" : pass a stream of mock messages to serial (dataflow)
*
* (name: sat, value: 20.4) sat20.2 "the temperature reading on microbit A is 20.2 degrees"
* (name: sbt, value: 17.32) sbt17.32 "the temperature reading on microbit B is 17.32 degrees"
* (name: sah, value: 40) sah40 "the humidity reading on microbit A is 40 percent"
* (name: sar2, value: 1) sar21 "the state of microbit A's relay 2 is on"
* (name: sar2, value: 0) sar20 "the state of microbit A's relay 2 is off"
*
* [ B ] - mode 0 - "off": dont' send any mock messages
*/
let mode = 0
let mIndex = 0
function turnOn() {
mode = 1
basic.showIcon(IconNames.Happy)
}
function turnOff() {
mode = 0
basic.showIcon(IconNames.Asleep)
}
let messageObjects = [
{ name: "sat", value: 10.4 }, // temp a rises
{ name: "sat", value: 10.5 },
{ name: "sat", value: 10.6 },
{ name: "sbt", value: 11.32 }, // temb b falls
{ name: "sbt", value: 11.22 },
{ name: "sbt", value: 11.12 },
{ name: "sah", value: 40 },
{ name: "sah", value: 20 },
{ name: "sah", value: 10 },
{ name: "sar2", value: 1 }, // relay A2 on, then off
{ name: "sar2", value: 0 },
{ name: "sar1", value: 0 }, // relay A1 off, then on
{ name: "sbr1", value: 1 }
]
function startUp() {
serial.redirect(
SerialPin.USB_TX,
SerialPin.USB_RX,
BaudRate.BaudRate9600
)
mode = 0
mIndex = 0
basic.showIcon(IconNames.Asleep)
}
function nextIndex(){
mIndex = mIndex + 1;
if (mIndex > messageObjects.length - 1) {
mIndex = 0
}
}
function sendStreamMessage(name: string, value: number) {
if (mode == 1) {
// send with :
// serial.writeValue(name, value)
// send without :
serial.writeLine(`${name}${value}`)
}
}
startUp()
input.onButtonPressed(Button.A, function () { turnOn() })
input.onButtonPressed(Button.B, function () { turnOff() })
basic.forever(function () {
if (mode == 1) {
nextIndex()
for (let i = 0; i < 10; i++) {
sendStreamMessage(messageObjects[mIndex].name, messageObjects[mIndex].value)
}
} else {
basic.showIcon(IconNames.Asleep)
}
})
@bacalj
Copy link
Author

bacalj commented Mar 30, 2023

You can easily test mock_communicator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment