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 01:37:59 2016 +0000
Revision:
9:8058541a8e2d
Parent:
8:2b9b24f3154d
Child:
11:a9a838035b87
Updated library so it doesn't require an TX task anymore outside of the library

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 9:8058541a8e2d 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 9:8058541a8e2d 47 #define gHostInterface_rxConfirmMask 0x01
cotigac 9:8058541a8e2d 48 #define gHostInterface_txPacketMask 0x10
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 2:bb66c19c3c04 146 /** current app enum */
cotigac 0:c2d52562f36b 147 typedef enum
cotigac 0:c2d52562f36b 148 {
cotigac 2:bb66c19c3c04 149 GUI_CURRENT_APP_IDLE = 0, /**< no app active */
cotigac 2:bb66c19c3c04 150 GUI_CURRENT_APP_SENSOR_TAG = 2, /**< sensor tag */
cotigac 2:bb66c19c3c04 151 GUI_CURRENT_APP_HEART_RATE = 5, /**< heart rate */
cotigac 2:bb66c19c3c04 152 GUI_CURRENT_APP_PEDOMETER = 6 /**< Pedometer */
cotigac 2:bb66c19c3c04 153 } gui_current_app_t;
cotigac 0:c2d52562f36b 154
cotigac 1:f6f9b24aea57 155 typedef void (*button_t)(void);
cotigac 1:f6f9b24aea57 156 typedef void (*alert_t)(uint8_t *data, uint8_t length);
khuang 3:9e92f113c671 157 //typedef void (*passkey_t)(uint8_t *data);
khuang 3:9e92f113c671 158 typedef void (*passkey_t)(void);
cotigac 1:f6f9b24aea57 159 typedef void (*notifications_t)(uint8_t eventId, uint8_t categoryId);
cotigac 0:c2d52562f36b 160
cotigac 0:c2d52562f36b 161 typedef struct name
cotigac 0:c2d52562f36b 162 {
cotigac 0:c2d52562f36b 163 uint8_t ver_patchNumber;
cotigac 0:c2d52562f36b 164 uint8_t ver_minorNumber;
cotigac 0:c2d52562f36b 165 uint8_t ver_majorNumber;
cotigac 0:c2d52562f36b 166
cotigac 0:c2d52562f36b 167 } hexiwear_version_t;
cotigac 0:c2d52562f36b 168
cotigac 0:c2d52562f36b 169 class KW40Z{
cotigac 0:c2d52562f36b 170
cotigac 0:c2d52562f36b 171 public:
cotigac 0:c2d52562f36b 172
cotigac 0:c2d52562f36b 173 /**
cotigac 0:c2d52562f36b 174 * Create a Hexiwear BLE KW40Z Driver connected to the UART pins
cotigac 0:c2d52562f36b 175 *
cotigac 0:c2d52562f36b 176 * @param txPin UART TX pin
cotigac 0:c2d52562f36b 177 * @param rxPin UART RX pin
cotigac 0:c2d52562f36b 178 */
cotigac 0:c2d52562f36b 179 KW40Z(PinName txPin,PinName rxPin);
cotigac 0:c2d52562f36b 180
cotigac 0:c2d52562f36b 181 /**
cotigac 0:c2d52562f36b 182 * Destroy the Hexiwear instance
cotigac 0:c2d52562f36b 183 */
cotigac 0:c2d52562f36b 184 ~KW40Z();
cotigac 0:c2d52562f36b 185
cotigac 1:f6f9b24aea57 186 void attach_buttonUp(button_t btnFct);
cotigac 1:f6f9b24aea57 187 void attach_buttonDown(button_t btnFct);
cotigac 1:f6f9b24aea57 188 void attach_buttonLeft(button_t btnFct);
cotigac 1:f6f9b24aea57 189 void attach_buttonRight(button_t btnFct);
cotigac 1:f6f9b24aea57 190 void attach_buttonSlide(button_t btnFct);
cotigac 1:f6f9b24aea57 191
cotigac 1:f6f9b24aea57 192 void attach_alert(alert_t alertFct);
cotigac 1:f6f9b24aea57 193 void attach_passkey(passkey_t passkeyFct);
cotigac 1:f6f9b24aea57 194 void attach_notifications(notifications_t notFct);
cotigac 1:f6f9b24aea57 195
cotigac 2:bb66c19c3c04 196 void SendBatteryLevel(uint8_t percentage);
khuang 8:2b9b24f3154d 197 void SendAccel(int16_t x, int16_t y, int16_t z);
khuang 8:2b9b24f3154d 198 void SendGyro(int16_t x, int16_t y, int16_t z);
khuang 8:2b9b24f3154d 199 void SendMag(int16_t x, int16_t y, int16_t z);
cotigac 2:bb66c19c3c04 200 void SendAmbientLight(uint8_t percentage);
cotigac 2:bb66c19c3c04 201 void SendTemperature(uint16_t celsius);
cotigac 2:bb66c19c3c04 202 void SendHumidity(uint16_t percentage);
cotigac 2:bb66c19c3c04 203 void SendPressure(uint16_t pascal);
khuang 3:9e92f113c671 204 void SendHeartRate(uint8_t rate);
cotigac 2:bb66c19c3c04 205 void SendSteps(uint16_t steps);
cotigac 2:bb66c19c3c04 206 void SendCalories(uint16_t calories);
cotigac 2:bb66c19c3c04 207 void SendAlert(uint8_t *pData, uint8_t length);
cotigac 2:bb66c19c3c04 208 void SendSetApplicationMode(gui_current_app_t mode);
cotigac 9:8058541a8e2d 209 void SendGetVersion(void);
cotigac 2:bb66c19c3c04 210
cotigac 2:bb66c19c3c04 211 void ToggleTsiGroup(void);
cotigac 2:bb66c19c3c04 212 void ToggleAdvertisementMode(void);
cotigac 2:bb66c19c3c04 213
cotigac 2:bb66c19c3c04 214 uint8_t GetTsiGroup(void);
cotigac 2:bb66c19c3c04 215 uint8_t GetAdvertisementMode(void);
cotigac 2:bb66c19c3c04 216 uint8_t GetLinkState(void);
cotigac 2:bb66c19c3c04 217 hexiwear_version_t GetVersion(void);
khuang 3:9e92f113c671 218
khuang 3:9e92f113c671 219 uint32_t GetPassKey(void);
khuang 3:9e92f113c671 220
cotigac 0:c2d52562f36b 221 private:
cotigac 9:8058541a8e2d 222 #if defined (LIB_DEBUG)
cotigac 9:8058541a8e2d 223 RawSerial pc;
cotigac 9:8058541a8e2d 224 #endif
cotigac 9:8058541a8e2d 225
cotigac 9:8058541a8e2d 226 RawSerial device;
cotigac 9:8058541a8e2d 227 Thread rxThread;
cotigac 0:c2d52562f36b 228 Thread mainThread;
cotigac 0:c2d52562f36b 229
cotigac 0:c2d52562f36b 230 hostInterface_packet_t hostInterface_rxPacket;
cotigac 0:c2d52562f36b 231 hostInterface_packet_t hostInterface_txPacket;
cotigac 0:c2d52562f36b 232
cotigac 1:f6f9b24aea57 233 button_t buttonUpCb;
cotigac 1:f6f9b24aea57 234 button_t buttonDownCb;
cotigac 1:f6f9b24aea57 235 button_t buttonLeftCb;
cotigac 1:f6f9b24aea57 236 button_t buttonRightCb;
cotigac 1:f6f9b24aea57 237 button_t buttonSlideCb;
cotigac 1:f6f9b24aea57 238
cotigac 1:f6f9b24aea57 239 alert_t alertCb;
cotigac 1:f6f9b24aea57 240 passkey_t passkeyCb;
cotigac 1:f6f9b24aea57 241 notifications_t notificationsCb;
cotigac 1:f6f9b24aea57 242
cotigac 0:c2d52562f36b 243 uint8_t * rxBuff;
cotigac 0:c2d52562f36b 244 bool confirmReceived;
cotigac 0:c2d52562f36b 245
cotigac 2:bb66c19c3c04 246 hexiwear_version_t kw40_version;
cotigac 2:bb66c19c3c04 247 uint8_t activeTsiGroup;
cotigac 2:bb66c19c3c04 248 uint8_t advertisementMode;
cotigac 2:bb66c19c3c04 249 uint8_t linkState;
khuang 3:9e92f113c671 250 uint32_t bondPassKey;
cotigac 2:bb66c19c3c04 251
cotigac 0:c2d52562f36b 252 MemoryPool<hostInterface_packet_t, 16> mpool;
cotigac 0:c2d52562f36b 253 Queue<hostInterface_packet_t, 16> queue;
cotigac 0:c2d52562f36b 254
cotigac 0:c2d52562f36b 255 void mainTask(void);
cotigac 0:c2d52562f36b 256 void rxTask(void);
cotigac 0:c2d52562f36b 257
cotigac 0:c2d52562f36b 258 void ProcessBuffer();
cotigac 9:8058541a8e2d 259 void ProcessPacket(hostInterface_packet_t * packet);
cotigac 0:c2d52562f36b 260 void SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested);
cotigac 9:8058541a8e2d 261 void SendInternal(hostInterface_packet_t * txPacket);
cotigac 0:c2d52562f36b 262 void SearchStartByte();
cotigac 2:bb66c19c3c04 263
cotigac 2:bb66c19c3c04 264 void SendPacketOK(void);
cotigac 2:bb66c19c3c04 265 void SendGetActiveTsiGroup(void);
cotigac 2:bb66c19c3c04 266 void SendGetAdvertisementMode(void);
cotigac 2:bb66c19c3c04 267 void SendGetLinkState(void);
cotigac 0:c2d52562f36b 268
cotigac 9:8058541a8e2d 269 #if defined (LIB_DEBUG)
cotigac 9:8058541a8e2d 270 void DebugPrintPacket(hostInterface_packet_t * packet);
cotigac 0:c2d52562f36b 271 #endif
cotigac 0:c2d52562f36b 272 };
cotigac 0:c2d52562f36b 273
cotigac 0:c2d52562f36b 274 #endif