Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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