Done

Dependencies:   HTU21D Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101

Fork of Hexiwear by Jacob Shebesh

Committer:
Jashebes
Date:
Mon Apr 30 18:06:25 2018 +0000
Revision:
0:b9ba2cb49fbe
For final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jashebes 0:b9ba2cb49fbe 1 #include "mbed.h"
Jashebes 0:b9ba2cb49fbe 2 #include "Hexi_OLED_SSD1351.h"
Jashebes 0:b9ba2cb49fbe 3 #include "Ticker.h"
Jashebes 0:b9ba2cb49fbe 4 #include "Hexi_KW40Z.h"
Jashebes 0:b9ba2cb49fbe 5 #include "HTU21D.h"
Jashebes 0:b9ba2cb49fbe 6 #include "string.h"
Jashebes 0:b9ba2cb49fbe 7 #include "MAX30101.h"
Jashebes 0:b9ba2cb49fbe 8
Jashebes 0:b9ba2cb49fbe 9 #define LED_ON 0
Jashebes 0:b9ba2cb49fbe 10 #define LED_OFF 1
Jashebes 0:b9ba2cb49fbe 11
Jashebes 0:b9ba2cb49fbe 12 void StartHaptic(void);
Jashebes 0:b9ba2cb49fbe 13 void StopHaptic(void const *n);
Jashebes 0:b9ba2cb49fbe 14 void ButtonUp(void);
Jashebes 0:b9ba2cb49fbe 15 void ButtonDown(void);
Jashebes 0:b9ba2cb49fbe 16 void ButtonRight(void);
Jashebes 0:b9ba2cb49fbe 17 void ButtonLeft(void);
Jashebes 0:b9ba2cb49fbe 18 void ButtonSlide(void);
Jashebes 0:b9ba2cb49fbe 19 void attime(void);
Jashebes 0:b9ba2cb49fbe 20
Jashebes 0:b9ba2cb49fbe 21 DigitalOut redLed(LED1);
Jashebes 0:b9ba2cb49fbe 22 DigitalOut greenLed(LED2);
Jashebes 0:b9ba2cb49fbe 23 DigitalOut blueLed(LED3);
Jashebes 0:b9ba2cb49fbe 24 DigitalOut haptic(PTB9);
Jashebes 0:b9ba2cb49fbe 25
Jashebes 0:b9ba2cb49fbe 26 MAX30101 heart(PTB1, PTB0);
Jashebes 0:b9ba2cb49fbe 27
Jashebes 0:b9ba2cb49fbe 28 /* Instantiate the SSD1351 OLED Driver */
Jashebes 0:b9ba2cb49fbe 29 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
Jashebes 0:b9ba2cb49fbe 30
Jashebes 0:b9ba2cb49fbe 31 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
Jashebes 0:b9ba2cb49fbe 32 KW40Z kw40z_device(PTE24, PTE25);
Jashebes 0:b9ba2cb49fbe 33
Jashebes 0:b9ba2cb49fbe 34 /* Define timer for haptic feedback */
Jashebes 0:b9ba2cb49fbe 35 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
Jashebes 0:b9ba2cb49fbe 36
Jashebes 0:b9ba2cb49fbe 37 /* Instantiate the HTU21D Sensor */
Jashebes 0:b9ba2cb49fbe 38 HTU21D temphumid(PTB1,PTB0);
Jashebes 0:b9ba2cb49fbe 39 DigitalOut powerEN (PTB12); // Power Enable HTU21D Sensor
Jashebes 0:b9ba2cb49fbe 40
Jashebes 0:b9ba2cb49fbe 41 int sample_ftemp; //values for the humidity/temp sensors
Jashebes 0:b9ba2cb49fbe 42 int sample_ctemp;
Jashebes 0:b9ba2cb49fbe 43 int sample_ktemp;
Jashebes 0:b9ba2cb49fbe 44 int sample_humid;
Jashebes 0:b9ba2cb49fbe 45 int sample_HR;
Jashebes 0:b9ba2cb49fbe 46
Jashebes 0:b9ba2cb49fbe 47 char text[20];
Jashebes 0:b9ba2cb49fbe 48 int flag=0;
Jashebes 0:b9ba2cb49fbe 49 int counter=0;
Jashebes 0:b9ba2cb49fbe 50 Ticker timer;
Jashebes 0:b9ba2cb49fbe 51
Jashebes 0:b9ba2cb49fbe 52 int main()
Jashebes 0:b9ba2cb49fbe 53 {
Jashebes 0:b9ba2cb49fbe 54 powerEN =0;
Jashebes 0:b9ba2cb49fbe 55
Jashebes 0:b9ba2cb49fbe 56 /* Register callbacks to application functions */
Jashebes 0:b9ba2cb49fbe 57 kw40z_device.attach_buttonUp(&ButtonUp);
Jashebes 0:b9ba2cb49fbe 58 kw40z_device.attach_buttonDown(&ButtonDown);
Jashebes 0:b9ba2cb49fbe 59 kw40z_device.attach_buttonLeft(&ButtonLeft);
Jashebes 0:b9ba2cb49fbe 60 kw40z_device.attach_buttonRight(&ButtonRight);
Jashebes 0:b9ba2cb49fbe 61 kw40z_device.attach_buttonSlide(&ButtonSlide);
Jashebes 0:b9ba2cb49fbe 62
Jashebes 0:b9ba2cb49fbe 63 heart.enable();
Jashebes 0:b9ba2cb49fbe 64
Jashebes 0:b9ba2cb49fbe 65 /* Get OLED Class Default Text Properties */
Jashebes 0:b9ba2cb49fbe 66 oled_text_properties_t textProperties = {0};
Jashebes 0:b9ba2cb49fbe 67 oled.GetTextProperties(&textProperties);
Jashebes 0:b9ba2cb49fbe 68
Jashebes 0:b9ba2cb49fbe 69 /* Turn on the backlight of the OLED Display */
Jashebes 0:b9ba2cb49fbe 70 oled.DimScreenON();
Jashebes 0:b9ba2cb49fbe 71
Jashebes 0:b9ba2cb49fbe 72 /* Fills the screen with solid black */
Jashebes 0:b9ba2cb49fbe 73 oled.FillScreen(COLOR_BLACK);
Jashebes 0:b9ba2cb49fbe 74
Jashebes 0:b9ba2cb49fbe 75 /* Display Text at (x=7,y=0) */
Jashebes 0:b9ba2cb49fbe 76 strcpy((char *) text,"Welcome");
Jashebes 0:b9ba2cb49fbe 77 oled.Label((uint8_t *)text,7,0);
Jashebes 0:b9ba2cb49fbe 78
Jashebes 0:b9ba2cb49fbe 79 /* Display text at (x=5,y=40) */
Jashebes 0:b9ba2cb49fbe 80 strcpy(text,"Initializing..");
Jashebes 0:b9ba2cb49fbe 81 oled.Label((uint8_t *)text,5,40);
Jashebes 0:b9ba2cb49fbe 82 /*
Jashebes 0:b9ba2cb49fbe 83 Thread::wait(3000);
Jashebes 0:b9ba2cb49fbe 84
Jashebes 0:b9ba2cb49fbe 85 oled.FillScreen(COLOR_BLACK);
Jashebes 0:b9ba2cb49fbe 86
Jashebes 0:b9ba2cb49fbe 87 strcpy((char *) text,"Functions List:");
Jashebes 0:b9ba2cb49fbe 88 oled.Label((uint8_t *)text,7,0);
Jashebes 0:b9ba2cb49fbe 89
Jashebes 0:b9ba2cb49fbe 90 textProperties.fontColor = COLOR_BLUE;
Jashebes 0:b9ba2cb49fbe 91 oled.SetTextProperties(&textProperties);
Jashebes 0:b9ba2cb49fbe 92
Jashebes 0:b9ba2cb49fbe 93
Jashebes 0:b9ba2cb49fbe 94 strcpy((char *) text,"Left:Bike Menu");
Jashebes 0:b9ba2cb49fbe 95 oled.Label((uint8_t *)text,0,15);
Jashebes 0:b9ba2cb49fbe 96
Jashebes 0:b9ba2cb49fbe 97
Jashebes 0:b9ba2cb49fbe 98 strcpy((char *) text,"Right:Biometrics");
Jashebes 0:b9ba2cb49fbe 99 oled.Label((uint8_t *)text,0,35);
Jashebes 0:b9ba2cb49fbe 100 */
Jashebes 0:b9ba2cb49fbe 101 Thread::wait(3000);
Jashebes 0:b9ba2cb49fbe 102
Jashebes 0:b9ba2cb49fbe 103
Jashebes 0:b9ba2cb49fbe 104 oled.FillScreen(COLOR_BLACK);
Jashebes 0:b9ba2cb49fbe 105
Jashebes 0:b9ba2cb49fbe 106 textProperties.fontColor = COLOR_RED;
Jashebes 0:b9ba2cb49fbe 107 oled.SetTextProperties(&textProperties);
Jashebes 0:b9ba2cb49fbe 108
Jashebes 0:b9ba2cb49fbe 109
Jashebes 0:b9ba2cb49fbe 110 strcpy((char *) text,"Temp:");
Jashebes 0:b9ba2cb49fbe 111 oled.Label((uint8_t *)text,5,15);
Jashebes 0:b9ba2cb49fbe 112
Jashebes 0:b9ba2cb49fbe 113 //display Humidity-linked to while loop
Jashebes 0:b9ba2cb49fbe 114 strcpy((char *) text,"Humidity:");
Jashebes 0:b9ba2cb49fbe 115 oled.Label((uint8_t *)text,5,30);
Jashebes 0:b9ba2cb49fbe 116
Jashebes 0:b9ba2cb49fbe 117 //display timer-linked to while loop for continuous readings
Jashebes 0:b9ba2cb49fbe 118 strcpy((char *) text,"Timer:");
Jashebes 0:b9ba2cb49fbe 119 oled.Label((uint8_t *)text,5,45);
Jashebes 0:b9ba2cb49fbe 120
Jashebes 0:b9ba2cb49fbe 121 //display Humidity-linked to while loop
Jashebes 0:b9ba2cb49fbe 122 // strcpy((char *) text,"HR:");
Jashebes 0:b9ba2cb49fbe 123 // oled.Label((uint8_t *)text,5,60);
Jashebes 0:b9ba2cb49fbe 124
Jashebes 0:b9ba2cb49fbe 125
Jashebes 0:b9ba2cb49fbe 126
Jashebes 0:b9ba2cb49fbe 127
Jashebes 0:b9ba2cb49fbe 128 while(true)
Jashebes 0:b9ba2cb49fbe 129 {
Jashebes 0:b9ba2cb49fbe 130 sample_ftemp = temphumid.sample_ftemp();
Jashebes 0:b9ba2cb49fbe 131 sample_humid = temphumid.sample_humid();
Jashebes 0:b9ba2cb49fbe 132 sample_ftemp-=10;
Jashebes 0:b9ba2cb49fbe 133 // sample_HR = heart.getRevisionID();
Jashebes 0:b9ba2cb49fbe 134
Jashebes 0:b9ba2cb49fbe 135 textProperties.fontColor = COLOR_WHITE;
Jashebes 0:b9ba2cb49fbe 136 textProperties.alignParam = OLED_TEXT_ALIGN_RIGHT;
Jashebes 0:b9ba2cb49fbe 137 oled.SetTextProperties(&textProperties);
Jashebes 0:b9ba2cb49fbe 138
Jashebes 0:b9ba2cb49fbe 139 /* Format the value */
Jashebes 0:b9ba2cb49fbe 140 sprintf(text,"%d F",sample_ftemp);
Jashebes 0:b9ba2cb49fbe 141 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
Jashebes 0:b9ba2cb49fbe 142 oled.TextBox((uint8_t *)text,55,15,35,15); //Increase textbox for more digits
Jashebes 0:b9ba2cb49fbe 143
Jashebes 0:b9ba2cb49fbe 144 /* Format the value */
Jashebes 0:b9ba2cb49fbe 145 sprintf(text,"%d %%",sample_humid);
Jashebes 0:b9ba2cb49fbe 146 /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
Jashebes 0:b9ba2cb49fbe 147 oled.TextBox((uint8_t *)text,55,30,35,15); //Increase textbox for more digits
Jashebes 0:b9ba2cb49fbe 148
Jashebes 0:b9ba2cb49fbe 149 sprintf(text,"%d s",counter);
Jashebes 0:b9ba2cb49fbe 150 // Display time reading in 35px by 15px textbox at(x=55, y=40)
Jashebes 0:b9ba2cb49fbe 151 oled.TextBox((uint8_t *)text,55,45,35,15); //Increase textbox for more digits
Jashebes 0:b9ba2cb49fbe 152
Jashebes 0:b9ba2cb49fbe 153 // sprintf(text,"%d bpm",sample_HR);
Jashebes 0:b9ba2cb49fbe 154 // Display time reading in 35px by 15px textbox at(x=55, y=40)
Jashebes 0:b9ba2cb49fbe 155 //oled.TextBox((uint8_t *)text,55,60,35,15); //Increase textbox for more digits
Jashebes 0:b9ba2cb49fbe 156 //Thread::wait(300);
Jashebes 0:b9ba2cb49fbe 157 }
Jashebes 0:b9ba2cb49fbe 158 }
Jashebes 0:b9ba2cb49fbe 159 void attime(void)
Jashebes 0:b9ba2cb49fbe 160 {
Jashebes 0:b9ba2cb49fbe 161 counter++;
Jashebes 0:b9ba2cb49fbe 162 }
Jashebes 0:b9ba2cb49fbe 163 void ButtonUp(void)
Jashebes 0:b9ba2cb49fbe 164 {
Jashebes 0:b9ba2cb49fbe 165 StartHaptic();
Jashebes 0:b9ba2cb49fbe 166 Thread::wait(50);
Jashebes 0:b9ba2cb49fbe 167 StartHaptic();
Jashebes 0:b9ba2cb49fbe 168
Jashebes 0:b9ba2cb49fbe 169 redLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 170 greenLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 171 blueLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 172
Jashebes 0:b9ba2cb49fbe 173
Jashebes 0:b9ba2cb49fbe 174 if(flag==0)
Jashebes 0:b9ba2cb49fbe 175 {
Jashebes 0:b9ba2cb49fbe 176 timer.attach(&attime,1);
Jashebes 0:b9ba2cb49fbe 177 flag=1;
Jashebes 0:b9ba2cb49fbe 178 }
Jashebes 0:b9ba2cb49fbe 179 else
Jashebes 0:b9ba2cb49fbe 180 {
Jashebes 0:b9ba2cb49fbe 181 timer.detach();
Jashebes 0:b9ba2cb49fbe 182 counter=0;
Jashebes 0:b9ba2cb49fbe 183 flag=0;
Jashebes 0:b9ba2cb49fbe 184 }
Jashebes 0:b9ba2cb49fbe 185 }
Jashebes 0:b9ba2cb49fbe 186
Jashebes 0:b9ba2cb49fbe 187 void ButtonDown(void)
Jashebes 0:b9ba2cb49fbe 188 {
Jashebes 0:b9ba2cb49fbe 189 StartHaptic();
Jashebes 0:b9ba2cb49fbe 190 Thread::wait(50);
Jashebes 0:b9ba2cb49fbe 191 StartHaptic();
Jashebes 0:b9ba2cb49fbe 192
Jashebes 0:b9ba2cb49fbe 193 redLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 194 greenLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 195 blueLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 196
Jashebes 0:b9ba2cb49fbe 197 if(flag==0)
Jashebes 0:b9ba2cb49fbe 198 {
Jashebes 0:b9ba2cb49fbe 199 timer.attach(&attime,1);
Jashebes 0:b9ba2cb49fbe 200 flag=1;
Jashebes 0:b9ba2cb49fbe 201 }
Jashebes 0:b9ba2cb49fbe 202 else
Jashebes 0:b9ba2cb49fbe 203 {
Jashebes 0:b9ba2cb49fbe 204 timer.detach();
Jashebes 0:b9ba2cb49fbe 205 counter=0;
Jashebes 0:b9ba2cb49fbe 206 flag=0;
Jashebes 0:b9ba2cb49fbe 207 }
Jashebes 0:b9ba2cb49fbe 208 }
Jashebes 0:b9ba2cb49fbe 209
Jashebes 0:b9ba2cb49fbe 210 void ButtonRight(void)
Jashebes 0:b9ba2cb49fbe 211 {
Jashebes 0:b9ba2cb49fbe 212 StartHaptic();
Jashebes 0:b9ba2cb49fbe 213
Jashebes 0:b9ba2cb49fbe 214 redLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 215 greenLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 216
Jashebes 0:b9ba2cb49fbe 217 blueLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 218 }
Jashebes 0:b9ba2cb49fbe 219
Jashebes 0:b9ba2cb49fbe 220 void ButtonLeft(void)
Jashebes 0:b9ba2cb49fbe 221 {
Jashebes 0:b9ba2cb49fbe 222 StartHaptic();
Jashebes 0:b9ba2cb49fbe 223
Jashebes 0:b9ba2cb49fbe 224 redLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 225 greenLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 226 blueLed = LED_OFF;
Jashebes 0:b9ba2cb49fbe 227 }
Jashebes 0:b9ba2cb49fbe 228
Jashebes 0:b9ba2cb49fbe 229 void ButtonSlide(void)
Jashebes 0:b9ba2cb49fbe 230 {
Jashebes 0:b9ba2cb49fbe 231 StartHaptic();
Jashebes 0:b9ba2cb49fbe 232
Jashebes 0:b9ba2cb49fbe 233 redLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 234 greenLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 235 blueLed = LED_ON;
Jashebes 0:b9ba2cb49fbe 236 }
Jashebes 0:b9ba2cb49fbe 237
Jashebes 0:b9ba2cb49fbe 238 void StartHaptic(void)
Jashebes 0:b9ba2cb49fbe 239 {
Jashebes 0:b9ba2cb49fbe 240 hapticTimer.start(50);
Jashebes 0:b9ba2cb49fbe 241 haptic = 1;
Jashebes 0:b9ba2cb49fbe 242 }
Jashebes 0:b9ba2cb49fbe 243
Jashebes 0:b9ba2cb49fbe 244 void StopHaptic(void const *n) {
Jashebes 0:b9ba2cb49fbe 245 haptic = 0;
Jashebes 0:b9ba2cb49fbe 246 hapticTimer.stop();
Jashebes 0:b9ba2cb49fbe 247 }