F. Yao / Mbed OS Hexi_swimit

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Example by Hexiwear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Hexi_KW40Z.h"
00003 #include "Hexi_OLED_SSD1351.h"
00004 #include "OLED_types.h"
00005 #include "OpenSans_Font.h"
00006 #include "string.h"
00007 
00008 #define LED_ON      0
00009 #define LED_OFF     1
00010 
00011 void UpdateSensorData(void);
00012 void StartHaptic(void);
00013 void StopHaptic(void const *n);
00014 void txTask(void);
00015 
00016 DigitalOut redLed(LED1,1);
00017 DigitalOut greenLed(LED2,1);
00018 DigitalOut blueLed(LED3,1);
00019 DigitalOut haptic(PTB9);
00020 
00021 /* Define timer for haptic feedback */
00022 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
00023 
00024 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
00025 KW40Z kw40z_device(PTE24, PTE25);
00026 
00027 /* Instantiate the SSD1351 OLED Driver */ 
00028 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
00029 
00030 /*Create a Thread to handle sending BLE Sensor Data */ 
00031 Thread txThread;
00032 
00033  /* Text Buffer */ 
00034 char text[20]; 
00035 
00036 uint8_t battery = 100;
00037 uint8_t light = 0;
00038 uint16_t humidity = 4500;
00039 uint16_t temperature = 2000;
00040 uint16_t pressure = 9000;
00041 uint16_t x = 0;
00042 uint16_t y = 5000;
00043 uint16_t z = 10000;
00044 
00045 /****************************Call Back Functions*******************************/
00046 void ButtonRight(void)
00047 {
00048     StartHaptic();
00049     kw40z_device.ToggleAdvertisementMode();
00050 }
00051 
00052 void ButtonLeft(void)
00053 {
00054     StartHaptic();
00055     kw40z_device.ToggleAdvertisementMode();
00056 }
00057 
00058 void PassKey(void)
00059 {
00060     StartHaptic();
00061     strcpy((char *) text,"PAIR CODE");
00062     oled.TextBox((uint8_t *)text,0,25,95,18);
00063   
00064     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00065     sprintf(text,"%d", kw40z_device.GetPassKey());
00066     oled.TextBox((uint8_t *)text,0,40,95,18);
00067 }
00068 
00069 /***********************End of Call Back Functions*****************************/
00070 
00071 /********************************Main******************************************/
00072 
00073 int main()
00074 {    
00075     /* Register callbacks to application functions */
00076     kw40z_device.attach_buttonLeft(&ButtonLeft);
00077     kw40z_device.attach_buttonRight(&ButtonRight);
00078     kw40z_device.attach_passkey(&PassKey);
00079 
00080     /* Turn on the backlight of the OLED Display */
00081     oled.DimScreenON();
00082     
00083     /* Fills the screen with solid black */         
00084     oled.FillScreen(COLOR_BLACK);
00085 
00086     /* Get OLED Class Default Text Properties */
00087     oled_text_properties_t textProperties = {0};
00088     oled.GetTextProperties(&textProperties);    
00089         
00090     /* Change font color to Blue */ 
00091     textProperties.fontColor   = COLOR_BLUE;
00092     oled.SetTextProperties(&textProperties);
00093     
00094     /* Display Bluetooth Label at x=17,y=65 */ 
00095     strcpy((char *) text,"BLUETOOTH");
00096     oled.Label((uint8_t *)text,17,65);
00097     
00098     /* Change font color to white */ 
00099     textProperties.fontColor   = COLOR_WHITE;
00100     textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
00101     oled.SetTextProperties(&textProperties);
00102     
00103     /* Display Label at x=22,y=80 */ 
00104     strcpy((char *) text,"Tap Below");
00105     oled.Label((uint8_t *)text,22,80);
00106          
00107     uint8_t prevLinkState = 0; 
00108     uint8_t currLinkState = 0;
00109      
00110     txThread.start(txTask); /*Start transmitting Sensor Tag Data */
00111     
00112     while (true) 
00113     {
00114         blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
00115         Thread::wait(50);
00116     }
00117 }
00118 
00119 /******************************End of Main*************************************/
00120 
00121 
00122 /* txTask() transmits the sensor data */
00123 void txTask(void){
00124    
00125    while (true) 
00126    {
00127         UpdateSensorData();
00128         
00129         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00130         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00131                 
00132         /*The following is sending dummy data over BLE. Replace with real data*/
00133     
00134         /*Send Battery Level for 20% */ 
00135         kw40z_device.SendBatteryLevel(battery);
00136                
00137         /*Send Ambient Light Level at 50% */ 
00138         kw40z_device.SendAmbientLight(light);
00139         
00140         /*Send Humidity at 90% */
00141         kw40z_device.SendHumidity(humidity);
00142         
00143         /*Send Temperature at 25 degrees Celsius */
00144         kw40z_device.SendTemperature(temperature);
00145 
00146         /*Send Pressure at 100kPA */ 
00147         kw40z_device.SendPressure(pressure);
00148         
00149         /*Send Mag,Accel,Gyro Data. */
00150         kw40z_device.SendGyro(x,y,z);
00151         kw40z_device.SendAccel(z,x,y);
00152         kw40z_device.SendMag(y,z,x);
00153 
00154         Thread::wait(1000);                 
00155     }
00156 }
00157 
00158 void UpdateSensorData(void)
00159 {    
00160     battery -= 5;
00161     if(battery < 5) battery = 100;
00162     
00163     light += 20;
00164     if(light > 100) light = 0;
00165     
00166     humidity += 500;
00167     if(humidity > 8000) humidity = 2000;
00168     
00169     temperature -= 200;
00170     if(temperature < 200) temperature = 4200;
00171     
00172     pressure += 300;
00173     if(pressure > 10300) pressure = 7500;
00174     
00175     x += 1400;
00176     y -= 2300;
00177     z += 1700;
00178 }
00179 
00180 void StartHaptic(void)  {
00181     hapticTimer.start(50);
00182     haptic = 1;
00183 }
00184 
00185 void StopHaptic(void const *n) {
00186     haptic = 0;
00187     hapticTimer.stop();
00188 }
00189