mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitIOPinService.h Source File

MicroBitIOPinService.h

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #ifndef MICROBIT_IO_PIN_SERVICE_H
00027 #define MICROBIT_IO_PIN_SERVICE_H
00028 
00029 #include "MicroBitConfig.h"
00030 #include "ble/BLE.h"
00031 #include "MicroBitIO.h"
00032 
00033 #define MICROBIT_IO_PIN_SERVICE_PINCOUNT       19
00034 #define MICROBIT_IO_PIN_SERVICE_DATA_SIZE      10
00035 
00036 // UUIDs for our service and characteristics
00037 extern const uint8_t  MicroBitIOPinServiceUUID[];
00038 extern const uint8_t  MicroBitIOPinServiceADConfigurationUUID[];
00039 extern const uint8_t  MicroBitIOPinServiceIOConfigurationUUID[];
00040 extern const uint8_t  MicroBitIOPinServiceDataUUID[];
00041 extern MicroBitPin * const MicroBitIOPins[];
00042 
00043 /**
00044   * Name value pair definition, as used to read and write pin values over BLE.
00045   */
00046 struct IOData
00047 {
00048     uint8_t     pin;
00049     uint8_t     value;
00050 };
00051 
00052 /**
00053   * Class definition for the custom MicroBit IOPin Service.
00054   * Provides a BLE service to remotely read the state of the I/O Pin, and configure its behaviour.
00055   */
00056 class MicroBitIOPinService : public MicroBitComponent
00057 {
00058     public:
00059 
00060     /**
00061       * Constructor.
00062       * Create a representation of the IOPinService
00063       * @param _ble The instance of a BLE device that we're running on.
00064       * @param _io An instance of MicroBitIO that this service will use to perform
00065       *            I/O operations.
00066       */
00067     MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io);
00068 
00069     /**
00070      * Periodic callback from MicroBit scheduler.
00071      *
00072      * Check if any of the pins we're watching need updating. Notify any connected
00073      * device with any changes.
00074      */
00075     virtual void idleTick();
00076 
00077     private:
00078 
00079     /**
00080       * Callback. Invoked when any of our attributes are written via BLE.
00081       */
00082     void onDataWritten(const GattWriteCallbackParams *params);
00083 
00084     /**
00085      * Callback. invoked when the BLE data characteristic is read.
00086      *
00087      * Reads all the pins marked as inputs, and updates the data stored in the characteristic.
00088      */
00089     void onDataRead(GattReadAuthCallbackParams *params);
00090 
00091     /**
00092       * Determines if the given pin was configured as a digital pin by the BLE ADPinConfigurationCharacterisitic.
00093       *
00094       * @param i the enumeration of the pin to test
00095       * @return 1 if this pin is configured as digital, 0 otherwise
00096       */
00097     int isDigital(int i);
00098 
00099     /**
00100       * Determines if the given pin was configured as an analog pin by the BLE ADPinConfigurationCharacterisitic.
00101       *
00102       * @param i the enumeration of the pin to test
00103       * @return 1 if this pin is configured as analog, 0 otherwise
00104       */
00105     int isAnalog(int i);
00106 
00107     /**
00108       * Determines if the given pin was configured as an input by the BLE IOPinConfigurationCharacterisitic.
00109       *
00110       * @param i the enumeration of the pin to test
00111       * @return 1 if this pin is configured as an input, 0 otherwise
00112       */
00113     int isInput(int i);
00114 
00115     /**
00116       * Determines if the given pin was configured as output by the BLE IOPinConfigurationCharacterisitic.
00117       *
00118       * @param i the enumeration of the pin to test
00119       * @return 1 if this pin is configured as an output, 0 otherwise
00120       */
00121     int isOutput(int i);
00122 
00123 
00124     // Bluetooth stack we're running on.
00125     BLEDevice           &ble;
00126     MicroBitIO          &io;
00127 
00128     // memory for our 8 bit control characteristics.
00129     uint32_t            ioPinServiceADCharacteristicBuffer;
00130     uint32_t            ioPinServiceIOCharacteristicBuffer;
00131     IOData              ioPinServiceDataCharacteristicBuffer[MICROBIT_IO_PIN_SERVICE_DATA_SIZE];
00132 
00133     // Historic information about our pin data data.
00134     uint8_t             ioPinServiceIOData[MICROBIT_IO_PIN_SERVICE_PINCOUNT];
00135 
00136     // Handles to access each characteristic when they are held by Soft Device.
00137     GattAttribute::Handle_t ioPinServiceADCharacteristicHandle;
00138     GattAttribute::Handle_t ioPinServiceIOCharacteristicHandle;
00139     GattCharacteristic *ioPinServiceDataCharacteristic;
00140 };
00141 
00142 
00143 #endif