131 Week 11 - MicroBit radio module Flashcards
Radio hardware
Micro:bit devices are equipped with a Nordic Semiconductor nRF52833 System on Chip (SoC) which contains a built-in 2.4GHz radio module.
Micro:bit signal strength
Strength of the received signal is expressed in
decibels (dB) with respect to 1 mW
Datagram
A datagram is a sequence of bytes (default 32 bytes) that represents a single packet of data. Micro:bits can transmit 1 datagram at a time.
Datagram types
An array of bytes: uint8_t myArray[10];
A sequence of characters: ManagedString s(“HELLO”);
A packet buffer: PacketBuffer b(16);
Transmitting a datagram
Bit.radio.datagram.send(datagram)
Where datagram is any of the datagram types.
Receiving a datagram
uBit.radio.datagram.recv()
Should only use .recv() after a MICROBIT_RADIO_EVT_DATAGRAM event has been raised.
Setting up a radio listener
uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onRx);
MICROBIT_ID_RADIO is used to specify to monitor the radio component.
MICROBIT_RADIO_EVT_DATAGRAM is used to raise an event when a
datagram is received
onRx is an example name for an event handler which is called to receive the datagram.
Setting up the radio module
Need to enable radio module using: uBit.radio.enable()
Managed string
Managed type - automatically reserves and releases memory.
Immutable – cannot be changed once it is created.
Can be compared using ==, < and >.
Created using the syntax:
ManagedString stringName(“String Contents”);
Joining managed strings
Although they cannot be changed, 2 managed strings can be joined to create a new string:
ManagedString greeting(“HAPPY NEW YEAR “);
ManagedString year(2024);
ManagedString msg = greeting + year;
Packet buffer
Managed type - automatically reserves and releases memory.
Created using syntax:
PacketBuffer bufferName(numBytes); where numBytes is an integer representing how many bytes the packet buffer will store.
Can be read from or written to once created using square bracket notation:
b[0] = 255;
b[1] = 10;
Useful methods
uBit.radio.setGroup(int) sets a group. Only other microbits in that group will receive datagrams. Default group is 0.
uBit.radio.enable() initialises the radio component of micro:bit for transmission/reception.
uBit.radio.disable() disables radio component for use as a multipoint sender/receiver.
datagramName.getRSSI() retrieves the received signal strength indicator (RSSI), which is measured in dBm, of the most recently received datagram.