debug_wip

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of Biometricos_0 by Tuna Shields

Committer:
Jacacent
Date:
Tue Apr 10 17:35:48 2018 +0000
Revision:
1:49659ca954b3
Parent:
0:fb0d095dc5dd
debug;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jacacent 0:fb0d095dc5dd 1 #include "mbed.h"
Jacacent 0:fb0d095dc5dd 2 #include "biometricos.h"
Jacacent 0:fb0d095dc5dd 3 #include "Hotboards_rtcc.h"
Jacacent 0:fb0d095dc5dd 4 #include "MMA8451Q.h"
Jacacent 0:fb0d095dc5dd 5 #include "MAX30100_PulseOximeter.h"
Jacacent 0:fb0d095dc5dd 6 #include "ble/BLE.h"
Jacacent 0:fb0d095dc5dd 7 #include "ble/services/UARTService.h"
Jacacent 0:fb0d095dc5dd 8
Jacacent 0:fb0d095dc5dd 9 #define NEED_CONSOLE_OUTPUT 1
Jacacent 0:fb0d095dc5dd 10 #define MMA8451_I2C_ADDRESS (0x1d<<1)
Jacacent 0:fb0d095dc5dd 11 #define REPORTING_PERIOD_MS 1000
Jacacent 0:fb0d095dc5dd 12
Jacacent 0:fb0d095dc5dd 13 #if NEED_CONSOLE_OUTPUT
Jacacent 0:fb0d095dc5dd 14
Jacacent 0:fb0d095dc5dd 15 Serial pc(LED_RED,LED_ORANGE);
Jacacent 0:fb0d095dc5dd 16 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Jacacent 0:fb0d095dc5dd 17 #else
Jacacent 0:fb0d095dc5dd 18 #define DEBUG(...)
Jacacent 0:fb0d095dc5dd 19 #endif
Jacacent 0:fb0d095dc5dd 20
Jacacent 0:fb0d095dc5dd 21 /* general ------------------------------------------------------------------ */
Jacacent 0:fb0d095dc5dd 22
Jacacent 0:fb0d095dc5dd 23 I2C i2c(I2C_SDA, I2C_SCL);
Jacacent 0:fb0d095dc5dd 24 Ticker counter_tick;
Jacacent 0:fb0d095dc5dd 25
Jacacent 0:fb0d095dc5dd 26 bool timestmap = 0;
Jacacent 0:fb0d095dc5dd 27 float tick_time;
Jacacent 0:fb0d095dc5dd 28 uint8_t data[13];
Jacacent 0:fb0d095dc5dd 29 uint32_t general_counter;
Jacacent 0:fb0d095dc5dd 30
Jacacent 0:fb0d095dc5dd 31 void increment_counter() {
Jacacent 0:fb0d095dc5dd 32 general_counter++;
Jacacent 0:fb0d095dc5dd 33 }
Jacacent 0:fb0d095dc5dd 34
Jacacent 0:fb0d095dc5dd 35 uint16_t get_cicles(float seconds) {
Jacacent 0:fb0d095dc5dd 36 return seconds * (1/tick_time);
Jacacent 0:fb0d095dc5dd 37 }
Jacacent 0:fb0d095dc5dd 38
Jacacent 0:fb0d095dc5dd 39 void general_init() {
Jacacent 0:fb0d095dc5dd 40 tick_time = 0.01;
Jacacent 0:fb0d095dc5dd 41 i2c.frequency(100000);
Jacacent 0:fb0d095dc5dd 42 counter_tick.attach(&increment_counter, tick_time);
Jacacent 0:fb0d095dc5dd 43 }
Jacacent 0:fb0d095dc5dd 44 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 45
Jacacent 0:fb0d095dc5dd 46 /* led_red ------------------------------------------------------------------ */
Jacacent 0:fb0d095dc5dd 47 /*DigitalOut led_red(LED_RED);
Jacacent 0:fb0d095dc5dd 48 Ticker led_red_ticker;
Jacacent 0:fb0d095dc5dd 49
Jacacent 0:fb0d095dc5dd 50 void periodicCallback(void) {
Jacacent 0:fb0d095dc5dd 51 led_red = !led_red;
Jacacent 0:fb0d095dc5dd 52 }
Jacacent 0:fb0d095dc5dd 53
Jacacent 0:fb0d095dc5dd 54 void led_red_init() {
Jacacent 0:fb0d095dc5dd 55 led_red_ticker.attach(periodicCallback, 3);
Jacacent 0:fb0d095dc5dd 56 }*/
Jacacent 0:fb0d095dc5dd 57 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 58
Jacacent 0:fb0d095dc5dd 59
Jacacent 0:fb0d095dc5dd 60 /* real time clock ---------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 61 Hotboards_rtcc rtcc(i2c);
Jacacent 0:fb0d095dc5dd 62
Jacacent 0:fb0d095dc5dd 63 void rtcc_init(){
Jacacent 0:fb0d095dc5dd 64 if(rtcc.begin()) {
Jacacent 0:fb0d095dc5dd 65 DEBUG("rtcc initialized!\r\n");
Jacacent 0:fb0d095dc5dd 66 timestmap = 1;
Jacacent 0:fb0d095dc5dd 67 } else {
Jacacent 0:fb0d095dc5dd 68 DEBUG("rtcc not initialized!\r\n");
Jacacent 0:fb0d095dc5dd 69 timestmap = 0;
Jacacent 0:fb0d095dc5dd 70 }
Jacacent 0:fb0d095dc5dd 71 }
Jacacent 0:fb0d095dc5dd 72
Jacacent 0:fb0d095dc5dd 73 bool rtcc_now(DateTime &t) {
Jacacent 0:fb0d095dc5dd 74 if(rtcc.getStatus()) {
Jacacent 0:fb0d095dc5dd 75 if(!rtcc.now(t)) {
Jacacent 0:fb0d095dc5dd 76 DEBUG("Error rtcc.now(t)\r\n");
Jacacent 0:fb0d095dc5dd 77 return 0;
Jacacent 0:fb0d095dc5dd 78 }
Jacacent 0:fb0d095dc5dd 79 } else {
Jacacent 0:fb0d095dc5dd 80 DEBUG("Error rtcc.getStatus()\r\n");
Jacacent 0:fb0d095dc5dd 81 return 0;
Jacacent 0:fb0d095dc5dd 82 }
Jacacent 0:fb0d095dc5dd 83
Jacacent 0:fb0d095dc5dd 84 return true;
Jacacent 0:fb0d095dc5dd 85 }
Jacacent 0:fb0d095dc5dd 86
Jacacent 0:fb0d095dc5dd 87 uint8_t rtcc_verify(const uint8_t *d) {
Jacacent 0:fb0d095dc5dd 88 DateTime t;
Jacacent 0:fb0d095dc5dd 89 if(rtcc_now(t)) {
Jacacent 0:fb0d095dc5dd 90 DEBUG("Time- %d:%d:%d Date- %d/ %d/ %d\r\n",t.hour(),t.minute(),t.second(),t.day(),t.month()+1,t.year());
Jacacent 0:fb0d095dc5dd 91 if((d[2] == t.minute()) && (d[3] == t.hour()) && (d[4] == t.day()) && (d[5] == t.month()) && (d[6] == (t.year() - 2000))) {
Jacacent 0:fb0d095dc5dd 92 DEBUG("MCP79410 good\r\n");
Jacacent 0:fb0d095dc5dd 93 return 1;
Jacacent 0:fb0d095dc5dd 94 } else {
Jacacent 0:fb0d095dc5dd 95 rtcc.setVBAT(1);
Jacacent 0:fb0d095dc5dd 96 if(rtcc.adjust(DateTime(d[6] + 2000, d[5], d[4], d[3], d[2], d[1]))) {
Jacacent 0:fb0d095dc5dd 97 DEBUG("MCP79410 adjusted\r\n");
Jacacent 0:fb0d095dc5dd 98 return 2;
Jacacent 0:fb0d095dc5dd 99 } else {
Jacacent 0:fb0d095dc5dd 100 DEBUG("MCP79410 not adjusted\r\n");
Jacacent 0:fb0d095dc5dd 101 return 0;
Jacacent 0:fb0d095dc5dd 102 }
Jacacent 0:fb0d095dc5dd 103 }
Jacacent 0:fb0d095dc5dd 104 } else {
Jacacent 0:fb0d095dc5dd 105 DEBUG("Error rtcc_now in rtcc_verify\r\n");
Jacacent 0:fb0d095dc5dd 106 return 0;
Jacacent 0:fb0d095dc5dd 107 }
Jacacent 0:fb0d095dc5dd 108 }
Jacacent 0:fb0d095dc5dd 109 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 110
Jacacent 0:fb0d095dc5dd 111
Jacacent 0:fb0d095dc5dd 112 /* bluetooth ---------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 113 uint8_t device_name[] = "HNCR-06";
Jacacent 0:fb0d095dc5dd 114 BLEDevice ble;
Jacacent 0:fb0d095dc5dd 115 UARTService *uartServicePtr;
Jacacent 0:fb0d095dc5dd 116
Jacacent 0:fb0d095dc5dd 117 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
Jacacent 0:fb0d095dc5dd 118 DEBUG("ble disconnected!\r\n");
Jacacent 0:fb0d095dc5dd 119 DEBUG("ble restarting the advertising process\r\n");
Jacacent 0:fb0d095dc5dd 120 ble.startAdvertising();
Jacacent 0:fb0d095dc5dd 121 }
Jacacent 0:fb0d095dc5dd 122
Jacacent 0:fb0d095dc5dd 123 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
Jacacent 0:fb0d095dc5dd 124 DEBUG("ble connected!\r\n");
Jacacent 0:fb0d095dc5dd 125 }
Jacacent 0:fb0d095dc5dd 126
Jacacent 0:fb0d095dc5dd 127 void onDataWritten(const GattWriteCallbackParams *params) {
Jacacent 0:fb0d095dc5dd 128 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
Jacacent 0:fb0d095dc5dd 129 DEBUG("received %u bytes\r\n", params->len);
Jacacent 0:fb0d095dc5dd 130 uint8_t data_send[2];
Jacacent 0:fb0d095dc5dd 131 uint8_t bytes_send;
Jacacent 0:fb0d095dc5dd 132
Jacacent 0:fb0d095dc5dd 133 switch (params->data[0]) {
Jacacent 0:fb0d095dc5dd 134 case 0x00:
Jacacent 0:fb0d095dc5dd 135 data_send[0] = 0x00;
Jacacent 0:fb0d095dc5dd 136 data_send[1] = rtcc_verify(params->data);
Jacacent 0:fb0d095dc5dd 137 bytes_send = 2;
Jacacent 0:fb0d095dc5dd 138 break;
Jacacent 0:fb0d095dc5dd 139 }
Jacacent 0:fb0d095dc5dd 140 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), data_send, bytes_send);
Jacacent 0:fb0d095dc5dd 141 }
Jacacent 0:fb0d095dc5dd 142 }
Jacacent 0:fb0d095dc5dd 143
Jacacent 0:fb0d095dc5dd 144 void ble_init() {
Jacacent 0:fb0d095dc5dd 145 DEBUG("ble initialized!\r\n");
Jacacent 0:fb0d095dc5dd 146 ble.init();
Jacacent 0:fb0d095dc5dd 147 ble.onConnection(connectionCallback);
Jacacent 0:fb0d095dc5dd 148 ble.onDisconnection(disconnectionCallback);
Jacacent 0:fb0d095dc5dd 149 ble.onDataWritten(onDataWritten);
Jacacent 0:fb0d095dc5dd 150
Jacacent 0:fb0d095dc5dd 151 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Jacacent 0:fb0d095dc5dd 152 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Jacacent 0:fb0d095dc5dd 153 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Jacacent 0:fb0d095dc5dd 154 (const uint8_t *)device_name, sizeof(device_name) - 1);
Jacacent 0:fb0d095dc5dd 155 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Jacacent 0:fb0d095dc5dd 156 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Jacacent 0:fb0d095dc5dd 157
Jacacent 0:fb0d095dc5dd 158 ble.setAdvertisingInterval(1000);
Jacacent 0:fb0d095dc5dd 159 ble.startAdvertising();
Jacacent 0:fb0d095dc5dd 160 }
Jacacent 0:fb0d095dc5dd 161
Jacacent 0:fb0d095dc5dd 162 void send_ble(uint8_t *data, uint8_t size) {
Jacacent 0:fb0d095dc5dd 163 if (uartServicePtr != NULL && ble.getGapState().connected) {
Jacacent 0:fb0d095dc5dd 164 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), data, size);
Jacacent 0:fb0d095dc5dd 165 } else {
Jacacent 0:fb0d095dc5dd 166 // DEBUG("Error send_ble\r\n");
Jacacent 0:fb0d095dc5dd 167 }
Jacacent 0:fb0d095dc5dd 168 }
Jacacent 0:fb0d095dc5dd 169
Jacacent 0:fb0d095dc5dd 170 void send_behavior(uint8_t i) {
Jacacent 0:fb0d095dc5dd 171 uint8_t bytes_send;
Jacacent 0:fb0d095dc5dd 172 if(!timestmap) {
Jacacent 0:fb0d095dc5dd 173 data[1] = 0x00;
Jacacent 0:fb0d095dc5dd 174 bytes_send = i;
Jacacent 0:fb0d095dc5dd 175 } else {
Jacacent 0:fb0d095dc5dd 176 DateTime t;
Jacacent 0:fb0d095dc5dd 177 if(rtcc_now(t)) {
Jacacent 0:fb0d095dc5dd 178 data[1] = 0x01;
Jacacent 0:fb0d095dc5dd 179 data[i] = t.second();
Jacacent 0:fb0d095dc5dd 180 data[i+1] = t.minute();
Jacacent 0:fb0d095dc5dd 181 data[i+2] = t.hour();
Jacacent 0:fb0d095dc5dd 182 data[i+3] = t.day();
Jacacent 0:fb0d095dc5dd 183 data[i+4] = t.month();
Jacacent 0:fb0d095dc5dd 184 data[i+5] = (t.year() - 2000);
Jacacent 0:fb0d095dc5dd 185 bytes_send = i+6;
Jacacent 0:fb0d095dc5dd 186 } else {
Jacacent 0:fb0d095dc5dd 187 DEBUG("Error rtcc_now in lm35_sens\r\n");
Jacacent 0:fb0d095dc5dd 188 bytes_send = 0;
Jacacent 0:fb0d095dc5dd 189 }
Jacacent 0:fb0d095dc5dd 190 }
Jacacent 0:fb0d095dc5dd 191 if(bytes_send)
Jacacent 0:fb0d095dc5dd 192 send_ble(data, bytes_send);
Jacacent 0:fb0d095dc5dd 193 }
Jacacent 0:fb0d095dc5dd 194 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 195
Jacacent 0:fb0d095dc5dd 196
Jacacent 0:fb0d095dc5dd 197 /* button ------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 198 DigitalIn button(USER_BUTTON, PullUp);
Jacacent 0:fb0d095dc5dd 199 bool button_state, button_last_state;
Jacacent 0:fb0d095dc5dd 200 uint16_t button_debounce_delay;
Jacacent 0:fb0d095dc5dd 201 uint32_t button_last_debounce_time;
Jacacent 0:fb0d095dc5dd 202
Jacacent 0:fb0d095dc5dd 203 void button_init() {
Jacacent 0:fb0d095dc5dd 204 button_debounce_delay = get_cicles(0.5);
Jacacent 0:fb0d095dc5dd 205 button_last_state = 0;
Jacacent 0:fb0d095dc5dd 206 }
Jacacent 0:fb0d095dc5dd 207
Jacacent 0:fb0d095dc5dd 208 void button_sens() {
Jacacent 0:fb0d095dc5dd 209 int reading = button;
Jacacent 0:fb0d095dc5dd 210
Jacacent 0:fb0d095dc5dd 211 if(reading != button_last_state) {
Jacacent 0:fb0d095dc5dd 212 button_last_debounce_time = general_counter;
Jacacent 0:fb0d095dc5dd 213 }
Jacacent 0:fb0d095dc5dd 214
Jacacent 0:fb0d095dc5dd 215 if(general_counter >= (button_last_debounce_time + button_debounce_delay)) {
Jacacent 0:fb0d095dc5dd 216 if(reading != button_state) {
Jacacent 0:fb0d095dc5dd 217 button_state = reading;
Jacacent 0:fb0d095dc5dd 218
Jacacent 0:fb0d095dc5dd 219 if(button_state) {
Jacacent 0:fb0d095dc5dd 220 DEBUG("button_state == 1\r\n");
Jacacent 0:fb0d095dc5dd 221 } else {
Jacacent 0:fb0d095dc5dd 222 DEBUG("button_state == 0\r\n");
Jacacent 0:fb0d095dc5dd 223 }
Jacacent 0:fb0d095dc5dd 224
Jacacent 0:fb0d095dc5dd 225 data[0] = 0x02;
Jacacent 0:fb0d095dc5dd 226 data[2] = button_state;
Jacacent 0:fb0d095dc5dd 227
Jacacent 0:fb0d095dc5dd 228 send_behavior(3);
Jacacent 0:fb0d095dc5dd 229 }
Jacacent 0:fb0d095dc5dd 230 }
Jacacent 0:fb0d095dc5dd 231
Jacacent 0:fb0d095dc5dd 232 button_last_state = reading;
Jacacent 0:fb0d095dc5dd 233 }
Jacacent 0:fb0d095dc5dd 234 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 235
Jacacent 0:fb0d095dc5dd 236
Jacacent 0:fb0d095dc5dd 237 /* lm35 temperature sensor -------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 238 AnalogIn lm35(TEMP);
Jacacent 0:fb0d095dc5dd 239
Jacacent 0:fb0d095dc5dd 240 bool lm35_state;
Jacacent 0:fb0d095dc5dd 241 float lm35_temperature_celsius, factor_temperature;
Jacacent 0:fb0d095dc5dd 242 uint8_t lm35_num_samples;
Jacacent 0:fb0d095dc5dd 243 uint16_t lm35_polling_time_main, lm35_min, lm35_max;
Jacacent 0:fb0d095dc5dd 244 uint16_t lm35_samples_counter, lm35_polling_time_samples, lm35_samples[5];
Jacacent 0:fb0d095dc5dd 245 uint32_t lm35_last_counter_value, lm35_last_samples_counter_value;
Jacacent 0:fb0d095dc5dd 246
Jacacent 0:fb0d095dc5dd 247 uint16_t lm35_get_adc_value(float degrees_celsius) {
Jacacent 0:fb0d095dc5dd 248 return degrees_celsius / factor_temperature;
Jacacent 0:fb0d095dc5dd 249 }
Jacacent 0:fb0d095dc5dd 250
Jacacent 0:fb0d095dc5dd 251 void lm35_init() {
Jacacent 0:fb0d095dc5dd 252 lm35_last_counter_value = 0;
Jacacent 0:fb0d095dc5dd 253 lm35_samples_counter = 0;
Jacacent 0:fb0d095dc5dd 254 lm35_last_samples_counter_value = 0;
Jacacent 0:fb0d095dc5dd 255 lm35_num_samples = 5;
Jacacent 0:fb0d095dc5dd 256 lm35_polling_time_samples = get_cicles(0.5);
Jacacent 0:fb0d095dc5dd 257 lm35_polling_time_main = get_cicles(3);
Jacacent 0:fb0d095dc5dd 258 factor_temperature = 0.3200391; // ((3.274 * 100) / 1023)
Jacacent 0:fb0d095dc5dd 259 lm35_min = lm35_get_adc_value(20);//62.4923
Jacacent 0:fb0d095dc5dd 260 lm35_max = lm35_get_adc_value(50);//156.2309
Jacacent 0:fb0d095dc5dd 261
Jacacent 0:fb0d095dc5dd 262 uint16_t val = lm35.read_u16();
Jacacent 0:fb0d095dc5dd 263
Jacacent 0:fb0d095dc5dd 264 if(val >= lm35_min && val <= lm35_max) {
Jacacent 0:fb0d095dc5dd 265 DEBUG("lm35 initialized!\r\n");
Jacacent 0:fb0d095dc5dd 266 lm35_state = 1;
Jacacent 0:fb0d095dc5dd 267 } else {
Jacacent 0:fb0d095dc5dd 268 DEBUG("lm35 not initialized!\r\n");
Jacacent 0:fb0d095dc5dd 269 lm35_state = 0;
Jacacent 0:fb0d095dc5dd 270 }
Jacacent 0:fb0d095dc5dd 271 }
Jacacent 0:fb0d095dc5dd 272
Jacacent 0:fb0d095dc5dd 273 void lm35_sens() {
Jacacent 0:fb0d095dc5dd 274 if(lm35_state)
Jacacent 0:fb0d095dc5dd 275 if(general_counter >= (lm35_last_counter_value + lm35_polling_time_main))
Jacacent 0:fb0d095dc5dd 276 if(general_counter >= (lm35_last_samples_counter_value + lm35_polling_time_samples)) {
Jacacent 0:fb0d095dc5dd 277 uint16_t val = lm35.read_u16();
Jacacent 0:fb0d095dc5dd 278
Jacacent 0:fb0d095dc5dd 279 if(val >= lm35_min && val <= lm35_max) {
Jacacent 0:fb0d095dc5dd 280 uint16_t temperature_sample = val;
Jacacent 0:fb0d095dc5dd 281 data[0] = 0x01;
Jacacent 0:fb0d095dc5dd 282 data[2] = temperature_sample & 0xff;
Jacacent 0:fb0d095dc5dd 283 data[3] = temperature_sample >> 8;
Jacacent 0:fb0d095dc5dd 284
Jacacent 0:fb0d095dc5dd 285 send_behavior(4);
Jacacent 0:fb0d095dc5dd 286 lm35_last_counter_value = general_counter;
Jacacent 0:fb0d095dc5dd 287 }
Jacacent 0:fb0d095dc5dd 288
Jacacent 0:fb0d095dc5dd 289
Jacacent 0:fb0d095dc5dd 290
Jacacent 0:fb0d095dc5dd 291 lm35_last_samples_counter_value = general_counter;
Jacacent 0:fb0d095dc5dd 292 }
Jacacent 0:fb0d095dc5dd 293 }
Jacacent 0:fb0d095dc5dd 294 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 295
Jacacent 0:fb0d095dc5dd 296
Jacacent 0:fb0d095dc5dd 297 /* termistor breathing sensor ---------------------------------------------- */
Jacacent 0:fb0d095dc5dd 298 AnalogIn breath(BREATHING);
Jacacent 0:fb0d095dc5dd 299
Jacacent 0:fb0d095dc5dd 300 bool init_exhalation, breathing_status;
Jacacent 0:fb0d095dc5dd 301 uint8_t val, min_val_allowed, max_val_allowed;
Jacacent 0:fb0d095dc5dd 302 uint8_t base, base_umbral, umbral, dif, max_val_readed;
Jacacent 0:fb0d095dc5dd 303 uint16_t breathing_pollingTime;
Jacacent 0:fb0d095dc5dd 304 uint32_t breathing_last_counter_value;
Jacacent 0:fb0d095dc5dd 305
Jacacent 0:fb0d095dc5dd 306 uint16_t breath_get_temp() {
Jacacent 0:fb0d095dc5dd 307 return breath.read_u16() - 768;
Jacacent 0:fb0d095dc5dd 308 }
Jacacent 0:fb0d095dc5dd 309
Jacacent 0:fb0d095dc5dd 310 void breathing_init() {
Jacacent 0:fb0d095dc5dd 311 init_exhalation = 0;
Jacacent 0:fb0d095dc5dd 312 max_val_readed = 0;
Jacacent 0:fb0d095dc5dd 313 breathing_status = 0;
Jacacent 0:fb0d095dc5dd 314 breathing_last_counter_value = 0;
Jacacent 0:fb0d095dc5dd 315 min_val_allowed = 80;
Jacacent 0:fb0d095dc5dd 316 max_val_allowed = 150;
Jacacent 0:fb0d095dc5dd 317 base_umbral = 4;
Jacacent 0:fb0d095dc5dd 318 dif = 2;
Jacacent 0:fb0d095dc5dd 319 breathing_pollingTime = get_cicles(5);
Jacacent 0:fb0d095dc5dd 320 base = breath_get_temp();
Jacacent 0:fb0d095dc5dd 321
Jacacent 0:fb0d095dc5dd 322 if(base > min_val_allowed && base < max_val_allowed) {
Jacacent 0:fb0d095dc5dd 323 DEBUG("breathing initialized!\r\n");
Jacacent 0:fb0d095dc5dd 324 umbral = base - base_umbral;
Jacacent 0:fb0d095dc5dd 325 breathing_status = 1;
Jacacent 0:fb0d095dc5dd 326 } else {
Jacacent 0:fb0d095dc5dd 327 DEBUG("breathing not initialized!\r\n");
Jacacent 0:fb0d095dc5dd 328 breathing_status = 0;
Jacacent 0:fb0d095dc5dd 329 }
Jacacent 0:fb0d095dc5dd 330 }
Jacacent 0:fb0d095dc5dd 331
Jacacent 0:fb0d095dc5dd 332 void breath_sens() {
Jacacent 0:fb0d095dc5dd 333 if(breathing_status) {
Jacacent 0:fb0d095dc5dd 334 val = breath_get_temp();
Jacacent 0:fb0d095dc5dd 335 //DEBUG("%d\r\n", val);
Jacacent 0:fb0d095dc5dd 336
Jacacent 0:fb0d095dc5dd 337 if(val > min_val_allowed && val < max_val_allowed) {
Jacacent 0:fb0d095dc5dd 338
Jacacent 0:fb0d095dc5dd 339 if(val > max_val_readed)
Jacacent 0:fb0d095dc5dd 340 max_val_readed = val;
Jacacent 0:fb0d095dc5dd 341
Jacacent 0:fb0d095dc5dd 342 if((general_counter >= (breathing_last_counter_value + breathing_pollingTime))) {
Jacacent 0:fb0d095dc5dd 343 base = max_val_readed;
Jacacent 0:fb0d095dc5dd 344 umbral = base - base_umbral;
Jacacent 0:fb0d095dc5dd 345 max_val_readed = 0;
Jacacent 0:fb0d095dc5dd 346 //DEBUG("Updated %d %d %d\r\n",val, base ,umbral);
Jacacent 0:fb0d095dc5dd 347 breathing_last_counter_value = general_counter;
Jacacent 0:fb0d095dc5dd 348 }
Jacacent 0:fb0d095dc5dd 349
Jacacent 0:fb0d095dc5dd 350 if(val < umbral && !init_exhalation) {
Jacacent 0:fb0d095dc5dd 351 init_exhalation = true;
Jacacent 0:fb0d095dc5dd 352 //DEBUG("init_exhalation\r\n");
Jacacent 0:fb0d095dc5dd 353 }
Jacacent 0:fb0d095dc5dd 354
Jacacent 0:fb0d095dc5dd 355 //DEBUG("%d %d\r\n",val, umbral);
Jacacent 0:fb0d095dc5dd 356 if(val > (umbral + dif) && init_exhalation) {
Jacacent 0:fb0d095dc5dd 357 DEBUG("Inhalation\r\n");
Jacacent 0:fb0d095dc5dd 358
Jacacent 0:fb0d095dc5dd 359 data[0] = 0x05;
Jacacent 0:fb0d095dc5dd 360 data[2] = 0x01;
Jacacent 0:fb0d095dc5dd 361
Jacacent 0:fb0d095dc5dd 362 send_behavior(3);
Jacacent 0:fb0d095dc5dd 363
Jacacent 0:fb0d095dc5dd 364 init_exhalation = false;
Jacacent 0:fb0d095dc5dd 365 }
Jacacent 0:fb0d095dc5dd 366 }
Jacacent 0:fb0d095dc5dd 367 }
Jacacent 0:fb0d095dc5dd 368 }
Jacacent 0:fb0d095dc5dd 369 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 370
Jacacent 0:fb0d095dc5dd 371
Jacacent 0:fb0d095dc5dd 372 /* max30100 herat rate and oximetry sensor ---------------------------------- */
Jacacent 0:fb0d095dc5dd 373 MAX30100 max30100(i2c);
Jacacent 0:fb0d095dc5dd 374 PulseOximeter pox(max30100);
Jacacent 0:fb0d095dc5dd 375
Jacacent 0:fb0d095dc5dd 376 bool max30100_status;
Jacacent 0:fb0d095dc5dd 377 float max_heart_rate, min_heart_rate;
Jacacent 0:fb0d095dc5dd 378 uint8_t min_sp02, max_sp02;
Jacacent 0:fb0d095dc5dd 379 uint8_t samples_max30100, heart_rate_min_dif, sp02_min_dif, counter_max30100;
Jacacent 0:fb0d095dc5dd 380
Jacacent 0:fb0d095dc5dd 381 float samples_heart_rate[5];
Jacacent 0:fb0d095dc5dd 382 uint8_t samples_sp02[5];
Jacacent 0:fb0d095dc5dd 383
Jacacent 0:fb0d095dc5dd 384 Timer t;
Jacacent 0:fb0d095dc5dd 385
Jacacent 0:fb0d095dc5dd 386 void pox_init() {
Jacacent 0:fb0d095dc5dd 387 max30100_status = 0;
Jacacent 0:fb0d095dc5dd 388 samples_max30100 = 5;
Jacacent 0:fb0d095dc5dd 389 heart_rate_min_dif = 6;
Jacacent 0:fb0d095dc5dd 390 sp02_min_dif = 2;
Jacacent 0:fb0d095dc5dd 391 counter_max30100 = 0;
Jacacent 0:fb0d095dc5dd 392
Jacacent 0:fb0d095dc5dd 393 if(pox.begin()) {
Jacacent 0:fb0d095dc5dd 394 DEBUG("max30100 initialized!\r\n");
Jacacent 0:fb0d095dc5dd 395 max30100_status = 1;
Jacacent 0:fb0d095dc5dd 396 t.start();
Jacacent 0:fb0d095dc5dd 397 } else {
Jacacent 0:fb0d095dc5dd 398 DEBUG("max30100 not initialized!\r\n");
Jacacent 0:fb0d095dc5dd 399 max30100_status = 0;
Jacacent 0:fb0d095dc5dd 400 }
Jacacent 0:fb0d095dc5dd 401 }
Jacacent 0:fb0d095dc5dd 402
Jacacent 0:fb0d095dc5dd 403 void pox_sens() {
Jacacent 0:fb0d095dc5dd 404 if(max30100_status) {
Jacacent 0:fb0d095dc5dd 405 // Make sure to call update as fast as possible
Jacacent 0:fb0d095dc5dd 406 if(!pox.update()) {
Jacacent 0:fb0d095dc5dd 407 DEBUG("Error: if(!pox.update())\r\n");
Jacacent 0:fb0d095dc5dd 408 pox_init();
Jacacent 0:fb0d095dc5dd 409 }
Jacacent 0:fb0d095dc5dd 410
Jacacent 0:fb0d095dc5dd 411 // Asynchronously dump heart rate and oxidation levels to the serial
Jacacent 0:fb0d095dc5dd 412 // For both, a value of 0 means "invalid"
Jacacent 0:fb0d095dc5dd 413 if (t.read_ms() > REPORTING_PERIOD_MS) {
Jacacent 0:fb0d095dc5dd 414 float new_heart_rate = pox.getHeartRate();
Jacacent 0:fb0d095dc5dd 415 uint8_t new_sp02 = pox.getSpO2();
Jacacent 0:fb0d095dc5dd 416
Jacacent 0:fb0d095dc5dd 417 if(new_heart_rate != 0 && new_sp02 != 0) {
Jacacent 0:fb0d095dc5dd 418 DEBUG("Heart rate: %f bmp\r\n", new_heart_rate);
Jacacent 0:fb0d095dc5dd 419 DEBUG("SpO2: %d%\r\n\n", new_sp02);
Jacacent 0:fb0d095dc5dd 420
Jacacent 0:fb0d095dc5dd 421
Jacacent 0:fb0d095dc5dd 422
Jacacent 0:fb0d095dc5dd 423 uint32_t int_heart_rate;
Jacacent 0:fb0d095dc5dd 424 memcpy(&int_heart_rate,&new_heart_rate,4);
Jacacent 0:fb0d095dc5dd 425
Jacacent 0:fb0d095dc5dd 426 data[0] = 0x03;
Jacacent 0:fb0d095dc5dd 427 data[2] = new_sp02;
Jacacent 0:fb0d095dc5dd 428 data[3] = (int_heart_rate & 0xFF);
Jacacent 0:fb0d095dc5dd 429 data[4] = ((int_heart_rate >> 8) & 0xFF);
Jacacent 0:fb0d095dc5dd 430 data[5] = ((int_heart_rate >> 16) & 0xFF);
Jacacent 0:fb0d095dc5dd 431 data[6] = ((int_heart_rate >> 24) & 0xFF);
Jacacent 0:fb0d095dc5dd 432
Jacacent 0:fb0d095dc5dd 433 send_behavior(7);
Jacacent 0:fb0d095dc5dd 434 //DEBUG("No finger\r\n");
Jacacent 0:fb0d095dc5dd 435 }
Jacacent 0:fb0d095dc5dd 436
Jacacent 0:fb0d095dc5dd 437 t.reset();
Jacacent 0:fb0d095dc5dd 438 }
Jacacent 0:fb0d095dc5dd 439 }
Jacacent 0:fb0d095dc5dd 440 }
Jacacent 0:fb0d095dc5dd 441 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 442
Jacacent 0:fb0d095dc5dd 443
Jacacent 0:fb0d095dc5dd 444 /* mma8451q accelerometer --------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 445 MMA8451Q mma(i2c, MMA8451_I2C_ADDRESS);
Jacacent 0:fb0d095dc5dd 446
Jacacent 0:fb0d095dc5dd 447 bool mma_status, mma_new_value = false;
Jacacent 0:fb0d095dc5dd 448 float mma_y;
Jacacent 0:fb0d095dc5dd 449 uint8_t sample_counter = 0;
Jacacent 0:fb0d095dc5dd 450 uint8_t num_samples = 3;
Jacacent 0:fb0d095dc5dd 451 uint8_t mma_samples[3];
Jacacent 0:fb0d095dc5dd 452
Jacacent 0:fb0d095dc5dd 453 void mma_init() {
Jacacent 0:fb0d095dc5dd 454 if(mma.begin()) {
Jacacent 0:fb0d095dc5dd 455 mma_status = 1;
Jacacent 0:fb0d095dc5dd 456 DEBUG("MMA8451 initialized\r\n");
Jacacent 0:fb0d095dc5dd 457 } else {
Jacacent 0:fb0d095dc5dd 458 mma_status = 0;
Jacacent 0:fb0d095dc5dd 459 DEBUG("MMA8451 not initialized\r\n");
Jacacent 0:fb0d095dc5dd 460 }
Jacacent 0:fb0d095dc5dd 461 }
Jacacent 0:fb0d095dc5dd 462
Jacacent 0:fb0d095dc5dd 463 bool mma_get_acc_y(float &y) {
Jacacent 0:fb0d095dc5dd 464 if(mma.getAccY(y))
Jacacent 0:fb0d095dc5dd 465 y = abs(y);
Jacacent 0:fb0d095dc5dd 466 else
Jacacent 0:fb0d095dc5dd 467 return 0;
Jacacent 0:fb0d095dc5dd 468
Jacacent 0:fb0d095dc5dd 469 return 1;
Jacacent 0:fb0d095dc5dd 470 }
Jacacent 0:fb0d095dc5dd 471
Jacacent 0:fb0d095dc5dd 472 void mma_sens() {
Jacacent 0:fb0d095dc5dd 473 if(mma_status)
Jacacent 0:fb0d095dc5dd 474 if(mma.getStatus()) {
Jacacent 0:fb0d095dc5dd 475 float y;
Jacacent 0:fb0d095dc5dd 476 if(mma_get_acc_y(y)) {
Jacacent 0:fb0d095dc5dd 477 if(y > 1.2) {
Jacacent 0:fb0d095dc5dd 478 mma_new_value = true;
Jacacent 0:fb0d095dc5dd 479 if(mma_y < y)
Jacacent 0:fb0d095dc5dd 480 mma_y = y;
Jacacent 0:fb0d095dc5dd 481
Jacacent 0:fb0d095dc5dd 482 } else if(mma_new_value) {
Jacacent 0:fb0d095dc5dd 483 mma_samples[sample_counter++] = mma_y;
Jacacent 0:fb0d095dc5dd 484 if(sample_counter == num_samples) {
Jacacent 0:fb0d095dc5dd 485 for (uint8_t i = 0; i < num_samples; i++) {
Jacacent 0:fb0d095dc5dd 486 if(mma_y < mma_samples[i])
Jacacent 0:fb0d095dc5dd 487 mma_y = mma_samples[i];
Jacacent 0:fb0d095dc5dd 488 }
Jacacent 0:fb0d095dc5dd 489
Jacacent 0:fb0d095dc5dd 490 uint8_t val = (mma_y - 1) * 255;
Jacacent 0:fb0d095dc5dd 491
Jacacent 0:fb0d095dc5dd 492 DEBUG("%d\r\n", val);
Jacacent 0:fb0d095dc5dd 493 sample_counter = 0;
Jacacent 0:fb0d095dc5dd 494
Jacacent 0:fb0d095dc5dd 495 data[0] = 0x04;
Jacacent 0:fb0d095dc5dd 496 data[2] = val;
Jacacent 0:fb0d095dc5dd 497
Jacacent 0:fb0d095dc5dd 498 send_behavior(3);
Jacacent 0:fb0d095dc5dd 499 }
Jacacent 0:fb0d095dc5dd 500
Jacacent 0:fb0d095dc5dd 501 mma_new_value = false;
Jacacent 0:fb0d095dc5dd 502 mma_y = 0;
Jacacent 0:fb0d095dc5dd 503 }
Jacacent 0:fb0d095dc5dd 504 } else{
Jacacent 0:fb0d095dc5dd 505 DEBUG("MMA8451 Error getAccAxis()\r\n");
Jacacent 0:fb0d095dc5dd 506 mma.setStatus(false);
Jacacent 0:fb0d095dc5dd 507 }
Jacacent 0:fb0d095dc5dd 508 } else
Jacacent 0:fb0d095dc5dd 509 mma_init();
Jacacent 0:fb0d095dc5dd 510 }
Jacacent 0:fb0d095dc5dd 511 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 512
Jacacent 0:fb0d095dc5dd 513
Jacacent 0:fb0d095dc5dd 514 /* ecg ---------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 515 AnalogIn ecg(ECG_OUT);
Jacacent 0:fb0d095dc5dd 516 DigitalIn ecg_lo_p(ECG_LO_PLUS);
Jacacent 0:fb0d095dc5dd 517 DigitalIn ecg_lo_m(ECG_LO_MIN);
Jacacent 0:fb0d095dc5dd 518
Jacacent 0:fb0d095dc5dd 519 bool ecg_status;
Jacacent 0:fb0d095dc5dd 520 uint8_t ecg_samples[20], ecg_samples_counter;
Jacacent 0:fb0d095dc5dd 521 uint16_t ecg_polling_time_main;
Jacacent 0:fb0d095dc5dd 522 uint32_t ecg_last_counter_value;
Jacacent 0:fb0d095dc5dd 523
Jacacent 0:fb0d095dc5dd 524 void ecg_init() {
Jacacent 0:fb0d095dc5dd 525 ecg_samples_counter = 1;
Jacacent 0:fb0d095dc5dd 526 ecg_last_counter_value = 0;
Jacacent 0:fb0d095dc5dd 527 ecg_polling_time_main = get_cicles(0.02);
Jacacent 0:fb0d095dc5dd 528 ecg_samples[0] = 0x06;
Jacacent 0:fb0d095dc5dd 529
Jacacent 0:fb0d095dc5dd 530 if(!ecg_lo_p && !ecg_lo_m){
Jacacent 0:fb0d095dc5dd 531 ecg_status = 1;
Jacacent 0:fb0d095dc5dd 532 DEBUG("ecg initialized\r\n");
Jacacent 0:fb0d095dc5dd 533 } else {
Jacacent 0:fb0d095dc5dd 534 ecg_status = 0;
Jacacent 0:fb0d095dc5dd 535 DEBUG("ecg not initialized\r\n");
Jacacent 0:fb0d095dc5dd 536 }
Jacacent 0:fb0d095dc5dd 537 }
Jacacent 0:fb0d095dc5dd 538
Jacacent 0:fb0d095dc5dd 539 uint8_t get_ecg_value() {
Jacacent 0:fb0d095dc5dd 540 uint16_t val = ecg.read_u16();
Jacacent 0:fb0d095dc5dd 541 if(val > 232 && val < 743) {
Jacacent 0:fb0d095dc5dd 542 return (val - 232)/2;
Jacacent 0:fb0d095dc5dd 543 } else {
Jacacent 0:fb0d095dc5dd 544 return 0;
Jacacent 0:fb0d095dc5dd 545 }
Jacacent 0:fb0d095dc5dd 546 }
Jacacent 0:fb0d095dc5dd 547
Jacacent 0:fb0d095dc5dd 548 void ecg_sens() {
Jacacent 0:fb0d095dc5dd 549 if(ecg_status)
Jacacent 0:fb0d095dc5dd 550 if(general_counter >= (ecg_last_counter_value + ecg_polling_time_main)) {
Jacacent 0:fb0d095dc5dd 551 if(!ecg_lo_p && !ecg_lo_m){
Jacacent 0:fb0d095dc5dd 552 uint8_t val = get_ecg_value();
Jacacent 0:fb0d095dc5dd 553 ecg_samples[ecg_samples_counter++] = val;
Jacacent 0:fb0d095dc5dd 554
Jacacent 0:fb0d095dc5dd 555 if(ecg_samples_counter == 20) {
Jacacent 0:fb0d095dc5dd 556 send_ble(ecg_samples, 20);
Jacacent 0:fb0d095dc5dd 557 ecg_samples_counter = 1;
Jacacent 0:fb0d095dc5dd 558 }
Jacacent 0:fb0d095dc5dd 559
Jacacent 0:fb0d095dc5dd 560 ecg_last_counter_value = general_counter;
Jacacent 0:fb0d095dc5dd 561 }
Jacacent 0:fb0d095dc5dd 562 }
Jacacent 0:fb0d095dc5dd 563 }
Jacacent 0:fb0d095dc5dd 564 /* -------------------------------------------------------------------------- */
Jacacent 0:fb0d095dc5dd 565
Jacacent 0:fb0d095dc5dd 566 int main(void)
Jacacent 0:fb0d095dc5dd 567 {
Jacacent 0:fb0d095dc5dd 568 pc.printf("\r\nMensaje de prueba DEBUG");
Jacacent 0:fb0d095dc5dd 569 DEBUG("\r\nInitialising program\r\n");
Jacacent 0:fb0d095dc5dd 570
Jacacent 0:fb0d095dc5dd 571 ble_init();
Jacacent 0:fb0d095dc5dd 572 UARTService uartService(ble);
Jacacent 0:fb0d095dc5dd 573 uartServicePtr = &uartService;
Jacacent 0:fb0d095dc5dd 574
Jacacent 0:fb0d095dc5dd 575 // rtcc_init();
Jacacent 0:fb0d095dc5dd 576 general_init();
Jacacent 0:fb0d095dc5dd 577
Jacacent 0:fb0d095dc5dd 578 //led_red_init();
Jacacent 0:fb0d095dc5dd 579 button_init();
Jacacent 0:fb0d095dc5dd 580 lm35_init();
Jacacent 0:fb0d095dc5dd 581 mma_init();
Jacacent 0:fb0d095dc5dd 582 breathing_init();
Jacacent 0:fb0d095dc5dd 583 pox_init();
Jacacent 0:fb0d095dc5dd 584 ecg_init();
Jacacent 0:fb0d095dc5dd 585
Jacacent 0:fb0d095dc5dd 586 while (true) {
Jacacent 0:fb0d095dc5dd 587
Jacacent 0:fb0d095dc5dd 588 button_sens();
Jacacent 0:fb0d095dc5dd 589 lm35_sens();
Jacacent 0:fb0d095dc5dd 590 mma_sens();
Jacacent 0:fb0d095dc5dd 591 breath_sens();
Jacacent 0:fb0d095dc5dd 592 pox_sens();
Jacacent 0:fb0d095dc5dd 593 ecg_sens();
Jacacent 0:fb0d095dc5dd 594
Jacacent 0:fb0d095dc5dd 595 ble.waitForEvent();
Jacacent 0:fb0d095dc5dd 596 }
Jacacent 0:fb0d095dc5dd 597 }