Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXAS21002 FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351
Fork of Hexi_BLE_Example_Modified by
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 #include "FXAS21002.h" 00008 #include "FXOS8700.h" 00009 #include <cmath> 00010 00011 #define LED_ON 0 00012 #define LED_OFF 1 00013 00014 void UpdateSensorData(void); 00015 void GetSensorData(void); 00016 void StartHaptic(void); 00017 void StopHaptic(void const *n); 00018 void txTask(void); 00019 FXOS8700 accel(PTC11, PTC10); 00020 00021 Serial pc(USBTX, USBRX); 00022 00023 DigitalOut redLed(LED1,1); 00024 DigitalOut greenLed(LED2,1); 00025 DigitalOut blueLed(LED3,1); 00026 DigitalOut haptic(PTB9); 00027 00028 /* Define timer for haptic feedback */ 00029 RtosTimer hapticTimer(StopHaptic, osTimerOnce); 00030 00031 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 00032 KW40Z kw40z_device(PTE24, PTE25); 00033 00034 /* Instantiate the SSD1351 OLED Driver */ 00035 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ 00036 00037 /*Create a Thread to handle sending BLE Sensor Data */ 00038 Thread txThread; 00039 00040 /* Text Buffer */ 00041 char text[20]; 00042 00043 uint8_t battery = 100; 00044 int16_t x = 0; 00045 int16_t y = 0; 00046 int16_t z = 0; 00047 00048 uint8_t abs_x = 0; 00049 uint8_t abs_y = 0; 00050 uint8_t abs_z = 0; 00051 float accel_data[3]; 00052 00053 /****************************Call Back Functions*******************************/ 00054 void ButtonRight(void) 00055 { 00056 StartHaptic(); 00057 kw40z_device.ToggleAdvertisementMode(); 00058 } 00059 00060 void ButtonLeft(void) 00061 { 00062 StartHaptic(); 00063 kw40z_device.ToggleAdvertisementMode(); 00064 } 00065 00066 void PassKey(void) 00067 { 00068 StartHaptic(); 00069 strcpy((char *) text,"PAIR CODE"); 00070 oled.TextBox((uint8_t *)text,0,25,95,18); 00071 00072 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ 00073 sprintf(text,"%d", kw40z_device.GetPassKey()); 00074 oled.TextBox((uint8_t *)text,0,40,95,18); 00075 } 00076 00077 // Key modification: use the alert functionality enabled by the host-ble interface 00078 // to define our own command. 00079 void AlertReceived(uint8_t *data, uint8_t length) 00080 { 00081 StartHaptic(); 00082 data[19] = 0; 00083 pc.printf("%s\n\r", data); 00084 00085 // data (our command) must 20 bytes long. 00086 // CMD for turning on: 'ledonledonledonledon' 00087 if (data[4] == 'n') { 00088 greenLed = LED_ON; 00089 redLed = LED_ON; 00090 blueLed = LED_ON; 00091 pc.printf("on\n\r", data); 00092 00093 // CMD for turning off: 'ledoffledoffledoffled' 00094 } else if (data[4] == 'f') { 00095 greenLed = LED_OFF; 00096 redLed = LED_OFF; 00097 blueLed = LED_OFF; 00098 pc.printf("off\n\r", data); 00099 } 00100 } 00101 /***********************End of Call Back Functions*****************************/ 00102 00103 /********************************Main******************************************/ 00104 00105 int main() 00106 { 00107 /* Register callbacks to application functions */ 00108 kw40z_device.attach_buttonLeft(&ButtonLeft); 00109 kw40z_device.attach_buttonRight(&ButtonRight); 00110 kw40z_device.attach_passkey(&PassKey); 00111 kw40z_device.attach_alert(&AlertReceived); 00112 00113 accel.accel_config(); 00114 00115 pc.printf("hello\n\r"); 00116 00117 /* Turn on the backlight of the OLED Display */ 00118 oled.DimScreenON(); 00119 00120 /* Fills the screen with solid black */ 00121 oled.FillScreen(COLOR_BLACK); 00122 00123 /* Get OLED Class Default Text Properties */ 00124 oled_text_properties_t textProperties = {0}; 00125 oled.GetTextProperties(&textProperties); 00126 00127 /* Change font color to Blue */ 00128 textProperties.fontColor = COLOR_BLUE; 00129 oled.SetTextProperties(&textProperties); 00130 00131 /* Display Bluetooth Label at x=17,y=65 */ 00132 strcpy((char *) text,"BLUETOOTH"); 00133 oled.Label((uint8_t *)text,17,65); 00134 00135 /* Change font color to white */ 00136 textProperties.fontColor = COLOR_WHITE; 00137 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00138 oled.SetTextProperties(&textProperties); 00139 00140 /* Display Label at x=22,y=80 */ 00141 strcpy((char *) text,"Tap Below"); 00142 oled.Label((uint8_t *)text,22,80); 00143 00144 uint8_t prevLinkState = 0; 00145 uint8_t currLinkState = 0; 00146 00147 txThread.start(txTask); /*Start transmitting Sensor Tag Data */ 00148 00149 while (true) 00150 { 00151 // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ 00152 Thread::wait(50); 00153 } 00154 } 00155 00156 /******************************End of Main*************************************/ 00157 00158 /* txTask() transmits the sensor data */ 00159 void txTask(void){ 00160 00161 while (true) 00162 { 00163 GetSensorData(); 00164 00165 /*Notify Hexiwear App that it is running Sensor Tag mode*/ 00166 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); 00167 00168 /*The following is sending dummy data over BLE. Replace with real data*/ 00169 00170 /*Send Battery Level for 20% */ 00171 // SendAccel does not work and is not stable over bluetooth 00172 // So we will use SendBattery 00173 // But since SendBattery cannot send multiple values at a time 00174 // We will have to advertise to Pi what value it should expect next 00175 kw40z_device.SendBatteryLevel('x'); 00176 kw40z_device.SendBatteryLevel(abs_x); 00177 kw40z_device.SendBatteryLevel('y'); 00178 kw40z_device.SendBatteryLevel(abs_y); 00179 kw40z_device.SendBatteryLevel('z'); 00180 kw40z_device.SendBatteryLevel(abs_z); 00181 00182 Thread::wait(1000); 00183 } 00184 } 00185 00186 void GetSensorData(void) { 00187 accel.acquire_accel_data_g(accel_data); 00188 00189 // get x,y,z data and amplify them by 100 00190 // convert x,y,z data to positive numbers since SendBattery only supports unsigned ints 00191 x = (int16_t)(accel_data[0] * 100); 00192 y = (int16_t)(accel_data[1] * 100); 00193 z = (int16_t)(accel_data[2] * 100); 00194 abs_x = abs(x); 00195 abs_y = abs(y); 00196 abs_z = abs(z); 00197 printf("ACCEL (x100)(ABS): X:%d Y:%d Z:%d\n",abs_x,abs_y,abs_z); 00198 } 00199 00200 void StartHaptic(void) { 00201 hapticTimer.start(50); 00202 haptic = 1; 00203 } 00204 00205 void StopHaptic(void const *n) { 00206 haptic = 0; 00207 hapticTimer.stop(); 00208 } 00209
Generated on Tue Jul 19 2022 03:44:06 by
1.7.2
