⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attrmagm.nc

📁 传感器网络中的嵌入式操作系统源代码
💻 NC
📖 第 1 页 / 共 2 页
字号:
		 iTimerThreshold = TIME_UNDER_MIN-2;	//dont let the counter underflow	      if ( iTimerThresholdCummulative < 0)		 iTimerThresholdCummulative = 0;	//dont let the counter underflow	      	      // Check if ADC is close to saturation	      if( xsum>MAG_ADC_ALMOSTMAX 	|| xsum<MAG_ADC_ALMOSTMIN ||		  ysum>MAG_ADC_ALMOSTMAX || ysum<MAG_ADC_ALMOSTMIN)		 iTimeinSaturation++;	      else		 iTimeinSaturation = 0;	//clear count if drop out of saturation	      	      /*	        State Machine for Event Detection	      */	      switch( EVState) 		{		case EV_UNDER:	   					  if ( iTimerThreshold > TIME_OVER_MIN) {	   //Are we over the threshold yet?		     iTimerThreshold = 0;	//reset the count - it now reflects elapsed time over		    NextEV = EV_OVER;	//trigger event		  }		  break;		case EV_OVER:						  if(  iTimerThreshold<TIME_UNDER_MIN) { //Are we still over the threshold ?		     iTimerThreshold = 0; //reset the count		    //	 iTimerThresholdCummulative) = 0;		    NextEV = EV_UNDER;	//been under for awhile  		  }		  if(  iTimerThresholdCummulative > TIME_OVER_MAX) { //been on too long		     baselineCount = 0;	//Establish a new BASELINE measure state		     iTimerThreshold = 0;		     iTimerThresholdCummulative = 0;		    NextEV = EV_UNDER;		  }		  break;		default: NextEV = EV_UNDER ; break;	 //should never get here!		}//switch EVState	      #ifdef new	    if(  EVState) == EV_UNDER ) {					  if( iTimerThreshold > TIME_OVER_MIN) {	   //Are we over the threshold yet?		    iTimerThreshold = 0;	//reset the count		    NextEV = EV_OVER;	//trigger event		  }	    }// EV_UNDER#endif //new	      //--------------------------process message-----------------------	    if( EVState >= EV_OVER)  // it is a real signal   		{ // Event Detected  		  if(  iTimerThresholdCummulative > TIME_OVER_MAX) { //been on too long		     baselineCount = 0;	//Establish a new BASELINE measure state		     iTimerThreshold = 0;		     iTimerThresholdCummulative = 0;		     NextEV = EV_UNDER;		  }		 		} //event detected	      break; //DM_NORMAL	      	    default:	      //should never get here	      NextDAQMode = DM_NORMAL;	      break;	    }	//switch DAQMode	}  //else DAQModes    }	//baseline>baseline count   DAQMode = NextDAQMode;		//update state   EVState = NextEV;		//update Event state}/***************************************************************************** *   SimpleInit.init *   - init all states****************************************************************************/command result_t StdControl.init() {   DAQMode = DM_NORMAL;    //DAQ MOde   EVState = EV_UNDER;   cAZBit  = 0;   I2CBusy = I2C_IDLE;     // clear I2C commbus busy flag   iTimeinSaturation = 0;	//reset saturation flag   iTimerThreshold = 0;   iTimerThresholdCummulative = 0;    // initialized bufferdata to look like mag - static info   bufr_data.xsum = 0;   bufr_data.ysum = 500;   avgMagX = 511;   avgMagY = 512;  call MagControl.init();  isRunning = FALSE;  maxMagX = 0;  maxMagY = 0;  if (call AttrMagX.registerAttr("mag_x", UINT16, 2) != SUCCESS)  	return FAIL;  if (call AttrMagY.registerAttr("mag_y", UINT16, 2) != SUCCESS)  	return FAIL;  dbg(DBG_BOOT, ("MAGSZ is initialized.\n"));  return SUCCESS;}command result_t StdControl.start(){	return SUCCESS;}void startMag(){  // Turn on magnetometer  //SET_MAG_CTL_PIN();	call MagControl.start();   isRunning = TRUE;   InitSampling();   DAQMode = DM_STARTUP;	  //wait for system to settle down   iTimerThreshold = STARTUP_HOLDOFF;   call Timer.start(TIMER_REPEAT, 32);}void stopMag(){  call Timer.stop();  isRunning = FALSE;}command result_t StdControl.stop() {  stopMag();  return SUCCESS;}//*****************************************************************************//  CLOCK_EVENT// - clock triggered// - check if sampling enabled// - start ADC for  Magnetomer A//*****************************************************************************event result_t Timer.fired(){    if ( DAQMode == DM_STARTUP ) {	    //idle until startup of radio etc has finished    iTimerThreshold--;    if (!iTimerThreshold)       DAQMode = DM_AUTOZERO_START;		// do an autozero  }    if( iTimeinSaturation>RE_ZERO_COUNT && (DAQMode==DM_NORMAL) ){      DAQMode = DM_AUTOZERO_START;	  //start autozero operation     iTimeinSaturation = 0;  }    // if (call MagX.getData() == FAIL)  	// call SounderControl.start();  // return SUCCESS;  return call MagX.getData(); //start ADC for X -maps to Event-2  }//*****************************************************************************// MAGSZ_DATA_EVENT_2// - x axis ADC data ready// - start ADC for magnetometer B//*****************************************************************************event result_t MagX.dataReady(uint16_t data){   bufr_data.adata[ bufr_data.index].x_val = data;   if (maxMagX < data)       maxMagX = data;   return call  MagY.getData(); //get data for MagnetometerB}//*****************************************************************************// MAGSZ_DATA_EVENT_3// - y axis ADC data ready// - place data into next available buffer location//*****************************************************************************event result_t MagY.dataReady(uint16_t data){  bufr_data.adata[ bufr_data.index].y_val = data;   if (maxMagY < data)       maxMagY = data;  post FILTER_DATA();  return SUCCESS;  }//*****************************************************************************// Magnetometer Offset support services// - Acknowledgement of write pot operation//*****************************************************************************event result_t MagSetting.gainAdjustXDone(bool success) {  // Offset has been updated, clear busy flag    if(  I2CBusy==I2C_POTX) { //update MAG Y POT     I2CBusy = I2C_POTY;     call MagSetting.gainAdjustY( cMagYOffset);  }   return SUCCESS;}event result_t MagSetting.gainAdjustYDone(bool success) {   I2CBusy = I2C_IDLE;    if(  DAQMode == DM_AUTOZERO)	//Autozeroing so force a new ADC baseline acq     bufr_data.minCount = 0;  //force a new 4-sample average  return SUCCESS;}/***************************************************************************** * InitSampling * - sets frame parameters and led associated with sampling and triggering back  *   to their initial values *****************************************************************************/void InitSampling(){   int i;   avgMagX = 0;   avgMagY = 0;   baselineCount = 0;   //Init buffer contents   bufr_data.index = 0;   bufr_data.minCount = 0;   bufr_data.xsum = 0;   bufr_data.ysum = 0;   bufr_data.trgHoldoffCount = 0;   for(i=0;i<BUFR_WSAMPLES;i++)   {     bufr_data.adata[i].x_val = 0;     bufr_data.adata[i].y_val = 0;   }}event result_t AttrMagX.startAttr(){	if (!isRunning)		startMag();	return call AttrMagX.startAttrDone();}event result_t AttrMagX.getAttr(char *name, char *resultBuf, SchemaErrorNo *errorNo){	*errorNo = SCHEMA_RESULT_READY;	*(uint16_t*)resultBuf = maxMagX;	maxMagX = 0;	return SUCCESS;}event result_t AttrMagX.setAttr(char *name, char *attrVal){	return FAIL;}event result_t AttrMagY.startAttr(){	if (!isRunning)		startMag();	return call AttrMagY.startAttrDone();}event result_t AttrMagY.getAttr(char *name, char *resultBuf, SchemaErrorNo *errorNo){	*errorNo = SCHEMA_RESULT_READY;	*(uint16_t*)resultBuf = maxMagY;	maxMagY = 0;	return SUCCESS;}event result_t AttrMagY.setAttr(char *name, char *attrVal){	return FAIL;}} // end of implemnetation

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -