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_DFU_SERVICE_H
MrBedfordVan 0:b9164b348919 27 #define MICROBIT_DFU_SERVICE_H
MrBedfordVan 0:b9164b348919 28
MrBedfordVan 0:b9164b348919 29 #include "mbed.h"
MrBedfordVan 0:b9164b348919 30 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 31 #include "ble/BLE.h"
MrBedfordVan 0:b9164b348919 32 #include "MicroBitEvent.h"
MrBedfordVan 0:b9164b348919 33
MrBedfordVan 0:b9164b348919 34 // MicroBit ControlPoint OpCodes
MrBedfordVan 0:b9164b348919 35 // Requests transfer to the Nordic DFU bootloader.
MrBedfordVan 0:b9164b348919 36 #define MICROBIT_DFU_OPCODE_START_DFU 1
MrBedfordVan 0:b9164b348919 37
MrBedfordVan 0:b9164b348919 38 // visual ID code constants
MrBedfordVan 0:b9164b348919 39 #define MICROBIT_DFU_HISTOGRAM_WIDTH 5
MrBedfordVan 0:b9164b348919 40 #define MICROBIT_DFU_HISTOGRAM_HEIGHT 5
MrBedfordVan 0:b9164b348919 41
MrBedfordVan 0:b9164b348919 42 // UUIDs for our service and characteristics
MrBedfordVan 0:b9164b348919 43 extern const uint8_t MicroBitDFUServiceUUID[];
MrBedfordVan 0:b9164b348919 44 extern const uint8_t MicroBitDFUServiceControlCharacteristicUUID[];
MrBedfordVan 0:b9164b348919 45 extern const uint8_t MicroBitDFUServiceFlashCodeCharacteristicUUID[];
MrBedfordVan 0:b9164b348919 46
MrBedfordVan 0:b9164b348919 47 // Handle on the memory resident Nordic bootloader.
MrBedfordVan 0:b9164b348919 48 extern "C" void bootloader_start(void);
MrBedfordVan 0:b9164b348919 49
MrBedfordVan 0:b9164b348919 50 /**
MrBedfordVan 0:b9164b348919 51 * Class definition for a MicroBit Device Firmware Update loader.
MrBedfordVan 0:b9164b348919 52 * This service allows hexes to be flashed remotely from another Bluetooth
MrBedfordVan 0:b9164b348919 53 * device.
MrBedfordVan 0:b9164b348919 54 */
MrBedfordVan 0:b9164b348919 55 class MicroBitDFUService
MrBedfordVan 0:b9164b348919 56 {
MrBedfordVan 0:b9164b348919 57 public:
MrBedfordVan 0:b9164b348919 58
MrBedfordVan 0:b9164b348919 59 /**
MrBedfordVan 0:b9164b348919 60 * Constructor.
MrBedfordVan 0:b9164b348919 61 * Initialise the Device Firmware Update service.
MrBedfordVan 0:b9164b348919 62 * @param _ble The instance of a BLE device that we're running on.
MrBedfordVan 0:b9164b348919 63 */
MrBedfordVan 0:b9164b348919 64 MicroBitDFUService(BLEDevice &_ble);
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 /**
MrBedfordVan 0:b9164b348919 67 * Callback. Invoked when any of our attributes are written via BLE.
MrBedfordVan 0:b9164b348919 68 */
MrBedfordVan 0:b9164b348919 69 virtual void onDataWritten(const GattWriteCallbackParams *params);
MrBedfordVan 0:b9164b348919 70
MrBedfordVan 0:b9164b348919 71 private:
MrBedfordVan 0:b9164b348919 72
MrBedfordVan 0:b9164b348919 73 // State of paiting process.
MrBedfordVan 0:b9164b348919 74 bool authenticated;
MrBedfordVan 0:b9164b348919 75 bool flashCodeRequested;
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77 // Bluetooth stack we're running on.
MrBedfordVan 0:b9164b348919 78 BLEDevice &ble;
MrBedfordVan 0:b9164b348919 79
MrBedfordVan 0:b9164b348919 80 // memory for our 8 bit control characteristic.
MrBedfordVan 0:b9164b348919 81 uint8_t controlByte;
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 // BLE pairing name of this device, encoded as an integer.
MrBedfordVan 0:b9164b348919 84 uint32_t flashCode;
MrBedfordVan 0:b9164b348919 85
MrBedfordVan 0:b9164b348919 86 GattAttribute::Handle_t microBitDFUServiceControlCharacteristicHandle;
MrBedfordVan 0:b9164b348919 87 GattAttribute::Handle_t microBitDFUServiceFlashCodeCharacteristicHandle;
MrBedfordVan 0:b9164b348919 88
MrBedfordVan 0:b9164b348919 89 // Displays the device's ID code as a histogram on the LED matrix display.
MrBedfordVan 0:b9164b348919 90 void showNameHistogram();
MrBedfordVan 0:b9164b348919 91
MrBedfordVan 0:b9164b348919 92 // Displays an acknowledgement on the LED matrix display.
MrBedfordVan 0:b9164b348919 93 void showTick();
MrBedfordVan 0:b9164b348919 94
MrBedfordVan 0:b9164b348919 95 // Update BLE characteristic to release our flash code.
MrBedfordVan 0:b9164b348919 96 void releaseFlashCode();
MrBedfordVan 0:b9164b348919 97
MrBedfordVan 0:b9164b348919 98 // Event handlers for button clicks.
MrBedfordVan 0:b9164b348919 99 void onButtonA(MicroBitEvent e);
MrBedfordVan 0:b9164b348919 100 void onButtonB(MicroBitEvent e);
MrBedfordVan 0:b9164b348919 101 };
MrBedfordVan 0:b9164b348919 102
MrBedfordVan 0:b9164b348919 103 #endif