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

Committer:
cotigac
Date:
Mon Sep 26 05:28:55 2016 +0000
Revision:
12:3f5ed7abc5c7
Parent:
11:a9a838035b87
Increased memory poll and queues to handle multiple messages

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