/* Radio control interface */ int menuTask(task* t) { if(USART_HasReceived(0)) { char uInput = USART_Receive(0); switch(uInput) { // Fan case 'f': tempProcess(0x3F); // emulate "?" function button tempProcess(0x32); // emulate "2" function button break; // Enable case 'e': tempProcess(0x3F); // emulate "?" function button tempProcess(0x31); // emulate "2" function button break; default: if(uInput >= '0' && uInput <= '9') { tempProcess(0x30 | (uInput - '0')); } break; } } return 0; }
unsigned char receiveData(unsigned char port) { unsigned char temp1 = 2; while(1) { if(USART_HasReceived(port)) { temp1 = USART_Receive(port); return temp1; } } }
int TickFct_D(int state) { // State Transitions switch(state) { case START_D: state = INIT_D; break; // transition to INIT_D state case INIT_D: incoming_data = data_avail = 0; state = WAIT_D; break; // transition to WAIT_D state case WAIT_D: if(USART_HasReceived(0)) { data_avail = 1; // if(!wired) state = READ_D; /*if (USART_IsSendReady(0)) { USART_Send(incoming_data,0); }*/ } else { data_avail = 0; } break; case READ_D: state = WAIT_D; break; default: state = START_D; // error likely happened, restart break; } // State Actions switch(state) { case READ_D: incoming_data = USART_Receive(0); break; default: break; } return state; }
int main(void) { DDRA = 0xF0; PORTA = 0x0F; DDRB = 0x00; PORTB = 0xFF; DDRC = 0xFF; PORTC = 0x00; DDRD = 0xFF; PORTD = 0x00; gameSelector = 0xFF; LINE_init(); TimerSet(1); TimerOn(); srand(0); InitADC(); initUSART(1); while (1) { if(SandM_period >= 10) { if(USART_HasReceived(1)){ gameSelector = receiveData(1); } SandM_period = 0; } ++SandM_period; if(gameSelector == 0x02) { if(TRLINES_period >= 9) { TRLINES_period = 0; } if(TRuser_period >= 80) { TunRun_ChkLoss(); TunRun_JoyStk(); TUNRUN_LINES(); TunRun_Input(); TRuser_period = 0; } displayMatrix(); ++TRLINES_period; ++TRuser_period; } if(gameSelector == 0x04) { if(arrows_period >= 750) { DDR_arrows(); if((arrow == 0) || (arrow == 2)) { ADMUX = 0x40; } else { ADMUX = 0x41; } arrows_period = 0; } if(arrows_inputP >= 20) { DDR_Input(); DDR_JoyStk(); arrows_inputP = 0; } if(arrowsPeriod >= 250) { DDR_ChkLoss(); arrowsPeriod = 0; } displayMatrix(); ++arrows_inputP; ++arrows_period; ++arrowsPeriod; } if(gameSelector == 0x03) { if(snakeJoystick > 30) { Snake_JoyStk(); Snake_Input(); snakeJoystick = 0; } if(body > body_period) { SnakeBody(); Snake_Movement(); body = 0; } ++snakeJoystick; ++joystick; ++body; displayMatrix(); } while(!TimerFlag); TimerFlag = 0; } }
void Tick_Fct(){ distance = read_ultrasonic(); if (USART_HasReceived(0)) { direction = USART_Receive(0); PORTC = direction; } switch(state){//transitions case init: state = start; break; case start: if(direction == 0xFF){ state = wait_impact; }else{ state = controlled; }break; case controlled: if(direction == 0xFF){ state = wait_impact; }else{ count_1 = 0; state = hold_direction; }break; case hold_direction: if(count_1 < 30){ state = hold_direction; }else{ state = reset_direction; } break; case reset_direction: state = controlled; break; case wait_impact: if (direction != 0xFF) { state = controlled; } else if(direction == 0xFF && distance > 30){ state = wait_impact; }else { state = stop_car; count = 0; PORTA = 0x00; } break; case stop_car: if (direction != 0xFF) { state = controlled; } else if(direction == 0xFF && count < 100){ state = stop_car; } else if(direction == 0xFF && !(count < 100)){ state = turn_left; count = 0; } break; case turn_left: if (direction != 0xFF) { state = controlled; } else if(direction == 0xFF && count < 100){ state = turn_left; } else if(direction == 0xFF && !(count < 100)){ state = wait_impact; } break; default: state = init; break; } switch(state){//actions case wait_impact: PORTA = 0x04; break; case stop_car: count++; PORTA = 0x42; break; case turn_left: count++; PORTA = 0x24; break; case start: PORTA = 0x00; break; case controlled: if(direction == 0x0A){ PORTA = 0x00; }else if (direction == 0x01){ PORTA = 0x44;//forward }else if (direction == 0x02){//reverse or backward PORTA = 0x42; }else if (direction == 0x04){//left PORTA = 0x24; }else if(direction == 0x08){//right PORTA = 0x44; } break; case hold_direction: count_1++; break; case reset_direction: PORTA = 0x00; break; default: break; } }
int TickFct_BluetoothReceiver(int state){ switch(state){ case bluetoothWait: if(USART_HasReceived(0) && !bluetoothEnable){ USART_Flush(0); state = bluetoothWait; } else if(USART_HasReceived(0) && bluetoothEnable){ state = bluetoothReceived; position = 0; lastReceivedChar = 0; clearInputPin(); } else{ state = bluetoothWait; } break; case bluetoothReceived: if(!bluetoothEnable){ state = bluetoothWait; } else if(lastReceivedChar != '*'){ state = bluetoothWaitForNext; } else{ state = bluetoothWait; } break; case bluetoothWaitForNext: if(!bluetoothEnable){ state = bluetoothWait; } else if(!USART_HasReceived(0)){ state = bluetoothWaitForNext; } else{ state = bluetoothReceived; } break; default: state = bluetoothWait; break; } switch(state){ case bluetoothWait: break; case bluetoothReceived: while(lastReceivedChar != '*'){ lastReceivedChar = USART_Receive(0); if(lastReceivedChar != '*'){ inputPin[position++] = lastReceivedChar; } else{ inputPin[position] = '\0'; pinInputComplete = 1; } } break; case bluetoothWaitForNext: break; } return state; }
void mainTick(){ static unsigned char button; static unsigned char pressed; //decrease states for buttonpress button = ~PINA; switch(gameStatus){ case intro: if(GetBit(button,0)){ gameStatus = play; playTime = 0; pressed = 1; currScore = 0; lcdTick(); if(USART_IsSendReady()) USART_Send(0x41); } else if(USART_HasReceived()){ tempMessage = USART_Receive(); if( tempMessage == 0xC1 ){ gameStatus = highscore; playTime = 0; lcdTick(); } else if( tempMessage == 0xC2){ gameStatus = resetHighScore; currHighScore = 0; eeprom_write_byte ((uint8_t*) 46, currHighScore); playTime = 0; lcdTick(); } } else gameStatus = intro; break; case highscore: if(playTime == 500){ gameStatus = intro; lcdTick(); } else gameStatus = highscore; break; case resetHighScore: if(playTime == 500){ gameStatus = intro; lcdTick(); } else gameStatus = resetHighScore; break; case play: if(!(GetBit(button,0) && !pressed) && playTime < 4000 ) gameStatus = play; else if((GetBit(button,0) && !pressed) || playTime == 4000){ gameStatus = gameOver; lcdTick(); playTime = 0; if(currScore > currHighScore){ currHighScore = currScore; eeprom_write_byte ((uint8_t*) 46, currHighScore); } if(USART_IsSendReady()) USART_Send(0x40); } else gameStatus = play; break; case gameOver: if(playTime == 500){ gameStatus = intro; lcdTick(); } else if(playTime < 500) gameStatus = gameOver; else gameStatus = gameOver; break; } switch(gameStatus){ case intro: PORTB = 0; currScore = 0; break; case highscore: playTime++; break; case resetHighScore: playTime++; break; case play: if(!GetBit(button,0)) pressed = 0; playTime++; if(USART_HasReceived()){ tempMessage = USART_Receive(); if(GetBit(tempMessage,7) && !GetBit(tempMessage,6)) if((tempMessage & 0x3F) != currScore) updateScore(tempMessage & 0x3F); } break; case gameOver: playTime++; break; } }