// Display the menu on screen void ShowMenu() { // Show menu item on top row Clear(); Cursor(TOP, 0); Print(parameters[menuIndex]->Name); Print(" ", parameters[menuIndex]->Value); // Show sensor info on bottom row. Useful for threshold calibration Cursor(BOTTOM, 0); Print(" ", analogRead(LEFT_SENSOR)); Print(" ", analogRead(RIGHT_SENSOR)); Print(" ", analogRead(CENTER_SENSOR)); Print(" "); lcd.print(float(lapTime)/10.0); Print("s"); switch(ReadButton()) { case UP: // Increase item value holdCounter = (previousButton == UP) ? (holdCounter + 1) : 0; previousButton = UP; parameters[menuIndex]->Value += 1 + (holdCounter / 20); break; case DOWN: // Lower item value holdCounter = (previousButton == DOWN) ? (holdCounter + 1) : 0; previousButton = DOWN; parameters[menuIndex]->Value -= (1 + (holdCounter / 20)); break; case LEFT: // Next menu item menuIndex = (menuIndex > 0) ? (menuIndex - 1) : (PARAMETER_COUNT - 1); break; case RIGHT: // Previous menu item menuIndex = (menuIndex < PARAMETER_COUNT - 1) ? (menuIndex + 1) : 0; break; case SELECT:// Exit menu delay(500); if (ReadButton() == SELECT) { Clear(); Cursor(TOP, 0); Print("Exiting menu"); SaveToEEPROM(); // Save values to EEPROM before exiting delay(750); currentState = moveStraight; intersectionTurnFlag = 0; lapTime = 0; batteryCount = 1; Clear(); return; } break; } delay(150); }
void ModeSelect::StartModeSelect(void) { mode_new = 0; mode_act = 0; blinkcnt = 0; timecnt = 0; EELIB_Command command; EELIB_Result result; uint8_t buf8; button_state = ReadButton(); button_debounce = 0; command[0] = EELIB_IAP_COMMAND_EEPROM_READ; command[1] = 0; // Eeprom address command[2] = (uint32_t) &buf8; command[3] = 1; // Read length command[4] = SystemCoreClock / 1000; EELIB_entry(command, result); if (result[0] != EELIB_IAP_STATUS_CMD_SUCCESS) { buf8 = 0; } if (buf8 >= MODENUM) buf8 = 0; mode_new = buf8; mode_act = buf8; SetLeds(); }
/******************************************************************************* * Function Name : TIM2_IRQHandler * Description : This function handles TIM2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void TIM2_IRQHandler(void) { static byte b1Sec=0; if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) // 120us, 8000Hz { TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); ISR_ADC(); TIM_SetCounter(TIM2, 0); TIM_SetCompare4(TIM2, CCR4_Val); if( !( gwCounter1 & 7 ) ) // 840us { ISR_1ms_TIMER(); } if( !( gwCounter1 & 3 ) ) // 480us, 2000Hz { ISR_LED_RGB_TIMER(); } if( !( gwCounter1 & 31 ) ) // 3840us, 250Hz { ISR_SPI_READ(); __ISR_Buzzer_Manage(); GB_BUTTON = ReadButton(); } if( !( gwCounter1 & 0x3FF ) ) // 125ms { LED_SetState(LED_RX,OFF); LED_SetState(LED_TX,OFF); if( !(b1Sec&0x07) ) { ISR_BATTERY_CHECK(); } b1Sec++; } /* if( !( Counter1 & 32 ) ) // 3960us, 250Hz { } */ gwCounter1++; } }
// Updates the sensors and machine state void Update() { if ((lcdRefreshCount == 0) && (currentState != menu) && (ReadButton() == SELECT)) { delay(750); if(ReadButton() == SELECT) // debounce MENU button { // Stop motors before entering menu MotorSpeed(LEFT_MOTOR, 0); MotorSpeed(RIGHT_MOTOR, 0); Clear(); Cursor(TOP, 0); Print("Entering menu"); currentState = menu; delay(1000); lapTimer.disable(0); return; } } // Read raw sensor values left = analogRead(LEFT_SENSOR); right = analogRead(RIGHT_SENSOR); leftDetected = left > thresholdLeft.Value; rightDetected = right > thresholdRight.Value; if (leftDetected || rightDetected) { CenterSmartUpdate(); } // Display values on LCD lcdRefreshCount = (lcdRefreshCount <= 0) ? lcdRefreshPeriod : (lcdRefreshCount - 1); batteryCount = (batteryCount <= 0) ? batteryPeriod : (batteryCount - 1); DisplaySensorValues(); }
int main(void) { DDRD &= ~BUTTON_PIN; // Configure PORTD pin 2 as input PORTD |= BUTTON_PIN; // Activate pull-ups in PORTD pin 2 DDRB |= LED_PIN; // Configure arduino pin 13 (led) to output while (1) { if (ReadButton()) { PORTB |= LED_PIN; // toggle the led } else { PORTB &= ~LED_PIN; } _delay_ms(250); } }
bool ModeSelect::ButtonDebounce(void) { bool act_button = ReadButton(); bool btndnevent = false; if (button_state) { if (act_button) { button_debounce = 0; } else { if (button_debounce >= BUTTONUPDELAY) { // Beim Loslassen der Taste keine weiteren Aktionen button_state = false; button_debounce = 0; } else { button_debounce++; } } } else { if (act_button) { if (button_debounce >= BUTTONDOWNDELAY) { // Flag setzen btndnevent = true; button_state = true; button_debounce = 0; } else { button_debounce++; } } else { button_debounce = 0; } } return btndnevent; }
// ---------------------------------------------------------------------------- // skin retrieval helper // ---------------------------------------------------------------------------- bool CSkin::GetSkinData(const char *skinFile) { // ------------------------------------------------- // retrieve the skin bitmap from resource. // ------------------------------------------------- char buffer[2048]; if(!GetPrivateProfileString("skin", "image", "", buffer, 2048, skinFile)) { m_error = "Missing skin bitmap"; return false; } CString bmpName = buffer; CString rgn = ""; if(GetPrivateProfileString("skin", "region", "", buffer, 2048, skinFile)) { rgn = buffer; } if(!GetPrivateProfileString("skin", "draw", "", buffer, 2048, skinFile)) { m_error = "Missing draw rectangle"; return false; } if(!ParseRect(buffer, m_rect)) { m_error = "Invalid draw rectangle"; return false; } m_nButtons = GetPrivateProfileInt("skin", "buttons", 0, skinFile); if(m_nButtons) { m_buttons = new SkinButton[m_nButtons]; for(int i = 0; i < m_nButtons; i++) { if(!ReadButton(skinFile, i)) return false; } } CString path = skinFile; int index = path.ReverseFind('\\'); if(index != -1) { path = path.Left(index+1); } bmpName = path + bmpName; if(strcmp(rgn, "")) rgn = path + rgn; m_hBmp = LoadImage(bmpName); if (!m_hBmp) { m_error = "Error loading skin bitmap " + bmpName; return false; } // get skin info BITMAP bmp; GetObject(m_hBmp, sizeof(bmp), &bmp); // get skin dimensions m_iWidth = bmp.bmWidth; m_iHeight = bmp.bmHeight; if(strcmp(rgn, "")) { m_rgnSkin = LoadRegion(rgn); if(m_rgnSkin == NULL) { m_error = "Error loading skin region " + rgn; return false; } } // ------------------------------------------------- // well, things are looking good... // as a quick providence, just create and keep // a device context for our later blittings. // ------------------------------------------------- // create a context compatible with the user desktop m_dcSkin = CreateCompatibleDC(0); if (!m_dcSkin) return false; // select our bitmap m_hOldBmp = (HBITMAP)SelectObject(m_dcSkin, m_hBmp); // ------------------------------------------------- // done // ------------------------------------------------- return true; }
/* Called from: PAD_GetStatus() Input: The virtual device 0, 1, 2 or 3 Function: Updates the PadState struct with the current pad status. The input value "controller" is for a virtual controller 0 to 3. */ void GetJoyState(CONTROLLER_STATE &_PadState, CONTROLLER_MAPPING _PadMapping, int Controller, int NumButtons) { // Update the gamepad status SDL_JoystickUpdate(); // Update axis states. It doesn't hurt much if we happen to ask for nonexisting axises here. _PadState.axis[CTL_MAIN_X] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.axis[CTL_MAIN_X]); _PadState.axis[CTL_MAIN_Y] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.axis[CTL_MAIN_Y]); _PadState.axis[CTL_SUB_X] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.axis[CTL_SUB_X]); _PadState.axis[CTL_SUB_Y] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.axis[CTL_SUB_Y]); // Update the analog trigger axis values #ifdef _WIN32 if (_PadMapping.triggertype == CTL_TRIGGER_SDL) { #endif // If we are using SDL analog triggers the buttons have to be mapped as 1000 or up, otherwise they are not used if(_PadMapping.buttons[CTL_L_SHOULDER] >= 1000) _PadState.axis[CTL_L_SHOULDER] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.buttons[CTL_L_SHOULDER] - 1000); else _PadState.axis[CTL_L_SHOULDER] = 0; if(_PadMapping.buttons[CTL_R_SHOULDER] >= 1000) _PadState.axis[CTL_R_SHOULDER] = SDL_JoystickGetAxis(_PadState.joy, _PadMapping.buttons[CTL_R_SHOULDER] - 1000); else _PadState.axis[CTL_R_SHOULDER] = 0; #ifdef _WIN32 } else { // XInput triggers for Xbox360 pads _PadState.axis[CTL_L_SHOULDER] = XInput::GetXI(0, _PadMapping.buttons[CTL_L_SHOULDER] - 1000); _PadState.axis[CTL_R_SHOULDER] = XInput::GetXI(0, _PadMapping.buttons[CTL_R_SHOULDER] - 1000); } #endif // Update button states to on or off ReadButton(_PadState, _PadMapping, CTL_L_SHOULDER, NumButtons); ReadButton(_PadState, _PadMapping, CTL_R_SHOULDER, NumButtons); ReadButton(_PadState, _PadMapping, CTL_A_BUTTON, NumButtons); ReadButton(_PadState, _PadMapping, CTL_B_BUTTON, NumButtons); ReadButton(_PadState, _PadMapping, CTL_X_BUTTON, NumButtons); ReadButton(_PadState, _PadMapping, CTL_Y_BUTTON, NumButtons); ReadButton(_PadState, _PadMapping, CTL_Z_TRIGGER, NumButtons); ReadButton(_PadState, _PadMapping, CTL_START, NumButtons); // Update Halfpress state, this one is not in the standard _PadState.buttons array if (_PadMapping.halfpress < NumButtons && _PadMapping.halfpress >= 0) _PadState.halfpress = SDL_JoystickGetButton(_PadState.joy, _PadMapping.halfpress); else _PadState.halfpress = 0; // Check if we have an analog or digital joypad if (_PadMapping.controllertype == CTL_DPAD_HAT) { _PadState.dpad = SDL_JoystickGetHat(_PadState.joy, _PadMapping.dpad); } else { // Only do this if the assigned button is in range (to allow for the current way of saving keyboard // keys in the same array) if(_PadMapping.dpad2[CTL_D_PAD_UP] <= NumButtons) _PadState.dpad2[CTL_D_PAD_UP] = SDL_JoystickGetButton(_PadState.joy, _PadMapping.dpad2[CTL_D_PAD_UP]); if(_PadMapping.dpad2[CTL_D_PAD_DOWN] <= NumButtons) _PadState.dpad2[CTL_D_PAD_DOWN] = SDL_JoystickGetButton(_PadState.joy, _PadMapping.dpad2[CTL_D_PAD_DOWN]); if(_PadMapping.dpad2[CTL_D_PAD_LEFT] <= NumButtons) _PadState.dpad2[CTL_D_PAD_LEFT] = SDL_JoystickGetButton(_PadState.joy, _PadMapping.dpad2[CTL_D_PAD_LEFT]); if(_PadMapping.dpad2[CTL_D_PAD_RIGHT] <= NumButtons) _PadState.dpad2[CTL_D_PAD_RIGHT] = SDL_JoystickGetButton(_PadState.joy, _PadMapping.dpad2[CTL_D_PAD_RIGHT]); } #ifdef SHOW_PAD_STATUS // Show the status of all connected pads //ConsoleListener* Console = LogManager::GetInstance()->getConsoleListener(); //if ((g_LastPad == 0 && Controller == 0) || Controller < g_LastPad) Console->ClearScreen(); g_LastPad = Controller; NOTICE_LOG(CONSOLE, "Pad | Number:%i Enabled:%i Handle:%i\n" "Main Stick | X:%03i Y:%03i\n" "C Stick | X:%03i Y:%03i\n" "Trigger | Type:%s DigitalL:%i DigitalR:%i AnalogL:%03i AnalogR:%03i HalfPress:%i\n" "Buttons | A:%i X:%i\n" "D-Pad | Type:%s Hat:%i U:%i D:%i\n" "======================================================\n", Controller, _PadMapping.enabled, _PadState.joy, _PadState.axis[InputCommon::CTL_MAIN_X], _PadState.axis[InputCommon::CTL_MAIN_Y], _PadState.axis[InputCommon::CTL_SUB_X], _PadState.axis[InputCommon::CTL_SUB_Y], (_PadMapping.triggertype ? "CTL_TRIGGER_XINPUT" : "CTL_TRIGGER_SDL"), _PadState.buttons[InputCommon::CTL_L_SHOULDER], _PadState.buttons[InputCommon::CTL_R_SHOULDER], _PadState.axis[InputCommon::CTL_L_SHOULDER], _PadState.axis[InputCommon::CTL_R_SHOULDER], _PadState.halfpress, _PadState.buttons[InputCommon::CTL_A_BUTTON], _PadState.buttons[InputCommon::CTL_X_BUTTON], (_PadMapping.controllertype ? "CTL_DPAD_CUSTOM" : "CTL_DPAD_HAT"), _PadState.dpad, _PadState.dpad2[InputCommon::CTL_D_PAD_UP], _PadState.dpad2[InputCommon::CTL_D_PAD_DOWN] ); #endif }