static void CheckButton(void) { uint32_t timeTicks; /* time in ticks */ #define BUTTON_CNT_MS 100 /* iteration count for button */ bool autoCalibrate = FALSE; if (SW1_GetVal()==0) { /* button pressed */ /* short press (1 beep): start or stop line following if calibrated * 1 s press (2 beep): calibrate manually * 2 s press (3 beep): calibrate with auto-move * 3 s press (4 beep or more): clear path * */ FRTOS1_vTaskDelay(50/portTICK_RATE_MS); /* simple debounce */ if (SW1_GetVal()==0) { /* still pressed */ LEDG_On(); timeTicks = 0; while(SW1_GetVal()==0 && timeTicks<=6000/BUTTON_CNT_MS) { FRTOS1_vTaskDelay(BUTTON_CNT_MS/portTICK_RATE_MS); if ((timeTicks%(1000/BUTTON_CNT_MS))==0) { #if PL_HAS_BUZZER BUZ_Beep(300, 200); #endif } timeTicks++; } /* wait until released */ autoCalibrate = FALSE; if (timeTicks<1000/BUTTON_CNT_MS) { /* less than 1 second */ CLS1_SendStr((unsigned char*)"button press.\r\n", CLS1_GetStdio()->stdOut); StateMachine(TRUE); /* <1 s, short button press, according to state machine */ } else if (timeTicks>=(1000/BUTTON_CNT_MS) && timeTicks<(2000/BUTTON_CNT_MS)) { CLS1_SendStr((unsigned char*)"calibrate.\r\n", CLS1_GetStdio()->stdOut); APP_StateStartCalibrate(); /* 1-2 s: start calibration by hand */ } else if (timeTicks>=(2000/BUTTON_CNT_MS) && timeTicks<(3000/BUTTON_CNT_MS)) { CLS1_SendStr((unsigned char*)"auto calibrate.\r\n", CLS1_GetStdio()->stdOut); APP_StateStartCalibrate(); /* 2-3 s: start auto calibration */ autoCalibrate = TRUE; } else if (timeTicks>=(3000/BUTTON_CNT_MS)) { CLS1_SendStr((unsigned char*)"delete solution.\r\n", CLS1_GetStdio()->stdOut); MAZE_ClearSolution(); } while (SW1_GetVal()==0) { /* wait until button is released */ FRTOS1_vTaskDelay(BUTTON_CNT_MS/portTICK_RATE_MS); } if (autoCalibrate) { CLS1_SendStr((unsigned char*)"start auto-calibration...\r\n", CLS1_GetStdio()->stdOut); /* perform automatic calibration */ WAIT1_WaitOSms(1500); /* wait some time */ TURN_Turn(TURN_LEFT90); TURN_Turn(TURN_RIGHT90); TURN_Turn(TURN_RIGHT90); TURN_Turn(TURN_LEFT90); TURN_Turn(TURN_STOP); APP_StateStopCalibrate(); CLS1_SendStr((unsigned char*)"auto-calibration finished.\r\n", CLS1_GetStdio()->stdOut); } } } /* if */ }
static byte PrintStatus(CLS1_ConstStdIOType *io) { uint8_t buf[48]; CLS1_SendStatusStr((unsigned char*)"Hoval", (const unsigned char*)"\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Scheduler", mySchedule.isSchedulerOn ? (const unsigned char*)"on\r\n":(const unsigned char*)"off\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Switch", SW1_GetVal()==0 ? (const unsigned char*)"ON\r\n":(const unsigned char*)"OFF\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Hoval", isHovalManualMode ? (const unsigned char*)"manual mode on\r\n":(const unsigned char*)"manual mode off\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Pump", isPumpOn ? (const unsigned char*)"on\r\n":(const unsigned char*)"off\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Relay 1", isRelay1on ? (const unsigned char*)"on (button)\r\n":(const unsigned char*)"off (pump)\r\n", io->stdOut); CLS1_SendStatusStr((unsigned char*)" Relay 2", isRelay2on ? (const unsigned char*)"on (pump)\r\n":(const unsigned char*)"off (manual mode)\r\n", io->stdOut); buf[0]='\0'; #if PL_HAS_DATE_SUPPORT StrCatDate(buf, sizeof(buf), mySchedule.on.date.day, mySchedule.on.date.month, mySchedule.on.date.year); UTIL1_strcat(buf, sizeof(buf), (unsigned char*)", "); #endif StrCatTime(buf, sizeof(buf), mySchedule.on.time.hour, mySchedule.on.time.min, mySchedule.on.time.sec); UTIL1_chcat(buf, sizeof(buf), '-'); #if PL_HAS_DATE_SUPPORT StrCatDate(buf, sizeof(buf), mySchedule.off.date.day, mySchedule.off.date.month, mySchedule.off.date.year); UTIL1_strcat(buf, sizeof(buf), (unsigned char*)", "); #endif StrCatTime(buf, sizeof(buf), mySchedule.off.time.hour, mySchedule.off.time.min, mySchedule.off.time.sec); UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r\n"); CLS1_SendStatusStr((unsigned char*)" time on", (unsigned char*)buf, io->stdOut); return ERR_OK; }
/*lint -save -e970 Disable MISRA rule (6.3) checking. */ int main(void) /*lint -restore Enable MISRA rule (6.3) checking. */ { /* Write your local variable definition here */ /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); /*** End of Processor Expert internal initialization. ***/ #if OPTIMIZE_BIT_ACCESS GREEN_Clr(); /* turn on green LED */ WAIT1_Waitms(1000); GREEN_Set(); /* turn off green LED */ for(;;) { if (SW1_Get()==0) { /* button low level => pressed */ RED_Clr(); /* LED cathode is connected to microcontroller pin: low level turns it on */ BLUE_Set(); /* turn off blue led */ } else { RED_Set(); /* turn off red led */ BLUE_Clr(); /* turn on blue led */ } if (SW2_Get()==0) { /* switch low level => pressed */ BLUE_Clr(); /* turn on blue led */ } else { BLUE_Set(); /* turn off blue led */ } } #else GREEN_ClrVal(GREEN_DeviceData); /* turn on green LED */ WAIT1_Waitms(1000); GREEN_SetVal(GREEN_DeviceData); /* turn off green LED */ for(;;) { if (SW1_GetVal(SW1_DeviceData)==0) { /* button low level => pressed */ RED_ClrVal(RED_DeviceData); /* LED cathode is connected to microcontroller pin: low level turns it on */ BLUE_SetVal(BLUE_DeviceData); /* turn off blue led */ } else { RED_SetVal(RED_DeviceData); /* turn off red led */ BLUE_ClrVal(BLUE_DeviceData); /* turn on blue led */ } if (SW2_GetVal(SW2_DeviceData)==0) { /* switch low level => pressed */ BLUE_ClrVal(BLUE_DeviceData); /* turn on blue led */ } else { BLUE_SetVal(BLUE_DeviceData); /* turn off blue led */ } } #endif /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ #ifdef PEX_RTOS_START PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of RTOS startup code. ***/ /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ for(;;){} /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
static void CheckKey(void) { uint16_t cnt; uint8_t data; if (SW1_GetVal()==0) { /* low active */ WAIT1_WaitOSms(50); /* simple debounce */ if (SW1_GetVal()==0) { /* still pressed? */ cnt = 0; while(SW1_GetVal()==0) { /* wait until released */ WAIT1_WaitOSms(10); cnt += 10; } if (cnt>1000) { /* long press */ data = 2; /* toggle LED2 */ } else { data = 1; /* toggle LED1 */ } (void)RAPP_SendPayloadDataBlock(&data, sizeof(data), RAPP_MSG_TYPE_DATA, RNWK_ADDR_BROADCAST, RPHY_PACKET_FLAGS_NONE); } } }
static portTASK_FUNCTION(MainTask, pvParameters) { unsigned char lcdBuf[sizeof("1234")]; uint16_t cntr; (void)pvParameters; /* parameter not used */ TRACE_Init(); MMA1_Init(); /* enable accelerometer, if not already enabled */ MAG1_Enable(); /* enable magnetometer */ SHELL_Init(); cntr = 0; for(;;) { UTIL1_Num16uToStrFormatted(lcdBuf, sizeof(lcdBuf), cntr, '0', 4); vfnLCD_Write_Msg(lcdBuf); cntr++; if (cntr>9999) { /* can only display 4 digits */ cntr = 0; } #if APP_USE_KEY_COMPONENT KEY1_ScanKeys(); /* using component in polling mode: poll keys, this will create events as needed. */ EVNT1_HandleEvent(); #else if (SW1_GetVal()==0) { /* button pressed */ FRTOS1_vTaskDelay(50/portTICK_RATE_MS); /* wait to debounce */ while (SW1_GetVal()==0) { /* still pressed? */ LED1_On(); } } if (SW3_GetVal()==0) { /* button pressed */ FRTOS1_vTaskDelay(50/portTICK_RATE_MS); /* wait to debounce */ while (SW3_GetVal()==0) { /* still pressed? */ LED2_On(); } } #endif LED1_Neg(); FRTOS1_vTaskDelay(50/portTICK_RATE_MS); } }
void APP_Run(void) { int cnt=0; /* counter to slow down LED blinking */ for(;;) { WAIT1_Waitms(10); cnt++; if (SW1_GetVal()==0) { /* button pressed */ WAIT1_Waitms(100); /* wait for debouncing */ if (SW1_GetVal()==0) { /* still pressed */ #if 1 /* send a string */ //(void)HIDK1_SendStr((unsigned char*)"SW1 press "); /* send a string */ #endif #if 0 /* send print screen */ //HIDK1_Send(MODIFERKEYS_NONE, KEY_PRINTSCREEN); //HIDK1_Send(MODIFERKEYS_NONE, KEY_NONE); /* release key */ #endif #if 0 /* send CTRL+ALT+DELETE */ HIDK1_Send(MODIFERKEYS_LEFT_CTRL|MODIFERKEYS_RIGHT_ALT, KEY_DELETE); HIDK1_Send(MODIFERKEYS_NONE, KEY_NONE); /* release key */ #endif } while(SW1_GetVal()==0) {} /* wait until button is released */ } if (HIDK1_App_Task()==ERR_OK) { /* run the USB application task: this will send the buffer */ if ((cnt%100)==0) { LEDR_Off(); LEDG_Neg(); /* blink green LED if connected */ } } else { if ((cnt%200)==0) { LEDG_Off(); LEDR_Neg(); /* blink red LED if not connected */ } } } }
void APP_Run(void) { int cnt=0; /* counter to slow down LED blinking */ uint8_t res; int16_t xyz[3]; res = MMA1_Init(); if (res!=ERR_OK) { Err(); } res = MMA1_SetFastMode(FALSE); if (res!=ERR_OK) { Err(); } res = MMA1_Enable(); /* enable accelerometer */ if (res!=ERR_OK) { Err(); } for(;;) { WAIT1_Waitms(10); /* implement 'air-mouse' movement */ xyz[0] = MMA1_GetX(); xyz[1] = MMA1_GetY(); xyz[3] = MMA1_GetZ(); /* y axis */ if (xyz[1]>AIR_THRESHOLD) { (void)HIDK2_Move(0, 2); /* moving the cursor left */ } else if (xyz[1]<-AIR_THRESHOLD) { (void)HIDK2_Move(0, -2); /* moving the cursor right */ } /* x axis */ if (xyz[0]>AIR_THRESHOLD) { (void)HIDK2_Move(-2, 0); /* moving the cursor up */ } else if (xyz[0]<-AIR_THRESHOLD) { (void)HIDK2_Move(2, 0); /* moving the cursor down */ } cnt++; if (HIDK2_App_Task()==ERR_BUSOFF) { if ((cnt%128)==0) { /* just slow down blinking */ LEDG_Off(); LEDR_Neg(); } } else { if ((cnt%128)==0) { /* just slow down blinking */ LEDR_Off(); LEDG_Neg(); } if (SW1_GetVal()==0) { /* button pressed */ WAIT1_Waitms(100); /* wait for debouncing */ if (SW1_GetVal()==0) { /* still pressed */ //(void)HIDK2_Send(0, 8, 8); /* raw move message */ //(void)HIDK2_Send(HIDK2_MOUSE_LEFT, 0, 0); /* send left mouse button */ //(void)HIDK2_Send(0, 0, 0); /* send release button message */ //(void)HIDK2_Move(-8, 0); /* moving the cursor up */ //HIDK2_Click(HIDK2_MOUSE_LEFT); /* left mouse click */ HIDK2_Click(HIDK2_MOUSE_RIGHT); /* right mouse click */ } while(SW1_GetVal()==0) {} /* wait until button is released */ } } } }
static portTASK_FUNCTION(Hoval, pvParameters) { /* LED status: * Green: 1 Hz Heartbeat * Red: On: pump is on * 1 Hz: Scheduler enabled * Off: no scheduler */ CLS1_ConstStdIOType *io = CLS1_GetStdio(); bool isEnabled = SW1_GetVal()==0; /* initial SW1 status */ uint16_t secondsOn; /* number of seconds turned on */ (void)pvParameters; /* parameter not used */ if (GetRTCData(&mySchedule)!=ERR_OK) { CLS1_SendStr((unsigned char*)"*** Failed getting RTC data\r\n", io->stdErr); for(;;); /* cannot operate like this! */ } isPumpOn = FALSE; for(;;) { /* check mode switch: turns scheduler on/off */ if (!isEnabled && SW1_GetVal()==0) { Log((unsigned char*)"Switch changed to ON position", io); } else if (isEnabled && SW1_GetVal()!=0) { Log((unsigned char*)"Switch changed to OFF position", io); } isEnabled = SW1_GetVal()==0; /* Switch in ON position */ /* check schedule */ if (isEnabled && mySchedule.isSchedulerOn) { if (!isPumpOn) { /* pump is off, check if we need to turn it on */ if (isInRange(&mySchedule.on, &mySchedule.off)) { SetCooling(TRUE, io); } } else { /* pump is on, check if we have to turn it off */ if (!isInRange(&mySchedule.on, &mySchedule.off)) { SetCooling(FALSE, io); } } } if (isPumpOn) { /* check if we need to toggle the button on the Hoval to avoid timeout */ secondsOn++; if (secondsOn>HOVAL_CYCLE_TIME_SECONDS) { Log((unsigned char*)"Toggle manual button to avoid timeout...", io); SetCooling(FALSE, io); FRTOS1_vTaskDelay(5000/portTICK_RATE_MS); /* wait some time until re-enable manual mode again */ SetCooling(TRUE, io); secondsOn = 0; /* restart counter */ } } /* show status LED */ SD_GreenLed_Neg(); /* 1 Hz heartbeat LED */ if (isPumpOn) { /* steady red LED if pump is on for cooling */ SD_RedLed_On(); LEDR_On(); } else if (isEnabled && mySchedule.isSchedulerOn) { /* blinking red LED for enabled scheduler */ SD_RedLed_Put(SD_GreenLed_Get()); /* blink red led in sync with green one */ LEDR_Put(SD_GreenLed_Get()); } else { SD_RedLed_Off(); /* no red LED if not cooling and no schedule */ LEDR_Off(); } FRTOS1_vTaskDelay(1000/portTICK_RATE_MS); } }