lancaster-university / codal-core

MIT License
11 stars 28 forks source link

`Serial::eventOn` and `Serial::eventAfter` do not enable `rxInterrupt` #158

Open c272 opened 1 year ago

c272 commented 1 year ago

When configuring CODAL_SERIAL_EVT_HEAD_MATCH and CODAL_SERIAL_EVT_DELIM_MATCH events for Serial (through the use of eventOn and eventAfter), there is no lazy initialisation of the rxBuffer/rxInterrupt performed with initialiseRx. This results in the events not being fired until the the rxInterrupt and buffer is then later configured by another function call, even if data is sent over serial to the device.

This seems to me quite unintuitive behaviour; if the event is configured and data is then sent to the device over serial, then it should follow that the event is called. You can replicate this behaviour with the following code:

#include <MicroBit.h>
MicroBit uBit;

//Callback for when serial data is received.
void callback(MicroBitEvent e) {
    while (true)
        uBit.display.scroll("DATA!");
}

int main() {
    uBit.init();

    //Set up an eventAfter for when we receive any bytes.
    uBit.messageBus.listen(MICROBIT_ID_SERIAL, MICROBIT_SERIAL_EVT_HEAD_MATCH, callback);
    uBit.serial.eventAfter(1);

    //Release to the scheduler.
    release_fiber();
}

If this is is seen as unintended behaviour, then I can submit a pull request to lazy initialise the rxBuffer from both of these event functions. Let me know if this would be a change that would be acceptable!

microbit-carlos commented 1 year ago

Great catch @c272! are you happy to submit a PR?