Natasha Sarkar / Mbed OS Hexi_BLE_Example_ModifiedPOTATO

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

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 char seconds = 0;
00047 DigitalOut output(PTA10);
00048 
00049 /****************************Call Back Functions*******************************/
00050 void ButtonRight(void)
00051 {
00052     pc.printf("Right\r\n");
00053     StartHaptic();
00054     
00055     uint8_t buf[6] = "Right";
00056     kw40z_device.SendMessage(buf, 5);
00057     
00058 }
00059 
00060 void ButtonLeft(void)
00061 {
00062     pc.printf("Left\r\n");
00063     StartHaptic();
00064 
00065     uint8_t buf[6] = "Left";
00066     kw40z_device.SendMessage(buf, 5);
00067     
00068 }
00069 
00070 void PassKey(void)
00071 {
00072     pc.printf("passkey\n");
00073     //StartHaptic();
00074     strcpy((char *) text,"PAIR CODE");
00075     oled.TextBox((uint8_t *)text,0,25,95,18);
00076   
00077     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00078     sprintf(text,"%d", kw40z_device.GetPassKey());
00079     oled.TextBox((uint8_t *)text,0,40,95,18);
00080 }
00081 
00082 // Key modification: use the alert functionality enabled by the host-ble interface
00083 // to define our own command.
00084 void alertReceived(uint8_t *data, uint8_t length)
00085 {
00086     StartHaptic();
00087     data[length] = 0;
00088     printf("%s\n\r", data);
00089     oled.TextBox((uint8_t *)data,0,40,95,18);
00090     seconds = data[7];
00091     if ((seconds - '0')%2 == 0) {
00092         output.write(0);
00093     } else {
00094         output.write(1);
00095     }
00096     
00097    // data (our command) must 20 bytes long.
00098    // CMD for turning on: 'ledonledonledonledon'
00099 //    if (data[4] == 'n') {
00100 //        greenLed = LED_ON;
00101 //        redLed = LED_ON;
00102 //        blueLed = LED_ON;
00103 //        pc.printf("on\n\r", data);
00104 //    
00105 //     //CMD for turning off: 'ledoffledoffledoffled'
00106 //    } else if (data[4] == 'f') {
00107 //        greenLed = LED_OFF;
00108 //        redLed = LED_OFF;
00109 //        blueLed = LED_OFF;
00110 //        pc.printf("off\n\r", data);
00111 //    }
00112 }
00113 /***********************End of Call Back Functions*****************************/
00114 
00115 /********************************Main******************************************/
00116 
00117 int main()
00118 {    
00119     pc.baud(9600);
00120     pc.printf("hello\n\r");
00121     
00122     /* Register callbacks to application functions */
00123     kw40z_device.attach_buttonLeft(&ButtonLeft);
00124     kw40z_device.attach_buttonRight(&ButtonRight);
00125     kw40z_device.attach_passkey(&PassKey);
00126     kw40z_device.attach_alert(&alertReceived);
00127     
00128     while (kw40z_device.GetAdvertisementMode() != 0) {
00129         kw40z_device.ToggleAdvertisementMode();
00130     }
00131     
00132     
00133     memset(text, 0, sizeof(char)*20);
00134 
00135     
00136     /* Turn on the backlight of the OLED Display */
00137     oled.DimScreenON();
00138     
00139     /* Fills the screen with solid black */         
00140     oled.FillScreen(COLOR_BLACK);
00141 
00142     /* Get OLED Class Default Text Properties */
00143     oled_text_properties_t textProperties = {0};
00144     oled.GetTextProperties(&textProperties);    
00145         
00146     /* Change font color to Blue */ 
00147     textProperties.fontColor   = COLOR_BLUE;
00148     oled.SetTextProperties(&textProperties);
00149     
00150     /* Display Bluetooth Label at x=17,y=65 */ 
00151     strcpy((char *) text,"BLUETOOTH");
00152     oled.Label((uint8_t *)text,17,65);
00153     
00154     /* Change font color to white */ 
00155     textProperties.fontColor   = COLOR_WHITE;
00156     textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
00157     oled.SetTextProperties(&textProperties);
00158     
00159     /* Display Label at x=22,y=80 */ 
00160     strcpy((char *) text,"Tap Below");
00161     oled.Label((uint8_t *)text,22,80);
00162          
00163     uint8_t prevLinkState = 0; 
00164     uint8_t currLinkState = 0;
00165      
00166     txThread.start(txTask); /*Start transmitting Sensor Tag Data */
00167     
00168     int i = 0;
00169     while (true) 
00170     {
00171         if (i % 20 == 0) 
00172 //            strcpy((char *) text,"potato");
00173 //            text[0] = 'a' + (i%26);
00174 //            oled.Label((uint8_t *)text,17,65);
00175             pc.printf("still here, %d\n\r", i);
00176 //        }
00177         i = (i + 1);
00178         // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
00179         Thread::wait(50);
00180     }
00181 }
00182 
00183 /******************************End of Main*************************************/
00184 
00185 
00186 /* txTask() transmits the sensor data */
00187 void txTask(void) {
00188    
00189    while (true) 
00190    {
00191         UpdateSensorData();
00192         
00193         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00194         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00195                 
00196         /*The following is sending dummy data over BLE. Replace with real data*/
00197     
00198         /*Send Battery Level for 20% */ 
00199         kw40z_device.SendBatteryLevel(battery);
00200                
00201         /*Send Ambient Light Level at 50% */ 
00202         kw40z_device.SendAmbientLight(light);
00203         
00204         /*Send Humidity at 90% */
00205         kw40z_device.SendHumidity(humidity);
00206         
00207         /*Send Temperature at 25 degrees Celsius */
00208         kw40z_device.SendTemperature(temperature);
00209 
00210         /*Send Pressure at 100kPA */ 
00211         kw40z_device.SendPressure(pressure);
00212         
00213         /*Send Mag,Accel,Gyro Data. */
00214         kw40z_device.SendGyro(x,y,z);
00215         kw40z_device.SendAccel(z,x,y);
00216         kw40z_device.SendMag(y,z,x);
00217         
00218         
00219         uint8_t buf[6] = "Hello";
00220         kw40z_device.SendMessage(buf, 5);
00221 
00222         Thread::wait(1000);                 
00223     }
00224 }
00225 
00226 void UpdateSensorData(void)
00227 {    
00228     battery -= 5;
00229     if(battery < 5) battery = 100;
00230     
00231     light += 20;
00232     if(light > 100) light = 0;
00233     
00234     humidity += 500;
00235     if(humidity > 8000) humidity = 2000;
00236     
00237     temperature -= 200;
00238     if(temperature < 200) temperature = 4200;
00239     
00240     pressure += 300;
00241     if(pressure > 10300) pressure = 7500;
00242     
00243     x += 1400;
00244     y -= 2300;
00245     z += 1700;
00246 }
00247 
00248 void StartHaptic(void)  {
00249     hapticTimer.start(50);
00250     haptic = 1;
00251 }
00252 
00253 void StopHaptic(void const *n) {
00254     haptic = 0;
00255     hapticTimer.stop();
00256 }
00257