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 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 27 /*
MrBedfordVan 0:b9164b348919 28 * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ
MrBedfordVan 0:b9164b348919 29 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
MrBedfordVan 0:b9164b348919 30 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
MrBedfordVan 0:b9164b348919 31 * as a compatability option, but does not support the options used...
MrBedfordVan 0:b9164b348919 32 */
MrBedfordVan 0:b9164b348919 33 #if !defined(__arm)
MrBedfordVan 0:b9164b348919 34 #pragma GCC diagnostic ignored "-Wunused-function"
MrBedfordVan 0:b9164b348919 35 #pragma GCC diagnostic push
MrBedfordVan 0:b9164b348919 36 #pragma GCC diagnostic ignored "-Wunused-parameter"
MrBedfordVan 0:b9164b348919 37 #endif
MrBedfordVan 0:b9164b348919 38
MrBedfordVan 0:b9164b348919 39 #include "MicroBit.h"
MrBedfordVan 0:b9164b348919 40
MrBedfordVan 0:b9164b348919 41 #include "nrf_soc.h"
MrBedfordVan 0:b9164b348919 42
MrBedfordVan 0:b9164b348919 43 /*
MrBedfordVan 0:b9164b348919 44 * Return to our predefined compiler settings.
MrBedfordVan 0:b9164b348919 45 */
MrBedfordVan 0:b9164b348919 46 #if !defined(__arm)
MrBedfordVan 0:b9164b348919 47 #pragma GCC diagnostic pop
MrBedfordVan 0:b9164b348919 48 #endif
MrBedfordVan 0:b9164b348919 49
MrBedfordVan 0:b9164b348919 50 #if CONFIG_ENABLED(MICROBIT_DBG)
MrBedfordVan 0:b9164b348919 51 // We create and initialize to NULL here, but MicroBitSerial will automatically update this as needed in its constructor.
MrBedfordVan 0:b9164b348919 52 RawSerial* SERIAL_DEBUG = NULL;
MrBedfordVan 0:b9164b348919 53 #endif
MrBedfordVan 0:b9164b348919 54
MrBedfordVan 0:b9164b348919 55 /**
MrBedfordVan 0:b9164b348919 56 * Constructor.
MrBedfordVan 0:b9164b348919 57 *
MrBedfordVan 0:b9164b348919 58 * Create a representation of a MicroBit device, which includes member variables
MrBedfordVan 0:b9164b348919 59 * that represent various device drivers used to control aspects of the micro:bit.
MrBedfordVan 0:b9164b348919 60 */
MrBedfordVan 0:b9164b348919 61 MicroBit::MicroBit() :
MrBedfordVan 0:b9164b348919 62 serial(USBTX, USBRX),
MrBedfordVan 0:b9164b348919 63 resetButton(MICROBIT_PIN_BUTTON_RESET),
MrBedfordVan 0:b9164b348919 64 storage(),
MrBedfordVan 0:b9164b348919 65 i2c(I2C_SDA0, I2C_SCL0),
MrBedfordVan 0:b9164b348919 66 messageBus(),
MrBedfordVan 0:b9164b348919 67 display(),
MrBedfordVan 0:b9164b348919 68 buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A),
MrBedfordVan 0:b9164b348919 69 buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B),
MrBedfordVan 0:b9164b348919 70 buttonAB(MICROBIT_ID_BUTTON_A,MICROBIT_ID_BUTTON_B, MICROBIT_ID_BUTTON_AB),
MrBedfordVan 0:b9164b348919 71 accelerometer(i2c),
MrBedfordVan 0:b9164b348919 72 compass(i2c, accelerometer, storage),
MrBedfordVan 0:b9164b348919 73 compassCalibrator(compass, accelerometer, display),
MrBedfordVan 0:b9164b348919 74 thermometer(storage),
MrBedfordVan 0:b9164b348919 75 io(MICROBIT_ID_IO_P0,MICROBIT_ID_IO_P1,MICROBIT_ID_IO_P2,
MrBedfordVan 0:b9164b348919 76 MICROBIT_ID_IO_P3,MICROBIT_ID_IO_P4,MICROBIT_ID_IO_P5,
MrBedfordVan 0:b9164b348919 77 MICROBIT_ID_IO_P6,MICROBIT_ID_IO_P7,MICROBIT_ID_IO_P8,
MrBedfordVan 0:b9164b348919 78 MICROBIT_ID_IO_P9,MICROBIT_ID_IO_P10,MICROBIT_ID_IO_P11,
MrBedfordVan 0:b9164b348919 79 MICROBIT_ID_IO_P12,MICROBIT_ID_IO_P13,MICROBIT_ID_IO_P14,
MrBedfordVan 0:b9164b348919 80 MICROBIT_ID_IO_P15,MICROBIT_ID_IO_P16,MICROBIT_ID_IO_P19,
MrBedfordVan 0:b9164b348919 81 MICROBIT_ID_IO_P20),
MrBedfordVan 0:b9164b348919 82 bleManager(storage),
MrBedfordVan 0:b9164b348919 83 radio(),
MrBedfordVan 0:b9164b348919 84 ble(NULL)
MrBedfordVan 0:b9164b348919 85 {
MrBedfordVan 0:b9164b348919 86 // Clear our status
MrBedfordVan 0:b9164b348919 87 status = 0;
MrBedfordVan 0:b9164b348919 88
MrBedfordVan 0:b9164b348919 89 // Bring up soft reset functionality as soon as possible.
MrBedfordVan 0:b9164b348919 90 resetButton.mode(PullUp);
MrBedfordVan 0:b9164b348919 91 resetButton.fall(this, &MicroBit::reset);
MrBedfordVan 0:b9164b348919 92 }
MrBedfordVan 0:b9164b348919 93
MrBedfordVan 0:b9164b348919 94 /**
MrBedfordVan 0:b9164b348919 95 * Post constructor initialisation method.
MrBedfordVan 0:b9164b348919 96 *
MrBedfordVan 0:b9164b348919 97 * This call will initialised the scheduler, memory allocator and Bluetooth stack.
MrBedfordVan 0:b9164b348919 98 *
MrBedfordVan 0:b9164b348919 99 * This is required as the Bluetooth stack can't be brought up in a
MrBedfordVan 0:b9164b348919 100 * static context i.e. in a constructor.
MrBedfordVan 0:b9164b348919 101 *
MrBedfordVan 0:b9164b348919 102 * @code
MrBedfordVan 0:b9164b348919 103 * uBit.init();
MrBedfordVan 0:b9164b348919 104 * @endcode
MrBedfordVan 0:b9164b348919 105 *
MrBedfordVan 0:b9164b348919 106 * @note This method must be called before user code utilises any functionality
MrBedfordVan 0:b9164b348919 107 * contained by uBit.
MrBedfordVan 0:b9164b348919 108 */
MrBedfordVan 0:b9164b348919 109 void MicroBit::init()
MrBedfordVan 0:b9164b348919 110 {
MrBedfordVan 0:b9164b348919 111 if (status & MICROBIT_INITIALIZED)
MrBedfordVan 0:b9164b348919 112 return;
MrBedfordVan 0:b9164b348919 113
MrBedfordVan 0:b9164b348919 114 #if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR)
MrBedfordVan 0:b9164b348919 115 // Bring up a nested heap allocator.
MrBedfordVan 0:b9164b348919 116 microbit_create_nested_heap(MICROBIT_NESTED_HEAP_SIZE);
MrBedfordVan 0:b9164b348919 117 #endif
MrBedfordVan 0:b9164b348919 118
MrBedfordVan 0:b9164b348919 119 // Bring up fiber scheduler.
MrBedfordVan 0:b9164b348919 120 scheduler_init(messageBus);
MrBedfordVan 0:b9164b348919 121
MrBedfordVan 0:b9164b348919 122 // Seed our random number generator
MrBedfordVan 0:b9164b348919 123 seedRandom();
MrBedfordVan 0:b9164b348919 124
MrBedfordVan 0:b9164b348919 125 // Create an event handler to trap any handlers being created for I2C services.
MrBedfordVan 0:b9164b348919 126 // We do this to enable initialisation of those services only when they're used,
MrBedfordVan 0:b9164b348919 127 // which saves processor time, memeory and battery life.
MrBedfordVan 0:b9164b348919 128 messageBus.listen(MICROBIT_ID_MESSAGE_BUS_LISTENER, MICROBIT_EVT_ANY, this, &MicroBit::onListenerRegisteredEvent);
MrBedfordVan 0:b9164b348919 129
MrBedfordVan 0:b9164b348919 130 status |= MICROBIT_INITIALIZED;
MrBedfordVan 0:b9164b348919 131
MrBedfordVan 0:b9164b348919 132 #if CONFIG_ENABLED(MICROBIT_BLE_PAIRING_MODE)
MrBedfordVan 0:b9164b348919 133 // Test if we need to enter BLE pairing mode...
MrBedfordVan 0:b9164b348919 134 int i=0;
MrBedfordVan 0:b9164b348919 135 sleep(100);
MrBedfordVan 0:b9164b348919 136 while (buttonA.isPressed() && buttonB.isPressed() && i<10)
MrBedfordVan 0:b9164b348919 137 {
MrBedfordVan 0:b9164b348919 138 sleep(100);
MrBedfordVan 0:b9164b348919 139 i++;
MrBedfordVan 0:b9164b348919 140
MrBedfordVan 0:b9164b348919 141 if (i == 10)
MrBedfordVan 0:b9164b348919 142 {
MrBedfordVan 0:b9164b348919 143 #if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR) && CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
MrBedfordVan 0:b9164b348919 144 microbit_create_heap(MICROBIT_SD_GATT_TABLE_START + MICROBIT_SD_GATT_TABLE_SIZE, MICROBIT_SD_LIMIT);
MrBedfordVan 0:b9164b348919 145 #endif
MrBedfordVan 0:b9164b348919 146 // Start the BLE stack, if it isn't already running.
MrBedfordVan 0:b9164b348919 147 if (!ble)
MrBedfordVan 0:b9164b348919 148 {
MrBedfordVan 0:b9164b348919 149 bleManager.init(getName(), getSerial(), messageBus, true);
MrBedfordVan 0:b9164b348919 150 ble = bleManager.ble;
MrBedfordVan 0:b9164b348919 151 }
MrBedfordVan 0:b9164b348919 152
MrBedfordVan 0:b9164b348919 153 // Enter pairing mode, using the LED matrix for any necessary pairing operations
MrBedfordVan 0:b9164b348919 154 bleManager.pairingMode(display, buttonA);
MrBedfordVan 0:b9164b348919 155 }
MrBedfordVan 0:b9164b348919 156 }
MrBedfordVan 0:b9164b348919 157 #endif
MrBedfordVan 0:b9164b348919 158
MrBedfordVan 0:b9164b348919 159 // Attempt to bring up a second heap region, using unused memory normally reserved for Soft Device.
MrBedfordVan 0:b9164b348919 160 #if CONFIG_ENABLED(MICROBIT_HEAP_ALLOCATOR) && CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
MrBedfordVan 0:b9164b348919 161 #if CONFIG_ENABLED(MICROBIT_BLE_ENABLED)
MrBedfordVan 0:b9164b348919 162 microbit_create_heap(MICROBIT_SD_GATT_TABLE_START + MICROBIT_SD_GATT_TABLE_SIZE, MICROBIT_SD_LIMIT);
MrBedfordVan 0:b9164b348919 163 #else
MrBedfordVan 0:b9164b348919 164 microbit_create_heap(MICROBIT_SRAM_BASE, MICROBIT_SD_LIMIT);
MrBedfordVan 0:b9164b348919 165 #endif
MrBedfordVan 0:b9164b348919 166 #endif
MrBedfordVan 0:b9164b348919 167
MrBedfordVan 0:b9164b348919 168 #if CONFIG_ENABLED(MICROBIT_BLE_ENABLED)
MrBedfordVan 0:b9164b348919 169 // Start the BLE stack, if it isn't already running.
MrBedfordVan 0:b9164b348919 170 if (!ble)
MrBedfordVan 0:b9164b348919 171 {
MrBedfordVan 0:b9164b348919 172 bleManager.init(getName(), getSerial(), messageBus, false);
MrBedfordVan 0:b9164b348919 173 ble = bleManager.ble;
MrBedfordVan 0:b9164b348919 174 }
MrBedfordVan 0:b9164b348919 175 #endif
MrBedfordVan 0:b9164b348919 176 }
MrBedfordVan 0:b9164b348919 177
MrBedfordVan 0:b9164b348919 178 /**
MrBedfordVan 0:b9164b348919 179 * A listener to perform actions as a result of Message Bus reflection.
MrBedfordVan 0:b9164b348919 180 *
MrBedfordVan 0:b9164b348919 181 * In some cases we want to perform lazy instantiation of components, such as
MrBedfordVan 0:b9164b348919 182 * the compass and the accelerometer, where we only want to add them to the idle
MrBedfordVan 0:b9164b348919 183 * fiber when someone has the intention of using these components.
MrBedfordVan 0:b9164b348919 184 */
MrBedfordVan 0:b9164b348919 185 void MicroBit::onListenerRegisteredEvent(MicroBitEvent evt)
MrBedfordVan 0:b9164b348919 186 {
MrBedfordVan 0:b9164b348919 187 switch(evt.value)
MrBedfordVan 0:b9164b348919 188 {
MrBedfordVan 0:b9164b348919 189 case MICROBIT_ID_BUTTON_AB:
MrBedfordVan 0:b9164b348919 190 // A user has registered to receive events from the buttonAB multibutton.
MrBedfordVan 0:b9164b348919 191 // Disable click events from being generated by ButtonA and ButtonB, and defer the
MrBedfordVan 0:b9164b348919 192 // control of this to the multibutton handler.
MrBedfordVan 0:b9164b348919 193 //
MrBedfordVan 0:b9164b348919 194 // This way, buttons look independent unless a buttonAB is requested, at which
MrBedfordVan 0:b9164b348919 195 // point button A+B clicks can be correclty handled without breaking
MrBedfordVan 0:b9164b348919 196 // causal ordering.
MrBedfordVan 0:b9164b348919 197 buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
MrBedfordVan 0:b9164b348919 198 buttonB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
MrBedfordVan 0:b9164b348919 199 buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
MrBedfordVan 0:b9164b348919 200 break;
MrBedfordVan 0:b9164b348919 201
MrBedfordVan 0:b9164b348919 202 case MICROBIT_ID_COMPASS:
MrBedfordVan 0:b9164b348919 203 // A listener has been registered for the compass.
MrBedfordVan 0:b9164b348919 204 // The compass uses lazy instantiation, we just need to read the data once to start it running.
MrBedfordVan 0:b9164b348919 205 // Touch the compass through the heading() function to ensure it is calibrated. if it isn't this will launch any associated calibration algorithms.
MrBedfordVan 0:b9164b348919 206 compass.heading();
MrBedfordVan 0:b9164b348919 207
MrBedfordVan 0:b9164b348919 208 break;
MrBedfordVan 0:b9164b348919 209
MrBedfordVan 0:b9164b348919 210 case MICROBIT_ID_ACCELEROMETER:
MrBedfordVan 0:b9164b348919 211 case MICROBIT_ID_GESTURE:
MrBedfordVan 0:b9164b348919 212 // A listener has been registered for the accelerometer.
MrBedfordVan 0:b9164b348919 213 // The accelerometer uses lazy instantiation, we just need to read the data once to start it running.
MrBedfordVan 0:b9164b348919 214 accelerometer.updateSample();
MrBedfordVan 0:b9164b348919 215 break;
MrBedfordVan 0:b9164b348919 216
MrBedfordVan 0:b9164b348919 217 case MICROBIT_ID_THERMOMETER:
MrBedfordVan 0:b9164b348919 218 // A listener has been registered for the thermometer.
MrBedfordVan 0:b9164b348919 219 // The thermometer uses lazy instantiation, we just need to read the data once to start it running.
MrBedfordVan 0:b9164b348919 220 thermometer.updateSample();
MrBedfordVan 0:b9164b348919 221 break;
MrBedfordVan 0:b9164b348919 222 }
MrBedfordVan 0:b9164b348919 223 }