Hexiwear library for communicating with the on-board KW40Z BLE device. KW40Z handles also the touch buttons.
Dependents: Hexi_Buttons_Example Hexi_Click_Relay-v2_Example Hexi_Click_Relay-v3_Example Hexi_Catch-the-dot_Game ... more
Hexi_KW40Z.h@0:c2d52562f36b, 2016-09-19 (annotated)
- Committer:
- cotigac
- Date:
- Mon Sep 19 02:46:28 2016 +0000
- Revision:
- 0:c2d52562f36b
- Child:
- 1:f6f9b24aea57
Initial version of KW40Z library for Hexiwear
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cotigac | 0:c2d52562f36b | 1 | /** BLE KW40Z Driver for Hexiwear |
cotigac | 0:c2d52562f36b | 2 | * This file contains BLE and Touch Buttons driver functionality for Hexiwear |
cotigac | 0:c2d52562f36b | 3 | * |
cotigac | 0:c2d52562f36b | 4 | * Redistribution and use in source and binary forms, with or without modification, |
cotigac | 0:c2d52562f36b | 5 | * are permitted provided that the following conditions are met: |
cotigac | 0:c2d52562f36b | 6 | * |
cotigac | 0:c2d52562f36b | 7 | * Redistributions of source code must retain the above copyright notice, this list |
cotigac | 0:c2d52562f36b | 8 | * of conditions and the following disclaimer. |
cotigac | 0:c2d52562f36b | 9 | * |
cotigac | 0:c2d52562f36b | 10 | * Redistributions in binary form must reproduce the above copyright notice, this |
cotigac | 0:c2d52562f36b | 11 | * list of conditions and the following disclaimer in the documentation and/or |
cotigac | 0:c2d52562f36b | 12 | * other materials provided with the distribution. |
cotigac | 0:c2d52562f36b | 13 | * |
cotigac | 0:c2d52562f36b | 14 | * Neither the name of NXP, nor the names of its |
cotigac | 0:c2d52562f36b | 15 | * contributors may be used to endorse or promote products derived from this |
cotigac | 0:c2d52562f36b | 16 | * software without specific prior written permission. |
cotigac | 0:c2d52562f36b | 17 | * |
cotigac | 0:c2d52562f36b | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
cotigac | 0:c2d52562f36b | 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
cotigac | 0:c2d52562f36b | 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cotigac | 0:c2d52562f36b | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
cotigac | 0:c2d52562f36b | 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
cotigac | 0:c2d52562f36b | 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
cotigac | 0:c2d52562f36b | 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
cotigac | 0:c2d52562f36b | 25 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
cotigac | 0:c2d52562f36b | 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
cotigac | 0:c2d52562f36b | 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cotigac | 0:c2d52562f36b | 28 | * |
cotigac | 0:c2d52562f36b | 29 | * visit: http://www.mikroe.com and http://www.nxp.com |
cotigac | 0:c2d52562f36b | 30 | * |
cotigac | 0:c2d52562f36b | 31 | * get support at: http://www.mikroe.com/forum and https://community.nxp.com |
cotigac | 0:c2d52562f36b | 32 | * |
cotigac | 0:c2d52562f36b | 33 | * Project HEXIWEAR, 2015 |
cotigac | 0:c2d52562f36b | 34 | */ |
cotigac | 0:c2d52562f36b | 35 | |
cotigac | 0:c2d52562f36b | 36 | #ifndef HG_HEXI_KW40Z |
cotigac | 0:c2d52562f36b | 37 | #define HG_HEXI_KW40Z |
cotigac | 0:c2d52562f36b | 38 | |
cotigac | 0:c2d52562f36b | 39 | #include "mbed.h" |
cotigac | 0:c2d52562f36b | 40 | #include "rtos.h" |
cotigac | 0:c2d52562f36b | 41 | |
cotigac | 0:c2d52562f36b | 42 | //#define LIB_DEBUG 1 |
cotigac | 0:c2d52562f36b | 43 | |
cotigac | 0:c2d52562f36b | 44 | #define START_THREAD 1 |
cotigac | 0:c2d52562f36b | 45 | |
cotigac | 0:c2d52562f36b | 46 | #define gHostInterface_startByte1 0x55 |
cotigac | 0:c2d52562f36b | 47 | #define gHostInterface_startByte2 0xAA |
cotigac | 0:c2d52562f36b | 48 | #define gHostInterface_trailerByte 0x45 |
cotigac | 0:c2d52562f36b | 49 | |
cotigac | 0:c2d52562f36b | 50 | #define gHostInterface_dataSize 23 |
cotigac | 0:c2d52562f36b | 51 | #define gHostInterface_headerSize 4 |
cotigac | 0:c2d52562f36b | 52 | |
cotigac | 0:c2d52562f36b | 53 | #define gHostInterface_retransmitCount 3 |
cotigac | 0:c2d52562f36b | 54 | #define gHostInterface_retransmitTimeout 100 |
cotigac | 0:c2d52562f36b | 55 | |
cotigac | 0:c2d52562f36b | 56 | #define gHostInterface_TxConfirmationEnable 1 // send confirmation when receive packet |
cotigac | 0:c2d52562f36b | 57 | #define gHostInterface_RxConfirmationEnable 1 // wait on confirmation from remote side (do retransmit) |
cotigac | 0:c2d52562f36b | 58 | |
cotigac | 0:c2d52562f36b | 59 | /** HEXIWEAR firmware version */ |
cotigac | 0:c2d52562f36b | 60 | #define HEXIWEAR_VERSION_PATCH ( 2 ) |
cotigac | 0:c2d52562f36b | 61 | #define HEXIWEAR_VERSION_MINOR ( 0 ) |
cotigac | 0:c2d52562f36b | 62 | #define HEXIWEAR_VERSION_MAJOR ( 1 ) |
cotigac | 0:c2d52562f36b | 63 | |
cotigac | 0:c2d52562f36b | 64 | /** packet types */ |
cotigac | 0:c2d52562f36b | 65 | typedef enum |
cotigac | 0:c2d52562f36b | 66 | { |
cotigac | 0:c2d52562f36b | 67 | packetType_pressUp = 0, /**< touch press up */ |
cotigac | 0:c2d52562f36b | 68 | packetType_pressDown = 1, /**< touch press down */ |
cotigac | 0:c2d52562f36b | 69 | packetType_pressLeft = 2, /**< touch press left */ |
cotigac | 0:c2d52562f36b | 70 | packetType_pressRight = 3, /**< touch press right */ |
cotigac | 0:c2d52562f36b | 71 | packetType_slide = 4, /**< touch slide */ |
cotigac | 0:c2d52562f36b | 72 | |
cotigac | 0:c2d52562f36b | 73 | packetType_batteryLevel = 5, /**< battery Service */ |
cotigac | 0:c2d52562f36b | 74 | |
cotigac | 0:c2d52562f36b | 75 | packetType_accel = 6, /**< motion service */ |
cotigac | 0:c2d52562f36b | 76 | packetType_ambiLight = 7, /**< weather service */ |
cotigac | 0:c2d52562f36b | 77 | packetType_pressure = 8, /**< weather service */ |
cotigac | 0:c2d52562f36b | 78 | |
cotigac | 0:c2d52562f36b | 79 | |
cotigac | 0:c2d52562f36b | 80 | packetType_gyro = 9, /**< motion service */ |
cotigac | 0:c2d52562f36b | 81 | packetType_temperature = 10, /**< weather service */ |
cotigac | 0:c2d52562f36b | 82 | packetType_humidity = 11, /**< weather service */ |
cotigac | 0:c2d52562f36b | 83 | packetType_magnet = 12, /**< motion service */ |
cotigac | 0:c2d52562f36b | 84 | |
cotigac | 0:c2d52562f36b | 85 | packetType_heartRate = 13, /**< health service */ |
cotigac | 0:c2d52562f36b | 86 | packetType_steps = 14, /**< health service */ |
cotigac | 0:c2d52562f36b | 87 | packetType_calories = 15, /**< health service */ |
cotigac | 0:c2d52562f36b | 88 | |
cotigac | 0:c2d52562f36b | 89 | /* Alert Service */ |
cotigac | 0:c2d52562f36b | 90 | packetType_alertIn = 16, /**< incoming alerts */ |
cotigac | 0:c2d52562f36b | 91 | packetType_alertOut = 17, /**< outcoming alerts */ |
cotigac | 0:c2d52562f36b | 92 | |
cotigac | 0:c2d52562f36b | 93 | packetType_passDisplay = 18, /**< key display type */ |
cotigac | 0:c2d52562f36b | 94 | |
cotigac | 0:c2d52562f36b | 95 | /* OTAP procedure types */ |
cotigac | 0:c2d52562f36b | 96 | packetType_otapKW40Started = 19, |
cotigac | 0:c2d52562f36b | 97 | packetType_otapMK64Started = 20, |
cotigac | 0:c2d52562f36b | 98 | packetType_otapCompleted = 21, |
cotigac | 0:c2d52562f36b | 99 | packetType_otapFailed = 22, |
cotigac | 0:c2d52562f36b | 100 | |
cotigac | 0:c2d52562f36b | 101 | /* active buttons types */ |
cotigac | 0:c2d52562f36b | 102 | packetType_buttonsGroupToggleActive = 23, |
cotigac | 0:c2d52562f36b | 103 | packetType_buttonsGroupGetActive = 24, |
cotigac | 0:c2d52562f36b | 104 | packetType_buttonsGroupSendActive = 25, |
cotigac | 0:c2d52562f36b | 105 | |
cotigac | 0:c2d52562f36b | 106 | /* Turn off/on bluetooth advertising */ |
cotigac | 0:c2d52562f36b | 107 | packetType_advModeGet = 26, |
cotigac | 0:c2d52562f36b | 108 | packetType_advModeSend = 27, |
cotigac | 0:c2d52562f36b | 109 | packetType_advModeToggle = 28, |
cotigac | 0:c2d52562f36b | 110 | |
cotigac | 0:c2d52562f36b | 111 | packetType_appMode = 29, /**< app mode service */ |
cotigac | 0:c2d52562f36b | 112 | |
cotigac | 0:c2d52562f36b | 113 | /* Link State */ |
cotigac | 0:c2d52562f36b | 114 | packetType_linkStateGet = 30, /**< connected */ |
cotigac | 0:c2d52562f36b | 115 | packetType_linkStateSend = 31, /**< disconnected */ |
cotigac | 0:c2d52562f36b | 116 | |
cotigac | 0:c2d52562f36b | 117 | packetType_notification = 32, /**< notifications */ |
cotigac | 0:c2d52562f36b | 118 | |
cotigac | 0:c2d52562f36b | 119 | packetType_buildVersion = 33, /**< build version */ |
cotigac | 0:c2d52562f36b | 120 | |
cotigac | 0:c2d52562f36b | 121 | packetType_sleepON = 34, /**< sleep ON */ |
cotigac | 0:c2d52562f36b | 122 | packetType_sleepOFF = 35, /**< sleep OFF */ |
cotigac | 0:c2d52562f36b | 123 | |
cotigac | 0:c2d52562f36b | 124 | packetType_OK = 255 /**< OK packet */ |
cotigac | 0:c2d52562f36b | 125 | } hostInterface_packetType_t; |
cotigac | 0:c2d52562f36b | 126 | |
cotigac | 0:c2d52562f36b | 127 | /** data-packet structure */ |
cotigac | 0:c2d52562f36b | 128 | typedef struct |
cotigac | 0:c2d52562f36b | 129 | { |
cotigac | 0:c2d52562f36b | 130 | /* NOTE: Size of struct must be multiplier of 4! */ |
cotigac | 0:c2d52562f36b | 131 | uint8_t start1; |
cotigac | 0:c2d52562f36b | 132 | uint8_t start2; |
cotigac | 0:c2d52562f36b | 133 | hostInterface_packetType_t type; |
cotigac | 0:c2d52562f36b | 134 | uint8_t length; |
cotigac | 0:c2d52562f36b | 135 | uint8_t data[gHostInterface_dataSize + 1]; |
cotigac | 0:c2d52562f36b | 136 | } hostInterface_packet_t; |
cotigac | 0:c2d52562f36b | 137 | |
cotigac | 0:c2d52562f36b | 138 | /** incoming alert types */ |
cotigac | 0:c2d52562f36b | 139 | typedef enum |
cotigac | 0:c2d52562f36b | 140 | { |
cotigac | 0:c2d52562f36b | 141 | alertIn_type_notification = 1, |
cotigac | 0:c2d52562f36b | 142 | alertIn_type_settings = 2, |
cotigac | 0:c2d52562f36b | 143 | alertIn_type_timeUpdate = 3, |
cotigac | 0:c2d52562f36b | 144 | } hostInterface_alertIn_type_t; |
cotigac | 0:c2d52562f36b | 145 | |
cotigac | 0:c2d52562f36b | 146 | typedef enum |
cotigac | 0:c2d52562f36b | 147 | { |
cotigac | 0:c2d52562f36b | 148 | pressUp = 0, /**< touch press up */ |
cotigac | 0:c2d52562f36b | 149 | pressDown = 1, /**< touch press down */ |
cotigac | 0:c2d52562f36b | 150 | pressLeft = 2, /**< touch press left */ |
cotigac | 0:c2d52562f36b | 151 | pressRight = 3, /**< touch press right */ |
cotigac | 0:c2d52562f36b | 152 | slide = 4, /**< touch slide */ |
cotigac | 0:c2d52562f36b | 153 | } hexi_buttons_t; |
cotigac | 0:c2d52562f36b | 154 | |
cotigac | 0:c2d52562f36b | 155 | typedef struct |
cotigac | 0:c2d52562f36b | 156 | { |
cotigac | 0:c2d52562f36b | 157 | void (*buttons)(hexi_buttons_t button); |
cotigac | 0:c2d52562f36b | 158 | void (*alert)(uint8_t *data, uint8_t length); |
cotigac | 0:c2d52562f36b | 159 | void (*passkey)(uint8_t *data); |
cotigac | 0:c2d52562f36b | 160 | void (*notifications)(uint8_t eventId, uint8_t categoryId); |
cotigac | 0:c2d52562f36b | 161 | } kw40z_callbacks_t; |
cotigac | 0:c2d52562f36b | 162 | |
cotigac | 0:c2d52562f36b | 163 | typedef struct name |
cotigac | 0:c2d52562f36b | 164 | { |
cotigac | 0:c2d52562f36b | 165 | uint8_t ver_patchNumber; |
cotigac | 0:c2d52562f36b | 166 | uint8_t ver_minorNumber; |
cotigac | 0:c2d52562f36b | 167 | uint8_t ver_majorNumber; |
cotigac | 0:c2d52562f36b | 168 | |
cotigac | 0:c2d52562f36b | 169 | } hexiwear_version_t; |
cotigac | 0:c2d52562f36b | 170 | |
cotigac | 0:c2d52562f36b | 171 | class KW40Z{ |
cotigac | 0:c2d52562f36b | 172 | |
cotigac | 0:c2d52562f36b | 173 | public: |
cotigac | 0:c2d52562f36b | 174 | |
cotigac | 0:c2d52562f36b | 175 | /** |
cotigac | 0:c2d52562f36b | 176 | * Create a Hexiwear BLE KW40Z Driver connected to the UART pins |
cotigac | 0:c2d52562f36b | 177 | * |
cotigac | 0:c2d52562f36b | 178 | * @param txPin UART TX pin |
cotigac | 0:c2d52562f36b | 179 | * @param rxPin UART RX pin |
cotigac | 0:c2d52562f36b | 180 | */ |
cotigac | 0:c2d52562f36b | 181 | KW40Z(PinName txPin,PinName rxPin); |
cotigac | 0:c2d52562f36b | 182 | |
cotigac | 0:c2d52562f36b | 183 | /** |
cotigac | 0:c2d52562f36b | 184 | * Destroy the Hexiwear instance |
cotigac | 0:c2d52562f36b | 185 | */ |
cotigac | 0:c2d52562f36b | 186 | ~KW40Z(); |
cotigac | 0:c2d52562f36b | 187 | |
cotigac | 0:c2d52562f36b | 188 | void attach(kw40z_callbacks_t * callbacks); |
cotigac | 0:c2d52562f36b | 189 | void GetVersion(); |
cotigac | 0:c2d52562f36b | 190 | |
cotigac | 0:c2d52562f36b | 191 | private: |
cotigac | 0:c2d52562f36b | 192 | RawSerial device; |
cotigac | 0:c2d52562f36b | 193 | Thread mainThread; |
cotigac | 0:c2d52562f36b | 194 | Thread rxThread; |
cotigac | 0:c2d52562f36b | 195 | |
cotigac | 0:c2d52562f36b | 196 | hostInterface_packet_t hostInterface_rxPacket; |
cotigac | 0:c2d52562f36b | 197 | hostInterface_packet_t hostInterface_txPacket; |
cotigac | 0:c2d52562f36b | 198 | |
cotigac | 0:c2d52562f36b | 199 | kw40z_callbacks_t * kw40z_Cbs; |
cotigac | 0:c2d52562f36b | 200 | uint8_t * rxBuff; |
cotigac | 0:c2d52562f36b | 201 | bool confirmReceived; |
cotigac | 0:c2d52562f36b | 202 | |
cotigac | 0:c2d52562f36b | 203 | MemoryPool<hostInterface_packet_t, 16> mpool; |
cotigac | 0:c2d52562f36b | 204 | Queue<hostInterface_packet_t, 16> queue; |
cotigac | 0:c2d52562f36b | 205 | |
cotigac | 0:c2d52562f36b | 206 | void mainTask(void); |
cotigac | 0:c2d52562f36b | 207 | void rxTask(void); |
cotigac | 0:c2d52562f36b | 208 | |
cotigac | 0:c2d52562f36b | 209 | void ProcessBuffer(); |
cotigac | 0:c2d52562f36b | 210 | void ProcessReceivedPacket(hostInterface_packet_t * rxPacket); |
cotigac | 0:c2d52562f36b | 211 | void SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested); |
cotigac | 0:c2d52562f36b | 212 | void SearchStartByte(); |
cotigac | 0:c2d52562f36b | 213 | void SendPacketOK(); |
cotigac | 0:c2d52562f36b | 214 | void ConfirmPacketOK(); |
cotigac | 0:c2d52562f36b | 215 | |
cotigac | 0:c2d52562f36b | 216 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 217 | void DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 218 | void DebugPrintTxPacket(hostInterface_packet_t * txPacket); |
cotigac | 0:c2d52562f36b | 219 | #endif |
cotigac | 0:c2d52562f36b | 220 | |
cotigac | 0:c2d52562f36b | 221 | static void rxStarter(void const *p); |
cotigac | 0:c2d52562f36b | 222 | static void mainStarter(void const *p); |
cotigac | 0:c2d52562f36b | 223 | }; |
cotigac | 0:c2d52562f36b | 224 | |
cotigac | 0:c2d52562f36b | 225 | #endif |