Jacob Shebesh / Hexi_KW40Z

Dependents:   Hexiwear HexiwearFinal HexiwearFinal1

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 
00044 #define START_THREAD                            1
00045 
00046 #define gHostInterface_startByte1               0x55
00047 #define gHostInterface_startByte2               0xAA
00048 #define gHostInterface_trailerByte              0x45
00049 
00050 #define gHostInterface_dataSize                 23
00051 #define gHostInterface_headerSize               4
00052 
00053 #define gHostInterface_retransmitCount          3
00054 #define gHostInterface_retransmitTimeout        100
00055 
00056 #define gHostInterface_TxConfirmationEnable     1 // send confirmation when receive packet
00057 #define gHostInterface_RxConfirmationEnable     1 // wait on confirmation from remote side (do retransmit)
00058 
00059 /** HEXIWEAR firmware version */
00060 #define HEXIWEAR_VERSION_PATCH ( 2 )
00061 #define HEXIWEAR_VERSION_MINOR ( 0 )
00062 #define HEXIWEAR_VERSION_MAJOR ( 1 )
00063 
00064 /** packet types */
00065 typedef enum
00066 {
00067     packetType_pressUp          = 0, /**< touch press up */
00068     packetType_pressDown        = 1, /**< touch press down */
00069     packetType_pressLeft        = 2, /**< touch press left */
00070     packetType_pressRight       = 3, /**< touch press right */
00071     packetType_slide            = 4, /**< touch slide */
00072     
00073     packetType_batteryLevel     = 5, /**< battery Service */
00074     
00075     packetType_accel            = 6, /**< motion service */
00076     packetType_ambiLight        = 7, /**< weather service */
00077     packetType_pressure         = 8, /**< weather service */
00078     
00079     
00080     packetType_gyro             = 9,  /**< motion service */
00081     packetType_temperature      = 10, /**< weather service */
00082     packetType_humidity         = 11, /**< weather service */
00083     packetType_magnet           = 12, /**< motion service */
00084     
00085     packetType_heartRate        = 13, /**< health service */
00086     packetType_steps            = 14, /**< health service */
00087     packetType_calories         = 15, /**< health service */
00088     
00089     /* Alert Service */
00090     packetType_alertIn          = 16, /**<  incoming alerts */
00091     packetType_alertOut         = 17, /**<  outcoming alerts */
00092     
00093     packetType_passDisplay      = 18, /**< key display type */
00094     
00095     /* OTAP procedure types */
00096     packetType_otapKW40Started  = 19,
00097     packetType_otapMK64Started  = 20,
00098     packetType_otapCompleted    = 21,
00099     packetType_otapFailed       = 22,
00100     
00101     /* active buttons types */
00102     packetType_buttonsGroupToggleActive = 23,
00103     packetType_buttonsGroupGetActive    = 24,
00104     packetType_buttonsGroupSendActive   = 25,
00105     
00106     /* Turn off/on bluetooth advertising */
00107     packetType_advModeGet    = 26,
00108     packetType_advModeSend   = 27,
00109     packetType_advModeToggle = 28,
00110     
00111     packetType_appMode       = 29, /**< app mode service */
00112     
00113     /* Link State */
00114     packetType_linkStateGet  = 30, /**< connected */
00115     packetType_linkStateSend = 31, /**< disconnected */
00116     
00117     packetType_notification  = 32, /**< notifications */
00118     
00119     packetType_buildVersion  = 33, /**< build version */
00120     
00121     packetType_sleepON       = 34, /**< sleep ON */
00122     packetType_sleepOFF      = 35, /**< sleep OFF */
00123     
00124     packetType_OK            = 255 /**< OK packet */
00125 } hostInterface_packetType_t;
00126 
00127 /** data-packet structure */
00128 typedef struct
00129 {
00130     /* NOTE: Size of struct must be multiplier of 4! */
00131     uint8_t start1;
00132     uint8_t start2;
00133     hostInterface_packetType_t type;
00134     uint8_t length;
00135     uint8_t data[gHostInterface_dataSize + 1];
00136 } hostInterface_packet_t;
00137 
00138 /** incoming alert types */
00139 typedef enum
00140 {
00141     alertIn_type_notification        = 1,
00142     alertIn_type_settings            = 2,
00143     alertIn_type_timeUpdate          = 3,
00144 } hostInterface_alertIn_type_t;
00145 
00146 typedef enum
00147 {
00148     pressUp          = 0, /**< touch press up */
00149     pressDown        = 1, /**< touch press down */
00150     pressLeft        = 2, /**< touch press left */
00151     pressRight       = 3, /**< touch press right */
00152     slide            = 4, /**< touch slide */
00153 } hexi_buttons_t;
00154 
00155 typedef void (*button_t)(void);
00156 typedef void (*alert_t)(uint8_t *data, uint8_t length);
00157 typedef void (*passkey_t)(uint8_t *data);
00158 typedef void (*notifications_t)(uint8_t eventId, uint8_t categoryId);
00159 
00160 typedef struct name
00161 {
00162     uint8_t ver_patchNumber;
00163     uint8_t ver_minorNumber;
00164     uint8_t ver_majorNumber;
00165 
00166 } hexiwear_version_t;
00167 
00168 class KW40Z{
00169 
00170 public:
00171 
00172     /**
00173     * Create a Hexiwear BLE KW40Z Driver connected to the UART pins
00174     *    
00175     * @param txPin UART TX pin
00176     * @param rxPin UART RX pin
00177     */
00178     KW40Z(PinName txPin,PinName rxPin);
00179     
00180     /**
00181     * Destroy the Hexiwear instance
00182     */   
00183     ~KW40Z();
00184     
00185     void attach_buttonUp(button_t btnFct);
00186     void attach_buttonDown(button_t btnFct);
00187     void attach_buttonLeft(button_t btnFct);
00188     void attach_buttonRight(button_t btnFct);
00189     void attach_buttonSlide(button_t btnFct);
00190     
00191     
00192     void attach_alert(alert_t alertFct);
00193     void attach_passkey(passkey_t passkeyFct);
00194     void attach_notifications(notifications_t notFct);
00195 
00196     void GetVersion();
00197   
00198 private:
00199     RawSerial device;    
00200     Thread mainThread;
00201     Thread rxThread;
00202     
00203     hostInterface_packet_t hostInterface_rxPacket;
00204     hostInterface_packet_t hostInterface_txPacket;
00205     
00206     button_t buttonUpCb;
00207     button_t buttonDownCb;
00208     button_t buttonLeftCb;
00209     button_t buttonRightCb;
00210     button_t buttonSlideCb;
00211     
00212     alert_t alertCb;
00213     passkey_t passkeyCb;
00214     notifications_t notificationsCb;
00215     
00216     uint8_t * rxBuff;
00217     bool confirmReceived;
00218     
00219     MemoryPool<hostInterface_packet_t, 16> mpool;
00220     Queue<hostInterface_packet_t, 16> queue;
00221 
00222     void mainTask(void);
00223     void rxTask(void);
00224     
00225     void ProcessBuffer();
00226     void ProcessReceivedPacket(hostInterface_packet_t * rxPacket);
00227     void SendPacket(hostInterface_packet_t * txPacket, bool confirmRequested);
00228     void SearchStartByte();
00229     void SendPacketOK();
00230     void ConfirmPacketOK();
00231     
00232 #if defined (LIB_DEBUG) 
00233     void DebugPrintRxPacket();
00234     void DebugPrintTxPacket(hostInterface_packet_t * txPacket);
00235 #endif
00236 
00237     static void rxStarter(void const *p);
00238     static void mainStarter(void const *p);
00239 };
00240 
00241 #endif