laser_tag_app.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "laser_tag_app.h"
  2. #include "laser_tag_view.h"
  3. #include "infrared_controller.h"
  4. #include "game_state.h"
  5. #include "lfrfid_reader.h"
  6. #include <furi.h>
  7. #include <gui/gui.h>
  8. #include <input/input.h>
  9. #include <notification/notification.h>
  10. #define TAG "LaserTagApp"
  11. struct LaserTagApp {
  12. Gui* gui;
  13. ViewPort* view_port;
  14. LaserTagView* view;
  15. FuriMessageQueue* event_queue;
  16. FuriTimer* timer;
  17. NotificationApp* notifications;
  18. InfraredController* ir_controller;
  19. GameState* game_state;
  20. LaserTagState state;
  21. bool need_redraw;
  22. LFRFIDReader* reader;
  23. };
  24. const NotificationSequence sequence_vibro_1 = {&message_vibro_on, &message_vibro_off, NULL};
  25. const NotificationSequence sequence_short_beep =
  26. {&message_note_c4, &message_delay_50, &message_sound_off, NULL};
  27. static void laser_tag_app_timer_callback(void* context) {
  28. furi_assert(context);
  29. LaserTagApp* app = context;
  30. FURI_LOG_D(TAG, "Timer callback triggered");
  31. if(app->state == LaserTagStateSplashScreen) {
  32. if(game_state_get_time(app->game_state) >= 2) {
  33. FURI_LOG_I(TAG, "Splash screen time over, switching to TeamSelect");
  34. app->state = LaserTagStateTeamSelect;
  35. game_state_reset(app->game_state);
  36. FURI_LOG_D(TAG, "Game state reset after splash screen");
  37. } else {
  38. FURI_LOG_D(TAG, "Updating splash screen time");
  39. game_state_update_time(app->game_state, 1);
  40. }
  41. } else if(app->state == LaserTagStateGame) {
  42. FURI_LOG_D(TAG, "Updating game time by 1 second");
  43. game_state_update_time(app->game_state, 1);
  44. }
  45. if(app->view) {
  46. FURI_LOG_D(TAG, "Updating view with the latest game state");
  47. laser_tag_view_update(app->view, app->game_state);
  48. app->need_redraw = true;
  49. }
  50. }
  51. static void laser_tag_app_input_callback(InputEvent* input_event, void* context) {
  52. furi_assert(context);
  53. LaserTagApp* app = context;
  54. FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", input_event->type, input_event->key);
  55. furi_message_queue_put(app->event_queue, input_event, 0);
  56. FURI_LOG_D(TAG, "Input event queued successfully");
  57. }
  58. static void laser_tag_app_draw_callback(Canvas* canvas, void* context) {
  59. furi_assert(context);
  60. LaserTagApp* app = context;
  61. FURI_LOG_D(TAG, "Entering draw callback");
  62. if(app->state == LaserTagStateSplashScreen) {
  63. canvas_clear(canvas);
  64. canvas_set_font(canvas, FontPrimary);
  65. canvas_draw_str(canvas, 5, 20, "Laser Tag!");
  66. canvas_set_font(canvas, FontSecondary);
  67. canvas_draw_str(canvas, 5, 40, "https://github.com/");
  68. canvas_draw_str(canvas, 5, 50, "RocketGod-git/");
  69. canvas_draw_str(canvas, 5, 60, "Flipper-Zero-Laser-Tag");
  70. canvas_draw_frame(canvas, 0, 0, 128, 64);
  71. canvas_draw_line(canvas, 0, 30, 127, 30);
  72. canvas_draw_circle(canvas, 110, 15, 12);
  73. canvas_draw_disc(canvas, 110, 15, 4);
  74. } else if(app->state == LaserTagStateTeamSelect) {
  75. canvas_clear(canvas);
  76. canvas_draw_frame(canvas, 0, 0, 128, 64);
  77. canvas_set_font(canvas, FontPrimary);
  78. canvas_draw_str(canvas, 14, 13, "SELECT TEAM");
  79. canvas_draw_line(canvas, 0, 16, 127, 16);
  80. canvas_set_font(canvas, FontSecondary);
  81. canvas_draw_str(canvas, 5, 30, "LEFT");
  82. canvas_draw_str(canvas, 95, 30, "RIGHT");
  83. canvas_set_font(canvas, FontPrimary);
  84. canvas_draw_str(canvas, 10, 45, "RED");
  85. canvas_draw_str(canvas, 95, 45, "BLUE");
  86. // Gun icon for Red team
  87. canvas_draw_line(canvas, 10, 50, 25, 50);
  88. canvas_draw_line(canvas, 25, 50, 25, 55);
  89. canvas_draw_line(canvas, 10, 55, 25, 55);
  90. canvas_draw_line(canvas, 15, 55, 15, 60);
  91. // Gun icon for Blue team (facing left)
  92. canvas_draw_line(canvas, 95, 50, 110, 50);
  93. canvas_draw_line(canvas, 95, 50, 95, 55);
  94. canvas_draw_line(canvas, 95, 55, 110, 55);
  95. canvas_draw_line(canvas, 105, 55, 105, 60);
  96. // Laser beams
  97. canvas_draw_line(canvas, 25, 52, 60, 32);
  98. canvas_draw_line(canvas, 95, 52, 60, 32);
  99. // Targets where lasers hit
  100. canvas_draw_circle(canvas, 60, 32, 5);
  101. canvas_draw_circle(canvas, 60, 32, 2);
  102. } else if(app->state == LaserTagStateGameOver) {
  103. canvas_clear(canvas);
  104. canvas_set_font(canvas, FontPrimary);
  105. // Display "GAME OVER!" centered on the screen
  106. canvas_draw_str_aligned(canvas, 64, 25, AlignCenter, AlignCenter, "GAME OVER!");
  107. // Add a solid block border around the screen
  108. for(int x = 0; x < 128; x += 8) {
  109. canvas_draw_box(canvas, x, 0, 8, 8);
  110. canvas_draw_box(canvas, x, 56, 8, 8);
  111. }
  112. for(int y = 8; y < 56; y += 8) {
  113. canvas_draw_box(canvas, 0, y, 8, 8);
  114. canvas_draw_box(canvas, 120, y, 8, 8);
  115. }
  116. canvas_set_font(canvas, FontSecondary);
  117. canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "Press OK to Restart");
  118. } else if(app->view) {
  119. FURI_LOG_D(TAG, "Drawing game view");
  120. laser_tag_view_draw(laser_tag_view_get_view(app->view), canvas);
  121. }
  122. FURI_LOG_D(TAG, "Exiting draw callback");
  123. }
  124. static bool matching_team(LaserTagApp* app, uint8_t data) {
  125. if(data == 0) {
  126. return true;
  127. } else if(game_state_get_team(app->game_state) == TeamRed) {
  128. return data == 0xA1;
  129. } else if(game_state_get_team(app->game_state) == TeamBlue) {
  130. return data == 0xB2;
  131. }
  132. return false;
  133. }
  134. static void tag_callback(uint8_t* data, uint8_t length, void* context) {
  135. LaserTagApp* app = (LaserTagApp*)context;
  136. if(length != 5) {
  137. FURI_LOG_W(TAG, "Tag is not for game. Length: %d", length);
  138. return;
  139. }
  140. if(data[0] != 0x13 || data[1] != 0x37) {
  141. FURI_LOG_D(
  142. TAG,
  143. "Tag is not for game. Data: %02x %02x %02x %02x %02x",
  144. data[0],
  145. data[1],
  146. data[2],
  147. data[3],
  148. data[4]);
  149. return;
  150. }
  151. if(matching_team(app, data[2])) {
  152. if(data[3] == 0xFD) {
  153. uint16_t max_delta_ammo = data[4];
  154. uint16_t ammo = game_state_get_ammo(app->game_state);
  155. uint16_t delta_ammo = INITIAL_AMMO - ammo;
  156. if(delta_ammo > max_delta_ammo) {
  157. delta_ammo = max_delta_ammo;
  158. }
  159. game_state_increase_ammo(app->game_state, delta_ammo);
  160. FURI_LOG_D(TAG, "Increased ammo by: %d", delta_ammo);
  161. } else {
  162. FURI_LOG_W(TAG, "Tag action unknown: %02x %02x", data[3], data[4]);
  163. }
  164. } else {
  165. FURI_LOG_I(TAG, "Tag not for team: %02x", data[2]);
  166. }
  167. }
  168. LaserTagApp* laser_tag_app_alloc() {
  169. FURI_LOG_D(TAG, "Allocating Laser Tag App");
  170. LaserTagApp* app = malloc(sizeof(LaserTagApp));
  171. if(!app) {
  172. FURI_LOG_E(TAG, "Failed to allocate LaserTagApp");
  173. return NULL;
  174. }
  175. FURI_LOG_I(TAG, "LaserTagApp allocated successfully");
  176. memset(app, 0, sizeof(LaserTagApp));
  177. app->gui = furi_record_open(RECORD_GUI);
  178. app->view_port = view_port_alloc();
  179. app->view = laser_tag_view_alloc();
  180. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  181. app->game_state = game_state_alloc();
  182. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  183. if(!app->gui || !app->view_port || !app->view || !app->notifications || !app->game_state ||
  184. !app->event_queue) {
  185. FURI_LOG_E(TAG, "Failed to allocate resources for LaserTagApp");
  186. laser_tag_app_free(app);
  187. return NULL;
  188. }
  189. app->state = LaserTagStateSplashScreen;
  190. app->need_redraw = true;
  191. FURI_LOG_I(TAG, "Initial state set to SplashScreen");
  192. view_port_draw_callback_set(app->view_port, laser_tag_app_draw_callback, app);
  193. view_port_input_callback_set(app->view_port, laser_tag_app_input_callback, app);
  194. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  195. FURI_LOG_D(TAG, "ViewPort callbacks set and added to GUI");
  196. app->timer = furi_timer_alloc(laser_tag_app_timer_callback, FuriTimerTypePeriodic, app);
  197. if(!app->timer) {
  198. FURI_LOG_E(TAG, "Failed to allocate timer");
  199. laser_tag_app_free(app);
  200. return NULL;
  201. }
  202. FURI_LOG_I(TAG, "Timer allocated");
  203. app->reader = lfrfid_reader_alloc();
  204. lfrfid_reader_set_tag_callback(app->reader, "EM4100", tag_callback, app);
  205. furi_timer_start(app->timer, furi_kernel_get_tick_frequency());
  206. FURI_LOG_D(TAG, "Timer started");
  207. return app;
  208. }
  209. void laser_tag_app_free(LaserTagApp* app) {
  210. FURI_LOG_D(TAG, "Freeing Laser Tag App");
  211. furi_assert(app);
  212. furi_timer_free(app->timer);
  213. view_port_enabled_set(app->view_port, false);
  214. gui_remove_view_port(app->gui, app->view_port);
  215. view_port_free(app->view_port);
  216. laser_tag_view_free(app->view);
  217. furi_message_queue_free(app->event_queue);
  218. if(app->ir_controller) {
  219. infrared_controller_free(app->ir_controller);
  220. }
  221. if(app->reader) {
  222. lfrfid_reader_free(app->reader);
  223. app->reader = NULL;
  224. }
  225. free(app->game_state);
  226. furi_record_close(RECORD_GUI);
  227. furi_record_close(RECORD_NOTIFICATION);
  228. free(app);
  229. FURI_LOG_I(TAG, "Laser Tag App freed successfully");
  230. }
  231. void laser_tag_app_fire(LaserTagApp* app) {
  232. furi_assert(app);
  233. FURI_LOG_D(TAG, "Firing laser");
  234. if(!app->ir_controller) {
  235. FURI_LOG_E(TAG, "IR controller is NULL in laser_tag_app_fire");
  236. return;
  237. }
  238. infrared_controller_send(app->ir_controller);
  239. FURI_LOG_D(TAG, "Laser fired, decreasing ammo by 1");
  240. game_state_decrease_ammo(app->game_state, 1);
  241. notification_message(app->notifications, &sequence_short_beep);
  242. if(game_state_get_team(app->game_state) == TeamBlue) {
  243. notification_message(app->notifications, &sequence_blink_blue_100);
  244. FURI_LOG_I(TAG, "Notifying user with blink blue and short beep");
  245. } else {
  246. notification_message(app->notifications, &sequence_blink_red_100);
  247. FURI_LOG_I(TAG, "Notifying user with blink red and short beep");
  248. }
  249. app->need_redraw = true;
  250. }
  251. void laser_tag_app_handle_hit(LaserTagApp* app) {
  252. furi_assert(app);
  253. FURI_LOG_D(TAG, "Handling hit, decreasing health by 10");
  254. game_state_decrease_health(app->game_state, 10);
  255. notification_message(app->notifications, &sequence_vibro_1);
  256. FURI_LOG_I(TAG, "Notifying user with vibration");
  257. if(game_state_is_game_over(app->game_state)) {
  258. FURI_LOG_I(TAG, "Game over, switching to Game Over screen");
  259. notification_message(app->notifications, &sequence_error);
  260. app->state = LaserTagStateGameOver;
  261. app->need_redraw = true;
  262. }
  263. }
  264. static bool laser_tag_app_enter_game_state(LaserTagApp* app) {
  265. furi_assert(app);
  266. FURI_LOG_I(TAG, "Entering game state");
  267. app->state = LaserTagStateGame;
  268. game_state_reset(app->game_state);
  269. FURI_LOG_D(TAG, "Game state reset");
  270. laser_tag_view_update(app->view, app->game_state);
  271. FURI_LOG_D(TAG, "View updated with new game state");
  272. if(app->ir_controller) {
  273. infrared_controller_free(app->ir_controller);
  274. app->ir_controller = NULL;
  275. }
  276. app->ir_controller = infrared_controller_alloc();
  277. if(!app->ir_controller) {
  278. FURI_LOG_E(TAG, "Failed to allocate IR controller");
  279. return false;
  280. }
  281. FURI_LOG_I(TAG, "IR controller allocated");
  282. infrared_controller_set_team(app->ir_controller, game_state_get_team(app->game_state));
  283. FURI_LOG_D(TAG, "IR controller team set");
  284. app->need_redraw = true;
  285. return true;
  286. }
  287. int32_t laser_tag_app(void* p) {
  288. UNUSED(p);
  289. FURI_LOG_I(TAG, "Laser Tag app starting");
  290. LaserTagApp* app = laser_tag_app_alloc();
  291. if(!app) {
  292. FURI_LOG_E(TAG, "Failed to allocate application");
  293. return -1;
  294. }
  295. FURI_LOG_D(TAG, "LaserTagApp allocated successfully");
  296. InputEvent event;
  297. bool running = true;
  298. while(running) {
  299. FURI_LOG_D(TAG, "Start of main loop iteration");
  300. update_infrared_board_status(app->ir_controller);
  301. FuriStatus status = furi_message_queue_get(app->event_queue, &event, 100);
  302. if(status == FuriStatusOk) {
  303. FURI_LOG_D(TAG, "Received input event: type=%d, key=%d", event.type, event.key);
  304. if(event.type == InputTypePress || event.type == InputTypeRepeat) {
  305. if(app->state == LaserTagStateSplashScreen ||
  306. app->state == LaserTagStateTeamSelect) {
  307. switch(event.key) {
  308. case InputKeyLeft:
  309. FURI_LOG_I(TAG, "Red team selected");
  310. game_state_set_team(app->game_state, TeamRed);
  311. if(!laser_tag_app_enter_game_state(app)) {
  312. running = false;
  313. }
  314. break;
  315. case InputKeyRight:
  316. FURI_LOG_I(TAG, "Blue team selected");
  317. game_state_set_team(app->game_state, TeamBlue);
  318. if(!laser_tag_app_enter_game_state(app)) {
  319. running = false;
  320. }
  321. break;
  322. case InputKeyBack:
  323. FURI_LOG_I(TAG, "Back key pressed, exiting");
  324. running = false;
  325. break;
  326. default:
  327. break;
  328. }
  329. } else if(app->state == LaserTagStateGameOver) {
  330. if(event.key == InputKeyOk) {
  331. FURI_LOG_I(TAG, "OK key pressed, restarting game");
  332. // Restart game by resetting game state and transitioning to splash screen
  333. game_state_reset(app->game_state);
  334. app->state = LaserTagStateSplashScreen;
  335. app->need_redraw = true;
  336. }
  337. } else if(app->state == LaserTagStateGame) {
  338. if(event.key == InputKeyDown && game_state_get_ammo(app->game_state) == 0) {
  339. // Reload ammo when Down button is pressed and ammo is depleted
  340. FURI_LOG_I(TAG, "Down key pressed, reloading ammo");
  341. game_state_increase_ammo(app->game_state, INITIAL_AMMO);
  342. app->need_redraw = true;
  343. } else {
  344. switch(event.key) {
  345. case InputKeyBack:
  346. FURI_LOG_I(TAG, "Back key pressed, exiting");
  347. running = false;
  348. break;
  349. case InputKeyOk:
  350. FURI_LOG_I(TAG, "OK key pressed, firing laser");
  351. laser_tag_app_fire(app);
  352. break;
  353. case InputKeyUp:
  354. FURI_LOG_I(TAG, "Up key pressed, scanning for ammo");
  355. notification_message(app->notifications, &sequence_short_beep);
  356. uint16_t ammo = game_state_get_ammo(app->game_state);
  357. infrared_controller_pause(app->ir_controller);
  358. lfrfid_reader_start(app->reader);
  359. for(int i = 0; i < 30; i++) {
  360. furi_delay_ms(100);
  361. if(ammo != game_state_get_ammo(app->game_state)) {
  362. break;
  363. }
  364. }
  365. lfrfid_reader_stop(app->reader);
  366. infrared_controller_resume(app->ir_controller);
  367. if(ammo != game_state_get_ammo(app->game_state)) {
  368. notification_message(app->notifications, &sequence_success);
  369. } else {
  370. notification_message(app->notifications, &sequence_error);
  371. }
  372. app->need_redraw = true;
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. }
  379. }
  380. } else if(status == FuriStatusErrorTimeout) {
  381. FURI_LOG_D(TAG, "No input event, continuing");
  382. } else {
  383. FURI_LOG_E(TAG, "Failed to get input event, status: %d", status);
  384. }
  385. if(app->state == LaserTagStateGame && app->ir_controller) {
  386. if(infrared_controller_receive(app->ir_controller)) {
  387. FURI_LOG_D(TAG, "Hit received, processing");
  388. laser_tag_app_handle_hit(app);
  389. }
  390. if(game_state_is_game_over(app->game_state)) {
  391. FURI_LOG_I(TAG, "Game over, notifying user with error sequence");
  392. notification_message(app->notifications, &sequence_error);
  393. // Stop game logic after game over
  394. app->state = LaserTagStateGameOver;
  395. app->need_redraw = true;
  396. }
  397. } else if(app->state == LaserTagStateGameOver) {
  398. if(event.key == InputKeyOk) {
  399. FURI_LOG_I(TAG, "OK key pressed, restarting game");
  400. game_state_reset(app->game_state);
  401. app->state = LaserTagStateSplashScreen;
  402. app->need_redraw = true;
  403. }
  404. }
  405. if(app->need_redraw) {
  406. FURI_LOG_D(TAG, "Updating viewport");
  407. view_port_update(app->view_port);
  408. app->need_redraw = false;
  409. }
  410. FURI_LOG_D(TAG, "End of main loop iteration");
  411. furi_delay_ms(10);
  412. }
  413. FURI_LOG_I(TAG, "Laser Tag app exiting");
  414. laser_tag_app_free(app);
  415. return 0;
  416. }