骑行开始或者结束:
if ( bycle_enable == 0 )
{
bycle_enable = 1;
config.train_bycle_utc = getUTC();
config.train_bycle_distance=pedometer.distance;
config.train_bycle_pedometer =pedometer.counter;
config.train_bycle_calorie =pedometer.calorie;
} else
{
bycle_enable = 0;
osal_set_event(taskStoreTaskId,TASK_STORE_SAVE_BYCLE_EVT );
}
步行开始或者结束:
if ( running_enable == 0 )
{
running_enable = 1;
config.train_running_utc = getUTC();
config.train_running_distance = pedometer.distance;
config.train_running_pedometer = pedometer.counter;
config.train_running_calorie = pedometer.calorie;
} else
{
running_enable = 0;
osal_set_event( taskStoreTaskId,TASK_STORE_SAVE_RUNNING_EVT );
}
数据保存:
if ( events & TASK_STORE_UPLOAD_RUNNING_EVT )
{
// 一次运动结束,上传,以便通知APP读取 (使用iBand)
// 上报协议格式 0x1B EN SS 00 00 00 D6 D5 D4 D3 D2 D1 D0 U3 U2 U1 U0 TH TL CRC
memset( txbuf, 0, sizeof(txbuf) );
txbuf[0] = 0x1B;
txbuf[1] = 3; // EN[3:0]--> 3 当前数据上报
txbuf[2] = 0; // SS
bt_protocol_tx( txbuf, sizeof(txbuf) );
return ( events ^ TASK_STORE_UPLOAD_RUNNING_EVT );
}
if ( events & TASK_STORE_SAVE_BYCLE_EVT )
{
// 运动数据-自行车
// 保存格式 0xEN SS D6 D5 D4 D3 D2 D1 D0 U3 U2 U1 U0 TH TL CRC
memset( txbuf, 0xFF, sizeof(txbuf) );
utc = getUTC();
unsigned long len = (utc - config.train_bycle_utc)/60;
utc = config.train_bycle_utc;
unsigned long va = pedometer.counter - config.train_running_pedometer;
unsigned long vb = pedometer.calorie - config.train_running_calorie;
unsigned long vc = pedometer.distance - config.train_running_distance;
if ( vb == 0 )
{
return ( events ^ TASK_STORE_SAVE_BYCLE_EVT );
}
txbuf[0] = (0x1<<4) | 0; // EN[7:4]--> 1 骑行
txbuf[1] = 0x0 | (0x1<<3); // SS[3] --> 1 卡路里有效
txbuf[2] = (va>>14) & 0x7; /* D6D5...D0 */
txbuf[3] = (va>>6) & 0xFF;
txbuf[4] = ((va&0x3F)<<2)|((vb>>15)&0x3);
txbuf[5] = (vb>>7) & 0xFF;
txbuf[6] = ((vb&0x7F)<<1) | ((vc>>16)&0x1);
txbuf[7] = (vc>>8) & 0xFF;
txbuf[8] = vc & 0xFF;
txbuf[9] = utc>>24; /* U3U2...U0 */
txbuf[10] = utc>>16;
txbuf[11] = utc>>8;
txbuf[12] = utc & 0xFF;
txbuf[13] = len>>8; /* TH */
txbuf[14] = len & 0xFF; /* TL */
txbuf[15] = 0xFF; /* CRC */;
flash_memory_put( FMC_ID_RUNNING, txbuf, 16);
osal_start_timerEx( task_id, TASK_STORE_UPLOAD_RUNNING_EVT, 800);
return ( events ^ TASK_STORE_SAVE_BYCLE_EVT );
}
if ( events & TASK_STORE_SAVE_RUNNING_EVT )
{
// 运动数据-跑步-慢走快走
// 保存格式 0xEN SS D6 D5 D4 D3 D2 D1 D0 U3 U2 U1 U0 TH TL CRC
memset( txbuf, 0xFF, sizeof(txbuf) );
utc = getUTC();
int len = (utc - config.train_running_utc)/60;
utc = config.train_running_utc;
unsigned long va = pedometer.counter - config.train_running_pedometer;
unsigned long vb = pedometer.calorie - config.train_running_calorie;
unsigned long vc = pedometer.distance - config.train_running_distance;
if ( va == 0 )
{
return ( events ^ TASK_STORE_SAVE_RUNNING_EVT );
}
txbuf[0] = 0x0<<4 | 0; // EN[7:4]--> 0 步行
txbuf[1] = (0x0<<0) | (0x7<<2); // SS[1:0]--> 0 慢走 SS[2:4]-->7 卡路里、里程和步数都有效
txbuf[2] = (va>>14) & 0x7; /* D6D5...D0 */
txbuf[3] = (va>>6) & 0xFF;
txbuf[4] = ((va&0x3F)<<2)|((vb>>15)&0x3);
txbuf[5] = (vb>>7) & 0xFF;
txbuf[6] = ((vb&0x7F)<<1) | ((vc>>16)&0x1);
txbuf[7] = (vc>>8) & 0xFF;
txbuf[8] = vc & 0xFF;
txbuf[9] = utc>>24; /* U3U2...U0 */
txbuf[10] = utc>>16;
txbuf[11] = utc>>8;
txbuf[12] = utc& 0xFF;
txbuf[13] = len>>8; /* TH */
txbuf[14] = len & 0xFF; /* TL */
txbuf[15] = 0xFF; /* CRC */;
flash_memory_put( FMC_ID_RUNNING, txbuf, 16);
osal_start_timerEx( task_id, TASK_STORE_UPLOAD_RUNNING_EVT, 800);
return ( events ^ TASK_STORE_SAVE_RUNNING_EVT );
}
数据发送
////////////////////////////////////////////////////////////////////////////// //
// Upload the running data to app //
// //
if ( events & TASK_UPLOAD_STORE_RUNNING_1_EVT )
{
// 运动数据
// 格式:1B EN SS C2 C1 C0 D6 D5 D4 D3 D2 D1 D0 U3 U2 U1 U0 TH TL CRC
/** 蓝牙断开,不发送 */
if ( ble.isConnected != 1 ){
return (events ^ TASK_UPLOAD_STORE_RUNNING_1_EVT);
}
memset( txbuf, 0, sizeof(txbuf));
running_all = q.items;
running_done = 0;
txbuf[0] = 0x1B;
txbuf[1] = 0x01; // EN
txbuf[2] = 0x00; // SS
txbuf[3] = running_all>>4;
txbuf[4] = ((running_all & 0Xf)<<4) | ((running_done>>8)&0xF);
txbuf[5] = running_done & 0xFF;
if ( running_all != 0 )
{
fret = load_queue( FMC_ID_RUNNING, &q, buf, 16 );
if ( fret == QUEUE_EMPTY )
{
return ( events ^ TASK_UPLOAD_STORE_RUNNING_1_EVT );
}
}
txbuf[1] |= (buf[0] & 0xF0); // EN
txbuf[2] = buf[1]; // SS
memcpy( txbuf+6, buf+2, 7+4+2);
bt_protocol_tx(txbuf, 20);
if ( ++running_done < running_all ){ /* Done ? */
running_done++;
osal_start_timerEx( task_id, TASK_UPLOAD_STORE_RUNNING_2_EVT, 400);
}
return (events ^ TASK_UPLOAD_STORE_RUNNING_1_EVT);
}
if ( events & TASK_UPLOAD_STORE_RUNNING_2_EVT )
{
// 运动数据
// 格式:1B EN SS C2 C1 C0 D6 D5 D4 D3 D2 D1 D0 U3 U2 U1 U0 TH TL CRC
if ( ble.isConnected != 1 ){
running_done = 0;
return ( events ^ TASK_UPLOAD_STORE_RUNNING_2_EVT );
}
txbuf[0] = 0x1B;
txbuf[1] = 0x01; // EN
txbuf[2] = 0x00; // SS
txbuf[3] = running_all>>4;
txbuf[4] = ((running_all & 0Xf)<<4) | ((running_done>>8)&0xF);
txbuf[5] = running_done & 0xFF;
fret = load_queue( FMC_ID_RUNNING, &q, buf, 16 );
if ( fret == QUEUE_EMPTY )
{
return ( events ^ TASK_UPLOAD_SEG_PEDO_2_EVT );
}
if ( running_done++ < running_all )
{
txbuf[1] |= (buf[0] & 0xF0); // EN
txbuf[2] = buf[1]; // SS
memcpy( txbuf+6, buf+2, 7+4+2);
running_done++;
bt_protocol_tx( txbuf,20 );
osal_start_timerEx(task_id, TASK_UPLOAD_STORE_RUNNING_2_EVT, 400);
}else{
running_done = 0;
}
return ( events ^ TASK_UPLOAD_STORE_RUNNING_2_EVT );
}
命令解析:
case 0x1B: /** Get Running Data (All Data Null now)*/
// 0xFC 1B EN 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
if ( buf[2] == 0x0 )
{
txbuf[1] = 0x0;
}else if ( buf[2] == 0x1 )
{
osal_set_event( taskUploadTaskId, TASK_UPLOAD_STORE_RUNNING_EVT );
return 0;
}else if ( buf[2] == 0x2 ){
txbuf[1] = 0x02; // EN
txbuf[2] = 0x00; // SS
txbuf[3] = fm.erea[FMC_ID_RUNNING].items>>4;
txbuf[4] = (fm.erea[FMC_ID_RUNNING].items & 0xF)<<4;
}else{
txbuf[1] = 0x1<<7;
}
break;