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_IO_PIN_SERVICE_H
MrBedfordVan 0:b9164b348919 27 #define MICROBIT_IO_PIN_SERVICE_H
MrBedfordVan 0:b9164b348919 28
MrBedfordVan 0:b9164b348919 29 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 30 #include "ble/BLE.h"
MrBedfordVan 0:b9164b348919 31 #include "MicroBitIO.h"
MrBedfordVan 0:b9164b348919 32
MrBedfordVan 0:b9164b348919 33 #define MICROBIT_IO_PIN_SERVICE_PINCOUNT 19
MrBedfordVan 0:b9164b348919 34 #define MICROBIT_IO_PIN_SERVICE_DATA_SIZE 10
MrBedfordVan 0:b9164b348919 35
MrBedfordVan 0:b9164b348919 36 // UUIDs for our service and characteristics
MrBedfordVan 0:b9164b348919 37 extern const uint8_t MicroBitIOPinServiceUUID[];
MrBedfordVan 0:b9164b348919 38 extern const uint8_t MicroBitIOPinServiceADConfigurationUUID[];
MrBedfordVan 0:b9164b348919 39 extern const uint8_t MicroBitIOPinServiceIOConfigurationUUID[];
MrBedfordVan 0:b9164b348919 40 extern const uint8_t MicroBitIOPinServiceDataUUID[];
MrBedfordVan 0:b9164b348919 41 extern MicroBitPin * const MicroBitIOPins[];
MrBedfordVan 0:b9164b348919 42
MrBedfordVan 0:b9164b348919 43 /**
MrBedfordVan 0:b9164b348919 44 * Name value pair definition, as used to read and write pin values over BLE.
MrBedfordVan 0:b9164b348919 45 */
MrBedfordVan 0:b9164b348919 46 struct IOData
MrBedfordVan 0:b9164b348919 47 {
MrBedfordVan 0:b9164b348919 48 uint8_t pin;
MrBedfordVan 0:b9164b348919 49 uint8_t value;
MrBedfordVan 0:b9164b348919 50 };
MrBedfordVan 0:b9164b348919 51
MrBedfordVan 0:b9164b348919 52 /**
MrBedfordVan 0:b9164b348919 53 * Class definition for the custom MicroBit IOPin Service.
MrBedfordVan 0:b9164b348919 54 * Provides a BLE service to remotely read the state of the I/O Pin, and configure its behaviour.
MrBedfordVan 0:b9164b348919 55 */
MrBedfordVan 0:b9164b348919 56 class MicroBitIOPinService : public MicroBitComponent
MrBedfordVan 0:b9164b348919 57 {
MrBedfordVan 0:b9164b348919 58 public:
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 /**
MrBedfordVan 0:b9164b348919 61 * Constructor.
MrBedfordVan 0:b9164b348919 62 * Create a representation of the IOPinService
MrBedfordVan 0:b9164b348919 63 * @param _ble The instance of a BLE device that we're running on.
MrBedfordVan 0:b9164b348919 64 * @param _io An instance of MicroBitIO that this service will use to perform
MrBedfordVan 0:b9164b348919 65 * I/O operations.
MrBedfordVan 0:b9164b348919 66 */
MrBedfordVan 0:b9164b348919 67 MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io);
MrBedfordVan 0:b9164b348919 68
MrBedfordVan 0:b9164b348919 69 /**
MrBedfordVan 0:b9164b348919 70 * Periodic callback from MicroBit scheduler.
MrBedfordVan 0:b9164b348919 71 *
MrBedfordVan 0:b9164b348919 72 * Check if any of the pins we're watching need updating. Notify any connected
MrBedfordVan 0:b9164b348919 73 * device with any changes.
MrBedfordVan 0:b9164b348919 74 */
MrBedfordVan 0:b9164b348919 75 virtual void idleTick();
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77 private:
MrBedfordVan 0:b9164b348919 78
MrBedfordVan 0:b9164b348919 79 /**
MrBedfordVan 0:b9164b348919 80 * Callback. Invoked when any of our attributes are written via BLE.
MrBedfordVan 0:b9164b348919 81 */
MrBedfordVan 0:b9164b348919 82 void onDataWritten(const GattWriteCallbackParams *params);
MrBedfordVan 0:b9164b348919 83
MrBedfordVan 0:b9164b348919 84 /**
MrBedfordVan 0:b9164b348919 85 * Callback. invoked when the BLE data characteristic is read.
MrBedfordVan 0:b9164b348919 86 *
MrBedfordVan 0:b9164b348919 87 * Reads all the pins marked as inputs, and updates the data stored in the characteristic.
MrBedfordVan 0:b9164b348919 88 */
MrBedfordVan 0:b9164b348919 89 void onDataRead(GattReadAuthCallbackParams *params);
MrBedfordVan 0:b9164b348919 90
MrBedfordVan 0:b9164b348919 91 /**
MrBedfordVan 0:b9164b348919 92 * Determines if the given pin was configured as a digital pin by the BLE ADPinConfigurationCharacterisitic.
MrBedfordVan 0:b9164b348919 93 *
MrBedfordVan 0:b9164b348919 94 * @param i the enumeration of the pin to test
MrBedfordVan 0:b9164b348919 95 * @return 1 if this pin is configured as digital, 0 otherwise
MrBedfordVan 0:b9164b348919 96 */
MrBedfordVan 0:b9164b348919 97 int isDigital(int i);
MrBedfordVan 0:b9164b348919 98
MrBedfordVan 0:b9164b348919 99 /**
MrBedfordVan 0:b9164b348919 100 * Determines if the given pin was configured as an analog pin by the BLE ADPinConfigurationCharacterisitic.
MrBedfordVan 0:b9164b348919 101 *
MrBedfordVan 0:b9164b348919 102 * @param i the enumeration of the pin to test
MrBedfordVan 0:b9164b348919 103 * @return 1 if this pin is configured as analog, 0 otherwise
MrBedfordVan 0:b9164b348919 104 */
MrBedfordVan 0:b9164b348919 105 int isAnalog(int i);
MrBedfordVan 0:b9164b348919 106
MrBedfordVan 0:b9164b348919 107 /**
MrBedfordVan 0:b9164b348919 108 * Determines if the given pin was configured as an input by the BLE IOPinConfigurationCharacterisitic.
MrBedfordVan 0:b9164b348919 109 *
MrBedfordVan 0:b9164b348919 110 * @param i the enumeration of the pin to test
MrBedfordVan 0:b9164b348919 111 * @return 1 if this pin is configured as an input, 0 otherwise
MrBedfordVan 0:b9164b348919 112 */
MrBedfordVan 0:b9164b348919 113 int isInput(int i);
MrBedfordVan 0:b9164b348919 114
MrBedfordVan 0:b9164b348919 115 /**
MrBedfordVan 0:b9164b348919 116 * Determines if the given pin was configured as output by the BLE IOPinConfigurationCharacterisitic.
MrBedfordVan 0:b9164b348919 117 *
MrBedfordVan 0:b9164b348919 118 * @param i the enumeration of the pin to test
MrBedfordVan 0:b9164b348919 119 * @return 1 if this pin is configured as an output, 0 otherwise
MrBedfordVan 0:b9164b348919 120 */
MrBedfordVan 0:b9164b348919 121 int isOutput(int i);
MrBedfordVan 0:b9164b348919 122
MrBedfordVan 0:b9164b348919 123
MrBedfordVan 0:b9164b348919 124 // Bluetooth stack we're running on.
MrBedfordVan 0:b9164b348919 125 BLEDevice &ble;
MrBedfordVan 0:b9164b348919 126 MicroBitIO &io;
MrBedfordVan 0:b9164b348919 127
MrBedfordVan 0:b9164b348919 128 // memory for our 8 bit control characteristics.
MrBedfordVan 0:b9164b348919 129 uint32_t ioPinServiceADCharacteristicBuffer;
MrBedfordVan 0:b9164b348919 130 uint32_t ioPinServiceIOCharacteristicBuffer;
MrBedfordVan 0:b9164b348919 131 IOData ioPinServiceDataCharacteristicBuffer[MICROBIT_IO_PIN_SERVICE_DATA_SIZE];
MrBedfordVan 0:b9164b348919 132
MrBedfordVan 0:b9164b348919 133 // Historic information about our pin data data.
MrBedfordVan 0:b9164b348919 134 uint8_t ioPinServiceIOData[MICROBIT_IO_PIN_SERVICE_PINCOUNT];
MrBedfordVan 0:b9164b348919 135
MrBedfordVan 0:b9164b348919 136 // Handles to access each characteristic when they are held by Soft Device.
MrBedfordVan 0:b9164b348919 137 GattAttribute::Handle_t ioPinServiceADCharacteristicHandle;
MrBedfordVan 0:b9164b348919 138 GattAttribute::Handle_t ioPinServiceIOCharacteristicHandle;
MrBedfordVan 0:b9164b348919 139 GattCharacteristic *ioPinServiceDataCharacteristic;
MrBedfordVan 0:b9164b348919 140 };
MrBedfordVan 0:b9164b348919 141
MrBedfordVan 0:b9164b348919 142
MrBedfordVan 0:b9164b348919 143 #endif