void SM_Tick2() { switch(SM_state2) { case Start2: SM_state2=take_input; break; case take_input: SM_state2=take_input; break; default:break; } switch(SM_state2) { case take_input: if(GetKeypadKey()=='\0') key_input='\0'; if(key_input!=0xFE) key_input=GetKeypadKey(); break; default: break; } }
void shotsFired(){ f = GetKeypadKey(); switch(shotsFired_state){ case -1: shotsFired_state = initShots; break; case initShots: if(f == 'B'){ shotsFired_state = fire; enemylife--; } else{ shotsFired_state = initShots; } break; case fire: if(!GetKeypadKey()){ shotsFired_state = cooldown; } else{ shotsFired_state = fire; } break; case cooldown: shotsFired_state = initShots; break; default: initShots; break; } switch(shotsFired_state){ case initShots: PORTA = SetBit(PORTA,7,0); break; case fire: PORTA = SetBit(PORTA,7,1); break; case cooldown: break; default: break; } }
void Tick() { unsigned char key; static unsigned char cursor = 0; // transitions' switch(state) { case start: LCD_Cursor(0); state = display; break; case display: state = display; break; default: state = start; } // state actions switch(state) { case start: break; case display: key = GetKeypadKey(); if(key!='\0'){ cursor = (cursor==16 ? 0 : cursor); LCD_Cursor(++cursor); LCD_WriteData(key); LCD_Cursor(0); } break; } }
void getkey_Tick(){ //Actions switch(getkey_state){ case getkey_INIT: break; case getkey_RUN: keypad_input = GetKeypadKey(); if (keypad_input != '\0') { lcd_output[31]=keypad_input; } break; default: break; } //Transitions switch(getkey_state){ case getkey_INIT: getkey_state = getkey_RUN; break; case getkey_RUN: getkey_state = getkey_RUN; break; default: getkey_state = getkey_INIT; break; } }
/////////////////////////////// KeyPad Controller //////////////////////////////// void GetKeyPress() { switch(KeyState) { case -1: { keyVal = -1; KeyState = Wait; break; } case Wait: { x = GetKeypadKey(); if(x != '\0') { KeyOut(x); KeyState = Pressed; //PORTA = keyVal; } break; } case Pressed: { x = GetKeypadKey(); if(x == '\0') { KeyState = Wait; } break; } default: { keyVal = -1; KeyState = Wait; break; } } }
int ReadKeypadSMtick(int state){ switch(state){ case ReadKeypad_update: // one state keypad update break; default: // initial state state = ReadKeypad_update; break; } switch(state){ case ReadKeypad_update: KeypadInput = GetKeypadKey(); break; default: break; } return state; }
void key_Tick() { unsigned char input = GetKeypadKey(); if(input == 'A') { input = ' '; } else if(input == 'B') { input = '.'; } else if(input == '#') { photoValueL = calibrate(0x02) - 4; photoValueR = calibrate(0x04) - 4; photoValueD = calibrate(0x01) - 4; photoValueU = calibrate(0x00) - 4; photoValueD2 = calibrate(0x06) - 4; photoValueU2 = calibrate(0x05) - 4; } if(input != '\0') { sendDir(input); } }
//State Machine Definitions int tickFct_Keypad(int state) { char key = GetKeypadKey(); switch(state) { case keypad_start: state = keypad_poll; break; case keypad_poll: state = keypad_poll; break; } switch(state) { case keypad_start: break; case keypad_poll: switch(key) { case '\0': break; case '1': if( gameover == OFF ) { toggle_pause(); } else if( gameover == ON ) { restart_game(); } break; case '4': if( gameover == OFF ) { move_player(PLAYER_TOP); } break; case '7': if( gameover == OFF ) { move_player(PLAYER_BOT); } break; default: break; } break; } return state; }
int TickFct_Controller(int state){ unsigned char key = GetKeypadKey(); unsigned char doorClosed = (~PIND & 0x10) >> 4; switch(state){ case controllerinit: clearFlags(); lockedFlag = 1; showPin = 0; state = controllerWait; clearInputPin(); LCD_DisplayString(1, "A for Keypad B for Bluetooth"); break; case controllerWait: if(!doorClosed && lockedFlag ){ state = controllerIntrusionDetected; PORTA = 0xFF; LCD_DisplayString(1, "ALARM! Enter Pin: "); } else if(key == 'A'){ state = controllerKeypad; keypadEnable = 1; clearInputPin(); LCD_DisplayString(1, "Pin: "); } else if(key == 'B'){ state = controllerBluetooth; bluetoothEnable = 1; clearInputPin(); LCD_DisplayString(1, "Waiting to Rec C to Cancel"); } break; case controllerBluetooth: if(!doorClosed && lockedFlag ){ state = controllerIntrusionDetected; PORTA = 0xFF; LCD_DisplayString(1, "ALARM! Enter Pin: "); } else if(key == 'C'){ state = controllerWait; bluetoothEnable = 0; LCD_DisplayString(1, "A for Keypad B for Bluetooth"); clearFlags(); } else if(pinInputComplete){ state = controllerCheck; checkPinFlag = 1; bluetoothEnable = 0; LCD_DisplayString(1, "Checking... C to Cancel"); } else{ state = controllerBluetooth; } break; case controllerKeypad: if(!doorClosed && lockedFlag ){ state = controllerIntrusionDetected; PORTA = 0xFF; LCD_DisplayString(1, "ALARM! Enter Pin: "); } else if(pinInputComplete){ state = controllerCheck; checkPinFlag = 1; keypadEnable = 0; LCD_DisplayString(1, "Checking... C to cancel"); } else{ state = controllerKeypad; } break; case controllerCheck: if(!doorClosed && lockedFlag ){ state = controllerIntrusionDetected; PORTA = 0xFF; LCD_DisplayString(1, "ALARM! Enter Pin: "); } else if(key == 'C'){ state = controllerWait; clearInputPin(); clearFlags(); LCD_DisplayString(1, "A for Keypad B for Bluetooth"); } else if(checkPinFlag){ state = controllerCheck; } else{ if(lockedFlag){ state = controllerLocked; controlCounter = 0; LCD_DisplayString(1, "Incorrect Pin"); } else{ state = controllerUnlocked; LCD_DisplayString(1, "Press A to Lock C to change Pin"); } } break; case controllerLocked: if(!doorClosed && lockedFlag ){ state = controllerIntrusionDetected; PORTA = 0xFF; LCD_DisplayString(1, "ALARM! Enter Pin: "); } else if(controlCounter >= 750){ state = controllerWait; clearInputPin(); LCD_DisplayString(1, "A for Keypad B for Bluetooth"); clearFlags(); } else{ state = controllerLocked; } break; case controllerUnlocked: if(key == 'A'){ state = controllerLockWaitRelease; } else if(key == 'C'){ state = controllerChangePin; showPin = 1; keypadEnable = 1; pinInputComplete = 0; newPinInputComplete = 0; clearInputPin(); LCD_DisplayString(1, "Pin: "); } break; case controllerLockWaitRelease: if(key != '\0'){ state = controllerLockWaitRelease; } else{ state = controllerWait; clearInputPin(); LCD_DisplayString(1, "A for Keypad B for Bluetooth"); clearFlags(); } break; case controllerChangePin: if(!newPinInputComplete){ state = controllerChangePin; } else{ state = controllerUnlocked; keypadEnable = 0; showPin = 0; LCD_DisplayString(1, "Press A to Lock C to change Pin"); } break; case controllerIntrusionDetected: state = controllerIntrusionPin; keypadEnable = 1; pinInputComplete = 0; clearInputPin(); break; case controllerIntrusionPin: if(pinInputComplete){ state = controllerIntrusionCheck; checkPinFlag = 1; keypadEnable = 0; LCD_DisplayString(1, "Checking... C to cancel"); } else{ state = controllerIntrusionPin; } break; case controllerIntrusionCheck: if(checkPinFlag){ state = controllerIntrusionCheck; } else{ if(lockedFlag){ state = controllerIntrusionDetected; controlCounter = 0; LCD_DisplayString(1, "Incorrect Pin"); delay_ms(500); LCD_DisplayString(1, "ALARM! Enter Pin: "); } else{ state = controllerUnlocked; PORTA = 0x00; LCD_DisplayString(1, "Press A to Lock C to change Pin"); } } break; default: state = controllerinit; break; } switch(state){ case controllerinit: break; case controllerWait: break; case controllerKeypad: keypadEnable = 1; break; case controllerBluetooth: bluetoothEnable = 1; break; case controllerCheck: break; case controllerLocked: ++controlCounter; break; case controllerUnlocked: break; case controllerLockWaitRelease: break; case controllerChangePin: break; case controllerIntrusionDetected: break; case controllerIntrusionPin: break; case controllerIntrusionCheck: break; } return state; }
int TickFct_KeypadReceiver(int state){ unsigned char key = GetKeypadKey(); switch(state){ case keypadWait1: if(key == '\0' || !keypadEnable){ state = keypadWait1; } else{ state = keypadPressed; position = 0; clearInputPin(); } break; case keypadPressed: if(!keypadEnable){ state = keypadWait1; } else if(key == '*'){ state = keypadWait1; } else{ state = keypadWaitRelease; } break; case keypadWaitRelease: if(!keypadEnable){ state = keypadWait1; } else if(key !='\0'){ state = keypadWaitRelease; } else{ state = keypadWait2; } break; case keypadWait2: if(!keypadEnable){ state = keypadWait1; } else if(key == '\0'){ state = keypadWait2; } else{ state = keypadPressed; } break; default: state = keypadWait1; break; } switch(state){ case keypadWait1: break; case keypadWait2: break; case keypadPressed: if(key != '*'){ if(key != 'A' && key != 'C'){ inputPin[position++] = key; if(showPin){ LCD_WriteData(key); } else{ LCD_WriteData('*'); } } } else{ if(showPin){ LCD_DisplayString(1, "Changing..."); inputPin[position] = '\0'; unsigned char counter = 0; correctPinAddr = CORRECTPINSTARTADDR; while(1){ eeprom_write_byte(correctPinAddr++, inputPin[counter]); if(inputPin[counter] == '\0'){ break; } ++counter; } newPinInputComplete = 1; } else{ inputPin[position] = '\0'; pinInputComplete = 1; } } break; case keypadWaitRelease: break; } return state; }
int SMTick1(int state) { switch(keysm) { case -1: keysm = wait; break; case wait: x = GetKeypadKey(); keysm = exec; break; case exec: break; default: break; } switch(keysm) { case wait: break; case exec: if(x !='\0') { if(x == '1') { entered = 1; } else if(x == '2') { entered = 2; } else if(x == '3') { entered = 3; } else if(x == '4') { entered = 4; } else if(x == '5') { entered = 5; } else if(x == '6') { entered = 6; } else if(x == '7') { entered = 7; } else if(x == '8') { entered = 8; } else if(x == '#') { entered = 0; } else { entered = 9; } correct = 0x00; if(start == 1) { if((entered != 9) && ( entered != 0)) { if(!GetBit(PORTB, entered-1)) { correct = 0x01; } else { correct = 0x02; } } else if(entered == 9) { correct = 0x01; } else if(entered == 0) { start = 0; lives = 9; level = 1; start = 0; lost = 1; score = 0; occurances = 5; changed = 0; occ_counter = occurances; willchange = 200; PORTB = 0x00; LCDstate = init; randGen = gameInit; } } } else { correct = 0x00; } keysm = wait; break; default: break; } return state; }
int main() { // Set Data Direction Registers // Buttons PORTA[0-7], set AVR PORTA to pull down logic DDRA = 0xF0; PORTA = 0x0F; DDRB = 0xFF; PORTB = 0x00; DDRC = 0xFF; PORTC = 0x00;// LCD data lines DDRD = 0xFF; PORTD = 0x00; PORTB = 0x00; // Set the timer and turn it on TimerSet(10); TimerOn(); LCD_init(); LCD_ClearScreen(); State0 = wait; State = DEC; State2 = wait0; LCD_Cursor(2); srand(1); i = rand() % 2; obstacle1 = i; while(1){ z = GetKeypadKey(); if (z == 'D') { // MUST DO A SOFT RESET IN MAIN holder = 0; PORTB = 0x00; playertwochecker = 0; gunz = 50; pause1 = 0; x = 0; y = 0; counter = 0; counter2 = 0; spotholder = 2; scorecounter = 0; gunvalue = 0; delaytimer = 0; spotholder2 = 18; playerperiod = 0; realtskperiod = 0; displayperiod = 0; displaycnt = 100; roundone = rand() % 2; if (roundone == 0) { obstacle1 = 16; obstacle2 = 22; obstacle3 = rand() % (15 + 1 - 12) + 12; } else if (roundone == 1) { obstacle1 = 32; obstacle2 = 8; obstacle3 = rand() % (24 + 1 - 21) + 21; } playerpos = 2; LCD_ClearScreen(); } if (holderperiod == 10 && holder == 0) { menu(); holderperiod = 0; } else { holderperiod++; } i = rand() % 5; if(playerperiod == 10){ if (holder != 0) { // preemptive check against all other functions running at the start menu // this prevents anything from really happening user can only press 5 to start game keypad(); } playerperiod = 0; } else{ playerperiod++; } if(realtskperiod == displaycnt){ if (holder != 0) { realtsk(); } realtskperiod = 0; if (displaycnt > 18 && holder != 0) { displaycnt = displaycnt - 2; } } else{ realtskperiod++; } if(displayperiod == 10){ if (holder != 0) { displaytsk(); } displayperiod = 0; } else{ ++displayperiod; } while(!TimerFlag){} TimerFlag = 0; LCD_Cursor(0); if (clearseventenn == 5) { LCD_Cursor(1); LCD_WriteData(' '); LCD_Cursor(17); LCD_WriteData(' '); } clearseventenn++; } // Error: Program should not exit! return 0; }