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_Final_Project 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 kw40z_device.SendBatteryLevel(0); 00059 } 00060 00061 void ButtonLeft(void) 00062 { 00063 StartHaptic(); 00064 //kw40z_device.ToggleAdvertisementMode(); 00065 kw40z_device.SendBatteryLevel(1); 00066 } 00067 void ButtonUp(void) 00068 { 00069 StartHaptic(); 00070 //kw40z_device.ToggleAdvertisementMode(); 00071 kw40z_device.SendBatteryLevel(2); 00072 } 00073 00074 void ButtonDown(void) 00075 { 00076 StartHaptic(); 00077 //kw40z_device.ToggleAdvertisementMode(); 00078 kw40z_device.SendBatteryLevel(3); 00079 } 00080 00081 void PassKey(void) 00082 { 00083 StartHaptic(); 00084 strcpy((char *) text,"PAIR CODE"); 00085 oled.TextBox((uint8_t *)text,0,25,95,18); 00086 00087 /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ 00088 sprintf(text,"%d", kw40z_device.GetPassKey()); 00089 oled.TextBox((uint8_t *)text,0,40,95,18); 00090 } 00091 00092 // Key modification: use the alert functionality enabled by the host-ble interface 00093 // to define our own command. 00094 void AlertReceived(uint8_t *data, uint8_t length) 00095 { 00096 StartHaptic(); 00097 data[19] = 0; 00098 pc.printf("%s\n\r", data); 00099 00100 // data (our command) must 20 bytes long. 00101 // CMD for turning on: 'ledonledonledonledon' 00102 if (data[4] == 'n') { 00103 greenLed = LED_ON; 00104 redLed = LED_ON; 00105 blueLed = LED_ON; 00106 pc.printf("on\n\r", data); 00107 00108 // CMD for turning off: 'ledoffledoffledoffled' 00109 } else if (data[4] == 'f') { 00110 greenLed = LED_OFF; 00111 redLed = LED_OFF; 00112 blueLed = LED_OFF; 00113 pc.printf("off\n\r", data); 00114 } 00115 } 00116 /***********************End of Call Back Functions*****************************/ 00117 00118 /********************************Main******************************************/ 00119 00120 int main() 00121 { 00122 /* Register callbacks to application functions */ 00123 kw40z_device.attach_buttonLeft(&ButtonLeft); 00124 kw40z_device.attach_buttonRight(&ButtonRight); 00125 kw40z_device.attach_buttonUp(&ButtonUp); 00126 kw40z_device.attach_buttonDown(&ButtonDown); 00127 kw40z_device.attach_passkey(&PassKey); 00128 kw40z_device.attach_alert(&AlertReceived); 00129 00130 accel.accel_config(); 00131 00132 pc.printf("hello\n\r"); 00133 00134 /* Turn on the backlight of the OLED Display */ 00135 oled.DimScreenON(); 00136 00137 /* Fills the screen with solid black */ 00138 oled.FillScreen(COLOR_BLACK); 00139 00140 /* Get OLED Class Default Text Properties */ 00141 oled_text_properties_t textProperties = {0}; 00142 oled.GetTextProperties(&textProperties); 00143 00144 /* Change font color to Blue */ 00145 textProperties.fontColor = COLOR_BLUE; 00146 oled.SetTextProperties(&textProperties); 00147 00148 /* Display Bluetooth Label at x=17,y=65 */ 00149 strcpy((char *) text,"BLUETOOTH"); 00150 oled.Label((uint8_t *)text,17,65); 00151 00152 /* Change font color to white */ 00153 textProperties.fontColor = COLOR_WHITE; 00154 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00155 oled.SetTextProperties(&textProperties); 00156 00157 /* Display Label at x=22,y=80 */ 00158 strcpy((char *) text,"Tap Below"); 00159 oled.Label((uint8_t *)text,22,80); 00160 00161 uint8_t prevLinkState = 0; 00162 uint8_t currLinkState = 0; 00163 00164 txThread.start(txTask); /*Start transmitting Sensor Tag Data */ 00165 00166 while (true) 00167 { 00168 // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ 00169 Thread::wait(50); 00170 } 00171 } 00172 00173 /******************************End of Main*************************************/ 00174 00175 /* txTask() transmits the sensor data */ 00176 void txTask(void){ 00177 00178 while (true) 00179 { 00180 GetSensorData(); 00181 00182 /*Notify Hexiwear App that it is running Sensor Tag mode*/ 00183 kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); 00184 00185 /*The following is sending dummy data over BLE. Replace with real data*/ 00186 00187 /*Send Battery Level for 20% */ 00188 // SendAccel does not work and is not stable over bluetooth 00189 // So we will use SendBattery 00190 // But since SendBattery cannot send multiple values at a time 00191 // We will have to advertise to Pi what value it should expect next 00192 if(abs_x >50 && abs_z< 50){ 00193 StartHaptic(); 00194 kw40z_device.SendBatteryLevel(4); 00195 /* //Code for sending raw orientation data below 00196 kw40z_device.SendBatteryLevel('x'); 00197 kw40z_device.SendBatteryLevel(abs_x); 00198 kw40z_device.SendBatteryLevel('y'); 00199 kw40z_device.SendBatteryLevel(abs_y); 00200 kw40z_device.SendBatteryLevel('z'); 00201 kw40z_device.SendBatteryLevel(abs_z); 00202 */ 00203 } 00204 Thread::wait(1000); 00205 } 00206 } 00207 00208 void GetSensorData(void) { 00209 accel.acquire_accel_data_g(accel_data); 00210 00211 // get x,y,z data and amplify them by 100 00212 // convert x,y,z data to positive numbers since SendBattery only supports unsigned ints 00213 x = (int16_t)(accel_data[0] * 100); 00214 y = (int16_t)(accel_data[1] * 100); 00215 z = (int16_t)(accel_data[2] * 100); 00216 abs_x = abs(x); 00217 abs_y = abs(y); 00218 abs_z = abs(z); 00219 printf("ACCEL (x100)(ABS): X:%d Y:%d Z:%d\n",abs_x,abs_y,abs_z); 00220 } 00221 00222 void StartHaptic(void) { 00223 hapticTimer.start(50); 00224 haptic = 1; 00225 } 00226 00227 void StopHaptic(void const *n) { 00228 haptic = 0; 00229 hapticTimer.stop(); 00230 } 00231
Generated on Fri Jul 22 2022 23:27:46 by
1.7.2
