int decode_scancode(alt_up_ps2_dev *ps2, KB_CODE_TYPE *decode_mode, alt_u8 *buf, char *ascii) { alt_u8 byte = 0; int status_read = 0; *decode_mode = KB_INVALID_CODE; static DECODE_STATE state = STATE_INIT; do { status_read = alt_up_ps2_read_data_byte(ps2, &byte); //FIXME: When the user press the keyboard extremely fast, data may get //occasionally get lost // if (byte != 0) // printf("byte read: %x status: %d\n", byte, status_read); if (status_read != 0) { if (state == STATE_INIT) return status_read; else return -2; } state = get_next_state(state, byte, decode_mode, buf, ascii); } while (state != STATE_DONE); state = STATE_INIT; return 0; }
alt_u32 reset_keyboard(alt_up_ps2_dev *ps2) { alt_u8 byte; // send out the reset command int status = alt_up_ps2_write_data_byte_with_ack(ps2, 0xff); if (status == 0) { // received the ACK for reset, now check the BAT result status = alt_up_ps2_read_data_byte(ps2, &byte); if (status == 0 && byte == 0xAA) { // BAT succeed } else { // BAT failed status = -1; } } return status; }
/*************************************************************************************** * Pushbutton - Interrupt Service Routine * * This routine checks which KEY has been pressed. If it is KEY1 or KEY2, it writes this * value to the global variable key_pressed. If it is KEY3 then it loads the SW switch * values and stores in the variable pattern ****************************************************************************************/ void PS2_ISR(struct alt_up_dev *up_dev, unsigned int id) { unsigned char PS2_data; /* check for PS/2 data--display on HEX displays */ if (alt_up_ps2_read_data_byte (up_dev->PS2_dev, &PS2_data) == 0) { /* allows save the last three bytes of data */ byte1 = byte2; byte2 = byte3; byte3 = PS2_data; if ( (byte2 == (unsigned char) 0xAA) && (byte3 == (unsigned char) 0x00) ) // mouse inserted; initialize sending of data (void) alt_up_ps2_write_data_byte (up_dev->PS2_dev, (unsigned char) 0xF4); } return; }
/* * Main Game Loop */ int main() { // Use the name of your pixel buffer DMA core pixel_buffer =alt_up_pixel_buffer_dma_open_dev("/dev/pixel_buffer_dma_0"); initVGA(); usleep(5000000); ps2 = alt_up_ps2_open_dev("/dev/ps2_0"); ps2->timeout = 2000000; alt_up_ps2_clear_fifo(ps2); alt_up_ps2_init(ps2); unsigned char byte1; while(alt_up_ps2_read_data_byte(ps2, &byte1)!=0); char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/character_lcd_0"); alt_up_character_lcd_init (char_lcd_dev); char_buffer = alt_up_char_buffer_open_dev("/dev/char_drawer"); alt_up_char_buffer_init(char_buffer); alt_up_sd_card_dev *device_reference = NULL; struct Env* p = initEnv(); initGameInfo(); struct Collidable* collisionChecker = initCollidable(); addCollisionToEnv(p, collisionChecker); promptSDcard(p, device_reference); usleep(1000); alt_up_char_buffer_string(char_buffer, "Loading ", 40, 30); unsigned end_time, start_time; int count = 0; lock = 0; struct animation* starAnimation = loadSDImageSeq("ST0.BMP", 2, 8); struct animation* star2Animation = loadSDImageSeq("ST00.BMP", 3, 7); struct animation* alien0 = loadSDImageSeq("A100.BMP", 2, 2); //2 images where first 2 characters are prefix struct animation* alien1 = loadSDImageSeq("A000.BMP", 2, 15); struct animation* ship0 = loadSDImageSeq("S00.BMP", 2, 16); struct animation* ship1 = loadSDImageSeq("S10.BMP", 2, 27); struct animation* bossAnimate = loadSDImageSeq("BO00.BMP", 2, 28); struct animation* ship2 = loadSDImageSeq("S20.BMP", 2, 35); struct animation* ship3 = loadSDImageSeq("S30.BMP", 2, 30); struct animation* ship4 = loadSDImageSeq("S40.BMP", 2, 10); struct animation* explode1 = initAnimation((int*)explode01, 1); addImage(explode1, initAnimation((int*)explode02, 0)); addImage(explode1, initAnimation((int*)explode03, 0)); addImage(explode1, initAnimation((int*)explode04, 0)); addImage(explode1, initAnimation((int*)explode05, 0)); struct animation** shipAnimationCollection = (struct animation**)malloc(sizeof(struct animation*)*5); shipAnimationCollection[0] = ship0; shipAnimationCollection[1] = ship1; shipAnimationCollection[2] = ship2; shipAnimationCollection[3] = ship3; shipAnimationCollection[4] = ship4; initWeapon(collisionChecker, p); struct Cursor* mouse = initCursor(p, collisionChecker); addToEnv(p, mouse->super); addObjToCollide(collisionChecker, mouse->super); setCursor(p, mouse); struct KeyController* keyController = initKeyController(); struct SwitchController* switchController = initSwitchController(); struct CursorController* ctrl = initCursorController(mouse->super, switchController, keyController); alt_up_char_buffer_string(char_buffer, "Loading Sounds ", 30, 30); audioController = initAudioController(); loadSound( audioController, LOOP_ONE ); loadSound( audioController, LASER_SOUND ); alt_irq_register(AUDIO_IRQ, audioController, (void*) audio_ISR); alt_irq_enable(AUDIO_IRQ); play_background_loop( audioController, LOOP_ONE ); enableAudioController( audioController ); printhex(info.score); mainMenu(mouse, ctrl, p); disableAudioController(audioController); stop_background_loop(audioController); unloadSoundById(audioController, LASER_SOUND); unloadSoundById(audioController, LOOP_ONE); alt_up_char_buffer_string(char_buffer, "Loading Sounds ", 30, 30); //loadSound(audioController, WIN_SOUND); //loadSound(audioController, LOSE_SOUND); loadSound( audioController, TOWER_UPGRADE_SOUND ); loadSound( audioController, LOOP_TWO ); play_background_loop(audioController, LOOP_TWO); enableAudioController( audioController ); alt_up_char_buffer_clear(char_buffer); //usleep(1000); struct Alien* testAlienCollection[60]; gameSetup(p, shipAnimationCollection, mouse, starAnimation, star2Animation); usleep(500000); //time delay for panel to be drawn // char LPS[50]; float lps_; int n = 0; for(n = 0; n < 20; n++) { testAlienCollection[n] =initAlien(n, 10*n, 10, alien0, explode1, "IdontKnow", 1.4, 150, 500, collisionChecker); addToEnvNR(p, testAlienCollection[n]->super); } for(n = 0; n < 20; n++) { testAlienCollection[n+20] =initAlien(10*n, n, 10, alien1, explode1, "whatName", 1.4, 190, 850, collisionChecker); addToEnvNR(p, testAlienCollection[n+20]->super); } for(n = 0; n < 20; n++) { testAlienCollection[n+40] =initAlien(10*n, n, 20, bossAnimate, explode1, "IamBoss", 1.6, 800, 1500, collisionChecker); testAlienCollection[n+40]->score = 300; addToEnvNR(p, testAlienCollection[n+40]->super); } int stage = 0; /* * Game Starts!!!!!! */ alt_alarm_start (&alarm,alt_ticks_per_second(),my_alarm_callback,(void*)p); int startTimer = 0; char second_row1[15]; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); sprintf(second_row1, "wave# %d ", stage); alt_up_character_lcd_string(char_lcd_dev, second_row1); while(1) { alt_timestamp_start(); start_time = (unsigned)alt_timestamp(); /*-----------------------------------------------------------------------------------------------*/ checkCollision(collisionChecker); //a major function that check each collision happen between each object updateCursorController(ctrl, 1); count++; if (startTimer > count) info.startButton = false; else { if(stage == 7) info.isWin = true; else if(startTimer == count){ //play_background_loop(audioController, LOOP_TWO); enableAudioController( audioController ); } } if (info.startButton){ disableAudioController(audioController); //stop_background_loop(audioController); startTimer = count + 15000; checkStages(testAlienCollection, stage%7, collisionChecker); stage++; //if(stage > 6) stage = 0; info.startButton = false; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); sprintf(second_row1, "wave# %d ", stage); alt_up_character_lcd_string(char_lcd_dev, second_row1); } if(info.isEnd || info.isWin) { disableAudioController(audioController); stop_background_loop(audioController); endGame(testAlienCollection, collisionChecker, p, mouse, ctrl, keyController); } /*-----------------------------------------------------------------------------------------------*/ end_time = (unsigned)alt_timestamp(); lps_ = (float)alt_timestamp_freq()/(float)(end_time - start_time); sprintf(LPS, "The current LPS is %.2f", lps_); alt_up_char_buffer_string(char_buffer, LPS, 3, 2); } return 0; }
alt_up_ps2_dev *init_mouse() { alt_up_ps2_dev *mouse = alt_up_ps2_open_dev("/dev/PS2_Port"); alt_up_ps2_init(mouse); alt_up_ps2_clear_fifo(mouse); alt_up_ps2_write_data_byte(mouse, 0xFF); int state = INIT; int status; while (state != ERROR && state != MSG1) { unsigned char data; do { status = alt_up_ps2_read_data_byte(mouse, &data); } while (status < 0); switch (state) { case INIT: if (data == 0xF4) { state = ACK; alt_printf("Mouse sent ACK (0xF4)."); } else if (data == 0xAA) { state = OK; alt_printf("Mouse skipped ACK (0xF4)."); } else { if (data == 0xFC) { state = ERROR; alt_printf("Mouse replied with an error message (0xFC).\n"); } else { alt_printf("Mouse replied with an unknown message: 0x%x. Ignoring...\n", data); } } break; case ACK: if (data == 0xAA) { state = OK; } else { alt_printf("Mouse replied sent unknown message: %x. Expected 0xAA. Ignoring...\n"); } break; case OK: if (data == 0x00) { state = ENABLING; alt_up_ps2_write_data_byte(mouse, 0xF4);; alt_printf("Mouse ready. Enabling position and button status messages...\n"); } else { state = ACK; // Back to waiting for 0xAA00. alt_printf("Mouse replied sent unknown byte: %x. Expected 0x00. Ignoring...\n"); } break; case ENABLING: if (data == 0xFA) { state = MSG1; alt_printf("OK ! Waiting for messages.\n"); } else { alt_printf("Mouse replied sent unknown message: %x. Expected 0xF4. Ignoring...\n"); } break; } } if (state == ERROR) return 0; return mouse; }