void game_end() { //turns off all communication with Node2 timer_disable(); //can_play_music(); oled_clear_screen(); //print GAME OVER oled_pos(3, 4); oled_printf("GAME OVER"); _delay_ms(1500); oled_clear_screen(); oled_pos(2, 2); oled_printf("Final score:"); oled_pos(4, 7); oled_printf("%d", game_score); printf("Final score: %d\n", game_score); oled_pos(7, 0); oled_printf("press any button"); while ((!button_read(2) && !button_read(1) && button_read(3)) ); //wait for button press _delay_ms(100); oled_clear_screen(); }
//外部中断 2 服务程序 KEY3 = PE2 void EXTI2_IRQHandler(void) { mydelay(10); //消抖 if(2==button_read()) { led_on(LED2); //LED2 亮 } EXTI_ClearITPendingBit(EXTI_Line2);//清除 LINE2 上的中断标志位 }
//PIN6外部中断 9_5 服务程序 KEY4 = PD6 void EXTI9_5_IRQHandler(void) { mydelay(10); //消抖 if(1==button_read()) { led_on(LED1); //LED1 亮 } EXTI_ClearITPendingBit(EXTI_Line6); //清除 LINE0 上的中断标志位 }
//外部中断 3 服务程序 KEY2 = PE3 void EXTI3_IRQHandler(void) { mydelay(10); //消抖 if(4==button_read()) { led_on(LED1); //LED1 亮 led_on(LED2); //LED2 亮 } EXTI_ClearITPendingBit(EXTI_Line3); //清除 LINE3 上的中断标志位 }
//外部中断 4 服务程序 KEY1= PE4 void EXTI4_IRQHandler(void) { mydelay(10); //消抖 if(8==button_read()) { led_off(LED1); //LED1 灭 led_off(LED2); //LED2 灭 } EXTI_ClearITPendingBit(EXTI_Line4);//清除 LINE4 上的中断标志位 }
void button_init(void) { int x = 0; while (x < 100) { if (button_read()) { x = 0; } else { x++; } delay_ms(1); } button_pressed = false; }
void main_init() { uint8_t i; bsp_init(); hal_init(); if( button_read(0) == FALSE ) { delay_ms(100); if( button_read(0) == FALSE ) { for( i=0; i<5; i++ ) { led_toggle(0); delay_ms(50); } jump_to_fw(); } } }
void buttons_read(){ button_read(&button_joy_up, Is_joy_up()); button_read(&button_joy_down, Is_joy_down()); button_read(&button_joy_right, Is_joy_right()); button_read(&button_joy_left, Is_joy_left()); button_read(&button_joy_mid, Is_btn_middle()); button_read(&button_hwb, Is_hwb()); }
void button_init(void) { /* Init used objects first */ queue_init(&button_queue, true); #ifdef HAVE_BUTTON_DATA int temp; #endif /* hardware inits */ button_init_device(); #ifdef HAVE_BUTTON_DATA button_read(&temp); lastbtn = button_read(&temp); #else button_read(); lastbtn = button_read(); #endif reset_poweroff_timer(); #ifdef HAVE_LCD_BITMAP flipped = false; #endif #ifdef HAVE_BACKLIGHT filter_first_keypress = false; #ifdef HAVE_REMOTE_LCD remote_filter_first_keypress = false; #endif #endif #ifdef HAVE_TOUCHSCREEN last_touchscreen_touch = 0xffff; #endif /* Start polling last */ tick_add_task(button_tick); }
void button_service(void) { if (button_pressed != button_read()) { //has the button state changes since last time? switch (button_pressed) { case true: //button was released (previously pressed), figure out ho long delta = time_get() - pressed_at; //calc press duration menu_send_BTN(); usb_tx_string_P(PSTR("RELEASE:")); // report to CMD interface menu_print_int(delta); menu_send_n_st(); button_pressed = false; //note the button was released break; case false: //button was pressed, capture press time pressed_at = time_get(); //store press time button_pressed = true; //hold button state menu_send_BTN(); usb_tx_string_P(PSTR("PRESS")); //report press event to CMD menu_send_n_st(); break; } } }
static void button_tick(void) { static int count = 0; static int repeat_speed = REPEAT_INTERVAL_START; static int repeat_count = 0; static bool repeat = false; static bool post = false; #ifdef HAVE_BACKLIGHT static bool skip_release = false; #ifdef HAVE_REMOTE_LCD static bool skip_remote_release = false; #endif #endif int diff; int btn; #ifdef HAVE_BUTTON_DATA int data = 0; #else const int data = 0; #endif #if defined(HAS_SERIAL_REMOTE) && !defined(SIMULATOR) /* Post events for the remote control */ btn = remote_control_rx(); if(btn) button_try_post(btn, 0); #endif #ifdef HAVE_BUTTON_DATA btn = button_read(&data); #else btn = button_read(); #endif #if defined(HAVE_HEADPHONE_DETECTION) if (headphones_inserted() != phones_present) { /* Use the autoresetting oneshot to debounce the detection signal */ phones_present = !phones_present; timeout_register(&hp_detect_timeout, btn_detect_callback, HZ/2, phones_present); } #endif /* Find out if a key has been released */ diff = btn ^ lastbtn; if(diff && (btn & diff) == 0) { #ifdef HAVE_BACKLIGHT #ifdef HAVE_REMOTE_LCD if(diff & BUTTON_REMOTE) if(!skip_remote_release) button_try_post(BUTTON_REL | diff, data); else skip_remote_release = false; else #endif if(!skip_release) button_try_post(BUTTON_REL | diff, data); else skip_release = false; #else button_try_post(BUTTON_REL | diff, data); #endif } else { if ( btn ) { /* normal keypress */ if ( btn != lastbtn ) { post = true; repeat = false; repeat_speed = REPEAT_INTERVAL_START; } else /* repeat? */ { if ( repeat ) { if (!post) count--; if (count == 0) { post = true; /* yes we have repeat */ if (repeat_speed > REPEAT_INTERVAL_FINISH) repeat_speed--; count = repeat_speed; repeat_count++; /* Send a SYS_POWEROFF event if we have a device which doesn't shut down easily with the OFF key */ #ifdef HAVE_SW_POWEROFF if ((btn & POWEROFF_BUTTON #ifdef RC_POWEROFF_BUTTON || btn == RC_POWEROFF_BUTTON #endif ) && #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING) !charger_inserted() && #endif repeat_count > POWEROFF_COUNT) { /* Tell the main thread that it's time to power off */ sys_poweroff(); /* Safety net for players without hardware poweroff */ #if (CONFIG_PLATFORM & PLATFORM_NATIVE) if(repeat_count > POWEROFF_COUNT * 10) power_off(); #endif } #endif } } else { if (count++ > REPEAT_START) { post = true; repeat = true; repeat_count = 0; /* initial repeat */ count = REPEAT_INTERVAL_START; } #ifdef HAVE_TOUCHSCREEN else if (lastdata != data && btn == lastbtn) { /* only coordinates changed, post anyway */ if (touchscreen_get_mode() == TOUCHSCREEN_POINT) post = true; } #endif } } if ( post ) { if (repeat) { /* Only post repeat events if the queue is empty, * to avoid afterscroll effects. */ if (button_try_post(BUTTON_REPEAT | btn, data)) { #ifdef HAVE_BACKLIGHT #ifdef HAVE_REMOTE_LCD skip_remote_release = false; #endif skip_release = false; #endif post = false; } } else { #ifdef HAVE_BACKLIGHT #ifdef HAVE_REMOTE_LCD if (btn & BUTTON_REMOTE) { if (!remote_filter_first_keypress || is_remote_backlight_on(false) #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES) || (remote_type()==REMOTETYPE_H300_NONLCD) #endif ) button_try_post(btn, data); else skip_remote_release = true; } else #endif if (!filter_first_keypress || is_backlight_on(false) #if BUTTON_REMOTE || (btn & BUTTON_REMOTE) #endif ) button_try_post(btn, data); else skip_release = true; #else /* no backlight, nothing to skip */ button_try_post(btn, data); #endif post = false; } #ifdef HAVE_REMOTE_LCD if(btn & BUTTON_REMOTE) remote_backlight_on(); else #endif { backlight_on(); #ifdef HAVE_BUTTON_LIGHT buttonlight_on(); #endif } reset_poweroff_timer(); } } else { repeat = false; count = 0; } } lastbtn = btn & ~(BUTTON_REL | BUTTON_REPEAT); #ifdef HAVE_BUTTON_DATA lastdata = data; #endif }
void control(int b) { struct vector u; // Left button if (! map_mode) switch (selected_weapon) { case 0: if ((enable_mouse && button_read(SDL_BUTTON_LEFT)) || kread(gkeys[kc_fire].kc)) bot[b].but.canon=1; break; case 1: if ((enable_mouse && button_reset(SDL_BUTTON_LEFT)) || kreset(gkeys[kc_fire].kc)) bot[b].but.bomb=1; break; } else if ((enable_mouse && button_read(SDL_BUTTON_LEFT)) || kread(gkeys[kc_fire].kc)) { bot[b].u.x = ((xmouse-win_center_x)*(MAP_LEN/2)*TILE_LEN)/zoom+map_x*TILE_LEN; bot[b].u.y = ((win_center_y-ymouse)*(MAP_LEN/2)*TILE_LEN)/zoom+map_y*TILE_LEN; bot[b].u.z = z_ground(bot[b].u.x, bot[b].u.y, true); } // Right button if ((enable_mouse && button_reset(SDL_BUTTON_RIGHT)) || kreset(gkeys[kc_weapon].kc)) { if (abs(xmouse) < 2 && abs(ymouse) < 2) { if (! prompt_quit) prompt_quit = true; else quit_game = true; } selected_weapon ^= 1; } // Esc if (prompt_quit) { if (kreset(gkeys[kc_yes].kc)) quit_game = true; if (kreset(gkeys[kc_no].kc)) prompt_quit = false; } else if (kreset(gkeys[kc_esc].kc) && (bot[controlled_bot].camp!=-1 || !enable_resurrection || !resurrect())) { prompt_quit = true; } // Engine # define INCR .02 if (kread(gkeys[kc_motormore].kc) && bot[b].thrust <= 1.-INCR) bot[b].thrust += INCR; if (kread(gkeys[kc_motorless].kc) && bot[b].thrust >= INCR) bot[b].thrust -= INCR; # undef INCR // Views if (kreset(gkeys[kc_externview].kc)) { map_mode = false; view = next_external_view(view); snd_thrust=-1; } if (kreset(gkeys[kc_internview].kc)) { map_mode = false; if (view == VIEW_IN_PLANE) { view = VIEW_DOGFIGHT; } else { view = VIEW_IN_PLANE; snd_thrust=-1; } } if (kreset(gkeys[kc_travelview].kc)) { float zs; map_mode = false; view = VIEW_STANDING; copyv(&obj[0].pos,&obj[bot[viewed_bot].vion].rot.x); mulv(&obj[0].pos,300+drand48()*600+extcam_dist); copyv(&u,&obj[bot[viewed_bot].vion].rot.y); mulv(&u,(drand48()-.5)*600); addv(&obj[0].pos,&u); copyv(&u,&obj[bot[viewed_bot].vion].rot.z); mulv(&u,(drand48()-.5)*600); addv(&obj[0].pos,&u); addv(&obj[0].pos,&obj[bot[viewed_bot].vion].pos); if (obj[0].pos.z<(zs=z_ground(obj[0].pos.x,obj[0].pos.y, false)+100)) obj[0].pos.z=zs; snd_thrust=-1; } if (kreset(gkeys[kc_nextbot].kc)) { if (view == VIEW_ANYTHING_CHEAT) { if (++viewed_obj >= nb_obj) viewed_obj = 0; } else if (view == VIEW_DOGFIGHT) { next_dog_bot(); } else { do { if (++viewed_bot>=NBBOT) viewed_bot=0; } while (!enable_view_enemy && bot[viewed_bot].camp!=camp); // pas controlled_bot.camp car peut etre tue snd_thrust=-1; if (bot[viewed_bot].camp==-1) playsound(VOICE_MOTOR, SAMPLE_FEU, 1., &voices_in_my_head, true, true); } } if (kreset(gkeys[kc_prevbot].kc)) { if (view == VIEW_ANYTHING_CHEAT) { if (--viewed_obj<0) viewed_obj = nb_obj-1; } else if (view == VIEW_DOGFIGHT) { prev_dog_bot(); } else { do { if (--viewed_bot<0) viewed_bot=NBBOT-1; } while (!enable_view_enemy && bot[viewed_bot].camp!=camp); snd_thrust=-1; if (bot[viewed_bot].camp==-1) playsound(VOICE_MOTOR, SAMPLE_FEU, 1., &voices_in_my_head, true, true); } } if (kreset(gkeys[kc_mybot].kc)) { if (view != VIEW_DOGFIGHT) { viewed_bot = b; snd_thrust = -1; } else { float d; int DBi, DBm; next_dog_bot(); d=DogBotDist; DBi=DogBot; DBm=DogBot; do { next_dog_bot(); if (DogBotDist<d && bot[DogBot].camp!=bot[controlled_bot].camp) { d=DogBotDist; DBm=DogBot; } } while (DogBot!=DBi); DogBot=DBm; DogBotDist=d; } } if (!accelerated_mode || frame_count > 64) { if (kread(gkeys[kc_zoomout].kc)) { if (! map_mode) extcam_dist += 10.; else zoom += win_center_x/6; } if (kread(gkeys[kc_zoomin].kc)) { if (! map_mode && extcam_dist > 10.) extcam_dist -= 10.; else if ((zoom -= win_center_x/6) < win_center_x) zoom = win_center_x; } if (kread(gkeys[kc_riseview].kc)) { if (! map_mode) { if ((sight_teta -= .2) < -M_PI) sight_teta += 2*M_PI; } else if ((map_y += 1 + (3*win_width)/zoom) > MAP_LEN/2) { map_y = MAP_LEN/2; } } if (kread(gkeys[kc_lowerview].kc)) { if (! map_mode) { if ((sight_teta += .2) > M_PI) sight_teta -= 2*M_PI; } else if ((map_y -= 1 + (3*win_width)/zoom) < -MAP_LEN/2) { map_y = -MAP_LEN/2; } } if (kread(gkeys[kc_rightenview].kc)) { if (! map_mode) { if ((sight_phi -= .2) < -M_PI) sight_phi += 2*M_PI; } else if ((map_x += 1 + (3*win_width)/zoom) > MAP_LEN/2) { map_x = MAP_LEN/2; } } if (kread(gkeys[kc_leftenview].kc)) { if (! map_mode) { if ((sight_phi += .2) > M_PI) sight_phi -= 2*M_PI; } else if ((map_x -= 1 + (3*win_width)/zoom) < -MAP_LEN/2) { map_x = -MAP_LEN/2; } } } if (view != VIEW_DOGFIGHT) { if (kreset(gkeys[kc_towardview].kc)) { sight_teta = sight_phi = 0; } if (kreset(gkeys[kc_backview].kc)) { sight_teta = 0; sight_phi = M_PI; } if (kreset(gkeys[kc_leftview].kc)) { sight_teta = 0; sight_phi = M_PI*.5; } if (kreset(gkeys[kc_rightview].kc)) { sight_teta = 0; sight_phi = -M_PI*.5; } if (kreset(gkeys[kc_upview].kc)) { sight_teta = -M_PI/2; sight_phi = 0; } } else { view_predef = false; if (kread(gkeys[kc_towardview].kc)) { view_predef = true; sight_teta = sight_phi = 0; } if (kread(gkeys[kc_backview].kc)) { view_predef = true; sight_teta = 0; sight_phi = M_PI; } if (kread(gkeys[kc_leftview].kc)) { view_predef = true; sight_teta = 0; sight_phi = M_PI*.5; } if (kread(gkeys[kc_rightview].kc)) { view_predef = true; sight_teta = 0; sight_phi = -M_PI*.5; } if (kread(gkeys[kc_upview].kc)) { view_predef = true; sight_teta = -M_PI/2; sight_phi = 0; } if (! view_predef) sight_teta = sight_phi = 0; } view_instruments = kread(gkeys[kc_movetowardview].kc); // Commands if (kreset(gkeys[kc_gear].kc)) bot[b].but.gear^=1; if (kreset(gkeys[kc_flaps].kc)) { bot[b].but.flap^=1; playsound(VOICE_GEAR, SAMPLE_BIPBIP, 1., &obj[bot[b].vion].pos, false, false); } bot[b].but.brakes=kread(gkeys[kc_brakes].kc); if (kreset(gkeys[kc_business].kc)) bot[b].but.business = 1; if (kreset(gkeys[kc_autopilot].kc)) { autopilot = ! autopilot; playsound(VOICE_GEAR, SAMPLE_BIPBIP, 1., &obj[bot[b].vion].pos, false, false); if (autopilot) { bot[controlled_bot].target_speed = BEST_SPEED_FOR_CONTROL; bot[controlled_bot].target_rel_alt = 100. * ONE_METER; } } // Game control if (kreset(gkeys[kc_pause].kc)) { gtime_toggle(); game_paused = ! game_paused; } draw_high_scores = kread(gkeys[kc_highscores].kc); if (kreset(gkeys[kc_accelmode].kc)) { accelerated_mode = ! accelerated_mode; frame_count&=63; } if (kreset(gkeys[kc_basenav].kc)) { bot[b].u = obj[bot[b].babase].pos; } if (kreset(gkeys[kc_mapmode].kc)) { map_mode = ! map_mode; playsound(VOICE_GEAR, SAMPLE_BIPBIP3, 1., &voices_in_my_head, true, false); } if (kreset(gkeys[kc_suicide].kc) && bot[controlled_bot].camp!=-1) explode(bot[viewed_bot].vion, 0, "commited suicide"); if (kreset(gkeys[kc_markpos].kc)) bot[b].but.mark=1; // Cheats if (cheat_mode && kread(gkeys[kc_alti].kc)) { obj[bot[viewed_bot].vion].pos.z += 500; bot[viewed_bot].vionvit.z = 0; } if (cheat_mode && kreset(gkeys[kc_gunned].kc)) bot[viewed_bot].gunned=controlled_bot; if (!autopilot && !map_mode) { if (enable_mouse) { bot[b].xctl = ((xmouse-win_center_x)/(double)win_center_x); bot[b].yctl = ((ymouse-win_center_y)/(double)win_center_y); } else { int i=0; i=kread(gkeys[kc_left].kc); i+=kread(gkeys[kc_right].kc)<<1; i+=kread(gkeys[kc_down].kc)<<2; i+=kread(gkeys[kc_up].kc)<<3; if (i) { CtlSensActu += CtlSensitiv; if (i&1) bot[b].xctl-=CtlSensActu; if (i&2) bot[b].xctl+=CtlSensActu; if (i&4) bot[b].yctl-=CtlSensActu; if (i&8) bot[b].yctl+=CtlSensActu; } else CtlSensActu=0; if (bot[b].xctl<-1 || bot[b].xctl>1 || bot[b].yctl<-1 || bot[b].yctl>1) CtlSensActu=0; if (!(i&3)) bot[b].xctl*=CtlAmortis; if (!(i&12)) bot[b].yctl=CtlYequ+(bot[b].yctl-CtlYequ)*CtlAmortis; if (kread(gkeys[kc_center].kc)) { bot[b].xctl=0; bot[b].yctl=CtlYequ; if (kread(gkeys[kc_down].kc) && CtlYequ>-1) CtlYequ-=.02; if (kread(gkeys[kc_up].kc) && CtlYequ<1) CtlYequ+=.02; } } } else { // autopilot or map_mode if (autopilot) { robot_autopilot(b); } else { robot_safe(b, SAFE_LOW_ALT); } } CLAMP(bot[b].xctl, 1.); CLAMP(bot[b].yctl, 1.); }