Micro:bit - Morse Transmitter
Written by Harry Fairhead   
Monday, 01 March 2021
Article Index
Micro:bit - Morse Transmitter
Polling v Events
A Morse Transmitter

microbite2360

Poll Or Event

If you want to send some data to other micro:bits the task is easy. All you have to do is create the buffer and use the send method. To receive data we have the usual problem that input is harder than output because you don’t know when it is going to happen. In the case of receiving a radio message the solution is to use polling or an event.

Once you enable the radio it continuously monitors for incoming messages and stores any that it receives.

If you use the radio.recv method then it either returns with the first message that is available or a NULL if there are no messages ready to process. If you use the datagram.recv method then either a message is returned or an empty buffer. In either case you can use the recv method to write a polling loop. For example, using the datagram object:

 while (1)
    {
        ManagedString s = uBit.radio.datagram.recv();
        if (s.length() != 0)
        {
          work with data
        }
        do other tasks
    }

The alternative to a polling loop is to set up an event handler. Events are only implemented for the datagram object and to enable them you need to use:

uBit.messageBus.listen(MICROBIT_ID_RADIO,
                  MICROBIT_RADIO_EVT_DATAGRAM, onData);

where the onData parameter is the function called when there is data to process.

For example:

void onData(MicroBitEvent e)
{
    ManagedString s = uBit.radio.datagram.recv();
     work with data
}
int main()
{
    uBit.init();
    uBit.messageBus.listen(MICROBIT_ID_RADIO,
                  MICROBIT_RADIO_EVT_DATAGRAM, onData);
    uBit.radio.enable();
      do other tasks

This is simple, but you have to rely on the queue of incoming messages not overflowing while you are processing the data.

In many cases the message that you want to sent to other micro:bits is just that something has happened. For example, if a button is pressed you might send a 1 and when it is released a 2. This works and is flexible and efficient, but really all you are doing is transferring an event to another micro:bit. The framework allows you to do this directly. The radio object has an event object, MicroBitRadioEvent. This object is designed to associate events that occur on the local micro:bit and pass them on via the radio to other micro:bits. The received events are handled on the remote micro:bits as if they occurred locally, i.e. they are treated as being the same as standard events.

The key method of the MicroBitRadioEvent is listen:

int listen(uint16_t id, uint16_t value)

which associates the event specified by id and value to the radio channel currently open. The id is that of the component that caused the interrupt and value is the exact event. You can use custom values for these or existing event types. For example:

int main()
{
    uBit.init();
    uBit.radio.enable();
    uBit.radio.event.listen(MICROBIT_ID_BUTTON_A,
                           MICROBIT_BUTTON_EVT_CLICK);
    while(1){
        uBit.sleep(1000);
    }
}

sets up the radio to pass any Button A clicks over the radio.

 

To respond to them the receiver has to have something like:

void onData(MicroBitEvent e)
{
  uBit.display.print("Click");
  return;
}
int main()
{
    uBit.init();
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A,
                     MICROBIT_BUTTON_EVT_CLICK, onData);
    uBit.radio.enable();
    while(1){
        uBit.sleep(1000);
    }
}

If you try this out you will see the receiving micro:bit display the “Click” message whenever the remote micro:bit has its Button A clicked. Of course, the message is displayed if the local micro:bit has its Button A clicked as well. The remote micro:bit cannot tell the difference between an event that originated from another micro:bit or from itself. This is the reason why custom events are most often used to uniquely identify events originating over a radio link.

The idea of transmitting local events to other micro:bits is a simplification when the situation itself is simple. It has the potential, however, to grow to a point of not being easy to debug or maintain.

<ASIN:1871962676>

<ASIN:B08XPRMK97>

<ASIN:B08WTBNJ5Q>

<ASIN:B08NLX7773>



Last Updated ( Wednesday, 03 March 2021 )