int main(void) { bool init_status; int retValue; int i = defaultPerso.fall_detection_window; // initialize LEDs nrf_gpio_cfg_output(LED_RESETTING); nrf_gpio_cfg_output(LED_NO_FALL); nrf_gpio_cfg_output(LED_FALL_DETECTED); nrf_gpio_cfg_output(LED_OTHER); welcomeLEDs(); // Initialize timers_init(); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); sec_params_init(); twi_master_init(); #ifdef TEST_ACTIVITY_LOG testActivityLog(); #endif initActivityLog(); initSnapshotBuffer(); initCoefficients(); // initialize values retValue = 0; init_status = mpu6050_init(MPU6050_DEVICE_ADDR); if ( false == init_status ) { init_status = mpu6050_init(MPU6050_DEVICE_ADDR+1); if ( false == init_status ) { retValue = -1; errorLEDs(); sd_nvic_SystemReset(); } } if ( 0 == retValue ) { init_status = false; i = 0; while ( false == init_status ) { if (i == 6) { errorLEDs(); sd_nvic_SystemReset(); } init_status = accel_setup(); i++; } } if (0 == retValue) { char outbuf[20]; // Start execution - write a log entry with zero steps to indicate startup // writeLogEntry(999); advertising_start(); setup_wdt(); timers_start(); // start sampliing while(1) { if (ReadSnapshots == 1) { FullSnapshotEntry* entry = (FullSnapshotEntry*)(SNAPSHOT_DATA_ADDRESS_START) ; SnapshotHeader *h; Sensor_Reading *r; sprintf (outbuf, "ID: %s ", getPerso()->uname); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); for(int i=0;i < getActivityLogSize(); i++) { h = &entry->hdr; sprintf (outbuf, ": SNAPSHOT # %d \n ",(i)); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 16); sprintf (outbuf, "Version: 2 \n");// ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 12); sprintf (outbuf, "t:%d n:%d last:%d",h->data.time, h->data.num_of_data_points, h->data.latest_data_point_index); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); // ptr++; r = entry->r; for (int j = 0; j < NUMBER_OF_ENTRIES; j++) { #define FLASH_READ_DELAY 40 nrf_gpio_pin_set(LED_RESETTING); sprintf (outbuf, "i: %d T: %04x ",j,r[j].val.temp); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); sprintf (outbuf, "A: x%04xy%04xz%04x",r[j].val.x_ac,r[j].val.y_ac,r[j].val.z_ac); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); sprintf (outbuf, "G: x%04xy%04xz%04x",r[j].val.x_gy,r[j].val.y_gy,r[j].val.z_gy); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); nrf_gpio_pin_clear(LED_RESETTING); nrf_delay_ms(FLASH_READ_DELAY); //ptr += sizeof(Sensor_Reading); } entry++; } ReadSnapshots = 0; } if (ReadSnapshotsPartial == 1) { PartialSnapTotal = ((PartialSnap1-('0'))*10)+(PartialSnap2-('0')); FullSnapshotEntry* entry = (FullSnapshotEntry*)(SNAPSHOT_DATA_ADDRESS_START+(((PartialSnapTotal*sizeof(FullSnapshotEntry)*10))/4)) ; SnapshotHeader *h; Sensor_Reading *r; sprintf (outbuf, "ID: %s ", getPerso()->uname); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); for(int i=0;i < 10; i++) { h = &entry->hdr; sprintf (outbuf, ": SNAPSHOT # %d \n ",(i+(PartialSnapTotal*10))); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 16); sprintf (outbuf, "Version: 2 \n");// ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 12); sprintf (outbuf, "t:%d n:%d last:%d",h->data.time, h->data.num_of_data_points, h->data.latest_data_point_index); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); // ptr++; r = entry->r; for (int j = 0; j < NUMBER_OF_ENTRIES; j++) { #define FLASH_READ_DELAY 40 nrf_gpio_pin_set(LED_RESETTING); sprintf (outbuf, "i: %d T: %04x ",j,r[j].val.temp); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); sprintf (outbuf, "A: x%04xy%04xz%04x",r[j].val.x_ac,r[j].val.y_ac,r[j].val.z_ac); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); sprintf (outbuf, "G: x%04xy%04xz%04x",r[j].val.x_gy,r[j].val.y_gy,r[j].val.z_gy); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(FLASH_READ_DELAY); nrf_gpio_pin_clear(LED_RESETTING); nrf_delay_ms(FLASH_READ_DELAY); //ptr += sizeof(Sensor_Reading); } entry++; } ReadSnapshotsPartial = 0; } if (ReadDataBuffer == 1) { LogEntry *addr; sprintf (outbuf, "ID: %s ", getPerso()->uname); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); addr = (LogEntry *)(STEP_DATA_ADDRESS_START); for(int i=0;i < getActivityLogSize(); i++) { nrf_gpio_pin_set(LED_RESETTING); nrf_delay_ms(20); nrf_gpio_pin_clear(LED_RESETTING); nrf_delay_ms(20); // sprintf ( outbuf, ": h:%d s:%d a:%04f ",addr->item.hour, addr->item.sec, addr->item.activity_level); sprintf ( outbuf, "%d:%d - %03f ",addr->item.hour, (addr->item.sec)/60, addr->item.activity_level); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); //nrf_delay_ms(1); addr++; } ReadDataBuffer = 0; } if (ReadDataBufferPartial == 1) { LogEntry *addr; sprintf (outbuf, "ID: %s ", getPerso()->uname); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); PartialCountTotal = ((PartialCount1-('0'))*10)+(PartialCount2-('0')); addr = (LogEntry *)(STEP_DATA_ADDRESS_START+(2*(PartialCountTotal*1000))); for(int i=0;i < 1000; i++) { nrf_gpio_pin_set(LED_RESETTING); nrf_delay_ms(20); nrf_gpio_pin_clear(LED_RESETTING); nrf_delay_ms(20); // sprintf ( outbuf, ": h:%d s:%d a:%04f ",addr->item.hour, addr->item.sec, addr->item.activity_level); sprintf ( outbuf, "%d:%d - %03f ",addr->item.hour, (addr->item.sec)/60, addr->item.activity_level); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); //nrf_delay_ms(1); addr++; } ReadDataBufferPartial = 0; } if (Dumper == 1) { sprintf (outbuf, "Log: %d ",getActivityLogSize()); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); Dumper = 0; } if (ReadBigSnapshot == 1) { SnapshotHeader *h = getSnapshotHeader(); sprintf (outbuf, "t:%d n:%d last:%d",h->data.time, h->data.num_of_data_points, h->data.latest_data_point_index); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); for (i = 0; i < NUMBER_OF_ENTRIES; i++) //i <= h->data.latest_data_point_index; i++) { #define RAM_READ_DELAY 15 sprintf (outbuf, "i: %d T: %04x ",i,raw_data[i].val.temp); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(RAM_READ_DELAY); sprintf (outbuf, "A: x%04xy%04xz%04x",raw_data[i].val.x_ac,raw_data[i].val.y_ac,raw_data[i].val.z_ac); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(RAM_READ_DELAY); sprintf (outbuf, "G: x%04xy%04xz%04x",raw_data[i].val.x_gy,raw_data[i].val.y_gy,raw_data[i].val.z_gy); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); nrf_delay_ms(RAM_READ_DELAY); } ReadBigSnapshot = 0; } if (do_post_processing == true) { do_post_processing = false; if (emergencyCall == 1) { makeTheCall(); emergencyCall = 0; } post_comm_processing(); } // Power Method 1 // nrf_gpio_pin_write(LED_OTHER,1); // power_manage(); // nrf_gpio_pin_write(LED_OTHER,0); } } return retValue; }
string Joueur::toString() { return (nom_ + " | " + getPerso()); }
/** * @brief Timer routine for fall & step detection. */ void process_step_count(void * p_context) { //timer_stop(m_app_timer_id); reset_wdt(); nrf_gpio_pin_toggle(LED_RESETTING); if (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do not interfere if flash write is ongoing. return; } if (SaveSnapshot == 1) { nrf_gpio_pin_set(LED_RESETTING); return; } if (ReadBigSnapshot == 1) return; // store snapshot readSensorsAndStore(); //process activity levels updateAverages(); curr_sv = latestResultant(); if (BLESessionActive == 1) return; if (curr_sv < getPerso()->freefall_threshold) { zero_g_detected = true; } // if ( checkOrientation() == ORIENTATION_STANDING){ // if (curr_sv < getPerso()->freefall_threshold){ // zero_g_detected = true; // } // } // else if (checkOrientation() == ORIENTATION_NOT_STANDING) // { // if (curr_sv < NOT_STANDING_FREEFALL_THRESHOLD_DEFAULT){ // zero_g_detected = true; // } // } else { zero_g_detected = false; } //zero_g_detected = fd_check_zero_g(); if (zero_g_detected == true) { nrf_gpio_pin_write(LED_OTHER,1); // if (count == 0) accel_get_angles(&xAngleBeforeImpact, &yAngleBeforeImpact); count = getPerso()->fall_detection_window; } if (count > 0) { count--; fall_detected = fd_check_for_impact(curr_sv); if (fall_detected == 0xFF) { accel_get_angles(&xAngleAfterImpact, &yAngleAfterImpact); // if (abs(xAngleAfterImpact - xAngleBeforeImpact) >= getPerso()->x_angle_threshold && // abs(yAngleAfterImpact - yAngleBeforeImpact) >= getPerso()->y_angle_threshold) { ClearDataBuffer = ClearSnapshots = SetTimestamp = 0; // backupRawData(); do_post_processing = true; emergencyCall = 1; zero_g_detected = false; fall_detected = 0; count = 0; } } } if (count == 0) nrf_gpio_pin_write(LED_OTHER,0); /* else { // detect if step occurred if (detect_step(xRaw,yRaw,zRaw)==1) { nSteps++; } } */ }
void commandAPI_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { nrf_gpio_pin_set(LED_RESETTING); nrf_delay_ms(20); nrf_gpio_pin_clear(LED_RESETTING); char outbuf[20]; if(writingPerso == 0) { if(p_data[0] == 'A') //0xEE; SET GOLDEN TIMESTAMP { ble_nus_send_string(&m_nus, "OK", 2); /* // Time Stamp Processing Device Side timeYear = (uint16_t)(p_data[1]); timeYear = (timeYear << 8) | p_data[2]; */ int timeHour = (uint16_t)(p_data[1]); timeHour = (timeHour << 8) | p_data[2]; int timeSeconds = p_data[3]* 60 + p_data[4]; setGoldenTime(timeHour,timeSeconds); // SetTimestamp = 1; ble_nus_send_string(&m_nus, "SetTimestamp ", 20); //comment out on final } else if (p_data[0] == 'O') //Check for orientation { //sendOrientation = 1; //comment out on final //ble_nus_send_string(&m_nus, "SendOrientation\n ", 16); //comment out on final // if (sendOrientation == 1) { float p,r; accel_get_angles(&p, &r); sprintf(outbuf, "pitch: %04f",p); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); sprintf(outbuf, "roll: %04f", r); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); if (checkOrientation() == ORIENTATION_STANDING) { sprintf (outbuf, "standing: %04f",p); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); } else { sprintf (outbuf, "not standing: %04f",p); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); } } } else if (p_data[0] == 'B')//0xB4; READ STEP DATA { ReadDataBuffer = 1; ble_nus_send_string(&m_nus, "ReadDataBuffer ", 20); //comment out on final } else if (p_data[0] == 'J')//0xB4; READ STEP DATA { ReadDataBufferPartial = 1; ble_nus_send_string(&m_nus, "ReadDataBuffer ", 20); //comment out on final PartialCount1 = p_data[1]; PartialCount2 = p_data[2]; } else if (p_data[0] == 'C')//0xB8; ERASE STEP DATA { ClearDataBuffer = 1; ble_nus_send_string(&m_nus, "OK", 2); ble_nus_send_string(&m_nus, "ClearDataBuffer ", 20); //comment out on final } else if (p_data[0] == 'D')//0xC4; READ SNAPSHOTS { ReadSnapshots = 1; ble_nus_send_string(&m_nus, "ReadSnapshots ", 20); //comment out on final } else if (p_data[0] == 'K')//0xC4; READ SNAPSHOTS { ReadSnapshotsPartial = 1; ble_nus_send_string(&m_nus, "ReadSnapshots ", 20); //comment out on final PartialSnap1 = p_data[1]; PartialSnap2 = p_data[2]; } else if (p_data[0] == 'E')//0xC8; ERASE SNAPSHOTS { ClearSnapshots = 1; ble_nus_send_string(&m_nus, "OK", 2); ble_nus_send_string(&m_nus, "ClearSnapshots ", 20); //comment out on final } else if ( (p_data[0] == 'X') || (p_data[0] == 'x') ) //DATA COUNT { Dumper = 1; //comment out on final ble_nus_send_string(&m_nus, "DumpData ", 20); //comment out on final } else if (p_data[0] == 'S') //READ BIG SNAPSHOT LIVE { ReadBigSnapshot = 1; //comment out on final ble_nus_send_string(&m_nus, "ReadBigSnapshot\n ", 16); //comment out on final } /* else if(p_data[0] == 'J') //TYPE SF_XYZ { ble_nus_send_string(&m_nus, "TYPE SF_XYZ", 13); XYZFlag = 1; } else if(p_data[0] == 'K') //TYPE SF_SV { ble_nus_send_string(&m_nus, "TYPE SF_SV", 13); SVFlag = 1; } else if(p_data[0] == 'L') //READ XYZ_ADDRESS { uint8_t xyzVar; uint8_t xyzVar2; for (xyzVar=0;xyzVar<5;xyzVar++) { for(xyzVar2=0;xyzVar2<4;xyzVar2++) { if(((xyzVar*4)+xyzVar2)<(*XYZ_LENGTH)) { XYZBuffer[(xyzVar*4)+xyzVar2]=(((*(XYZ_ADDRESS+xyzVar))>>(xyzVar2*8))&0xFF); } } } ble_nus_send_string(&m_nus, (unsigned char*)XYZBuffer, 20); } else if(p_data[0] == 'M') //read SVBuffer { uint8_t svVar; uint8_t svVar2; for (svVar=0;svVar<5;svVar++) { for(svVar2=0;svVar2<4;svVar2++) { if(((svVar*4)+svVar2)<(*SV_LENGTH)) { SVBuffer[(svVar*4)+svVar2]=(((*(SV_ADDRESS+svVar))>>(svVar2*8))&0xFF); } } } ble_nus_send_string(&m_nus, (unsigned char*)SVBuffer, 20); } */ /* else if(p_data[0] == 'N') //TYPE USERNAME { ble_nus_send_string(&m_nus, "TYPE USERNAME", 13); usernameFlag = 1; } else if(p_data[0] == 'U') //read USERNAME { uint8_t uVar; uint8_t uVar2; for (uVar=0;uVar<5;uVar++) { for(uVar2=0;uVar2<4;uVar2++) { if(((uVar*4)+uVar2)<(*USERNAME_LENGTH)) { UsernameBuffer[(uVar*4)+uVar2]=(((*(USERNAME_ADDRESS+uVar))>>(uVar2*8))&0xFF); } } } ble_nus_send_string(&m_nus, (unsigned char*)UsernameBuffer, 20); } */ else if(p_data[0] == 'R') // CLEAR PERSO { ble_nus_send_string(&m_nus, "PAGE CLEARED", 12); clearPerso = 1; } else if ( (p_data[0] == 'P') || (p_data[0] == 'N')) // Write PERSO { ble_nus_send_string(&m_nus, "New Personalization", 20); copyCurrentToTemp(); ble_nus_send_string(&m_nus, "TYPE USERNAME", 13); writingPerso = 1; usernameFlag = 1; } else if (p_data[0] == 'v') //SEND RAW DATA READING { sendRawDataItem(); } else if (p_data[0] == 'Z') //START SENDING DATA AT 1HZ { sendRawData = 1; ble_nus_send_string(&m_nus, "SendRawData-Start\n ", 16); //comment out on final } else if (p_data[0] == 'z') //STOP SENDING DATA AT 1HZ { sendRawData = 0; ble_nus_send_string(&m_nus, "SendRawData-Stop\n ", 16); //comment out on final } else if (p_data[0] == 'w') //current Perso { sprintf (outbuf, "impact: %02f",getPerso()->impact_threshold); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); sprintf (outbuf, "freeFall: %02f",getPerso()->freefall_threshold); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); sprintf (outbuf, "xAngle: %02f",getPerso()->x_angle_threshold); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); sprintf (outbuf, "yAngle: %02f",getPerso()->y_angle_threshold); ble_nus_send_string(&m_nus, (unsigned char*)outbuf, 20); } else { ble_nus_send_string(&m_nus, "NAK", 3); //comment out on final ble_nus_send_string(&m_nus, "NoCommand ", 20); //comment out on final } } else if (usernameFlag==1) { uint8_t i; for(i=0;i<length;i++) { tempPerso.uname[i]=p_data[i]; } tempPerso.uname[length] = '\n'; usernameFlag = 0; ble_nus_send_string(&m_nus, "TYPE IMPACT COEF", 16); impactFlag = 1; } /* else if (XYZFlag==1) { uint8_t j; for(j=0;j<length;j++) { tempXYZ[j]=p_data[j]; } tempXYZLength = length; ble_nus_send_string(&m_nus, "SF_XYZ ENTERED", 14); XYZFlag = 0; writeXYZ = 1; } else if (SVFlag==1) { uint8_t k; for(k=0;k<length;k++) { tempSV[k]=p_data[k]; } tempSVLength = length; ble_nus_send_string(&m_nus, "SF_SV ENTERED", 13); SVFlag = 0; writeSV = 1; } */ else if (impactFlag == 1) { tempPerso.impact_threshold = string_to_f(p_data); impactFlag = 0; ble_nus_send_string(&m_nus, "TYPE FF COEF", 16); FreeFallFlag = 1; } else if (FreeFallFlag == 1) { tempPerso.freefall_threshold = string_to_f(p_data); FreeFallFlag = 0; ble_nus_send_string(&m_nus, "TYPE XANGLE", 16); xAngleFlag = 1; } else if (xAngleFlag == 1) { tempPerso.x_angle_threshold = string_to_f(p_data); xAngleFlag = 0; ble_nus_send_string(&m_nus, "TYPE YANGLE", 16); yAngleFlag = 1; } else if (yAngleFlag == 1) { tempPerso.y_angle_threshold = string_to_f(p_data); yAngleFlag = 0; ble_nus_send_string(&m_nus, "rewriting perso", 16); rewritePerso = 1; writingPerso = 0; } }