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

Committer:
JonnyA
Date:
Wed Jul 13 14:32:54 2016 +0000
Revision:
73:eb91bba49623
Parent:
1:8aa5cdb4ab67
Link to latest mbed-dev-bin (mbed library)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
Jonathan Austin 1:8aa5cdb4ab67 26 #ifndef MICROBIT_RADIO_EVENT_H
Jonathan Austin 1:8aa5cdb4ab67 27 #define MICROBIT_RADIO_EVENT_H
Jonathan Austin 1:8aa5cdb4ab67 28
Jonathan Austin 1:8aa5cdb4ab67 29 #include "mbed.h"
Jonathan Austin 1:8aa5cdb4ab67 30 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 31 #include "MicroBitRadio.h"
Jonathan Austin 1:8aa5cdb4ab67 32 #include "EventModel.h"
Jonathan Austin 1:8aa5cdb4ab67 33
Jonathan Austin 1:8aa5cdb4ab67 34 /**
Jonathan Austin 1:8aa5cdb4ab67 35 * Provides a simple broadcast radio abstraction, built upon the raw nrf51822 RADIO module.
Jonathan Austin 1:8aa5cdb4ab67 36 *
Jonathan Austin 1:8aa5cdb4ab67 37 * This class provides the ability to extend the micro:bit's default EventModel to other micro:bits in the vicinity,
Jonathan Austin 1:8aa5cdb4ab67 38 * in a very similar way to the MicroBitEventService for BLE interfaces.
Jonathan Austin 1:8aa5cdb4ab67 39 *
Jonathan Austin 1:8aa5cdb4ab67 40 * It is envisaged that this would provide the basis for children to experiment with building their own, simple,
Jonathan Austin 1:8aa5cdb4ab67 41 * custom asynchronous events and actions.
Jonathan Austin 1:8aa5cdb4ab67 42 *
Jonathan Austin 1:8aa5cdb4ab67 43 * @note This API does not contain any form of encryption, authentication or authorisation. Its purpose is solely for use as a
Jonathan Austin 1:8aa5cdb4ab67 44 * teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
Jonathan Austin 1:8aa5cdb4ab67 45 * For serious applications, BLE should be considered a substantially more secure alternative.
Jonathan Austin 1:8aa5cdb4ab67 46 */
Jonathan Austin 1:8aa5cdb4ab67 47 class MicroBitRadioEvent
Jonathan Austin 1:8aa5cdb4ab67 48 {
Jonathan Austin 1:8aa5cdb4ab67 49 bool suppressForwarding; // A private flag used to prevent event forwarding loops.
Jonathan Austin 1:8aa5cdb4ab67 50 MicroBitRadio &radio; // A reference to the underlying radio module to use.
Jonathan Austin 1:8aa5cdb4ab67 51
Jonathan Austin 1:8aa5cdb4ab67 52 public:
Jonathan Austin 1:8aa5cdb4ab67 53
Jonathan Austin 1:8aa5cdb4ab67 54 /**
Jonathan Austin 1:8aa5cdb4ab67 55 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 56 *
Jonathan Austin 1:8aa5cdb4ab67 57 * Creates an instance of MicroBitRadioEvent which offers the ability to extend
Jonathan Austin 1:8aa5cdb4ab67 58 * the micro:bit's default EventModel to other micro:bits in the vicinity.
Jonathan Austin 1:8aa5cdb4ab67 59 *
Jonathan Austin 1:8aa5cdb4ab67 60 * @param r The underlying radio module used to send and receive data.
Jonathan Austin 1:8aa5cdb4ab67 61 */
Jonathan Austin 1:8aa5cdb4ab67 62 MicroBitRadioEvent(MicroBitRadio &r);
Jonathan Austin 1:8aa5cdb4ab67 63
Jonathan Austin 1:8aa5cdb4ab67 64 /**
Jonathan Austin 1:8aa5cdb4ab67 65 * Associates the given event with the radio channel.
Jonathan Austin 1:8aa5cdb4ab67 66 *
Jonathan Austin 1:8aa5cdb4ab67 67 * Once registered, all events matching the given registration sent to this micro:bit's
Jonathan Austin 1:8aa5cdb4ab67 68 * default EventModel will be automatically retransmitted on the radio.
Jonathan Austin 1:8aa5cdb4ab67 69 *
Jonathan Austin 1:8aa5cdb4ab67 70 * @param id The id of the event to register.
Jonathan Austin 1:8aa5cdb4ab67 71 *
Jonathan Austin 1:8aa5cdb4ab67 72 * @param value the value of the event to register.
Jonathan Austin 1:8aa5cdb4ab67 73 *
Jonathan Austin 1:8aa5cdb4ab67 74 * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if no default EventModel is available.
Jonathan Austin 1:8aa5cdb4ab67 75 *
Jonathan Austin 1:8aa5cdb4ab67 76 * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
Jonathan Austin 1:8aa5cdb4ab67 77 * id and value fields.
Jonathan Austin 1:8aa5cdb4ab67 78 */
Jonathan Austin 1:8aa5cdb4ab67 79 int listen(uint16_t id, uint16_t value);
Jonathan Austin 1:8aa5cdb4ab67 80
Jonathan Austin 1:8aa5cdb4ab67 81 /**
Jonathan Austin 1:8aa5cdb4ab67 82 * Associates the given event with the radio channel.
Jonathan Austin 1:8aa5cdb4ab67 83 *
Jonathan Austin 1:8aa5cdb4ab67 84 * Once registered, all events matching the given registration sent to the given
Jonathan Austin 1:8aa5cdb4ab67 85 * EventModel will be automatically retransmitted on the radio.
Jonathan Austin 1:8aa5cdb4ab67 86 *
Jonathan Austin 1:8aa5cdb4ab67 87 * @param id The id of the events to register.
Jonathan Austin 1:8aa5cdb4ab67 88 *
Jonathan Austin 1:8aa5cdb4ab67 89 * @param value the value of the event to register.
Jonathan Austin 1:8aa5cdb4ab67 90 *
Jonathan Austin 1:8aa5cdb4ab67 91 * @param eventBus The EventModel to listen for events on.
Jonathan Austin 1:8aa5cdb4ab67 92 *
Jonathan Austin 1:8aa5cdb4ab67 93 * @return MICROBIT_OK on success.
Jonathan Austin 1:8aa5cdb4ab67 94 *
Jonathan Austin 1:8aa5cdb4ab67 95 * @note The wildcards MICROBIT_ID_ANY and MICROBIT_EVT_ANY can also be in place of the
Jonathan Austin 1:8aa5cdb4ab67 96 * id and value fields.
Jonathan Austin 1:8aa5cdb4ab67 97 */
Jonathan Austin 1:8aa5cdb4ab67 98 int listen(uint16_t id, uint16_t value, EventModel &eventBus);
Jonathan Austin 1:8aa5cdb4ab67 99
Jonathan Austin 1:8aa5cdb4ab67 100 /**
Jonathan Austin 1:8aa5cdb4ab67 101 * Disassociates the given event with the radio channel.
Jonathan Austin 1:8aa5cdb4ab67 102 *
Jonathan Austin 1:8aa5cdb4ab67 103 * @param id The id of the events to deregister.
Jonathan Austin 1:8aa5cdb4ab67 104 *
Jonathan Austin 1:8aa5cdb4ab67 105 * @param value The value of the event to deregister.
Jonathan Austin 1:8aa5cdb4ab67 106 *
Jonathan Austin 1:8aa5cdb4ab67 107 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the default message bus does not exist.
Jonathan Austin 1:8aa5cdb4ab67 108 *
Jonathan Austin 1:8aa5cdb4ab67 109 * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
Jonathan Austin 1:8aa5cdb4ab67 110 */
Jonathan Austin 1:8aa5cdb4ab67 111 int ignore(uint16_t id, uint16_t value);
Jonathan Austin 1:8aa5cdb4ab67 112
Jonathan Austin 1:8aa5cdb4ab67 113 /**
Jonathan Austin 1:8aa5cdb4ab67 114 * Disassociates the given events with the radio channel.
Jonathan Austin 1:8aa5cdb4ab67 115 *
Jonathan Austin 1:8aa5cdb4ab67 116 * @param id The id of the events to deregister.
Jonathan Austin 1:8aa5cdb4ab67 117 *
Jonathan Austin 1:8aa5cdb4ab67 118 * @param value The value of the event to deregister.
Jonathan Austin 1:8aa5cdb4ab67 119 *
Jonathan Austin 1:8aa5cdb4ab67 120 * @param eventBus The EventModel to deregister on.
Jonathan Austin 1:8aa5cdb4ab67 121 *
Jonathan Austin 1:8aa5cdb4ab67 122 * @return MICROBIT_OK on success.
Jonathan Austin 1:8aa5cdb4ab67 123 *
Jonathan Austin 1:8aa5cdb4ab67 124 * @note MICROBIT_EVT_ANY can be used to deregister all event values matching the given id.
Jonathan Austin 1:8aa5cdb4ab67 125 */
Jonathan Austin 1:8aa5cdb4ab67 126 int ignore(uint16_t id, uint16_t value, EventModel &eventBus);
Jonathan Austin 1:8aa5cdb4ab67 127
Jonathan Austin 1:8aa5cdb4ab67 128 /**
Jonathan Austin 1:8aa5cdb4ab67 129 * Protocol handler callback. This is called when the radio receives a packet marked as using the event protocol.
Jonathan Austin 1:8aa5cdb4ab67 130 *
Jonathan Austin 1:8aa5cdb4ab67 131 * This function process this packet, and fires the event contained inside onto the default EventModel.
Jonathan Austin 1:8aa5cdb4ab67 132 */
Jonathan Austin 1:8aa5cdb4ab67 133 void packetReceived();
Jonathan Austin 1:8aa5cdb4ab67 134
Jonathan Austin 1:8aa5cdb4ab67 135 /**
Jonathan Austin 1:8aa5cdb4ab67 136 * Event handler callback. This is called whenever an event is received matching one of those registered through
Jonathan Austin 1:8aa5cdb4ab67 137 * the registerEvent() method described above. Upon receiving such an event, it is wrapped into
Jonathan Austin 1:8aa5cdb4ab67 138 * a radio packet and transmitted to any other micro:bits in the same group.
Jonathan Austin 1:8aa5cdb4ab67 139 */
Jonathan Austin 1:8aa5cdb4ab67 140 void eventReceived(MicroBitEvent e);
Jonathan Austin 1:8aa5cdb4ab67 141 };
Jonathan Austin 1:8aa5cdb4ab67 142
Jonathan Austin 1:8aa5cdb4ab67 143 #endif