dsdaf
Dependencies: FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101
Fork of HeartRate by
main.cpp@4:eb89733b8642, 2018-06-11 (annotated)
- Committer:
- catchvibes95
- Date:
- Mon Jun 11 23:10:17 2018 +0000
- Revision:
- 4:eb89733b8642
- Parent:
- 3:2e12e0cd1f26
jk;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xihan94 | 0:33686dd26bf9 | 1 | #include "mbed.h" |
xihan94 | 0:33686dd26bf9 | 2 | #include "mbed_events.h" |
xihan94 | 0:33686dd26bf9 | 3 | #include "MAX30101.h" |
catchvibes95 | 4:eb89733b8642 | 4 | #include "Hexi_KW40Z.h" |
catchvibes95 | 4:eb89733b8642 | 5 | #include "Hexi_OLED_SSD1351.h" |
catchvibes95 | 4:eb89733b8642 | 6 | #include "OLED_types.h" |
catchvibes95 | 4:eb89733b8642 | 7 | #include "OpenSans_Font.h" |
catchvibes95 | 4:eb89733b8642 | 8 | #include "string.h" |
catchvibes95 | 4:eb89733b8642 | 9 | #include "FXOS8700.h" |
catchvibes95 | 4:eb89733b8642 | 10 | |
xihan94 | 0:33686dd26bf9 | 11 | |
xihan94 | 0:33686dd26bf9 | 12 | #define FIFO_DATA_MAX 288 |
xihan94 | 0:33686dd26bf9 | 13 | |
catchvibes95 | 4:eb89733b8642 | 14 | |
catchvibes95 | 4:eb89733b8642 | 15 | |
catchvibes95 | 4:eb89733b8642 | 16 | #define LED_ON 0 |
catchvibes95 | 4:eb89733b8642 | 17 | #define LED_OFF 1 |
catchvibes95 | 4:eb89733b8642 | 18 | |
catchvibes95 | 4:eb89733b8642 | 19 | void UpdateSensorData(void); |
catchvibes95 | 4:eb89733b8642 | 20 | void StartHaptic(void); |
catchvibes95 | 4:eb89733b8642 | 21 | void StopHaptic(void const *n); |
catchvibes95 | 4:eb89733b8642 | 22 | void txTask(void); |
catchvibes95 | 4:eb89733b8642 | 23 | |
catchvibes95 | 4:eb89733b8642 | 24 | DigitalOut led1(LED_GREEN); // RGB LED |
catchvibes95 | 4:eb89733b8642 | 25 | FXOS8700 accel(PTC11, PTC10); |
catchvibes95 | 4:eb89733b8642 | 26 | |
xihan94 | 0:33686dd26bf9 | 27 | DigitalOut pwr1v8(PTA29); |
xihan94 | 1:ad1b075585bc | 28 | DigitalOut pwr3v3b(PTC13); |
xihan94 | 1:ad1b075585bc | 29 | DigitalOut pwr15v(PTB12); |
xihan94 | 0:33686dd26bf9 | 30 | I2C i2c0(PTB1, PTB0); |
xihan94 | 0:33686dd26bf9 | 31 | InterruptIn maximInterrupt(PTB18); |
xihan94 | 0:33686dd26bf9 | 32 | Serial pc(USBTX, USBRX); |
xihan94 | 0:33686dd26bf9 | 33 | |
catchvibes95 | 4:eb89733b8642 | 34 | DigitalOut redLed(LED1,1); |
catchvibes95 | 4:eb89733b8642 | 35 | DigitalOut greenLed(LED2,1); |
catchvibes95 | 4:eb89733b8642 | 36 | DigitalOut blueLed(LED3,1); |
catchvibes95 | 4:eb89733b8642 | 37 | DigitalOut haptic(PTB9); |
catchvibes95 | 4:eb89733b8642 | 38 | |
catchvibes95 | 4:eb89733b8642 | 39 | |
catchvibes95 | 4:eb89733b8642 | 40 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
catchvibes95 | 4:eb89733b8642 | 41 | |
catchvibes95 | 4:eb89733b8642 | 42 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
catchvibes95 | 4:eb89733b8642 | 43 | KW40Z kw40z_device(PTE24, PTE25); |
catchvibes95 | 4:eb89733b8642 | 44 | |
catchvibes95 | 4:eb89733b8642 | 45 | /* Instantiate the SSD1351 OLED Driver */ |
catchvibes95 | 4:eb89733b8642 | 46 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
catchvibes95 | 4:eb89733b8642 | 47 | |
catchvibes95 | 4:eb89733b8642 | 48 | /*Create a Thread to handle sending BLE Sensor Data */ |
catchvibes95 | 4:eb89733b8642 | 49 | Thread txThread; |
catchvibes95 | 4:eb89733b8642 | 50 | |
xihan94 | 0:33686dd26bf9 | 51 | EventQueue evqueue(32 * EVENTS_EVENT_SIZE); |
xihan94 | 0:33686dd26bf9 | 52 | Thread t; |
xihan94 | 0:33686dd26bf9 | 53 | |
xihan94 | 0:33686dd26bf9 | 54 | MAX30101 hr(i2c0); |
xihan94 | 0:33686dd26bf9 | 55 | |
xihan94 | 1:ad1b075585bc | 56 | int mask_ppg = 0; |
xihan94 | 1:ad1b075585bc | 57 | uint32_t count = 0; |
catchvibes95 | 4:eb89733b8642 | 58 | |
catchvibes95 | 4:eb89733b8642 | 59 | |
catchvibes95 | 4:eb89733b8642 | 60 | |
catchvibes95 | 4:eb89733b8642 | 61 | char text[20]; |
catchvibes95 | 4:eb89733b8642 | 62 | |
catchvibes95 | 4:eb89733b8642 | 63 | uint8_t battery = 100; |
catchvibes95 | 4:eb89733b8642 | 64 | uint8_t light = 0; |
catchvibes95 | 4:eb89733b8642 | 65 | uint16_t humidity = 4500; |
catchvibes95 | 4:eb89733b8642 | 66 | uint16_t temperature = 2000; |
catchvibes95 | 4:eb89733b8642 | 67 | uint16_t pressure = 9000; |
catchvibes95 | 4:eb89733b8642 | 68 | uint16_t x = 0; |
catchvibes95 | 4:eb89733b8642 | 69 | uint16_t y = 5000; |
catchvibes95 | 4:eb89733b8642 | 70 | uint16_t z = 10000; |
catchvibes95 | 4:eb89733b8642 | 71 | |
catchvibes95 | 4:eb89733b8642 | 72 | // Variables |
catchvibes95 | 4:eb89733b8642 | 73 | float accel_data[3]; // Storage for the data from the sensor |
catchvibes95 | 4:eb89733b8642 | 74 | float accel_rms=0.0; // RMS value from the sensor |
catchvibes95 | 4:eb89733b8642 | 75 | float ax, ay, az; // Integer value from the sensor to be displayed |
catchvibes95 | 4:eb89733b8642 | 76 | const uint8_t *image1; // Pointer for the image1 to be displayed |
catchvibes95 | 4:eb89733b8642 | 77 | char text1[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 78 | char text2[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 79 | char text3[20]; // Text Buffer for dynamic value displayed |
catchvibes95 | 4:eb89733b8642 | 80 | float dot; |
catchvibes95 | 4:eb89733b8642 | 81 | float old_acc=0; |
catchvibes95 | 4:eb89733b8642 | 82 | float new_acc=0; |
catchvibes95 | 4:eb89733b8642 | 83 | float old_accx, old_accy, old_accz, old_dot=0.0; |
catchvibes95 | 4:eb89733b8642 | 84 | uint8_t StepNum = 0, StepNumber = 0; |
catchvibes95 | 4:eb89733b8642 | 85 | |
catchvibes95 | 4:eb89733b8642 | 86 | float filter_buf[75]; |
catchvibes95 | 4:eb89733b8642 | 87 | |
catchvibes95 | 4:eb89733b8642 | 88 | /****************************Call Back Functions*******************************/ |
catchvibes95 | 4:eb89733b8642 | 89 | |
catchvibes95 | 4:eb89733b8642 | 90 | float Filter(int s) |
catchvibes95 | 4:eb89733b8642 | 91 | { |
catchvibes95 | 4:eb89733b8642 | 92 | accel.acquire_accel_data_g(accel_data); |
catchvibes95 | 4:eb89733b8642 | 93 | float filter_sum = 0.0; |
catchvibes95 | 4:eb89733b8642 | 94 | //printf("%d\n\r",s); |
catchvibes95 | 4:eb89733b8642 | 95 | for(int i = 0; i < 75; i++) |
catchvibes95 | 4:eb89733b8642 | 96 | { |
catchvibes95 | 4:eb89733b8642 | 97 | filter_buf[i] = accel_data[s]; |
catchvibes95 | 4:eb89733b8642 | 98 | //printf("%4.2f\n\r",filter_buf[i]); |
catchvibes95 | 4:eb89733b8642 | 99 | filter_sum += filter_buf[i]; |
catchvibes95 | 4:eb89733b8642 | 100 | } |
catchvibes95 | 4:eb89733b8642 | 101 | return (float)(filter_sum / 75); |
catchvibes95 | 4:eb89733b8642 | 102 | } |
catchvibes95 | 4:eb89733b8642 | 103 | |
catchvibes95 | 4:eb89733b8642 | 104 | |
catchvibes95 | 4:eb89733b8642 | 105 | void ButtonRight(void) |
catchvibes95 | 4:eb89733b8642 | 106 | { |
catchvibes95 | 4:eb89733b8642 | 107 | StartHaptic(); |
catchvibes95 | 4:eb89733b8642 | 108 | kw40z_device.ToggleAdvertisementMode(); |
catchvibes95 | 4:eb89733b8642 | 109 | } |
catchvibes95 | 4:eb89733b8642 | 110 | |
catchvibes95 | 4:eb89733b8642 | 111 | void ButtonLeft(void) |
catchvibes95 | 4:eb89733b8642 | 112 | { |
catchvibes95 | 4:eb89733b8642 | 113 | StartHaptic(); |
catchvibes95 | 4:eb89733b8642 | 114 | kw40z_device.ToggleAdvertisementMode(); |
catchvibes95 | 4:eb89733b8642 | 115 | } |
catchvibes95 | 4:eb89733b8642 | 116 | |
catchvibes95 | 4:eb89733b8642 | 117 | void ButtonUp(void) |
catchvibes95 | 4:eb89733b8642 | 118 | { |
catchvibes95 | 4:eb89733b8642 | 119 | StartHaptic(); |
catchvibes95 | 4:eb89733b8642 | 120 | oled.FillScreen(COLOR_BLACK); |
catchvibes95 | 4:eb89733b8642 | 121 | |
catchvibes95 | 4:eb89733b8642 | 122 | /* Get OLED Class Default Text Properties */ |
catchvibes95 | 4:eb89733b8642 | 123 | oled_text_properties_t textProperties = {0}; |
catchvibes95 | 4:eb89733b8642 | 124 | oled.GetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 125 | |
catchvibes95 | 4:eb89733b8642 | 126 | /* Change font color to Blue */ |
catchvibes95 | 4:eb89733b8642 | 127 | textProperties.fontColor = COLOR_BLUE; |
catchvibes95 | 4:eb89733b8642 | 128 | oled.SetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 129 | strcpy((char *) text1,"Steps: "); |
catchvibes95 | 4:eb89733b8642 | 130 | oled.Label((uint8_t *)text1,3,45); |
catchvibes95 | 4:eb89733b8642 | 131 | sprintf(text1,"%d",StepNumber); |
catchvibes95 | 4:eb89733b8642 | 132 | /* Display time reading in 35px by 15px textbox at(x=55, y=40) */ |
catchvibes95 | 4:eb89733b8642 | 133 | oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits |
catchvibes95 | 4:eb89733b8642 | 134 | |
catchvibes95 | 4:eb89733b8642 | 135 | |
catchvibes95 | 4:eb89733b8642 | 136 | } |
catchvibes95 | 4:eb89733b8642 | 137 | void ButtonDown(void) |
catchvibes95 | 4:eb89733b8642 | 138 | { |
catchvibes95 | 4:eb89733b8642 | 139 | oled.FillScreen(COLOR_BLACK); |
catchvibes95 | 4:eb89733b8642 | 140 | |
catchvibes95 | 4:eb89733b8642 | 141 | /* Get OLED Class Default Text Properties */ |
catchvibes95 | 4:eb89733b8642 | 142 | oled_text_properties_t textProperties = {0}; |
catchvibes95 | 4:eb89733b8642 | 143 | oled.GetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 144 | |
catchvibes95 | 4:eb89733b8642 | 145 | /* Change font color to Blue */ |
catchvibes95 | 4:eb89733b8642 | 146 | textProperties.fontColor = COLOR_BLUE; |
catchvibes95 | 4:eb89733b8642 | 147 | oled.SetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 148 | |
catchvibes95 | 4:eb89733b8642 | 149 | /* Display Bluetooth Label at x=17,y=65 */ |
catchvibes95 | 4:eb89733b8642 | 150 | strcpy((char *) text,"BLUETOOTH"); |
catchvibes95 | 4:eb89733b8642 | 151 | oled.Label((uint8_t *)text,17,65); |
catchvibes95 | 4:eb89733b8642 | 152 | |
catchvibes95 | 4:eb89733b8642 | 153 | /* Change font color to white */ |
catchvibes95 | 4:eb89733b8642 | 154 | textProperties.fontColor = COLOR_WHITE; |
catchvibes95 | 4:eb89733b8642 | 155 | textProperties.alignParam = OLED_TEXT_ALIGN_CENTER; |
catchvibes95 | 4:eb89733b8642 | 156 | oled.SetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 157 | |
catchvibes95 | 4:eb89733b8642 | 158 | /* Display Label at x=22,y=80 */ |
catchvibes95 | 4:eb89733b8642 | 159 | strcpy((char *) text,"Tap Below"); |
catchvibes95 | 4:eb89733b8642 | 160 | oled.Label((uint8_t *)text,22,80); |
catchvibes95 | 4:eb89733b8642 | 161 | |
catchvibes95 | 4:eb89733b8642 | 162 | |
catchvibes95 | 4:eb89733b8642 | 163 | } |
catchvibes95 | 4:eb89733b8642 | 164 | void PassKey(void) |
catchvibes95 | 4:eb89733b8642 | 165 | { |
catchvibes95 | 4:eb89733b8642 | 166 | StartHaptic(); |
catchvibes95 | 4:eb89733b8642 | 167 | strcpy((char *) text,"PAIR CODE"); |
catchvibes95 | 4:eb89733b8642 | 168 | oled.TextBox((uint8_t *)text,0,25,95,18); |
catchvibes95 | 4:eb89733b8642 | 169 | |
catchvibes95 | 4:eb89733b8642 | 170 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
catchvibes95 | 4:eb89733b8642 | 171 | sprintf(text,"%d", kw40z_device.GetPassKey()); |
catchvibes95 | 4:eb89733b8642 | 172 | oled.TextBox((uint8_t *)text,0,40,95,18); |
catchvibes95 | 4:eb89733b8642 | 173 | } |
catchvibes95 | 4:eb89733b8642 | 174 | |
catchvibes95 | 4:eb89733b8642 | 175 | // Key modification: use the alert functionality enabled by the host-ble interface |
catchvibes95 | 4:eb89733b8642 | 176 | // to define our own command. |
catchvibes95 | 4:eb89733b8642 | 177 | void AlertReceived(uint8_t *data, uint8_t length) |
catchvibes95 | 4:eb89733b8642 | 178 | { |
catchvibes95 | 4:eb89733b8642 | 179 | StartHaptic(); |
catchvibes95 | 4:eb89733b8642 | 180 | data[19] = 0; |
catchvibes95 | 4:eb89733b8642 | 181 | pc.printf("%s\n\r", data); |
catchvibes95 | 4:eb89733b8642 | 182 | |
catchvibes95 | 4:eb89733b8642 | 183 | // data (our command) must 20 bytes long. |
catchvibes95 | 4:eb89733b8642 | 184 | // CMD for turning on: 'ledonledonledonledon' |
catchvibes95 | 4:eb89733b8642 | 185 | if (data[4] == 'n') { |
catchvibes95 | 4:eb89733b8642 | 186 | greenLed = LED_ON; |
catchvibes95 | 4:eb89733b8642 | 187 | redLed = LED_ON; |
catchvibes95 | 4:eb89733b8642 | 188 | blueLed = LED_ON; |
catchvibes95 | 4:eb89733b8642 | 189 | pc.printf("on\n\r", data); |
catchvibes95 | 4:eb89733b8642 | 190 | |
catchvibes95 | 4:eb89733b8642 | 191 | // CMD for turning off: 'ledoffledoffledoffled' |
catchvibes95 | 4:eb89733b8642 | 192 | } else if (data[4] == 'f') { |
catchvibes95 | 4:eb89733b8642 | 193 | greenLed = LED_OFF; |
catchvibes95 | 4:eb89733b8642 | 194 | redLed = LED_OFF; |
catchvibes95 | 4:eb89733b8642 | 195 | blueLed = LED_OFF; |
catchvibes95 | 4:eb89733b8642 | 196 | pc.printf("off\n\r", data); |
catchvibes95 | 4:eb89733b8642 | 197 | } |
catchvibes95 | 4:eb89733b8642 | 198 | } |
xihan94 | 0:33686dd26bf9 | 199 | void interruptHandlerQueued() { |
xihan94 | 0:33686dd26bf9 | 200 | |
xihan94 | 0:33686dd26bf9 | 201 | MAX30101::InterruptBitField_u interruptStatus; |
xihan94 | 0:33686dd26bf9 | 202 | hr.getInterruptStatus(interruptStatus); |
xihan94 | 3:2e12e0cd1f26 | 203 | // printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all); |
xihan94 | 0:33686dd26bf9 | 204 | |
xihan94 | 0:33686dd26bf9 | 205 | if (interruptStatus.bits.pwr_rdy == 0x1) { |
xihan94 | 3:2e12e0cd1f26 | 206 | // printf("Powered on\r\n"); |
xihan94 | 0:33686dd26bf9 | 207 | |
xihan94 | 1:ad1b075585bc | 208 | // Soft reset |
xihan94 | 1:ad1b075585bc | 209 | MAX30101::ModeConfiguration_u modeConf; |
xihan94 | 1:ad1b075585bc | 210 | modeConf.all = 0; |
xihan94 | 1:ad1b075585bc | 211 | modeConf.bits.reset = 1; |
xihan94 | 1:ad1b075585bc | 212 | hr.setModeConfiguration(modeConf); |
xihan94 | 1:ad1b075585bc | 213 | wait(0.01); |
xihan94 | 1:ad1b075585bc | 214 | |
xihan94 | 0:33686dd26bf9 | 215 | // Configure FIFO |
xihan94 | 0:33686dd26bf9 | 216 | MAX30101::FIFO_Configuration_u fifoConf; |
xihan94 | 0:33686dd26bf9 | 217 | hr.getFIFOConfiguration(fifoConf); |
xihan94 | 3:2e12e0cd1f26 | 218 | // pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all); |
xihan94 | 0:33686dd26bf9 | 219 | |
xihan94 | 0:33686dd26bf9 | 220 | // Set LED power |
xihan94 | 1:ad1b075585bc | 221 | hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C); |
xihan94 | 1:ad1b075585bc | 222 | hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19); |
xihan94 | 3:2e12e0cd1f26 | 223 | // pc.printf("LED set\r\n"); |
xihan94 | 0:33686dd26bf9 | 224 | |
xihan94 | 0:33686dd26bf9 | 225 | MAX30101::SpO2Configuration_u spo2Conf; |
xihan94 | 0:33686dd26bf9 | 226 | hr.getSpO2Configuration(spo2Conf); |
xihan94 | 1:ad1b075585bc | 227 | spo2Conf.bits.led_pw = MAX30101::PW_1; |
xihan94 | 1:ad1b075585bc | 228 | spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz; |
xihan94 | 1:ad1b075585bc | 229 | hr.setSpO2Configuration(spo2Conf); |
xihan94 | 1:ad1b075585bc | 230 | hr.getSpO2Configuration(spo2Conf); |
xihan94 | 3:2e12e0cd1f26 | 231 | // pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all); |
xihan94 | 0:33686dd26bf9 | 232 | |
xihan94 | 1:ad1b075585bc | 233 | // Proximity settings |
xihan94 | 1:ad1b075585bc | 234 | hr.setProxIntThreshold(0x14); |
xihan94 | 1:ad1b075585bc | 235 | |
xihan94 | 0:33686dd26bf9 | 236 | // Enable HR mode |
xihan94 | 0:33686dd26bf9 | 237 | modeConf.all = 0; |
xihan94 | 0:33686dd26bf9 | 238 | modeConf.bits.mode = MAX30101::HeartRateMode; |
xihan94 | 0:33686dd26bf9 | 239 | hr.setModeConfiguration(modeConf); |
xihan94 | 3:2e12e0cd1f26 | 240 | // printf("Mode set\r\n"); |
xihan94 | 0:33686dd26bf9 | 241 | } |
xihan94 | 0:33686dd26bf9 | 242 | |
xihan94 | 1:ad1b075585bc | 243 | if (interruptStatus.bits.prox_int == 0x1) { |
xihan94 | 3:2e12e0cd1f26 | 244 | // printf("Proximity Triggered, entered HR Mode."); |
xihan94 | 1:ad1b075585bc | 245 | } |
xihan94 | 1:ad1b075585bc | 246 | |
xihan94 | 0:33686dd26bf9 | 247 | if (interruptStatus.bits.ppg_rdy == 0x1) { |
xihan94 | 3:2e12e0cd1f26 | 248 | // printf("PPG Ready.\r\n"); |
xihan94 | 1:ad1b075585bc | 249 | mask_ppg = 1; |
xihan94 | 0:33686dd26bf9 | 250 | } |
xihan94 | 0:33686dd26bf9 | 251 | |
xihan94 | 0:33686dd26bf9 | 252 | if (interruptStatus.bits.a_full == 0x1) { |
xihan94 | 3:2e12e0cd1f26 | 253 | // printf("FIFO Almost Full.\r\n"); |
xihan94 | 1:ad1b075585bc | 254 | uint8_t data[FIFO_DATA_MAX]; |
xihan94 | 1:ad1b075585bc | 255 | uint16_t readBytes = 0; |
xihan94 | 1:ad1b075585bc | 256 | hr.readFIFO(MAX30101::OneLedChannel, data, readBytes); |
xihan94 | 1:ad1b075585bc | 257 | |
xihan94 | 1:ad1b075585bc | 258 | for (uint16_t i = 0; i < readBytes; i += 3) { |
xihan94 | 1:ad1b075585bc | 259 | uint8_t sample[4] = {0}; |
xihan94 | 1:ad1b075585bc | 260 | sample[0] = data[i + 2]; |
xihan94 | 1:ad1b075585bc | 261 | sample[1] = data[i + 1]; |
xihan94 | 1:ad1b075585bc | 262 | sample[2] = data[i]; |
xihan94 | 1:ad1b075585bc | 263 | |
catchvibes95 | 4:eb89733b8642 | 264 | printf("%u\r\n", *(uint32_t *) sample); |
xihan94 | 1:ad1b075585bc | 265 | } |
xihan94 | 0:33686dd26bf9 | 266 | } |
xihan94 | 0:33686dd26bf9 | 267 | |
xihan94 | 0:33686dd26bf9 | 268 | interruptStatus.all = 0xFF; |
xihan94 | 1:ad1b075585bc | 269 | if (mask_ppg == 1) { |
xihan94 | 1:ad1b075585bc | 270 | interruptStatus.bits.ppg_rdy = 0; |
xihan94 | 1:ad1b075585bc | 271 | } |
xihan94 | 0:33686dd26bf9 | 272 | hr.enableInterrupts(interruptStatus); |
xihan94 | 0:33686dd26bf9 | 273 | } |
xihan94 | 0:33686dd26bf9 | 274 | |
xihan94 | 0:33686dd26bf9 | 275 | void interruptHandler() { |
xihan94 | 0:33686dd26bf9 | 276 | evqueue.call(interruptHandlerQueued); |
xihan94 | 0:33686dd26bf9 | 277 | } |
xihan94 | 0:33686dd26bf9 | 278 | |
xihan94 | 0:33686dd26bf9 | 279 | // main() runs in its own thread in the OS |
xihan94 | 0:33686dd26bf9 | 280 | int main() { |
xihan94 | 3:2e12e0cd1f26 | 281 | // printf("Hello world.\r\n"); |
xihan94 | 0:33686dd26bf9 | 282 | |
xihan94 | 0:33686dd26bf9 | 283 | t.start(callback(&evqueue, &EventQueue::dispatch_forever)); |
catchvibes95 | 4:eb89733b8642 | 284 | accel.accel_config(); |
catchvibes95 | 4:eb89733b8642 | 285 | /* Register callbacks to application functions */ |
catchvibes95 | 4:eb89733b8642 | 286 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
catchvibes95 | 4:eb89733b8642 | 287 | kw40z_device.attach_buttonRight(&ButtonRight); |
catchvibes95 | 4:eb89733b8642 | 288 | kw40z_device.attach_buttonDown(&ButtonDown); |
catchvibes95 | 4:eb89733b8642 | 289 | kw40z_device.attach_buttonUp(&ButtonUp); |
catchvibes95 | 4:eb89733b8642 | 290 | kw40z_device.attach_passkey(&PassKey); |
catchvibes95 | 4:eb89733b8642 | 291 | kw40z_device.attach_alert(&AlertReceived); |
catchvibes95 | 4:eb89733b8642 | 292 | |
catchvibes95 | 4:eb89733b8642 | 293 | pc.printf("hello\n\r"); |
catchvibes95 | 4:eb89733b8642 | 294 | |
catchvibes95 | 4:eb89733b8642 | 295 | /* Turn on the backlight of the OLED Display */ |
catchvibes95 | 4:eb89733b8642 | 296 | oled.DimScreenON(); |
catchvibes95 | 4:eb89733b8642 | 297 | |
catchvibes95 | 4:eb89733b8642 | 298 | /* Fills the screen with solid black */ |
catchvibes95 | 4:eb89733b8642 | 299 | |
catchvibes95 | 4:eb89733b8642 | 300 | |
catchvibes95 | 4:eb89733b8642 | 301 | uint8_t prevLinkState = 0; |
catchvibes95 | 4:eb89733b8642 | 302 | uint8_t currLinkState = 0; |
catchvibes95 | 4:eb89733b8642 | 303 | |
catchvibes95 | 4:eb89733b8642 | 304 | |
xihan94 | 0:33686dd26bf9 | 305 | pwr1v8 = 1; |
xihan94 | 0:33686dd26bf9 | 306 | pwr3v3b = 1; |
xihan94 | 1:ad1b075585bc | 307 | pwr15v = 0; |
xihan94 | 0:33686dd26bf9 | 308 | |
xihan94 | 0:33686dd26bf9 | 309 | maximInterrupt.fall(interruptHandler); |
xihan94 | 0:33686dd26bf9 | 310 | maximInterrupt.enable_irq(); |
xihan94 | 0:33686dd26bf9 | 311 | |
xihan94 | 0:33686dd26bf9 | 312 | MAX30101::InterruptBitField_u interruptStatus; |
xihan94 | 0:33686dd26bf9 | 313 | interruptStatus.all = 0xFF; |
xihan94 | 0:33686dd26bf9 | 314 | hr.enableInterrupts(interruptStatus); |
xihan94 | 0:33686dd26bf9 | 315 | |
catchvibes95 | 4:eb89733b8642 | 316 | |
catchvibes95 | 4:eb89733b8642 | 317 | txThread.start(txTask); /*Start transmitting Sensor Tag Data */ |
catchvibes95 | 4:eb89733b8642 | 318 | |
catchvibes95 | 4:eb89733b8642 | 319 | while (true) |
catchvibes95 | 4:eb89733b8642 | 320 | { |
catchvibes95 | 4:eb89733b8642 | 321 | // blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
catchvibes95 | 4:eb89733b8642 | 322 | Thread::wait(50); |
catchvibes95 | 4:eb89733b8642 | 323 | } |
catchvibes95 | 4:eb89733b8642 | 324 | |
xihan94 | 0:33686dd26bf9 | 325 | return 0; |
xihan94 | 0:33686dd26bf9 | 326 | } |
catchvibes95 | 4:eb89733b8642 | 327 | void txTask(void){ |
catchvibes95 | 4:eb89733b8642 | 328 | |
catchvibes95 | 4:eb89733b8642 | 329 | while (true) |
catchvibes95 | 4:eb89733b8642 | 330 | { |
catchvibes95 | 4:eb89733b8642 | 331 | UpdateSensorData(); |
catchvibes95 | 4:eb89733b8642 | 332 | |
catchvibes95 | 4:eb89733b8642 | 333 | /*Notify Hexiwear App that it is running Sensor Tag mode*/ |
catchvibes95 | 4:eb89733b8642 | 334 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
catchvibes95 | 4:eb89733b8642 | 335 | |
catchvibes95 | 4:eb89733b8642 | 336 | /*The following is sending dummy data over BLE. Replace with real data*/ |
catchvibes95 | 4:eb89733b8642 | 337 | |
catchvibes95 | 4:eb89733b8642 | 338 | /*Send Battery Level for 20% */ |
catchvibes95 | 4:eb89733b8642 | 339 | kw40z_device.SendBatteryLevel(StepNumber); |
catchvibes95 | 4:eb89733b8642 | 340 | |
catchvibes95 | 4:eb89733b8642 | 341 | /*Send Ambient Light Level at 50% */ |
catchvibes95 | 4:eb89733b8642 | 342 | //kw40z_device.SendAmbientLight(light); |
catchvibes95 | 4:eb89733b8642 | 343 | |
catchvibes95 | 4:eb89733b8642 | 344 | /*Send Humidity at 90% */ |
catchvibes95 | 4:eb89733b8642 | 345 | //kw40z_device.SendHumidity(humidity); |
catchvibes95 | 4:eb89733b8642 | 346 | |
catchvibes95 | 4:eb89733b8642 | 347 | /*Send Temperature at 25 degrees Celsius */ |
catchvibes95 | 4:eb89733b8642 | 348 | //kw40z_device.SendTemperature(temperature); |
xihan94 | 0:33686dd26bf9 | 349 | |
catchvibes95 | 4:eb89733b8642 | 350 | /*Send Pressure at 100kPA */ |
catchvibes95 | 4:eb89733b8642 | 351 | //kw40z_device.SendPressure(pressure); |
catchvibes95 | 4:eb89733b8642 | 352 | |
catchvibes95 | 4:eb89733b8642 | 353 | /*Send Mag,Accel,Gyro Data. */ |
catchvibes95 | 4:eb89733b8642 | 354 | // kw40z_device.SendGyro(x,y,z); |
catchvibes95 | 4:eb89733b8642 | 355 | // kw40z_device.SendAccel(z,x,y); |
catchvibes95 | 4:eb89733b8642 | 356 | // kw40z_device.SendMag(y,z,x); |
catchvibes95 | 4:eb89733b8642 | 357 | |
catchvibes95 | 4:eb89733b8642 | 358 | Thread::wait(10); |
catchvibes95 | 4:eb89733b8642 | 359 | } |
catchvibes95 | 4:eb89733b8642 | 360 | } |
catchvibes95 | 4:eb89733b8642 | 361 | |
catchvibes95 | 4:eb89733b8642 | 362 | void UpdateSensorData(void) |
catchvibes95 | 4:eb89733b8642 | 363 | { |
catchvibes95 | 4:eb89733b8642 | 364 | accel.acquire_accel_data_g(accel_data); |
catchvibes95 | 4:eb89733b8642 | 365 | //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); |
catchvibes95 | 4:eb89733b8642 | 366 | ax = Filter(0); |
catchvibes95 | 4:eb89733b8642 | 367 | ay = Filter(1); |
catchvibes95 | 4:eb89733b8642 | 368 | az = Filter(2); |
catchvibes95 | 4:eb89733b8642 | 369 | wait(0.02); |
catchvibes95 | 4:eb89733b8642 | 370 | accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3); |
catchvibes95 | 4:eb89733b8642 | 371 | //printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",ax,ay,az,accel_rms); |
catchvibes95 | 4:eb89733b8642 | 372 | dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az); |
catchvibes95 | 4:eb89733b8642 | 373 | old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz)); |
catchvibes95 | 4:eb89733b8642 | 374 | new_acc = abs(sqrt(ax*ax+ay*ay+az*az)); |
catchvibes95 | 4:eb89733b8642 | 375 | //printf("\nOld Acceleration: %4.2f\n\r",old_acc); |
catchvibes95 | 4:eb89733b8642 | 376 | //printf("New Acceleration: %4.2f\n\r",new_acc); |
catchvibes95 | 4:eb89733b8642 | 377 | dot /= (old_acc * new_acc); |
catchvibes95 | 4:eb89733b8642 | 378 | //printf("\nDot: %4.2f\n\r",dot); |
catchvibes95 | 4:eb89733b8642 | 379 | //printf("Old Dot: %4.2f\n\r",old_dot); |
catchvibes95 | 4:eb89733b8642 | 380 | |
catchvibes95 | 4:eb89733b8642 | 381 | oled_text_properties_t textProperties = {0}; |
catchvibes95 | 4:eb89733b8642 | 382 | oled.GetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 383 | |
catchvibes95 | 4:eb89733b8642 | 384 | /* Set text properties to white and right aligned for the dynamic text */ |
catchvibes95 | 4:eb89733b8642 | 385 | textProperties.fontColor = COLOR_BLUE; |
catchvibes95 | 4:eb89733b8642 | 386 | textProperties.alignParam = OLED_TEXT_ALIGN_LEFT; |
catchvibes95 | 4:eb89733b8642 | 387 | oled.SetTextProperties(&textProperties); |
catchvibes95 | 4:eb89733b8642 | 388 | |
catchvibes95 | 4:eb89733b8642 | 389 | /* Display Legends */ |
catchvibes95 | 4:eb89733b8642 | 390 | //strcpy((char *) text1,"Steps: "); |
catchvibes95 | 4:eb89733b8642 | 391 | //oled.Label((uint8_t *)text1,3,45); |
catchvibes95 | 4:eb89733b8642 | 392 | StepNum = StepNumber; |
catchvibes95 | 4:eb89733b8642 | 393 | if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) |
catchvibes95 | 4:eb89733b8642 | 394 | { |
catchvibes95 | 4:eb89733b8642 | 395 | StepNumber += 1; |
catchvibes95 | 4:eb89733b8642 | 396 | |
catchvibes95 | 4:eb89733b8642 | 397 | } |
catchvibes95 | 4:eb89733b8642 | 398 | //printf("%4.2f\n\r",dot); |
catchvibes95 | 4:eb89733b8642 | 399 | old_accx = ax; |
catchvibes95 | 4:eb89733b8642 | 400 | old_accy = ay; |
catchvibes95 | 4:eb89733b8642 | 401 | old_accz = az; |
catchvibes95 | 4:eb89733b8642 | 402 | old_dot = dot; |
catchvibes95 | 4:eb89733b8642 | 403 | |
catchvibes95 | 4:eb89733b8642 | 404 | Thread::wait(250); |
catchvibes95 | 4:eb89733b8642 | 405 | } |
catchvibes95 | 4:eb89733b8642 | 406 | |
catchvibes95 | 4:eb89733b8642 | 407 | |
catchvibes95 | 4:eb89733b8642 | 408 | void StartHaptic(void) { |
catchvibes95 | 4:eb89733b8642 | 409 | hapticTimer.start(50); |
catchvibes95 | 4:eb89733b8642 | 410 | haptic = 1; |
catchvibes95 | 4:eb89733b8642 | 411 | } |
catchvibes95 | 4:eb89733b8642 | 412 | |
catchvibes95 | 4:eb89733b8642 | 413 | void StopHaptic(void const *n) { |
catchvibes95 | 4:eb89733b8642 | 414 | haptic = 0; |
catchvibes95 | 4:eb89733b8642 | 415 | hapticTimer.stop(); |
catchvibes95 | 4:eb89733b8642 | 416 | } |
catchvibes95 | 4:eb89733b8642 | 417 |