Hexiwear library for communicating with the on-board KW40Z BLE device. KW40Z handles also the touch buttons.
Dependents: Hexi_BLE_Example_Modified Hexi_BLE_Example_ModifiedPOTATO
Fork of Hexi_KW40Z by
Hexi_KW40Z.h@6:23323e8aeda4, 2016-09-25 (annotated)
- Committer:
- cotigac
- Date:
- Sun Sep 25 04:28:52 2016 +0000
- Revision:
- 6:23323e8aeda4
- Parent:
- 3:9e92f113c671
- Child:
- 8:2b9b24f3154d
Updated thread initialization in the class constructor as ARM team suggested
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 gHostInterface_startByte1 0x55 |
cotigac | 0:c2d52562f36b | 45 | #define gHostInterface_startByte2 0xAA |
cotigac | 0:c2d52562f36b | 46 | #define gHostInterface_trailerByte 0x45 |
cotigac | 0:c2d52562f36b | 47 | |
cotigac | 0:c2d52562f36b | 48 | #define gHostInterface_dataSize 23 |
cotigac | 0:c2d52562f36b | 49 | #define gHostInterface_headerSize 4 |
cotigac | 0:c2d52562f36b | 50 | |
cotigac | 0:c2d52562f36b | 51 | #define gHostInterface_retransmitCount 3 |
cotigac | 0:c2d52562f36b | 52 | #define gHostInterface_retransmitTimeout 100 |
cotigac | 0:c2d52562f36b | 53 | |
cotigac | 0:c2d52562f36b | 54 | #define gHostInterface_TxConfirmationEnable 1 // send confirmation when receive packet |
cotigac | 0:c2d52562f36b | 55 | #define gHostInterface_RxConfirmationEnable 1 // wait on confirmation from remote side (do retransmit) |
cotigac | 0:c2d52562f36b | 56 | |
cotigac | 0:c2d52562f36b | 57 | /** HEXIWEAR firmware version */ |
cotigac | 0:c2d52562f36b | 58 | #define HEXIWEAR_VERSION_PATCH ( 2 ) |
cotigac | 0:c2d52562f36b | 59 | #define HEXIWEAR_VERSION_MINOR ( 0 ) |
cotigac | 0:c2d52562f36b | 60 | #define HEXIWEAR_VERSION_MAJOR ( 1 ) |
cotigac | 0:c2d52562f36b | 61 | |
cotigac | 0:c2d52562f36b | 62 | /** packet types */ |
cotigac | 0:c2d52562f36b | 63 | typedef enum |
cotigac | 0:c2d52562f36b | 64 | { |
cotigac | 0:c2d52562f36b | 65 | packetType_pressUp = 0, /**< touch press up */ |
cotigac | 0:c2d52562f36b | 66 | packetType_pressDown = 1, /**< touch press down */ |
cotigac | 0:c2d52562f36b | 67 | packetType_pressLeft = 2, /**< touch press left */ |
cotigac | 0:c2d52562f36b | 68 | packetType_pressRight = 3, /**< touch press right */ |
cotigac | 0:c2d52562f36b | 69 | packetType_slide = 4, /**< touch slide */ |
cotigac | 0:c2d52562f36b | 70 | |
cotigac | 0:c2d52562f36b | 71 | packetType_batteryLevel = 5, /**< battery Service */ |
cotigac | 0:c2d52562f36b | 72 | |
cotigac | 0:c2d52562f36b | 73 | packetType_accel = 6, /**< motion service */ |
cotigac | 0:c2d52562f36b | 74 | packetType_ambiLight = 7, /**< weather service */ |
cotigac | 0:c2d52562f36b | 75 | packetType_pressure = 8, /**< weather service */ |
cotigac | 0:c2d52562f36b | 76 | |
cotigac | 0:c2d52562f36b | 77 | |
cotigac | 0:c2d52562f36b | 78 | packetType_gyro = 9, /**< motion service */ |
cotigac | 0:c2d52562f36b | 79 | packetType_temperature = 10, /**< weather service */ |
cotigac | 0:c2d52562f36b | 80 | packetType_humidity = 11, /**< weather service */ |
cotigac | 0:c2d52562f36b | 81 | packetType_magnet = 12, /**< motion service */ |
cotigac | 0:c2d52562f36b | 82 | |
cotigac | 0:c2d52562f36b | 83 | packetType_heartRate = 13, /**< health service */ |
cotigac | 0:c2d52562f36b | 84 | packetType_steps = 14, /**< health service */ |
cotigac | 0:c2d52562f36b | 85 | packetType_calories = 15, /**< health service */ |
cotigac | 0:c2d52562f36b | 86 | |
cotigac | 0:c2d52562f36b | 87 | /* Alert Service */ |
cotigac | 0:c2d52562f36b | 88 | packetType_alertIn = 16, /**< incoming alerts */ |
cotigac | 0:c2d52562f36b | 89 | packetType_alertOut = 17, /**< outcoming alerts */ |
cotigac | 0:c2d52562f36b | 90 | |
cotigac | 0:c2d52562f36b | 91 | packetType_passDisplay = 18, /**< key display type */ |
cotigac | 0:c2d52562f36b | 92 | |
cotigac | 0:c2d52562f36b | 93 | /* OTAP procedure types */ |
cotigac | 0:c2d52562f36b | 94 | packetType_otapKW40Started = 19, |
cotigac | 0:c2d52562f36b | 95 | packetType_otapMK64Started = 20, |
cotigac | 0:c2d52562f36b | 96 | packetType_otapCompleted = 21, |
cotigac | 0:c2d52562f36b | 97 | packetType_otapFailed = 22, |
cotigac | 0:c2d52562f36b | 98 | |
cotigac | 0:c2d52562f36b | 99 | /* active buttons types */ |
cotigac | 0:c2d52562f36b | 100 | packetType_buttonsGroupToggleActive = 23, |
cotigac | 0:c2d52562f36b | 101 | packetType_buttonsGroupGetActive = 24, |
cotigac | 0:c2d52562f36b | 102 | packetType_buttonsGroupSendActive = 25, |
cotigac | 0:c2d52562f36b | 103 | |
cotigac | 0:c2d52562f36b | 104 | /* Turn off/on bluetooth advertising */ |
cotigac | 0:c2d52562f36b | 105 | packetType_advModeGet = 26, |
cotigac | 0:c2d52562f36b | 106 | packetType_advModeSend = 27, |
cotigac | 0:c2d52562f36b | 107 | packetType_advModeToggle = 28, |
cotigac | 0:c2d52562f36b | 108 | |
cotigac | 0:c2d52562f36b | 109 | packetType_appMode = 29, /**< app mode service */ |
cotigac | 0:c2d52562f36b | 110 | |
cotigac | 0:c2d52562f36b | 111 | /* Link State */ |
cotigac | 0:c2d52562f36b | 112 | packetType_linkStateGet = 30, /**< connected */ |
cotigac | 0:c2d52562f36b | 113 | packetType_linkStateSend = 31, /**< disconnected */ |
cotigac | 0:c2d52562f36b | 114 | |
cotigac | 0:c2d52562f36b | 115 | packetType_notification = 32, /**< notifications */ |
cotigac | 0:c2d52562f36b | 116 | |
cotigac | 0:c2d52562f36b | 117 | packetType_buildVersion = 33, /**< build version */ |
cotigac | 0:c2d52562f36b | 118 | |
cotigac | 0:c2d52562f36b | 119 | packetType_sleepON = 34, /**< sleep ON */ |
cotigac | 0:c2d52562f36b | 120 | packetType_sleepOFF = 35, /**< sleep OFF */ |
cotigac | 0:c2d52562f36b | 121 | |
cotigac | 0:c2d52562f36b | 122 | packetType_OK = 255 /**< OK packet */ |
cotigac | 0:c2d52562f36b | 123 | } hostInterface_packetType_t; |
cotigac | 0:c2d52562f36b | 124 | |
cotigac | 0:c2d52562f36b | 125 | /** data-packet structure */ |
cotigac | 0:c2d52562f36b | 126 | typedef struct |
cotigac | 0:c2d52562f36b | 127 | { |
cotigac | 0:c2d52562f36b | 128 | /* NOTE: Size of struct must be multiplier of 4! */ |
cotigac | 0:c2d52562f36b | 129 | uint8_t start1; |
cotigac | 0:c2d52562f36b | 130 | uint8_t start2; |
cotigac | 0:c2d52562f36b | 131 | hostInterface_packetType_t type; |
cotigac | 0:c2d52562f36b | 132 | uint8_t length; |
cotigac | 0:c2d52562f36b | 133 | uint8_t data[gHostInterface_dataSize + 1]; |
cotigac | 0:c2d52562f36b | 134 | } hostInterface_packet_t; |
cotigac | 0:c2d52562f36b | 135 | |
cotigac | 0:c2d52562f36b | 136 | /** incoming alert types */ |
cotigac | 0:c2d52562f36b | 137 | typedef enum |
cotigac | 0:c2d52562f36b | 138 | { |
cotigac | 0:c2d52562f36b | 139 | alertIn_type_notification = 1, |
cotigac | 0:c2d52562f36b | 140 | alertIn_type_settings = 2, |
cotigac | 0:c2d52562f36b | 141 | alertIn_type_timeUpdate = 3, |
cotigac | 0:c2d52562f36b | 142 | } hostInterface_alertIn_type_t; |
cotigac | 0:c2d52562f36b | 143 | |
cotigac | 2:bb66c19c3c04 | 144 | /** current app enum */ |
cotigac | 0:c2d52562f36b | 145 | typedef enum |
cotigac | 0:c2d52562f36b | 146 | { |
cotigac | 2:bb66c19c3c04 | 147 | GUI_CURRENT_APP_IDLE = 0, /**< no app active */ |
cotigac | 2:bb66c19c3c04 | 148 | GUI_CURRENT_APP_SENSOR_TAG = 2, /**< sensor tag */ |
cotigac | 2:bb66c19c3c04 | 149 | GUI_CURRENT_APP_HEART_RATE = 5, /**< heart rate */ |
cotigac | 2:bb66c19c3c04 | 150 | GUI_CURRENT_APP_PEDOMETER = 6 /**< Pedometer */ |
cotigac | 2:bb66c19c3c04 | 151 | } gui_current_app_t; |
cotigac | 0:c2d52562f36b | 152 | |
cotigac | 1:f6f9b24aea57 | 153 | typedef void (*button_t)(void); |
cotigac | 1:f6f9b24aea57 | 154 | typedef void (*alert_t)(uint8_t *data, uint8_t length); |
khuang | 3:9e92f113c671 | 155 | //typedef void (*passkey_t)(uint8_t *data); |
khuang | 3:9e92f113c671 | 156 | typedef void (*passkey_t)(void); |
cotigac | 1:f6f9b24aea57 | 157 | typedef void (*notifications_t)(uint8_t eventId, uint8_t categoryId); |
cotigac | 0:c2d52562f36b | 158 | |
cotigac | 0:c2d52562f36b | 159 | typedef struct name |
cotigac | 0:c2d52562f36b | 160 | { |
cotigac | 0:c2d52562f36b | 161 | uint8_t ver_patchNumber; |
cotigac | 0:c2d52562f36b | 162 | uint8_t ver_minorNumber; |
cotigac | 0:c2d52562f36b | 163 | uint8_t ver_majorNumber; |
cotigac | 0:c2d52562f36b | 164 | |
cotigac | 0:c2d52562f36b | 165 | } hexiwear_version_t; |
cotigac | 0:c2d52562f36b | 166 | |
cotigac | 0:c2d52562f36b | 167 | class KW40Z{ |
cotigac | 0:c2d52562f36b | 168 | |
cotigac | 0:c2d52562f36b | 169 | public: |
cotigac | 0:c2d52562f36b | 170 | |
cotigac | 0:c2d52562f36b | 171 | /** |
cotigac | 0:c2d52562f36b | 172 | * Create a Hexiwear BLE KW40Z Driver connected to the UART pins |
cotigac | 0:c2d52562f36b | 173 | * |
cotigac | 0:c2d52562f36b | 174 | * @param txPin UART TX pin |
cotigac | 0:c2d52562f36b | 175 | * @param rxPin UART RX pin |
cotigac | 0:c2d52562f36b | 176 | */ |
cotigac | 0:c2d52562f36b | 177 | KW40Z(PinName txPin,PinName rxPin); |
cotigac | 0:c2d52562f36b | 178 | |
cotigac | 0:c2d52562f36b | 179 | /** |
cotigac | 0:c2d52562f36b | 180 | * Destroy the Hexiwear instance |
cotigac | 0:c2d52562f36b | 181 | */ |
cotigac | 0:c2d52562f36b | 182 | ~KW40Z(); |
cotigac | 0:c2d52562f36b | 183 | |
cotigac | 1:f6f9b24aea57 | 184 | void attach_buttonUp(button_t btnFct); |
cotigac | 1:f6f9b24aea57 | 185 | void attach_buttonDown(button_t btnFct); |
cotigac | 1:f6f9b24aea57 | 186 | void attach_buttonLeft(button_t btnFct); |
cotigac | 1:f6f9b24aea57 | 187 | void attach_buttonRight(button_t btnFct); |
cotigac | 1:f6f9b24aea57 | 188 | void attach_buttonSlide(button_t btnFct); |
cotigac | 1:f6f9b24aea57 | 189 | |
cotigac | 1:f6f9b24aea57 | 190 | void attach_alert(alert_t alertFct); |
cotigac | 1:f6f9b24aea57 | 191 | void attach_passkey(passkey_t passkeyFct); |
cotigac | 1:f6f9b24aea57 | 192 | void attach_notifications(notifications_t notFct); |
cotigac | 1:f6f9b24aea57 | 193 | |
cotigac | 2:bb66c19c3c04 | 194 | void SendBatteryLevel(uint8_t percentage); |
cotigac | 2:bb66c19c3c04 | 195 | void SendAccel(uint8_t x, uint8_t y, uint8_t z); |
cotigac | 2:bb66c19c3c04 | 196 | void SendGyro(uint8_t x, uint8_t y, uint8_t z); |
cotigac | 2:bb66c19c3c04 | 197 | void SendMag(uint8_t x, uint8_t y, uint8_t z); |
cotigac | 2:bb66c19c3c04 | 198 | void SendAmbientLight(uint8_t percentage); |
cotigac | 2:bb66c19c3c04 | 199 | void SendTemperature(uint16_t celsius); |
cotigac | 2:bb66c19c3c04 | 200 | void SendHumidity(uint16_t percentage); |
cotigac | 2:bb66c19c3c04 | 201 | void SendPressure(uint16_t pascal); |
khuang | 3:9e92f113c671 | 202 | void SendHeartRate(uint8_t rate); |
cotigac | 2:bb66c19c3c04 | 203 | void SendSteps(uint16_t steps); |
cotigac | 2:bb66c19c3c04 | 204 | void SendCalories(uint16_t calories); |
cotigac | 2:bb66c19c3c04 | 205 | void SendAlert(uint8_t *pData, uint8_t length); |
cotigac | 2:bb66c19c3c04 | 206 | void SendSetApplicationMode(gui_current_app_t mode); |
cotigac | 2:bb66c19c3c04 | 207 | |
cotigac | 2:bb66c19c3c04 | 208 | void ToggleTsiGroup(void); |
cotigac | 2:bb66c19c3c04 | 209 | void ToggleAdvertisementMode(void); |
cotigac | 2:bb66c19c3c04 | 210 | |
cotigac | 2:bb66c19c3c04 | 211 | uint8_t GetTsiGroup(void); |
cotigac | 2:bb66c19c3c04 | 212 | uint8_t GetAdvertisementMode(void); |
cotigac | 2:bb66c19c3c04 | 213 | uint8_t GetLinkState(void); |
cotigac | 2:bb66c19c3c04 | 214 | hexiwear_version_t GetVersion(void); |
khuang | 3:9e92f113c671 | 215 | |
khuang | 3:9e92f113c671 | 216 | uint32_t GetPassKey(void); |
khuang | 3:9e92f113c671 | 217 | |
cotigac | 0:c2d52562f36b | 218 | private: |
cotigac | 0:c2d52562f36b | 219 | RawSerial device; |
cotigac | 0:c2d52562f36b | 220 | Thread mainThread; |
cotigac | 0:c2d52562f36b | 221 | Thread rxThread; |
cotigac | 0:c2d52562f36b | 222 | |
cotigac | 0:c2d52562f36b | 223 | hostInterface_packet_t hostInterface_rxPacket; |
cotigac | 0:c2d52562f36b | 224 | hostInterface_packet_t hostInterface_txPacket; |
cotigac | 0:c2d52562f36b | 225 | |
cotigac | 1:f6f9b24aea57 | 226 | button_t buttonUpCb; |
cotigac | 1:f6f9b24aea57 | 227 | button_t buttonDownCb; |
cotigac | 1:f6f9b24aea57 | 228 | button_t buttonLeftCb; |
cotigac | 1:f6f9b24aea57 | 229 | button_t buttonRightCb; |
cotigac | 1:f6f9b24aea57 | 230 | button_t buttonSlideCb; |
cotigac | 1:f6f9b24aea57 | 231 | |
cotigac | 1:f6f9b24aea57 | 232 | alert_t alertCb; |
cotigac | 1:f6f9b24aea57 | 233 | passkey_t passkeyCb; |
cotigac | 1:f6f9b24aea57 | 234 | notifications_t notificationsCb; |
cotigac | 1:f6f9b24aea57 | 235 | |
cotigac | 0:c2d52562f36b | 236 | uint8_t * rxBuff; |
cotigac | 0:c2d52562f36b | 237 | bool confirmReceived; |
cotigac | 0:c2d52562f36b | 238 | |
cotigac | 2:bb66c19c3c04 | 239 | hexiwear_version_t kw40_version; |
cotigac | 2:bb66c19c3c04 | 240 | uint8_t activeTsiGroup; |
cotigac | 2:bb66c19c3c04 | 241 | uint8_t advertisementMode; |
cotigac | 2:bb66c19c3c04 | 242 | uint8_t linkState; |
khuang | 3:9e92f113c671 | 243 | uint32_t bondPassKey; |
cotigac | 2:bb66c19c3c04 | 244 | |
cotigac | 0:c2d52562f36b | 245 | MemoryPool<hostInterface_packet_t, 16> mpool; |
cotigac | 0:c2d52562f36b | 246 | Queue<hostInterface_packet_t, 16> queue; |
cotigac | 0:c2d52562f36b | 247 | |
cotigac | 0:c2d52562f36b | 248 | void mainTask(void); |
cotigac | 0:c2d52562f36b | 249 | void rxTask(void); |
cotigac | 0:c2d52562f36b | 250 | |
cotigac | 0:c2d52562f36b | 251 | void ProcessBuffer(); |
cotigac | 0:c2d52562f36b | 252 | void ProcessReceivedPacket(hostInterface_packet_t * rxPacket); |
cotigac | 0:c2d52562f36b | 253 | void SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested); |
cotigac | 0:c2d52562f36b | 254 | void SearchStartByte(); |
cotigac | 2:bb66c19c3c04 | 255 | |
cotigac | 2:bb66c19c3c04 | 256 | void SendPacketOK(void); |
cotigac | 2:bb66c19c3c04 | 257 | void SendGetActiveTsiGroup(void); |
cotigac | 2:bb66c19c3c04 | 258 | void SendGetAdvertisementMode(void); |
cotigac | 2:bb66c19c3c04 | 259 | void SendGetLinkState(void); |
cotigac | 2:bb66c19c3c04 | 260 | void SendGetVersion(void); |
cotigac | 0:c2d52562f36b | 261 | |
cotigac | 0:c2d52562f36b | 262 | #if defined (LIB_DEBUG) |
cotigac | 0:c2d52562f36b | 263 | void DebugPrintRxPacket(); |
cotigac | 0:c2d52562f36b | 264 | void DebugPrintTxPacket(hostInterface_packet_t * txPacket); |
cotigac | 0:c2d52562f36b | 265 | #endif |
cotigac | 0:c2d52562f36b | 266 | }; |
cotigac | 0:c2d52562f36b | 267 | |
cotigac | 0:c2d52562f36b | 268 | #endif |