micro:bit radio components

In a problem involving radio, the problem author will need to provide a simulation of other micro:bits that talk via radio to the student's micro:bit. These are implemented entirely client-side in the learning interface.

They are built on the same functionality in the device view that enables additional external components (e.g. speakers, servos, etc) to be added.

The simplest case is when you want to make a remote micro:bit that sends a sensor value (e.g. temperature) in a loop. This is done by adding a "radio" config to an existing component. This will add a mini-microbit to the specified component.

You can provide a "name" field, which will show below the microbit.

[
  {"type": "temperature", "name": "Temp Sensor", "radio": {"interval": 3000,"sender_id":7,"simulate":"sensor"}}
]

The "radio" config has the following fields:

  • interval – This is an integer that sets how frequently in ms the radio will send its message.
  • sender_id – This is an integer that is a unique identifier for this micro:bit.
  • simulate – This will always be the string "sensor". This tells it that we are sending the value of the sensor.

Scripting the Radio Component

For problems where you want the radio micro:bit(s) to interact according to a script, you can create it like this example:

[
  {"type": "radio-microbit", "name": "Morse Sender",  "radio": {"simulate": "script", "sender_id": 3, "state": "waiting", "script": {
  "waiting": {
    "events": [
      {"event": "button", "button": 0, "state":"dot"},
      {"event": "button", "button": 1, "state":"dash"}
    ]
  },
  "dot": {
    "actions": [
      {"action": "send", "message": "dot"},
      {"action": "state", "state": "waiting"}
    ]
  },
  "dash": {
    "actions": [
      {"action": "send", "message": "dash"},
      {"action": "state", "state": "waiting"}
    ]
  }
}}}
]

In this "radio" definition, "simulate" must be set to "script", and "sender_id" is again the unique identifier.
 "state" is set to the name of the initial state you want the micro:bit to be in.

Then we have "script". This is an object containing a number of named states, which are themselves objects. They have two fields, "events" which describe event handlers for this state, and "actions", which are the steps that occur (in order), upon entering the state, after most of the event handlers are defined. "timeout" event handlers are started after all actions have occurred. Both of these fields are arrays containing event and action objects.

Events

Button Pressed

This event is triggered when one of the buttons is pressed, and has three fields:

  • "event" which always has the value "button",
  • "button" is the ID of the relevant button you are waiting for (0 is button A, 1 is button B),
  • "state" which provides the name of the state to enter when this button is pressed.
Receive Message

A receive event describes what to do when a radio message is received. It will always have these two fields:

  • "event" which always has the value "receive",
  • "state" which provides the name of the state to enter when this event is triggered.

In addition, it will have either a "message" field, or a "regexp" field, depending on how you want to match the received strings. If a "regexp" field is provided, an optional "flags" field can be added. If both a "message" and a "regexp" field are provided, it will check for both the message matching, and the regex matching.

  • "message" provides a case sensitive string to match exactly,
  • "regexp" provides a regular expression pattern to match against the radio message received,
  • "flags" provides options for the regexp matching. These are the standard JS RegExp flags.
Timeout

A timeout event simply waits a given number of milliseconds and then moves to the specified state. It has three fields:

  • "event" which always has the value "timeout",
  • "delay" which describes how long to wait in milliseconds,
  • "state" which provides the name of the state to enter after the given delay.

Actions

There are a number of different actions, which take a variety of parameters. These occur in the order listed in the "actions" array.

Show Image

This shows an image on the micro:bit display. It has two fields:

  • "action" is always "show",
  • "text" is a micro:bit image format string. e.g. '99999:77777:55555:33333:11111' -- the colons are optional.
Scroll Text

Scrolls text across the micro:bit display. It has two fields:

  • "action" is always "scroll",
  • "text" is a string message to scroll across the display. Can optionally include $N to insert match groups from the last successful regexp match. $0 is the whole match, $1 is the first bracketed group, etc.
Send Message

Sends a message on the radio. It has two fields:

  • "action" is always "send",
  • "message" is a string to transmit. Can optionally include $N to insert match groups from the last successful regexp match. $0 is the whole match, $1 is the first bracketed group, etc.
Set Channel

Sets the radio channel on which the micro:bit will transmit or receive. It has two fields:

  • "action" is always "channel",
  • "channel" specifies the new channel number (an integer).
Highlight Button

Highlights a specific button in the UI to simulate it being pressed. It has two fields:

  • "action" is always "highlight",
  • "button" specifies which button to highlight (0 is button A, 1 is button B).
Change State

Move specifically to a different state. This event has two fields:

  • "action" is always "state",
  • "state" specifies the name of the new state to enter.