Xi Han / Mbed OS Hexi_BLE_Example_Modified

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 static Serial pc(USBTX, USBRX);
00017 
00018 DigitalOut redLed(LED1,1);
00019 DigitalOut greenLed(LED2,1);
00020 DigitalOut blueLed(LED3,1);
00021 DigitalOut haptic(PTB9);
00022 
00023 /* Define timer for haptic feedback */
00024 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
00025 
00026 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 
00027 KW40Z kw40z_device(PTE24, PTE25);
00028 
00029 /* Instantiate the SSD1351 OLED Driver */ 
00030 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
00031 
00032 /*Create a Thread to handle sending BLE Sensor Data */ 
00033 Thread txThread;
00034 
00035  /* Text Buffer */ 
00036 char text[20]; 
00037 
00038 uint8_t battery = 100;
00039 uint8_t light = 0;
00040 uint16_t humidity = 4500;
00041 uint16_t temperature = 2000;
00042 uint16_t pressure = 9000;
00043 uint16_t x = 0;
00044 uint16_t y = 5000;
00045 uint16_t z = 10000;
00046 
00047 /****************************Call Back Functions*******************************/
00048 void ButtonRight(void)
00049 {
00050     printf("Right\r\n");
00051     StartHaptic();
00052     kw40z_device.ToggleAdvertisementMode();
00053 }
00054 
00055 void ButtonLeft(void)
00056 {
00057     printf("Left\r\n");
00058     StartHaptic();
00059     kw40z_device.ToggleAdvertisementMode();
00060 }
00061 
00062 void PassKey(void)
00063 {
00064     StartHaptic();
00065     strcpy((char *) text,"PAIR CODE");
00066     oled.TextBox((uint8_t *)text,0,25,95,18);
00067   
00068     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00069     sprintf(text,"%d", kw40z_device.GetPassKey());
00070     oled.TextBox((uint8_t *)text,0,40,95,18);
00071 }
00072 
00073 // Key modification: use the alert functionality enabled by the host-ble interface
00074 // to define our own command.
00075 void MessageReceived(uint8_t *data, uint8_t length)
00076 {
00077     StartHaptic();
00078     data[length] = 0;
00079     printf("%s\n\r", data);
00080     
00081     // data (our command) must 20 bytes long.
00082     // CMD for turning on: 'ledonledonledonledon'
00083     //if (data[4] == 'n') {
00084 //        greenLed = LED_ON;
00085 //        redLed = LED_ON;
00086 //        blueLed = LED_ON;
00087 //        pc.printf("on\n\r", data);
00088     
00089     // CMD for turning off: 'ledoffledoffledoffled'
00090 //    } else if (data[4] == 'f') {
00091 //        greenLed = LED_OFF;
00092 //        redLed = LED_OFF;
00093 //        blueLed = LED_OFF;
00094 //        pc.printf("off\n\r", data);
00095 //    }
00096 }
00097 /***********************End of Call Back Functions*****************************/
00098 
00099 /********************************Main******************************************/
00100 
00101 int main()
00102 {    
00103     /* Register callbacks to application functions */
00104     kw40z_device.attach_buttonLeft(&ButtonLeft);
00105     kw40z_device.attach_buttonRight(&ButtonRight);
00106     kw40z_device.attach_passkey(&PassKey);
00107     kw40z_device.attach_message(&MessageReceived);
00108 
00109     pc.baud(115200);
00110     pc.printf("hello\n\r");
00111     
00112     /* Turn on the backlight of the OLED Display */
00113     oled.DimScreenON();
00114     
00115     /* Fills the screen with solid black */         
00116     oled.FillScreen(COLOR_BLACK);
00117 
00118     /* Get OLED Class Default Text Properties */
00119     oled_text_properties_t textProperties = {0};
00120     oled.GetTextProperties(&textProperties);    
00121         
00122     /* Change font color to Blue */ 
00123     textProperties.fontColor   = COLOR_BLUE;
00124     oled.SetTextProperties(&textProperties);
00125     
00126     /* Display Bluetooth Label at x=17,y=65 */ 
00127     strcpy((char *) text,"BLUETOOTH");
00128     oled.Label((uint8_t *)text,17,65);
00129     
00130     /* Change font color to white */ 
00131     textProperties.fontColor   = COLOR_WHITE;
00132     textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
00133     oled.SetTextProperties(&textProperties);
00134     
00135     /* Display Label at x=22,y=80 */ 
00136     strcpy((char *) text,"Tap Below");
00137     oled.Label((uint8_t *)text,22,80);
00138          
00139     uint8_t prevLinkState = 0; 
00140     uint8_t currLinkState = 0;
00141      
00142     txThread.start(txTask); /*Start transmitting Sensor Tag Data */
00143     
00144     while (true) 
00145     {
00146         // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
00147         Thread::wait(50);
00148     }
00149 }
00150 
00151 /******************************End of Main*************************************/
00152 
00153 
00154 /* txTask() transmits the sensor data */
00155 void txTask(void){
00156    
00157    while (true) 
00158    {
00159         UpdateSensorData();
00160         
00161         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00162         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00163                 
00164         /*The following is sending dummy data over BLE. Replace with real data*/
00165     
00166         /*Send Battery Level for 20% */ 
00167         kw40z_device.SendBatteryLevel(battery);
00168                
00169         /*Send Ambient Light Level at 50% */ 
00170         kw40z_device.SendAmbientLight(light);
00171         
00172         /*Send Humidity at 90% */
00173         kw40z_device.SendHumidity(humidity);
00174         
00175         /*Send Temperature at 25 degrees Celsius */
00176         kw40z_device.SendTemperature(temperature);
00177 
00178         /*Send Pressure at 100kPA */ 
00179         kw40z_device.SendPressure(pressure);
00180         
00181         /*Send Mag,Accel,Gyro Data. */
00182         kw40z_device.SendGyro(x,y,z);
00183         kw40z_device.SendAccel(z,x,y);
00184         kw40z_device.SendMag(y,z,x);
00185         
00186         uint8_t buf[6] = "Hello";
00187         kw40z_device.SendMessage(buf, 5);
00188 
00189         Thread::wait(1000);                 
00190     }
00191 }
00192 
00193 void UpdateSensorData(void)
00194 {    
00195     battery -= 5;
00196     if(battery < 5) battery = 100;
00197     
00198     light += 20;
00199     if(light > 100) light = 0;
00200     
00201     humidity += 500;
00202     if(humidity > 8000) humidity = 2000;
00203     
00204     temperature -= 200;
00205     if(temperature < 200) temperature = 4200;
00206     
00207     pressure += 300;
00208     if(pressure > 10300) pressure = 7500;
00209     
00210     x += 1400;
00211     y -= 2300;
00212     z += 1700;
00213 }
00214 
00215 void StartHaptic(void)  {
00216     hapticTimer.start(50);
00217     haptic = 1;
00218 }
00219 
00220 void StopHaptic(void const *n) {
00221     haptic = 0;
00222     hapticTimer.stop();
00223 }
00224