void _isr_event(ng_netdev_t *dev, uint32_t event_type) { switch (event_type) { case ISR_EVENT_RX_DONE: _receive_data(); break; default: /* do nothing */ return; } }
int main() { if (_is_docked()){ for(int j=0;(j<10);j++) for(int i=0;(i<200);i++) _delay_ms(5); } scounter=0; //Initialized data buffer dataIndex=0; dataSubindex=0; // Blink green for 5 seconds _wocket_initialize(); AC_NUMS=_SAMPLING_RATE *60; power_adc_disable(); power_spi_disable(); power_timer0_disable(); power_timer1_disable(); power_twi_disable(); while(1){ //Sample only in the main loop because of p if(sampleFlag){ power_adc_enable(); _atmega_adc_turn_on(); sampleFlag=0; #ifdef _VERSION ==3 x=_atmega_a2dConvert10bit(ADC0); y=_atmega_a2dConvert10bit(ADC1); z=_atmega_a2dConvert10bit(ADC2); //x=y=z=cc++; //if (cc>=1024) // cc=0; vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2); if (_wPC>40){ //Skip the first samples if (summary_count==0) { vmag=vmag/24; if (vmag>65535) acount[ci]=65535; else acount[ci]=(unsigned short) vmag; vmag=0; ++ci; if (ci==AC_BUFFER_SIZE) ci=0; cseq++; if (ci==si) { si++; if (si==AC_BUFFER_SIZE) si=0; sseq++; } acount[ci]=0; summary_count=AC_NUMS; }else summary_count--; } else if (_wPC==40) vmag=0; #else //x=_atmega_a2dConvert10bit(ADC3); //y=_atmega_a2dConvert10bit(ADC2); //z=_atmega_a2dConvert10bit(ADC1); #endif m_SET_X(data[dataIndex],x,dataSubindex); m_SET_Y(data[dataIndex],y,dataSubindex); m_SET_Z(data[dataIndex],z,dataSubindex); dataSubindex++; if (dataSubindex>=4) dataSubindex=0; //Most of the time the data buffer with 750 will not overflow //and will be enough to transmit the data, data will go from 0 up to a specific //value if (_wTM==_TM_Continuous) { switch(dataSubindex){ case 1: m_GET_X(x,data[dataIndex].byte1,data[dataIndex].byte2,0); m_GET_Y(y,data[dataIndex].byte2,data[dataIndex].byte3,0); m_GET_Z(z,data[dataIndex].byte3,data[dataIndex].byte4,0); break; case 2: m_GET_X(x,data[dataIndex].byte4,data[dataIndex].byte5,1); m_GET_Y(y,data[dataIndex].byte6,data[dataIndex].byte7,1); m_GET_Z(z,data[dataIndex].byte7,data[dataIndex].byte8,1); break; case 3: m_GET_X(x,data[dataIndex].byte8,data[dataIndex].byte9,2); m_GET_Y(y,data[dataIndex].byte9,data[dataIndex].byte10,2); m_GET_Z(z,data[dataIndex].byte11,data[dataIndex].byte12,2); break; case 0: m_GET_X(x,data[dataIndex].byte12,data[dataIndex].byte13,3); m_GET_Y(y,data[dataIndex].byte13,data[dataIndex].byte14,3); m_GET_Z(z,data[dataIndex].byte14,data[dataIndex].byte15,3); break; } if (justconnected==1) { _send_tm(); justconnected=2; } //_send_pdu(x,y,z); _send_uncompressed_pdu(x, y, z); //Send summary activity count /* for (int i=0;(i<summaryindex);i++){ _send_summary_count(acount[i]); acount[i]=0; } if (summaryindex<AC_BUFFER_SIZE){ acount[0]=acount[summaryindex]; summaryindex=0; }*/ } else { if ((dataSubindex==0) && (batch_counter<750)) batch_counter++; if (connected){ _greenled_turn_on(); gotack=1; tester=0; if (_wTM==_TM_Continuous) continue; for (int ixz=0;(ixz<100);ixz++) { _bluetooth_transmit_uart0_byte(0xff); if (sampleFlag) { sampleFlag=0; x=_atmega_a2dConvert10bit(ADC0); y=_atmega_a2dConvert10bit(ADC1); z=_atmega_a2dConvert10bit(ADC2); vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2); if (_wPC>40){ //Skip the first samples if (summary_count==0) { vmag=vmag/24; if (vmag>65535) acount[ci]=65535; else acount[ci]=(unsigned short) vmag; vmag=0; ++ci; if (ci==AC_BUFFER_SIZE) ci=0; cseq++; if (ci==si) { si++; if (si==AC_BUFFER_SIZE) si=0; sseq++; } acount[ci]=0; summary_count=AC_NUMS; }else summary_count--; } else if (_wPC==40) vmag=0; m_SET_X(data[dataIndex],x,dataSubindex); m_SET_Y(data[dataIndex],y,dataSubindex); m_SET_Z(data[dataIndex],z,dataSubindex); dataSubindex++; if (dataSubindex>=4) dataSubindex=0; } } _send_fv(); _send_sr(); _send_tm(); _send_batch_count((batch_counter-1)*4); _send_acs(); //Send summary activity count /*for (int i=0;(i<summaryindex);i++){ _send_summary_count(acount[i]); acount[i]=0; } if (summaryindex<AC_BUFFER_SIZE){ acount[0]=acount[summaryindex]; summaryindex=0; }*/ if ((batch_counter>0) && (batch_counter<750)) // Go from 0 up to batch_counter { for (int i=0;(i<(batch_counter-1));i++) { m_GET_X(x,data[i].byte1,data[i].byte2,0); m_GET_Y(y,data[i].byte2,data[i].byte3,0); m_GET_Z(z,data[i].byte3,data[i].byte4,0); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); m_GET_X(x,data[i].byte4,data[i].byte5,1); m_GET_Y(y,data[i].byte6,data[i].byte7,1); m_GET_Z(z,data[i].byte7,data[i].byte8,1); //_send_uncompressed_pdu(x,y, z); _send_pdu(x,y,z); m_GET_X(x,data[i].byte8,data[i].byte9,2); m_GET_Y(y,data[i].byte9,data[i].byte10,2); m_GET_Z(z,data[i].byte11,data[i].byte12,2); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); m_GET_X(x,data[i].byte12,data[i].byte13,3); m_GET_Y(y,data[i].byte13,data[i].byte14,3); m_GET_Z(z,data[i].byte14,data[i].byte15,3); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); _receive_data(); if (sampleFlag) { sampleFlag=0; x=_atmega_a2dConvert10bit(ADC0); y=_atmega_a2dConvert10bit(ADC1); z=_atmega_a2dConvert10bit(ADC2); vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2); if (_wPC>40){ //Skip the first samples if (summary_count==0) { vmag=vmag/24; if (vmag>65535) acount[ci]=65535; else acount[ci]=(unsigned short) vmag; vmag=0; ++ci; if (ci==AC_BUFFER_SIZE) ci=0; cseq++; if (ci==si) { si++; if (si==AC_BUFFER_SIZE) si=0; sseq++; } acount[ci]=0; summary_count=AC_NUMS; }else summary_count--; } else if (_wPC==40) vmag=0; m_SET_X(data[dataIndex],x,dataSubindex); m_SET_Y(data[dataIndex],y,dataSubindex); m_SET_Z(data[dataIndex],z,dataSubindex); dataSubindex++; if (dataSubindex>=4) dataSubindex=0; } } if (batch_counter>0){ //copy end item into start data[0].byte1=data[batch_counter].byte1; data[0].byte2=data[batch_counter].byte2; data[0].byte3=data[batch_counter].byte3; data[0].byte4=data[batch_counter].byte4; data[0].byte5=data[batch_counter].byte5; data[0].byte6=data[batch_counter].byte6; data[0].byte7=data[batch_counter].byte7; data[0].byte8=data[batch_counter].byte8; data[0].byte9=data[batch_counter].byte9; data[0].byte10=data[batch_counter].byte10; data[0].byte11=data[batch_counter].byte11; data[0].byte12=data[batch_counter].byte12; data[0].byte13=data[batch_counter].byte13; data[0].byte14=data[batch_counter].byte14; data[0].byte15=data[batch_counter].byte15; } }else{ int current=dataIndex+1; int end =dataIndex; if (current>=750) current=0; while(current!=end) { m_GET_X(x,data[current].byte1,data[current].byte2,0); m_GET_Y(y,data[current].byte2,data[current].byte3,0); m_GET_Z(z,data[current].byte3,data[current].byte4,0); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); m_GET_X(x,data[current].byte4,data[current].byte5,1); m_GET_Y(y,data[current].byte6,data[current].byte7,1); m_GET_Z(z,data[current].byte7,data[current].byte8,1); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); m_GET_X(x,data[current].byte8,data[current].byte9,2); m_GET_Y(y,data[current].byte9,data[current].byte10,2); m_GET_Z(z,data[current].byte11,data[current].byte12,2); //_send_uncompressed_pdu(x, y, z); _send_pdu(x,y,z); m_GET_X(x,data[current].byte12,data[current].byte13,3); m_GET_Y(y,data[current].byte13,data[current].byte14,3); m_GET_Z(z,data[current].byte14,data[current].byte15,3); //_send_uncompressed_pdu(x,y, z); _send_pdu(x,y,z); current++; if (current==750) current=0; _receive_data(); if (sampleFlag) { sampleFlag=0; x=_atmega_a2dConvert10bit(ADC0); y=_atmega_a2dConvert10bit(ADC1); z=_atmega_a2dConvert10bit(ADC2); vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2); if (_wPC>40){ //Skip the first samples if (summary_count==0) { vmag=vmag/24; if (vmag>65535) acount[ci]=65535; else acount[ci]=(unsigned short) vmag; vmag=0; ++ci; if (ci==AC_BUFFER_SIZE) ci=0; cseq++; if (ci==si) { si++; if (si==AC_BUFFER_SIZE) si=0; sseq++; } acount[ci]=0; summary_count=AC_NUMS; }else summary_count--; } else if (_wPC==40) vmag=0; m_SET_X(data[dataIndex],x,dataSubindex); m_SET_Y(data[dataIndex],y,dataSubindex); m_SET_Z(data[dataIndex],z,dataSubindex); dataSubindex++; if (dataSubindex>=4) dataSubindex=0; } } //copy end item into start data[0].byte1=data[end].byte1; data[0].byte2=data[end].byte2; data[0].byte3=data[end].byte3; data[0].byte4=data[end].byte4; data[0].byte5=data[end].byte5; data[0].byte6=data[end].byte6; data[0].byte7=data[end].byte7; data[0].byte8=data[end].byte8; data[0].byte9=data[end].byte9; data[0].byte10=data[end].byte10; data[0].byte11=data[end].byte11; data[0].byte12=data[end].byte12; data[0].byte13=data[end].byte13; data[0].byte14=data[end].byte14; data[0].byte15=data[end].byte15; } batch_counter=0; dataIndex=0; seconds_passed=0; while (seconds_passed<400) { _delay_ms(5); seconds_passed++; _receive_data(); if (sampleFlag) { sampleFlag=0; x=_atmega_a2dConvert10bit(ADC0); y=_atmega_a2dConvert10bit(ADC1); z=_atmega_a2dConvert10bit(ADC2); vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2); if (_wPC>40){ //Skip the first samples if (summary_count==0) { vmag=vmag/24; if (vmag>65535) acount[ci]=65535; else acount[ci]=(unsigned short) vmag; vmag=0; ++ci; if (ci==AC_BUFFER_SIZE) ci=0; cseq++; if (ci==si) { si++; if (si==AC_BUFFER_SIZE) si=0; sseq++; } acount[ci]=0; summary_count=AC_NUMS; }else summary_count--; } else if (_wPC==40) vmag=0; m_SET_X(data[dataIndex],x,dataSubindex); m_SET_Y(data[dataIndex],y,dataSubindex); m_SET_Z(data[dataIndex],z,dataSubindex); dataSubindex++; if (dataSubindex>=4) dataSubindex=0; } } //connected=0; //Don't turn off the radio if a request to switch mode has been received if (_wTM==_TM_Continuous) _bluetooth_turn_on(); else _bluetooth_turn_off(); command_counter=0; seconds_disconnected=0; _greenled_turn_off(); } } _atmega_adc_turn_off(); power_adc_disable(); if ((dataSubindex==0) && (!connected)) dataIndex++; if (dataIndex==750) dataIndex=0; connected=0; } cli(); set_sleep_mode(SLEEP_MODE_IDLE); //set_sleep_mode(SLEEP_MODE_PWR_SAVE); sleep_enable(); sleep_bod_disable(); sei(); sleep_cpu(); sleep_disable(); } return 0; }