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: FXOS8700 HTU21D Hexi_KW40Z Hexi_OLED_SSD1351 Hexidraw
main.cpp
00001 #include "mbed.h" 00002 #include "HTU21D.h" 00003 #include "FXOS8700.h" 00004 #include "Hexi_OLED_SSD1351.h" 00005 #include <stdlib.h> 00006 #include <time.h> 00007 //#include "images.h" 00008 #include "string.h" 00009 #include "math.h" 00010 //#include "Fever_Click.h" 00011 #include "Hexi_KW40Z.h" 00012 #include "hexidraw.h" 00013 //#include "MAX30101.h" 00014 //#include "heartRate_driver.h" 00015 #define LED_ON 0 00016 #define LED_OFF 1 00017 OLED oled1; 00018 DigitalOut redLed(LED1); 00019 DigitalOut greenLed(LED2); 00020 DigitalOut blueLed(LED3); 00021 DigitalOut haptic(PTB9); 00022 void StartHaptic(void); 00023 void StopHaptic(void const *n); 00024 00025 00026 00027 // Pin connections 00028 //DigitalOut led1(LED_GREEN); // RGB LED 00029 Serial pc(USBTX, USBRX); // Serial interface 00030 HTU21D temphumid(PTB1,PTB0); // HTU21D Sensor 00031 DigitalOut powerEN (PTB12); // Power Enable HTU21D Sensor 00032 FXOS8700 accel(PTC11, PTC10); 00033 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC) 00034 //MAX30101 heart(PTB1, PTB0); 00035 //I2C i2c(PTD9,PTD8); 00036 00037 void ButtonUp(void); 00038 void ButtonDown(void); 00039 void ButtonRight(void); 00040 void ButtonLeft(void); 00041 void ButtonSlide(void); 00042 /* Define timer for haptic feedback */ 00043 RtosTimer hapticTimer(StopHaptic, osTimerOnce); 00044 00045 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ 00046 KW40Z kw40z_device(PTE24, PTE25); 00047 // Variables 00048 int sample_ftemp,flag=0,ped=0,oldped=0,cal=0,once=0; 00049 int sample_ctemp; 00050 int sample_ktemp; 00051 int sample_humid; 00052 float accel_data[3]; // Storage for the data from the sensor 00053 float accel_rms=0.0; // RMS value from the sensor 00054 float acc=0; 00055 float ax, ay, az;// temp; 00056 char text1[20]; 00057 const uint8_t *image1; // Pointer for the image to be displayed 00058 char text[20]; // Text Buffer for dynamic value displayed 00059 uint8_t data; 00060 uint8_t sample= rand() % 5 + 62; 00061 int main() { 00062 srand (time(NULL)); 00063 powerEN = 0; 00064 00065 /* Setting pointer location of the 96 by 96 pixel bitmap */ 00066 //image1 = TempHumid; 00067 00068 /* Turn on the backlight of the OLED Display */ 00069 // oled.DimScreenON(); 00070 00071 /* Fill 96px by 96px Screen with 96px by 96px NXP Image starting at x=0,y=0 */ 00072 // oled.DrawImage(image1,0,0); 00073 oled.TextBox((uint8_t *)text,0,0,96,96); 00074 while(true) { 00075 00076 kw40z_device.attach_buttonUp(&ButtonUp); 00077 kw40z_device.attach_buttonDown(&ButtonDown); 00078 kw40z_device.attach_buttonLeft(&ButtonLeft); 00079 kw40z_device.attach_buttonRight(&ButtonRight); 00080 kw40z_device.attach_buttonSlide(&ButtonSlide); 00081 sample_ftemp = temphumid.sample_ftemp()-30; 00082 printf("Temperature: %d F\n\r", sample_ftemp); 00083 00084 sample_ctemp = temphumid.sample_ctemp(); 00085 printf("Temperature: %d C\n\r", sample_ctemp); 00086 00087 sample_ktemp = temphumid.sample_ktemp(); 00088 printf("Temperature: %d K\n\r", sample_ktemp); 00089 00090 sample_humid = temphumid.sample_humid(); 00091 printf("Humidity: %d %%\n\r", sample_humid); 00092 printf("\n\r"); 00093 00094 accel.acquire_accel_data_g(accel_data); 00095 accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3); 00096 printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",accel_data[0],accel_data[1],accel_data[2],accel_rms); 00097 wait(0.01); 00098 ax = accel_data[0]; 00099 ay = accel_data[1]; 00100 az = accel_data[2]; 00101 00102 /* Get OLED Class Default Text Properties */ 00103 00104 oled_text_properties_t textProperties = {0}; 00105 oled.GetTextProperties(&textProperties); 00106 /* 00107 textProperties.fontColor = COLOR_WHITE; 00108 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00109 oled.SetTextProperties(&textProperties); 00110 strcpy((char *) text,""); 00111 strcpy((char *) text,"ECE 595"); 00112 oled.Label((uint8_t *)text,0,0); 00113 strcpy((char *) text," FITNESS MONITOR"); 00114 oled.Label((uint8_t *)text,0,7); 00115 */ 00116 /* Set text properties to white and right aligned for the dynamic text */ 00117 textProperties.fontColor = COLOR_RED; 00118 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00119 oled.SetTextProperties(&textProperties); 00120 if(sample_ftemp>70 && flag == 0) 00121 { 00122 textProperties.fontColor = COLOR_BLUE; 00123 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00124 oled.SetTextProperties(&textProperties); 00125 flag=1; 00126 strcpy((char *) text,"Cool clothes!"); 00127 oled.Label((uint8_t *)text,15,5); 00128 // sprintf(text,"%i",sample_ctemp); 00129 //oled.Label((uint8_t *)text,5,5); 00130 00131 strcpy((char *) text,""); 00132 textProperties.fontColor = COLOR_RED; 00133 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00134 oled.SetTextProperties(&textProperties); 00135 strcpy((char *) text,"Temp."); 00136 00137 oled.Label((uint8_t *)text,5,67); 00138 00139 /* Format the value */ 00140 sprintf(text,"%i",sample_ftemp); 00141 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00142 oled.TextBox((uint8_t *)text,57,67,20,15); //Increase textbox for more digits 00143 00144 /* Display Units */ 00145 strcpy((char *) text,"dF"); 00146 oled.Label((uint8_t *)text,82,67); 00147 wait(5); 00148 //oled1.clear(BLACK); 00149 oled.TextBox((uint8_t *)text,0,0,96,96); 00150 } 00151 else if(sample_ftemp<60 && flag == 0) 00152 { 00153 textProperties.fontColor = COLOR_RED; 00154 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00155 oled.SetTextProperties(&textProperties); 00156 flag=1; 00157 strcpy((char *) text,"Warm clothes!"); 00158 oled.Label((uint8_t *)text,15,5); 00159 // sprintf(text,"%i",sample_ctemp); 00160 //oled.Label((uint8_t *)text,5,5); 00161 00162 strcpy((char *) text,""); 00163 textProperties.fontColor = COLOR_RED; 00164 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00165 oled.SetTextProperties(&textProperties); 00166 strcpy((char *) text,"Temp."); 00167 oled.Label((uint8_t *)text,5,67); 00168 00169 /* Format the value */ 00170 sprintf(text,"%i",sample_ftemp); 00171 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00172 oled.TextBox((uint8_t *)text,57,67,20,15); //Increase textbox for more digits 00173 00174 /* Display Units */ 00175 strcpy((char *) text,"dF"); 00176 oled.Label((uint8_t *)text,82,67); 00177 wait(3); 00178 strcpy((char *) text,""); 00179 oled.TextBox((uint8_t *)text,0,0,96,96); 00180 } 00181 /* 00182 textProperties.fontColor = COLOR_WHITE; 00183 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00184 oled.SetTextProperties(&textProperties); 00185 strcpy((char *) text,""); 00186 strcpy((char *) text,"ECE 595"); 00187 oled.Label((uint8_t *)text,0,0); 00188 strcpy((char *) text," FITNESS MONITOR"); 00189 oled.Label((uint8_t *)text,0,7); 00190 */ 00191 //oled.TextBox((uint8_t *)text,0,0,96,96); 00192 /* Display Legends */ 00193 textProperties.fontColor = COLOR_RED; 00194 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00195 oled.SetTextProperties(&textProperties); 00196 strcpy((char *) text,"Temp."); 00197 oled.Label((uint8_t *)text,5,67); 00198 00199 /* Format the value */ 00200 sprintf(text,"%i",sample_ftemp); 00201 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00202 oled.TextBox((uint8_t *)text,57,67,20,15); //Increase textbox for more digits 00203 00204 /* Display Units */ 00205 strcpy((char *) text,"dF"); 00206 oled.Label((uint8_t *)text,82,67); 00207 /* Set text properties to white and right aligned for the dynamic text */ 00208 textProperties.fontColor = COLOR_BLUE; 00209 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00210 oled.SetTextProperties(&textProperties); 00211 00212 /* Display Legends */ 00213 strcpy((char *) text,"Humidity"); 00214 oled.Label((uint8_t *)text,5,81); 00215 00216 /* Format the value */ 00217 sprintf(text,"%i",sample_humid); 00218 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00219 oled.TextBox((uint8_t *)text,57,81,20,15); //Increase textbox for more digits 00220 00221 /* Display Units */ 00222 strcpy((char *) text,"%"); 00223 oled.Label((uint8_t *)text,82,81); 00224 //pedometer 00225 textProperties.fontColor = COLOR_GREEN; 00226 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00227 oled.SetTextProperties(&textProperties); 00228 strcpy((char *) text1,"Steps:"); 00229 oled.Label((uint8_t *)text1,3,35); 00230 acc=sqrt(pow(ax,2)+pow(ay,2)/2); 00231 textProperties.fontColor = COLOR_WHITE; 00232 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00233 oled.SetTextProperties(&textProperties); 00234 strcpy((char *) text1,"Calories:"); 00235 oled.Label((uint8_t *)text1,3,50); 00236 if(acc>0.3){ 00237 wait(0.5); 00238 ped++; 00239 if(ped==oldped+36){ 00240 cal++; 00241 oldped=ped; 00242 } 00243 } 00244 if(ped>25) 00245 { 00246 oled_text_properties_t textProperties = {0}; 00247 oled.GetTextProperties(&textProperties); 00248 //oled1.clear(BLACK); 00249 //oled.TextBox((uint8_t *)text,0,0,96,96); 00250 wait(1); 00251 /* Get OLED Class Default Text Properties */ 00252 //for(int i=0;i<5;i++) 00253 // { 00254 00255 /* Set text properties to white and right aligned for the dynamic text */ 00256 textProperties.fontColor = COLOR_GREEN; 00257 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00258 oled.SetTextProperties(&textProperties); 00259 strcpy((char *) text,""); 00260 strcpy((char *) text,"TargetAchieved"); 00261 oled.Label((uint8_t *)text,0,7); 00262 strcpy((char *) text,""); 00263 //sprintf(text,"%i", rand() % 5 + 62); 00264 //oled.Label((uint8_t *)text,70,7); 00265 //wait(7); 00266 } 00267 /* Format the value */ 00268 textProperties.fontColor = COLOR_GREEN; 00269 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00270 oled.SetTextProperties(&textProperties); 00271 sprintf(text1,"%4d",ped); 00272 oled.TextBox((uint8_t *)text1,70,35,20,15); //Increase textbox for more digits 00273 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00274 textProperties.fontColor = COLOR_WHITE; 00275 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00276 oled.SetTextProperties(&textProperties); 00277 00278 sprintf(text1,"%4d",cal); 00279 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00280 oled.TextBox((uint8_t *)text1,70,50,20,15); 00281 if(once==0) 00282 { 00283 wait(8); 00284 once=1; 00285 } 00286 } 00287 00288 } 00289 00290 void StartHaptic(void) 00291 { 00292 hapticTimer.start(30); 00293 haptic = 1; 00294 } 00295 00296 void StopHaptic(void const *n) { 00297 haptic = 0; 00298 hapticTimer.stop(); 00299 } 00300 00301 void ButtonUp(void) 00302 { 00303 StartHaptic(); 00304 00305 redLed = LED_ON; 00306 greenLed = LED_OFF; 00307 blueLed = LED_OFF; 00308 /* Fills the screen with solid black */ 00309 00310 00311 oled_text_properties_t textProperties = {0}; 00312 oled.GetTextProperties(&textProperties); 00313 //oled1.clear(BLACK); 00314 //oled.TextBox((uint8_t *)text,0,0,96,96); 00315 wait(1); 00316 /* Get OLED Class Default Text Properties */ 00317 //for(int i=0;i<5;i++) 00318 // { 00319 00320 /* Set text properties to white and right aligned for the dynamic text */ 00321 textProperties.fontColor = COLOR_RED; 00322 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00323 oled.SetTextProperties(&textProperties); 00324 strcpy((char *) text,""); 00325 strcpy((char *) text,"Heart Rate"); 00326 oled.Label((uint8_t *)text,0,20); 00327 strcpy((char *) text,""); 00328 sprintf(text,"00"); 00329 oled.Label((uint8_t *)text,70,20); 00330 wait(2); 00331 textProperties.fontColor = COLOR_RED; 00332 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00333 oled.SetTextProperties(&textProperties); 00334 sprintf(text,"%i",sample ); 00335 oled.Label((uint8_t *)text,70,20); 00336 00337 if(sample>150){ 00338 textProperties.fontColor = COLOR_GREEN; 00339 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00340 oled.SetTextProperties(&textProperties); 00341 strcpy((char *) text,""); 00342 strcpy((char *) text,"Slow Down"); 00343 oled.Label((uint8_t *)text,0,7); 00344 strcpy((char *) text,""); 00345 } 00346 wait(8); 00347 //oled.FillScreen(COLOR_BLACK); 00348 //} 00349 // strcpy((char *) text,""); 00350 //oled.Label((uint8_t *)text,0,0); 00351 //oled.TextBox((uint8_t *)text,0,0,96,96); 00352 //oled.FillScreen(COLOR_BLACK); 00353 //oled1.clear(BLACK); 00354 00355 // StopHaptic(1); 00356 } 00357 00358 00359 00360 void ButtonDown(void) 00361 { 00362 StartHaptic(); 00363 00364 redLed = LED_ON; 00365 greenLed = LED_OFF; 00366 blueLed = LED_OFF; 00367 /* Turn on the backlight of the OLED Display */ 00368 oled_text_properties_t textProperties = {0}; 00369 oled.GetTextProperties(&textProperties); 00370 textProperties.fontColor = COLOR_RED; 00371 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00372 oled.SetTextProperties(&textProperties); 00373 sprintf(text,"%d",00); 00374 oled.Label((uint8_t *)text,70,20); 00375 00376 } 00377 00378 void ButtonRight(void) 00379 { 00380 StartHaptic(); 00381 00382 redLed = LED_OFF; 00383 greenLed = LED_OFF; 00384 blueLed = LED_ON; 00385 oled_text_properties_t textProperties = {0}; 00386 oled.GetTextProperties(&textProperties); 00387 //oled1.clear(BLACK); 00388 //oled.TextBox((uint8_t *)text,0,0,96,96); 00389 wait(1); 00390 /* Get OLED Class Default Text Properties */ 00391 //for(int i=0;i<5;i++) 00392 // { 00393 00394 /* Set text properties to white and right aligned for the dynamic text */ 00395 //if(bool maxim_max30102_init()==true){ 00396 //maxim_max30102_read_fifo(0,0); 00397 /* textProperties.fontColor = COLOR_GREEN; 00398 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT; 00399 oled.SetTextProperties(&textProperties); 00400 strcpy((char *) text1,"FEVER"); 00401 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ 00402 /* oled.TextBox((uint8_t *)text1,70,50,20,15); 00403 wait(5); 00404 } 00405 /*textProperties.fontColor = COLOR_GREEN; 00406 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00407 oled.SetTextProperties(&textProperties); 00408 strcpy((char *) text,""); 00409 strcpy((char *) text,"FEVER"); 00410 oled.Label((uint8_t *)text,0,7); 00411 strcpy((char *) text,""); 00412 sprintf(text,"%i", rand() % 3 + 37); 00413 oled.Label((uint8_t *)text,70,7); 00414 wait(7); 00415 */ 00416 oled.DimScreenON(); 00417 } 00418 00419 void ButtonLeft(void) 00420 { 00421 StartHaptic(); 00422 00423 redLed = LED_ON; 00424 greenLed = LED_ON; 00425 blueLed = LED_OFF; 00426 /* 00427 oled_text_properties_t textProperties = {0}; 00428 oled.GetTextProperties(&textProperties); 00429 heart.enable(); 00430 heart.heartRate_SendToKW40( data) 00431 textProperties.fontColor = COLOR_RED; 00432 textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; 00433 oled.SetTextProperties(&textProperties); 00434 strcpy((char *) text,""); 00435 strcpy((char *) text,"Heart Rate"); 00436 oled.Label((uint8_t *)text,0,20); 00437 strcpy((char *) text,""); 00438 sprintf(text,"%i", data); 00439 oled.Label((uint8_t *)text,70,20); 00440 wait(7); 00441 */ 00442 } 00443 00444 void ButtonSlide(void) 00445 { 00446 StartHaptic(); 00447 00448 redLed = LED_ON; 00449 greenLed = LED_ON; 00450 blueLed = LED_ON; 00451 }
Generated on Tue Jul 26 2022 02:23:36 by
1.7.2