📄 oscopem.nc
字号:
includes userdata;module OscopeM{ provides interface StdControl; provides interface Oscope[uint8_t channel]; uses interface ReceiveMsg as ResetCounterMsg; uses interface CC2420Control; uses interface MacControl; uses interface Send; uses interface RouteControl;// uses interface Leds;// uses interface StdControl as MultiControl;}implementation{ enum { MAX_CHANNELS = OSCOPE_MAX_CHANNELS, BUFFER_SIZE = OSCOPE_BUFFER_SIZE, }; typedef struct { uint8_t index; bool send_data_ready; uint16_t lastCount; uint16_t count; uint16_t data[BUFFER_SIZE]; uint16_t send_data[BUFFER_SIZE]; } OscopeData_t; TOS_Msg m_msg; int m_is_sending; int m_send_next; TOS_Msg gMsgBuffer; uint32_t seqno; OscopeData_t m_data[ MAX_CHANNELS ]; command void Oscope.reset[uint8_t channel]() { OscopeData_t* ch = m_data+0; OscopeData_t* chend = m_data+MAX_CHANNELS; atomic { for( ; ch != chend; ch++ ) { ch->index = 0; ch->send_data_ready = FALSE; ch->count = 0; } } m_send_next = 0; m_is_sending = FALSE; } command result_t StdControl.init() { seqno = 0; // call MultiControl.init(); call Oscope.reset[MAX_CHANNELS](); return SUCCESS; } command result_t StdControl.start() { call CC2420Control.SetRFPower(15); call MacControl.enableAck();// call MultiControl.start(); return SUCCESS; } command result_t StdControl.stop() { //call MultiControl.stop(); return SUCCESS; } void sendHelper(); task void sendHelperTask() { sendHelper(); } void sendHelper() { if( m_is_sending == TRUE ) { if( (call Send.send(&gMsgBuffer,sizeof(struct OscopeMsg))) == FAIL ) { if( post sendHelperTask() == FALSE ) m_is_sending = FALSE; //the task didn't post, nothing to do but drop the msg } } } int channel_inc( int ch, int inc ) { ch += inc; if( ch >= MAX_CHANNELS ) ch -= MAX_CHANNELS; return ch; } task void send() { if( m_is_sending == FALSE ) { int i; OscopeData_t* chbegin = m_data + 0; OscopeData_t* chend = m_data + MAX_CHANNELS; OscopeData_t* ch = m_data + m_send_next; for( i=0; i<MAX_CHANNELS; i++ ) { if( ch->send_data_ready == TRUE ) { OscopeMsg_t * body; uint16_t Len; int n = channel_inc( i, m_send_next ); dbg(DBG_USR1, "OscopeM: Sending sensor reading\n"); if ((body = (struct OscopeMsg *)call Send.getBuffer(&gMsgBuffer,&Len)) != NULL) { body->parentaddr = call RouteControl.getParent(); body->seqno = seqno++; body->lastSampleNumber = ch->lastCount; body->channel = n; body->hopnum = 0; memcpy( body->data, ch->send_data, sizeof(uint16_t)*BUFFER_SIZE ); ch->send_data_ready = FALSE; m_is_sending = TRUE; m_send_next = channel_inc( n, 1 ); sendHelper(); return; } if( ++ch == chend ) ch = chbegin; } } } } event result_t Send.sendDone(TOS_MsgPtr pMsg, result_t success) { dbg(DBG_USR2, "OscopeM: output complete 0x%x\n", success); m_is_sending = FALSE; post send(); return SUCCESS; } task void prepare_send_data() { OscopeData_t* ch = m_data + 0; OscopeData_t* chend = m_data + MAX_CHANNELS; for( ; ch != chend; ch++ ) { atomic { if( ch->index >= BUFFER_SIZE ) { ch->lastCount = ch->count; memcpy( ch->send_data, ch->data, sizeof(uint16_t)*BUFFER_SIZE ); ch->send_data_ready = TRUE; ch->index = 0; post send(); } } } } async command result_t Oscope.put[uint8_t channel]( uint16_t value ) { result_t rv = FAIL; atomic { if( channel < MAX_CHANNELS ) { OscopeData_t* ch = &m_data[channel]; ch->count++; if( ch->index < BUFFER_SIZE ) { ch->data[ ch->index++ ] = value; if( ch->index >= BUFFER_SIZE ) post prepare_send_data(); rv = SUCCESS; } } } return rv; } event TOS_MsgPtr ResetCounterMsg.receive( TOS_MsgPtr msg ) { call Oscope.reset[MAX_CHANNELS](); return msg; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -