Index: apps/plugins/CATEGORIES =================================================================== --- apps/plugins/CATEGORIES (révision 17694) +++ apps/plugins/CATEGORIES (copie de travail) @@ -59,6 +59,7 @@ rockblox,games rockbox_flash,viewers rockboy,viewers +thinkfast,games rocklife,games rockpaint,apps search,viewers Index: apps/plugins/SOURCES =================================================================== --- apps/plugins/SOURCES (révision 17694) +++ apps/plugins/SOURCES (copie de travail) @@ -150,3 +150,4 @@ superdom.c #endif #endif /* m:robe 500 */ +thinkfast.c Index: apps/plugins/thinkfast.c =================================================================== --- apps/plugins/thinkfast.c (révision 0) +++ apps/plugins/thinkfast.c (révision 0) @@ -0,0 +1,170 @@ +#include "plugin.h" +#include "pluginlib_actions.h" + +PLUGIN_HEADER +static const struct plugin_api* rb; + +const struct button_mapping *plugin_contexts[] = + {generic_directions, generic_actions}; + +/* Mapping */ +#define EXIT PLA_QUIT +#define START PLA_FIRE +#define LEFT PLA_LEFT +#define RIGHT PLA_RIGHT +#define UP PLA_UP +#define DOWN PLA_DOWN + +#define NB_KEYS 4 +#define NB_TRIES 10 + +inline int get_button(void) { + return pluginlib_getaction(rb, TIMEOUT_BLOCK, plugin_contexts, 2); +} + +char directions[32]; + +const int KEY_CODES[NB_KEYS] = {LEFT, RIGHT, UP, DOWN}; + +int nb_times = 0; +int times[128] = {0}; + +int button; +bool btn_pressed = false; + +bool usb = false; +bool exit_game = false; +bool next_game = false; + +void update_list(int new_time) { + if (new_time > 0) { + times[nb_times] = new_time; + nb_times++; + return; + } + + rb->lcd_puts(0, 1, "--"); + + int cur_time; + char time_str[64]; + for(cur_time = 0; cur_time < nb_times; cur_time++) { + rb->snprintf(time_str, 64, "%d. %d.%d seconds needed to react", + cur_time + 1, times[cur_time] / 100, + times[cur_time] - (times[cur_time] / 100)); + rb->lcd_puts(0, cur_time + 2, time_str); + } + + rb->lcd_puts(0, cur_time + 2, "--"); +} + +void display_stats(void) { + int min_time, max_time; + char min_str[32], max_str[32], average_str[32]; + int average_time; + + average_time = times[0]; + min_time = max_time = times[0]; + + int i; + for(i = 1; i < nb_times; i++) { + if (times[i] < min_time) min_time = times[i]; + if (times[i] > max_time) max_time = times[i]; + average_time += times[i]; + } + average_time /= nb_times; + + rb->snprintf(min_str, 32, "Min: %d.%d", + (min_time / 100), min_time - (min_time / 100)); + rb->snprintf(max_str, 32, "Max: %d.%d", + (max_time / 100), max_time - (max_time / 100)); + rb->snprintf(average_str, 32, "Average: %d.%d", + (average_time / 100), average_time - (average_time / 100)); + + rb->lcd_clear_display(); + rb->lcd_puts(0, 1, "--"); + rb->lcd_puts(0, 2, min_str); + rb->lcd_puts(0, 3, max_str); + rb->lcd_puts(0, 4, average_str); + rb->lcd_puts(0, 5, "--"); + rb->lcd_update(); +} + +enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) +{ + (void)parameter; + rb = api; + + rb->splash(HZ, "Game started"); + + int button = -1; + while (!exit_game && !usb) { + rb->lcd_clear_display(); + + display_stats(); + + nb_times = 0; + next_game = false; + rb->srand(*rb->current_tick); + + rb->splash(HZ, "Press center when ready !"); + while (!next_game && !exit_game && !usb) { + button = get_button(); + if (button == EXIT) + exit_game = true; + else if (button == START) + next_game = true; + else { + if (rb->default_event_handler(button) == SYS_USB_CONNECTED) + usb = true; + } + } + + int cur_test; + for (cur_test = 0; cur_test < NB_TRIES && !exit_game; cur_test++) { + btn_pressed = false; + + char keyname[16]; + int btn_to_press = rb->rand() % NB_KEYS; + switch (KEY_CODES[btn_to_press]) { + case LEFT: + rb->strcpy(keyname, "left"); + break; + case RIGHT: + rb->strcpy(keyname, "right"); + break; + case UP: + rb->strcpy(keyname, "up"); + break; + case DOWN: + rb->strcpy(keyname, "down"); + break; + } + + rb->snprintf(directions, 15, "Press %s !", keyname); + + rb->lcd_clear_display(); + update_list(-1); + rb->lcd_puts(0, 0, directions); + rb->lcd_update(); + + int start = *rb->current_tick; + + button = get_button(); + while (!btn_pressed && !exit_game && !usb) { + button = get_button(); + if (button == EXIT) + exit_game = true; + else if (button == KEY_CODES[btn_to_press] && !btn_pressed) { + update_list(*rb->current_tick - start); + btn_pressed = true; + } + else { + if (rb->default_event_handler(button) == SYS_USB_CONNECTED) + usb = true; + } + } + } + } + + return (usb) ? PLUGIN_USB_CONNECTED : PLUGIN_OK; + }