Xi Han / Hexi_KW40Z

Dependents:   Hexi_BLE_Example_Modified Hexi_BLE_Example_ModifiedPOTATO

Fork of Hexi_KW40Z by Hexiwear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hexi_KW40Z.h Source File

Hexi_KW40Z.h

00001 /** BLE KW40Z Driver for Hexiwear
00002  *  This file contains BLE and Touch Buttons driver functionality for Hexiwear
00003  *
00004  * Redistribution and use in source and binary forms, with or without modification,
00005  * are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this list
00008  * of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice, this
00011  * list of conditions and the following disclaimer in the documentation and/or
00012  * other materials provided with the distribution.
00013  *
00014  * Neither the name of NXP, nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  * visit: http://www.mikroe.com and http://www.nxp.com
00030  *
00031  * get support at: http://www.mikroe.com/forum and https://community.nxp.com
00032  *
00033  * Project HEXIWEAR, 2015
00034  */
00035 
00036 #ifndef HG_HEXI_KW40Z
00037 #define HG_HEXI_KW40Z
00038 
00039 #include "mbed.h"
00040 #include "rtos.h"
00041 
00042 //#define LIB_DEBUG                               1
00043 //#define RAW_DEBUG                               1
00044 //#define RX_DEBUG 1
00045 
00046 #define gHostInterface_startByte1               0x55
00047 #define gHostInterface_startByte2               0xAA
00048 #define gHostInterface_trailerByte              0x45
00049 #define gHostInterface_rxConfirmMask            0x01
00050 #define gHostInterface_txPacketMask             0x10
00051 
00052 #define gHostInterface_dataSize                 23
00053 #define gHostInterface_headerSize               4
00054 
00055 #define gHostInterface_retransmitCount          3
00056 #define gHostInterface_retransmitTimeout        100
00057 
00058 #define gHostInterface_TxConfirmationEnable     1 // send confirmation when receive packet
00059 #define gHostInterface_RxConfirmationEnable     1 // wait on confirmation from remote side (do retransmit)
00060 
00061 /** HEXIWEAR firmware version */
00062 #define HEXIWEAR_VERSION_PATCH ( 2 )
00063 #define HEXIWEAR_VERSION_MINOR ( 0 )
00064 #define HEXIWEAR_VERSION_MAJOR ( 1 )
00065 
00066 /** packet types */
00067 typedef enum
00068 {
00069     packetType_pressUp          = 0, /**< touch press up */
00070     packetType_pressDown        = 1, /**< touch press down */
00071     packetType_pressLeft        = 2, /**< touch press left */
00072     packetType_pressRight       = 3, /**< touch press right */
00073     packetType_slide            = 4, /**< touch slide */
00074     
00075     packetType_batteryLevel     = 5, /**< battery Service */
00076     
00077     packetType_accel            = 6, /**< motion service */
00078     packetType_ambiLight        = 7, /**< weather service */
00079     packetType_pressure         = 8, /**< weather service */
00080     
00081     
00082     packetType_gyro             = 9,  /**< motion service */
00083     packetType_temperature      = 10, /**< weather service */
00084     packetType_humidity         = 11, /**< weather service */
00085     packetType_magnet           = 12, /**< motion service */
00086     
00087     packetType_heartRate        = 13, /**< health service */
00088     packetType_steps            = 14, /**< health service */
00089     packetType_calories         = 15, /**< health service */
00090     
00091     /* Alert Service */
00092     packetType_alertIn          = 16, /**<  incoming alerts */
00093     packetType_alertOut         = 17, /**<  outcoming alerts */
00094     
00095     packetType_passDisplay      = 18, /**< key display type */
00096     
00097     /* OTAP procedure types */
00098     packetType_otapKW40Started  = 19,
00099     packetType_otapMK64Started  = 20,
00100     packetType_otapCompleted    = 21,
00101     packetType_otapFailed       = 22,
00102     
00103     /* active buttons types */
00104     packetType_buttonsGroupToggleActive = 23,
00105     packetType_buttonsGroupGetActive    = 24,
00106     packetType_buttonsGroupSendActive   = 25,
00107     
00108     /* Turn off/on bluetooth advertising */
00109     packetType_advModeGet    = 26,
00110     packetType_advModeSend   = 27,
00111     packetType_advModeToggle = 28,
00112     
00113     packetType_appMode       = 29, /**< app mode service */
00114     
00115     /* Link State */
00116     packetType_linkStateGet  = 30, /**< connected */
00117     packetType_linkStateSend = 31, /**< disconnected */
00118     
00119     packetType_notification  = 32, /**< notifications */
00120     
00121     packetType_buildVersion  = 33, /**< build version */
00122     
00123     packetType_sleepON       = 34, /**< sleep ON */
00124     packetType_sleepOFF      = 35, /**< sleep OFF */
00125     
00126     /* Message Service */
00127     packetType_messageIn          = 116, /**<  incoming messages */
00128     packetType_messageOut         = 117, /**<  outcoming messages */
00129     
00130     packetType_OK            = 255 /**< OK packet */
00131 } hostInterface_packetType_t;
00132 
00133 /** data-packet structure */
00134 typedef struct
00135 {
00136     /* NOTE: Size of struct must be multiplier of 4! */
00137     uint8_t start1;
00138     uint8_t start2;
00139     hostInterface_packetType_t type;
00140     uint8_t length;
00141     uint8_t data[gHostInterface_dataSize + 1];
00142 } hostInterface_packet_t;
00143 
00144 /** incoming alert types */
00145 typedef enum
00146 {
00147     alertIn_type_notification        = 1,
00148     alertIn_type_settings            = 2,
00149     alertIn_type_timeUpdate          = 3,
00150 } hostInterface_alertIn_type_t;
00151 
00152 /** current app enum */
00153 typedef enum
00154 {
00155   GUI_CURRENT_APP_IDLE                = 0, /**< no app active */
00156   GUI_CURRENT_APP_SENSOR_TAG          = 2, /**< sensor tag */
00157   GUI_CURRENT_APP_HEART_RATE          = 5, /**< heart rate */
00158   GUI_CURRENT_APP_PEDOMETER           = 6  /**< Pedometer */
00159 } gui_current_app_t;
00160 
00161 typedef void (*button_t)(void);
00162 typedef void (*alert_t)(uint8_t *data, uint8_t length);
00163 typedef void (*message_t)(uint8_t *data, uint8_t length);
00164 //typedef void (*passkey_t)(uint8_t *data);
00165 typedef void (*passkey_t)(void);
00166 typedef void (*notifications_t)(uint8_t eventId, uint8_t categoryId);
00167 
00168 typedef struct name
00169 {
00170     uint8_t ver_patchNumber;
00171     uint8_t ver_minorNumber;
00172     uint8_t ver_majorNumber;
00173 
00174 } hexiwear_version_t;
00175 
00176 class KW40Z{
00177 
00178 public:
00179 
00180     /**
00181     * Create a Hexiwear BLE KW40Z Driver connected to the UART pins
00182     *    
00183     * @param txPin UART TX pin
00184     * @param rxPin UART RX pin
00185     */
00186     KW40Z(PinName txPin,PinName rxPin);
00187     
00188     /**
00189     * Destroy the Hexiwear instance
00190     */   
00191     ~KW40Z();
00192     
00193     void attach_buttonUp(button_t btnFct);
00194     void attach_buttonDown(button_t btnFct);
00195     void attach_buttonLeft(button_t btnFct);
00196     void attach_buttonRight(button_t btnFct);
00197     void attach_buttonSlide(button_t btnFct);
00198     
00199     void attach_alert(alert_t alertFct);
00200     void attach_message(message_t msgFct);
00201     void attach_passkey(passkey_t passkeyFct);
00202     void attach_notifications(notifications_t notFct);
00203 
00204     void SendBatteryLevel(uint8_t percentage);
00205     void SendAccel(int16_t x, int16_t y, int16_t z);
00206     void SendGyro(int16_t x, int16_t y, int16_t z);
00207     void SendMag(int16_t x, int16_t y, int16_t z);
00208     void SendAmbientLight(uint8_t percentage);
00209     void SendTemperature(uint16_t celsius);
00210     void SendHumidity(uint16_t percentage);
00211     void SendPressure(uint16_t pascal);
00212     void SendHeartRate(uint8_t rate);
00213     void SendSteps(uint16_t steps);
00214     void SendCalories(uint16_t calories);
00215     void SendAlert(uint8_t *pData, uint8_t length);
00216     void SendMessage(uint8_t *pData, uint8_t length);
00217     void SendSetApplicationMode(gui_current_app_t mode);
00218     void SendGetVersion(void);
00219     
00220     void ToggleTsiGroup(void);
00221     void ToggleAdvertisementMode(void);
00222     
00223     uint8_t GetTsiGroup(void);
00224     uint8_t GetAdvertisementMode(void);
00225     uint8_t GetLinkState(void);
00226     hexiwear_version_t GetVersion(void);
00227     
00228     uint32_t GetPassKey(void); 
00229 
00230 private:
00231 #if defined (LIB_DEBUG) || defined (RAW_DEBUG) || defined (RX_DEBUG)
00232     RawSerial pc;
00233 #endif
00234 
00235     RawSerial device;
00236     Thread rxThread;  
00237     Thread mainThread;
00238     
00239     hostInterface_packet_t hostInterface_rxPacket;
00240     hostInterface_packet_t hostInterface_txPacket;
00241     
00242     button_t buttonUpCb;
00243     button_t buttonDownCb;
00244     button_t buttonLeftCb;
00245     button_t buttonRightCb;
00246     button_t buttonSlideCb;
00247     
00248     alert_t alertCb;
00249     message_t messageCb;
00250     passkey_t passkeyCb;
00251     notifications_t notificationsCb;
00252     
00253     uint8_t * rxBuff;
00254     bool confirmReceived;
00255     
00256     hexiwear_version_t kw40_version;
00257     uint8_t activeTsiGroup;
00258     uint8_t advertisementMode;
00259     uint8_t linkState;
00260     uint32_t bondPassKey; 
00261 
00262     uint32_t allocated;
00263     uint32_t freed;
00264     
00265     MemoryPool<hostInterface_packet_t, 500> mpool;
00266     Queue<hostInterface_packet_t, 500> queue;
00267 
00268     void mainTask(void);
00269     void rxTask(void);
00270     
00271     void ProcessBuffer();
00272     void ProcessPacket(hostInterface_packet_t * packet);
00273     void SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested);
00274     void SendInternal(hostInterface_packet_t * txPacket);
00275     void SearchStartByte();
00276     
00277     void SendPacketOK(void);
00278     void SendGetActiveTsiGroup(void);
00279     void SendGetAdvertisementMode(void);
00280     void SendGetLinkState(void);
00281     
00282 #if defined (LIB_DEBUG) || defined (RX_DEBUG)
00283     void DebugPrintPacket(hostInterface_packet_t * packet);
00284 #endif
00285 };
00286 
00287 #endif