from bbc microbit library

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
Jonathan Austin
Date:
Thu Apr 07 01:33:22 2016 +0100
Revision:
1:8aa5cdb4ab67
Child:
41:da05ec75cd5d
Synchronized with git rev 55cb9199

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_COMPONENT_H
Jonathan Austin 1:8aa5cdb4ab67 27 #define MICROBIT_COMPONENT_H
Jonathan Austin 1:8aa5cdb4ab67 28
Jonathan Austin 1:8aa5cdb4ab67 29 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 30
Jonathan Austin 1:8aa5cdb4ab67 31 // Enumeration of core components.
Jonathan Austin 1:8aa5cdb4ab67 32 #define MICROBIT_ID_BUTTON_A 1
Jonathan Austin 1:8aa5cdb4ab67 33 #define MICROBIT_ID_BUTTON_B 2
Jonathan Austin 1:8aa5cdb4ab67 34 #define MICROBIT_ID_BUTTON_RESET 3
Jonathan Austin 1:8aa5cdb4ab67 35 #define MICROBIT_ID_ACCELEROMETER 4
Jonathan Austin 1:8aa5cdb4ab67 36 #define MICROBIT_ID_COMPASS 5
Jonathan Austin 1:8aa5cdb4ab67 37 #define MICROBIT_ID_DISPLAY 6
Jonathan Austin 1:8aa5cdb4ab67 38
Jonathan Austin 1:8aa5cdb4ab67 39 //EDGE connector events
Jonathan Austin 1:8aa5cdb4ab67 40 #define MICROBIT_IO_PINS 20
Jonathan Austin 1:8aa5cdb4ab67 41
Jonathan Austin 1:8aa5cdb4ab67 42 #define MICROBIT_ID_IO_P0 7 //P0 is the left most pad (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 43 #define MICROBIT_ID_IO_P1 8 //P1 is the middle pad (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 44 #define MICROBIT_ID_IO_P2 9 //P2 is the right most pad (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 45 #define MICROBIT_ID_IO_P3 10 //COL1 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 46 #define MICROBIT_ID_IO_P4 11 //BTN_A
Jonathan Austin 1:8aa5cdb4ab67 47 #define MICROBIT_ID_IO_P5 12 //COL2 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 48 #define MICROBIT_ID_IO_P6 13 //ROW2
Jonathan Austin 1:8aa5cdb4ab67 49 #define MICROBIT_ID_IO_P7 14 //ROW1
Jonathan Austin 1:8aa5cdb4ab67 50 #define MICROBIT_ID_IO_P8 15 //PIN 18
Jonathan Austin 1:8aa5cdb4ab67 51 #define MICROBIT_ID_IO_P9 16 //ROW3
Jonathan Austin 1:8aa5cdb4ab67 52 #define MICROBIT_ID_IO_P10 17 //COL3 (ANALOG/DIGITAL)
Jonathan Austin 1:8aa5cdb4ab67 53 #define MICROBIT_ID_IO_P11 18 //BTN_B
Jonathan Austin 1:8aa5cdb4ab67 54 #define MICROBIT_ID_IO_P12 19 //PIN 20
Jonathan Austin 1:8aa5cdb4ab67 55 #define MICROBIT_ID_IO_P13 20 //SCK
Jonathan Austin 1:8aa5cdb4ab67 56 #define MICROBIT_ID_IO_P14 21 //MISO
Jonathan Austin 1:8aa5cdb4ab67 57 #define MICROBIT_ID_IO_P15 22 //MOSI
Jonathan Austin 1:8aa5cdb4ab67 58 #define MICROBIT_ID_IO_P16 23 //PIN 16
Jonathan Austin 1:8aa5cdb4ab67 59 #define MICROBIT_ID_IO_P19 24 //SCL
Jonathan Austin 1:8aa5cdb4ab67 60 #define MICROBIT_ID_IO_P20 25 //SDA
Jonathan Austin 1:8aa5cdb4ab67 61
Jonathan Austin 1:8aa5cdb4ab67 62 #define MICROBIT_ID_BUTTON_AB 26 // Button A+B multibutton
Jonathan Austin 1:8aa5cdb4ab67 63 #define MICROBIT_ID_GESTURE 27 // Gesture events
Jonathan Austin 1:8aa5cdb4ab67 64
Jonathan Austin 1:8aa5cdb4ab67 65 #define MICROBIT_ID_THERMOMETER 28
Jonathan Austin 1:8aa5cdb4ab67 66 #define MICROBIT_ID_RADIO 29
Jonathan Austin 1:8aa5cdb4ab67 67 #define MICROBIT_ID_RADIO_DATA_READY 30
Jonathan Austin 1:8aa5cdb4ab67 68 #define MICROBIT_ID_MULTIBUTTON_ATTACH 31
Jonathan Austin 1:8aa5cdb4ab67 69 #define MICROBIT_ID_SERIAL 32
Jonathan Austin 1:8aa5cdb4ab67 70
Jonathan Austin 1:8aa5cdb4ab67 71 #define MICROBIT_ID_MESSAGE_BUS_LISTENER 1021 // Message bus indication that a handler for a given ID has been registered.
Jonathan Austin 1:8aa5cdb4ab67 72 #define MICROBIT_ID_NOTIFY_ONE 1022 // Notfication channel, for general purpose synchronisation
Jonathan Austin 1:8aa5cdb4ab67 73 #define MICROBIT_ID_NOTIFY 1023 // Notfication channel, for general purpose synchronisation
Jonathan Austin 1:8aa5cdb4ab67 74
Jonathan Austin 1:8aa5cdb4ab67 75 // Universal flags used as part of the status field
Jonathan Austin 1:8aa5cdb4ab67 76 #define MICROBIT_COMPONENT_RUNNING 0x01
Jonathan Austin 1:8aa5cdb4ab67 77
Jonathan Austin 1:8aa5cdb4ab67 78
Jonathan Austin 1:8aa5cdb4ab67 79 /**
Jonathan Austin 1:8aa5cdb4ab67 80 * Class definition for MicroBitComponent.
Jonathan Austin 1:8aa5cdb4ab67 81 *
Jonathan Austin 1:8aa5cdb4ab67 82 * All components should inherit from this class.
Jonathan Austin 1:8aa5cdb4ab67 83 *
Jonathan Austin 1:8aa5cdb4ab67 84 * If a component requires regular updates, then you should add the component
Jonathan Austin 1:8aa5cdb4ab67 85 * to the systemTick and idleTick queues.
Jonathan Austin 1:8aa5cdb4ab67 86 *
Jonathan Austin 1:8aa5cdb4ab67 87 * The system timer will call systemTick() once the component has been added to
Jonathan Austin 1:8aa5cdb4ab67 88 * the array of system components using system_timer_add_component. This callback
Jonathan Austin 1:8aa5cdb4ab67 89 * will be in interrupt context.
Jonathan Austin 1:8aa5cdb4ab67 90 *
Jonathan Austin 1:8aa5cdb4ab67 91 * The idle thread will call idleTick() once the component has been added to the array
Jonathan Austin 1:8aa5cdb4ab67 92 * of idle components using fiber_add_idle_component. Updates are determined by
Jonathan Austin 1:8aa5cdb4ab67 93 * the isIdleCallbackNeeded() member function.
Jonathan Austin 1:8aa5cdb4ab67 94 */
Jonathan Austin 1:8aa5cdb4ab67 95 class MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 96 {
Jonathan Austin 1:8aa5cdb4ab67 97 protected:
Jonathan Austin 1:8aa5cdb4ab67 98
Jonathan Austin 1:8aa5cdb4ab67 99 uint16_t id; // Event Bus ID
Jonathan Austin 1:8aa5cdb4ab67 100 uint8_t status; // keeps track of various component state, and also indicates if data is ready.
Jonathan Austin 1:8aa5cdb4ab67 101
Jonathan Austin 1:8aa5cdb4ab67 102 public:
Jonathan Austin 1:8aa5cdb4ab67 103
Jonathan Austin 1:8aa5cdb4ab67 104 /**
Jonathan Austin 1:8aa5cdb4ab67 105 * The default constructor of a MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 106 */
Jonathan Austin 1:8aa5cdb4ab67 107 MicroBitComponent()
Jonathan Austin 1:8aa5cdb4ab67 108 {
Jonathan Austin 1:8aa5cdb4ab67 109 this->id = 0;
Jonathan Austin 1:8aa5cdb4ab67 110 this->status = 0;
Jonathan Austin 1:8aa5cdb4ab67 111 }
Jonathan Austin 1:8aa5cdb4ab67 112
Jonathan Austin 1:8aa5cdb4ab67 113 /**
Jonathan Austin 1:8aa5cdb4ab67 114 * The system timer will call this member function once the component has been added to
Jonathan Austin 1:8aa5cdb4ab67 115 * the array of system components using system_timer_add_component. This callback
Jonathan Austin 1:8aa5cdb4ab67 116 * will be in interrupt context.
Jonathan Austin 1:8aa5cdb4ab67 117 */
Jonathan Austin 1:8aa5cdb4ab67 118 virtual void systemTick(){
Jonathan Austin 1:8aa5cdb4ab67 119
Jonathan Austin 1:8aa5cdb4ab67 120 }
Jonathan Austin 1:8aa5cdb4ab67 121
Jonathan Austin 1:8aa5cdb4ab67 122 /**
Jonathan Austin 1:8aa5cdb4ab67 123 * The idle thread will call this member function once the component has been added to the array
Jonathan Austin 1:8aa5cdb4ab67 124 * of idle components using fiber_add_idle_component. Updates are determined by
Jonathan Austin 1:8aa5cdb4ab67 125 * the isIdleCallbackNeeded() member function.
Jonathan Austin 1:8aa5cdb4ab67 126 */
Jonathan Austin 1:8aa5cdb4ab67 127 virtual void idleTick()
Jonathan Austin 1:8aa5cdb4ab67 128 {
Jonathan Austin 1:8aa5cdb4ab67 129
Jonathan Austin 1:8aa5cdb4ab67 130 }
Jonathan Austin 1:8aa5cdb4ab67 131
Jonathan Austin 1:8aa5cdb4ab67 132 /**
Jonathan Austin 1:8aa5cdb4ab67 133 * When added to the idleThreadComponents array, this function will be called to determine
Jonathan Austin 1:8aa5cdb4ab67 134 * if and when data is ready.
Jonathan Austin 1:8aa5cdb4ab67 135 *
Jonathan Austin 1:8aa5cdb4ab67 136 * @note override this if you want to request to be scheduled as soon as possible.
Jonathan Austin 1:8aa5cdb4ab67 137 */
Jonathan Austin 1:8aa5cdb4ab67 138 virtual int isIdleCallbackNeeded()
Jonathan Austin 1:8aa5cdb4ab67 139 {
Jonathan Austin 1:8aa5cdb4ab67 140 return 0;
Jonathan Austin 1:8aa5cdb4ab67 141 }
Jonathan Austin 1:8aa5cdb4ab67 142
Jonathan Austin 1:8aa5cdb4ab67 143 /**
Jonathan Austin 1:8aa5cdb4ab67 144 * If you have added your component to the idle or system tick component arrays,
Jonathan Austin 1:8aa5cdb4ab67 145 * you must remember to remove your component from them if your component is destructed.
Jonathan Austin 1:8aa5cdb4ab67 146 */
Jonathan Austin 1:8aa5cdb4ab67 147 virtual ~MicroBitComponent()
Jonathan Austin 1:8aa5cdb4ab67 148 {
Jonathan Austin 1:8aa5cdb4ab67 149 }
Jonathan Austin 1:8aa5cdb4ab67 150 };
Jonathan Austin 1:8aa5cdb4ab67 151
Jonathan Austin 1:8aa5cdb4ab67 152 #endif