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 Hexiwear

Committer:
xihan94
Date:
Thu Aug 09 22:45:01 2018 +0000
Revision:
13:84ead63f7353
Parent:
12:3f5ed7abc5c7
Child:
14:241c491f985f
dev;

Who changed what in which revision?

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