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