laser_tag_app.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. if(app->ir_controller->processing_signal) {
  239. FURI_LOG_W(TAG, "Cannot fire, hit is being processed");
  240. return;
  241. }
  242. infrared_controller_send(app->ir_controller);
  243. FURI_LOG_D(TAG, "Laser fired, decreasing ammo by 1");
  244. game_state_decrease_ammo(app->game_state, 1);
  245. notification_message(app->notifications, &sequence_short_beep);
  246. if(game_state_get_team(app->game_state) == TeamBlue) {
  247. notification_message(app->notifications, &sequence_blink_blue_100);
  248. FURI_LOG_I(TAG, "Notifying user with blink blue and short beep");
  249. } else {
  250. notification_message(app->notifications, &sequence_blink_red_100);
  251. FURI_LOG_I(TAG, "Notifying user with blink red and short beep");
  252. }
  253. app->need_redraw = true;
  254. }
  255. void laser_tag_app_handle_hit(LaserTagApp* app) {
  256. furi_assert(app);
  257. FURI_LOG_D(TAG, "Handling hit, decreasing health by 10");
  258. game_state_decrease_health(app->game_state, 10);
  259. notification_message(app->notifications, &sequence_vibro_1);
  260. FURI_LOG_I(TAG, "Notifying user with vibration");
  261. if(game_state_is_game_over(app->game_state)) {
  262. FURI_LOG_I(TAG, "Game over, switching to Game Over screen");
  263. notification_message(app->notifications, &sequence_error);
  264. app->state = LaserTagStateGameOver;
  265. app->need_redraw = true;
  266. }
  267. }
  268. static bool laser_tag_app_enter_game_state(LaserTagApp* app) {
  269. furi_assert(app);
  270. FURI_LOG_I(TAG, "Entering game state");
  271. app->state = LaserTagStateGame;
  272. game_state_reset(app->game_state);
  273. FURI_LOG_D(TAG, "Game state reset");
  274. laser_tag_view_update(app->view, app->game_state);
  275. FURI_LOG_D(TAG, "View updated with new game state");
  276. if(app->ir_controller) {
  277. infrared_controller_free(app->ir_controller);
  278. app->ir_controller = NULL;
  279. }
  280. app->ir_controller = infrared_controller_alloc();
  281. if(!app->ir_controller) {
  282. FURI_LOG_E(TAG, "Failed to allocate IR controller");
  283. return false;
  284. }
  285. FURI_LOG_I(TAG, "IR controller allocated");
  286. infrared_controller_set_team(app->ir_controller, game_state_get_team(app->game_state));
  287. FURI_LOG_D(TAG, "IR controller team set");
  288. app->need_redraw = true;
  289. return true;
  290. }
  291. int32_t laser_tag_app(void* p) {
  292. UNUSED(p);
  293. FURI_LOG_I(TAG, "Laser Tag app starting");
  294. LaserTagApp* app = laser_tag_app_alloc();
  295. if(!app) {
  296. FURI_LOG_E(TAG, "Failed to allocate application");
  297. return -1;
  298. }
  299. FURI_LOG_D(TAG, "LaserTagApp allocated successfully");
  300. InputEvent event;
  301. bool running = true;
  302. while(running) {
  303. FURI_LOG_D(TAG, "Start of main loop iteration");
  304. update_infrared_board_status(app->ir_controller);
  305. FuriStatus status = furi_message_queue_get(app->event_queue, &event, 100);
  306. if(status == FuriStatusOk) {
  307. FURI_LOG_D(TAG, "Received input event: type=%d, key=%d", event.type, event.key);
  308. if(event.type == InputTypePress || event.type == InputTypeRepeat) {
  309. if(app->state == LaserTagStateSplashScreen ||
  310. app->state == LaserTagStateTeamSelect) {
  311. switch(event.key) {
  312. case InputKeyLeft:
  313. FURI_LOG_I(TAG, "Red team selected");
  314. game_state_set_team(app->game_state, TeamRed);
  315. if(!laser_tag_app_enter_game_state(app)) {
  316. running = false;
  317. }
  318. break;
  319. case InputKeyRight:
  320. FURI_LOG_I(TAG, "Blue team selected");
  321. game_state_set_team(app->game_state, TeamBlue);
  322. if(!laser_tag_app_enter_game_state(app)) {
  323. running = false;
  324. }
  325. break;
  326. case InputKeyBack:
  327. FURI_LOG_I(TAG, "Back key pressed, exiting");
  328. running = false;
  329. break;
  330. default:
  331. break;
  332. }
  333. } else if(app->state == LaserTagStateGameOver) {
  334. if(event.key == InputKeyOk) {
  335. FURI_LOG_I(TAG, "OK key pressed, restarting game");
  336. // Restart game by resetting game state and transitioning to splash screen
  337. game_state_reset(app->game_state);
  338. app->state = LaserTagStateSplashScreen;
  339. app->need_redraw = true;
  340. }
  341. } else if(app->state == LaserTagStateGame) {
  342. if(event.key == InputKeyDown && game_state_get_ammo(app->game_state) == 0) {
  343. // Reload ammo when Down button is pressed and ammo is depleted
  344. FURI_LOG_I(TAG, "Down key pressed, reloading ammo");
  345. game_state_increase_ammo(app->game_state, INITIAL_AMMO);
  346. app->need_redraw = true;
  347. } else {
  348. switch(event.key) {
  349. case InputKeyBack:
  350. FURI_LOG_I(TAG, "Back key pressed, exiting");
  351. running = false;
  352. break;
  353. case InputKeyOk:
  354. FURI_LOG_I(TAG, "OK key pressed, firing laser");
  355. laser_tag_app_fire(app);
  356. break;
  357. case InputKeyUp:
  358. FURI_LOG_I(TAG, "Up key pressed, scanning for ammo");
  359. notification_message(app->notifications, &sequence_short_beep);
  360. uint16_t ammo = game_state_get_ammo(app->game_state);
  361. infrared_controller_pause(app->ir_controller);
  362. lfrfid_reader_start(app->reader);
  363. for(int i = 0; i < 30; i++) {
  364. furi_delay_ms(100);
  365. if(ammo != game_state_get_ammo(app->game_state)) {
  366. break;
  367. }
  368. }
  369. lfrfid_reader_stop(app->reader);
  370. infrared_controller_resume(app->ir_controller);
  371. if(ammo != game_state_get_ammo(app->game_state)) {
  372. notification_message(app->notifications, &sequence_success);
  373. } else {
  374. notification_message(app->notifications, &sequence_error);
  375. }
  376. app->need_redraw = true;
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. } else if(status == FuriStatusErrorTimeout) {
  385. FURI_LOG_D(TAG, "No input event, continuing");
  386. } else {
  387. FURI_LOG_E(TAG, "Failed to get input event, status: %d", status);
  388. }
  389. if(app->state == LaserTagStateGame && app->ir_controller) {
  390. if(infrared_controller_receive(app->ir_controller)) {
  391. FURI_LOG_D(TAG, "Hit received, processing");
  392. laser_tag_app_handle_hit(app);
  393. }
  394. if(game_state_is_game_over(app->game_state)) {
  395. FURI_LOG_I(TAG, "Game over, notifying user with error sequence");
  396. notification_message(app->notifications, &sequence_error);
  397. // Stop game logic after game over
  398. app->state = LaserTagStateGameOver;
  399. app->need_redraw = true;
  400. }
  401. } else if(app->state == LaserTagStateGameOver) {
  402. if(event.key == InputKeyOk) {
  403. FURI_LOG_I(TAG, "OK key pressed, restarting game");
  404. game_state_reset(app->game_state);
  405. app->state = LaserTagStateSplashScreen;
  406. app->need_redraw = true;
  407. }
  408. }
  409. if(app->need_redraw) {
  410. FURI_LOG_D(TAG, "Updating viewport");
  411. view_port_update(app->view_port);
  412. app->need_redraw = false;
  413. }
  414. FURI_LOG_D(TAG, "End of main loop iteration");
  415. furi_delay_ms(10);
  416. }
  417. FURI_LOG_I(TAG, "Laser Tag app exiting");
  418. laser_tag_app_free(app);
  419. return 0;
  420. }