//Esta função só é chamada quando houve algum botao pressionado unsigned int decodeButtons(unsigned int bitmap) { if(bitmap != 0) //botao pressionado { //Ler novamente para ter a certeza do valor //Pois é muito rapido para se consguir ver o pressionar de 2 botoes delay(10); unsigned int actualState1 = Button_Read(); delay(10); unsigned int actualState2 = Button_Read(); bitmap |= actualState1 | actualState2; //Long Pressed if((bitmap & BUTTON_DOWN) && (bitmap & BUTTON_UP)) { if(lastStateButton == 0){ delayButton = SYSCLK_GetValue(); lastStateButton = bitmap; return SHOW; } if(SYSCLK_Elapsed(delayButton)>=MAX_PRESSED_BUTTON){ lastStateButton = 0; delayButton = 0; return CHANGE_HOURS; } return SHOW; }else { lastStateButton = 0; delayButton = 0; } //Short press in radio if(bitmap & (BUTTON_DOWN | BUTTON_UP | BUTTON_MEN)) return CHANGE_RADIO; }else{ delayButton = 0; lastStateButton = 0; } return SHOW; }
void changeHours(struct tm *dt) { unsigned int buttonsState; unsigned int nextState; LCD_Clear(); LCD_Goto(0,1); LCD_WriteString(str1); while(nextState != SET_HOURS) { buttonsState = Button_Read(); nextState = decodeButtons_inHours(buttonsState); //Actualizar o LCD para os novos valores das horas LCD_Goto(0,0); strftime(buffer,16,"%T",dt); LCD_WriteString(buffer); switch(nextState) { case INC_HOURS: LCD_Goto(0,1); incrementHours(dt); break; case DEC_HOURS: LCD_Goto(0,1); decrementHours(dt); break; case INC_MINUTES: incrementMinutes(dt); break; case DEC_MINUTES: decrementMinutes(dt); break; case CHANGE_FIELD: actualPosition = (actualPosition + 1)%LEN_ENUM; break; default: break; } } RTC_SetTime(dt); RTC_SetDate(dt); RTC_SetDays(dt); LCD_Clear(); }
void changeRadio(TEA5767 *rad) { unsigned int buttonsState; unsigned int nextState; while(nextState != OUT_RADIO) { buttonsState = Button_Read(); nextState = decodeButtons_inRadio(buttonsState); switch(nextState) { case PREV_STATION: searchStation(SEARCH_DOWN, rad); break; case NEXT_STATION: searchStation(SEARCH_UP, rad); break; case SCAN_DOWN: setLcdScan(); scanStation(SEARCH_DOWN, rad); break; case SCAN_UP: setLcdScan(); scanStation(SEARCH_UP, rad); break; case SAVE_STATION: break; case MUTE: RADIO_Mute(~mute); mute = ~mute; WriteData(); break; default: break; } } }
int main() { CyDelay(200); uint16 Counts=0; // ADC value (0 to 4095) right shifted by 6 which gives // us 0 to 63 to be used to simulate actual temperature uint16 TempSet = 2400; // Temperature set default value (left justified) 24 deg uint16 DisplayTemp = 0; // The combined sum of desired temp and actual temp uint16 bleTemp = 0; // Temperature sent to BLE module uint16 bleTempSet = 0; // Temperature set value sent to BLE module uint8 button0 = 0; // Declare CapSense button name button0 uint8 button1 = 0; // Declare CapSense button name button1 uint8 firstpress0 = 0; // Detects a transition of button1 from 0 to 1 uint8 firstpress1 = 0; // Detects a transition of button1 from 0 to 1 int buttonPrevious = 1; CyGlobalIntEnable; ADC_Start(); // Starts the ADC component ADC_StartConvert(); // The ADC conversion process begins LCD_Start(); // Start the LCD component CapSense_Start(); CapSense_ScanAllWidgets(); LCD_WritePixel(LCD_COLON, TRUE); ResetTimer_Start(); sendBootload_StartEx(StartBootload_ISR); BLEIOT_Start(); /* Initialize temperuature values out of range so that main loop update is triggered */ BLEIOT_updateTemperature(10000); BLEIOT_updatePot(100); for(;;) { /* Turn BLE on/off with button press */ if(buttonPrevious && (Button_Read() == 0)) { if(BLEIOT_remote.bleState == BLEIOT_BLEOFF) { BLEIOT_updateBleState(BLEIOT_BLEON); } else { BLEIOT_updateBleState(BLEIOT_BLEOFF); } } buttonPrevious = Button_Read(); /* Local Thermostat Operation */ /* ADC */ // Read the ADC, shift right by 6 (i.e. divide by 64) // and store result in Counts Counts = ADC_GetResult16(POT_CHAN); Counts = Counts >> 6; /* CapSense */ if (!CapSense_IsBusy()) { // Check Button states and store CapSense_ProcessAllWidgets(); if(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) { button0 = 1; } else { button0 = 0; } if(CapSense_IsWidgetActive(CapSense_BUTTON1_WDGT_ID)) { button1 = 1; } else { button1 = 0; } // Light LEDs Based on Capsense buttons LED_CS0_Write(~button0); LED_CS1_Write(~button1); // Check for button touchdown transitions if (button0 == 1) { if(firstpress0 == 0) // Touchdown event { firstpress0 = 1; // Remember button0 was pressed TempSet = TempSet + 100; // Increment Temp by 1 deg } } else { firstpress0 = 0; // Button released } if (button1 == 1) { if(firstpress1 == 0) // Touchdown event { firstpress1 = 1; // Remember button0 was pressed TempSet = TempSet - 100; // Decrement Temp by 1 deg } } else { firstpress1 = 0; // Button released } CapSense_ScanAllWidgets(); // Start Next Scan } /* Warning LEDs and Buzzer */ if((Counts * 100) < TempSet) // Temperature Cold { LED_Blue_Write(LED_ON); // Blue On LED_Green_Write(LED_OFF); // Green Off LED_Red_Write(LED_OFF); // Red Off PWM_Stop(); // Buzzer Off } else if ((Counts * 100) <= (TempSet + 500)) // Temperature OK { LED_Blue_Write(LED_OFF); // Blue Off LED_Green_Write(LED_ON); // Green On LED_Red_Write(LED_OFF); // Red Off PWM_Stop();// Buzzer Off } else // Tempearture too high { LED_Blue_Write(LED_OFF); // Blue Off LED_Green_Write(LED_OFF); // Green Off LED_Red_Write(LED_ON); // Red On PWM_Start(); // Buzzer On } /* LCD Display */ DisplayTemp = TempSet + Counts; LCD_Write7SegNumber_0(DisplayTemp, POS, MODE); /* BLE operation - do only if BLE is not off */ if(BLEIOT_remote.bleState != BLEIOT_BLEOFF) { /* Send new temperature data to the BLE module */ if(bleTemp != Counts) { bleTemp = Counts; BLEIOT_updatePot(bleTemp); } if(bleTempSet != TempSet) { bleTempSet = TempSet; /* Scale set temperature down to whole number of degrees */ BLEIOT_updateTemperature(TempSet / 100); } /* Get new data from the BLE module */ /* LED0 is used for temperature changes */ if(BLEIOT_getDirtyFlags() & BLEIOT_FLAG_LED0) { /* Update local variable copy and clear dirty flag */ BLEIOT_updateLed0(BLEIOT_remote.led0); if(BLEIOT_local.led0 == UP) { TempSet = TempSet + 100; // Increment Temp by 1 deg } else if (BLEIOT_local.led0 == DOWN) { TempSet = TempSet - 100; // Decrement Temp by 1 deg } } } /* End of !BLEOFF state operations */ } /* End of superloop */ } /* End of main */
int main() { unsigned int nextState; unsigned int actualStateButton = 0; //Define a a data e hora que o sistema vai ter inicialmente //RTC_SetTime(00,00,00); ti.tm_hour = 00; ti.tm_min = 00; ti.tm_sec = 00; //RTC_SetDate(8,11,2013); ti.tm_mday = 8; ti.tm_mon = 11; ti.tm_year = 2013; //RTC_SetDays(5 ,365); ti.tm_wday = 5; ti.tm_yday = 365; //System init SYSCLK_Init(1000); /* Acertar o clock do TIMER*/ RTC_Init(&ti); /* Iniciar o RTC com a data e hora definida inifialmente*/ LCD_Init(); LCD_Clear(); I2C_Init(); RADIO_Init(); RADIO_SetFreq(89.9); WriteData(); while(1) { actualStateButton = Button_Read(); nextState = decodeButtons(actualStateButton); switch(nextState) { case CHANGE_HOURS: changeHours(&ti); break; case CHANGE_RADIO: changeRadio(&tea); LCD_Clear(); break; //Tambem devia de escrever a freq do radio case SHOW: RTC_GetValue(&ti); LCD_Goto(4,0); strftime(buffer,16,"%T",&ti); LCD_WriteString(buffer); ReadData(&tea); float fm = RADIO_GetFreq(&tea); int level = RADIO_Station_Level(&tea); int parteDecimal = (int)(fm*10)%10; sprintf(buffer, "%d.%dMHz L:%d",(int)fm,parteDecimal, level); LCD_Goto(0,1); LCD_WriteString(buffer); break; default: break; } } return 0; }