mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitRadioEvent.h Source File

MicroBitRadioEvent.h

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #ifndef MICROBIT_RADIO_EVENT_H
00027 #define MICROBIT_RADIO_EVENT_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitRadio.h"
00032 #include "EventModel.h"
00033 
00034 /**
00035  * Provides a simple broadcast radio abstraction, built upon the raw nrf51822 RADIO module.
00036  *
00037  * This class provides the ability to extend the micro:bit's default EventModel to other micro:bits in the vicinity,
00038  * in a very similar way to the MicroBitEventService for BLE interfaces.
00039  *
00040  * It is envisaged that this would provide the basis for children to experiment with building their own, simple,
00041  * custom asynchronous events and actions.
00042  *
00043  * @note This API does not contain any form of encryption, authentication or authorisation. Its purpose is solely for use as a
00044  * teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
00045  * For serious applications, BLE should be considered a substantially more secure alternative.
00046  */
00047 class MicroBitRadioEvent
00048 {
00049     bool            suppressForwarding;     // A private flag used to prevent event forwarding loops.
00050     MicroBitRadio   &radio;                 // A reference to the underlying radio module to use.
00051 
00052     public:
00053 
00054     /**
00055       * Constructor.
00056       *
00057       * Creates an instance of MicroBitRadioEvent which offers the ability to extend
00058       * the micro:bit's default EventModel to other micro:bits in the vicinity.
00059       *
00060       * @param r The underlying radio module used to send and receive data.
00061       */
00062     MicroBitRadioEvent(MicroBitRadio &r);
00063 
00064     /**
00065       * Associates the given event with the radio channel.
00066       *
00067       * Once registered, all events matching the given registration sent to this micro:bit's
00068       * default EventModel will be automatically retransmitted on the radio.
00069       *
00070       * @param id The id of the event to register.
00071       *
00072       * @param value the value of the event to register.
00073       *
00074       * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if no default EventModel is available.
00075       *
00076       * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
00077       *       id and value fields.
00078       */
00079     int listen(uint16_t id, uint16_t value);
00080 
00081     /**
00082       * Associates the given event with the radio channel.
00083       *
00084       * Once registered, all events matching the given registration sent to the given
00085       * EventModel will be automatically retransmitted on the radio.
00086       *
00087       * @param id The id of the events to register.
00088       *
00089       * @param value the value of the event to register.
00090       *
00091       * @param eventBus The EventModel to listen for events on.
00092       *
00093       * @return MICROBIT_OK on success.
00094       *
00095       * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
00096       *       id and value fields.
00097       */
00098     int listen(uint16_t id, uint16_t value, EventModel &eventBus);
00099 
00100     /**
00101       * Disassociates the given event with the radio channel.
00102       *
00103       * @param id The id of the events to deregister.
00104       *
00105       * @param value The value of the event to deregister.
00106       *
00107       * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the default message bus does not exist.
00108       *
00109       * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
00110       */
00111     int ignore(uint16_t id, uint16_t value);
00112 
00113     /**
00114       * Disassociates the given events with the radio channel.
00115       *
00116       * @param id The id of the events to deregister.
00117       *
00118       * @param value The value of the event to deregister.
00119       *
00120       * @param eventBus The EventModel to deregister on.
00121       *
00122       * @return MICROBIT_OK on success.
00123       *
00124       * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
00125       */
00126     int ignore(uint16_t id, uint16_t value, EventModel &eventBus);
00127 
00128     /**
00129       * Protocol handler callback. This is called when the radio receives a packet marked as using the event protocol.
00130       *
00131       * This function process this packet, and fires the event contained inside onto the default EventModel.
00132       */
00133     void packetReceived();
00134 
00135     /**
00136       * Event handler callback. This is called whenever an event is received matching one of those registered through
00137       * the registerEvent() method described above. Upon receiving such an event, it is wrapped into
00138       * a radio packet and transmitted to any other micro:bits in the same group.
00139       */
00140     void eventReceived(MicroBitEvent e);
00141 };
00142 
00143 #endif