cyborg_detector_app.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "cyborg_detector_app.h"
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <furi_hal_nfc.h>
  6. #include <core/log.h>
  7. #define TAG "CyborgDetectorApp"
  8. static void cyborg_detector_draw_callback(Canvas* canvas, void* context) {
  9. CyborgDetectorApp* app = context;
  10. (void)app;
  11. // Clear the canvas
  12. canvas_clear(canvas);
  13. // Draw the "CYBORG DETECTOR" text at the top, centered
  14. canvas_set_font(canvas, FontPrimary);
  15. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignTop, "CYBORG DETECTOR");
  16. // Draw a target in the center
  17. canvas_draw_circle(canvas, 64, 32, 10); // Outer circle
  18. canvas_draw_circle(canvas, 64, 32, 5); // Inner circle
  19. canvas_draw_line(canvas, 54, 32, 74, 32); // Horizontal line
  20. canvas_draw_line(canvas, 64, 22, 64, 42); // Vertical line
  21. // Draw the "Scanning..." text at the bottom, centered
  22. canvas_set_font(canvas, FontSecondary);
  23. canvas_draw_str_aligned(canvas, 64, 54, AlignCenter, AlignBottom, "Scanning...");
  24. }
  25. static void cyborg_detector_input_callback(InputEvent* input_event, void* context) {
  26. furi_assert(context);
  27. CyborgDetectorApp* app = context;
  28. furi_message_queue_put(app->event_queue, input_event, FuriWaitForever);
  29. }
  30. static bool cyborg_detector_activate_nfc_field(CyborgDetectorApp* app) {
  31. FURI_LOG_I(TAG, "Activating NFC field");
  32. furi_hal_nfc_acquire();
  33. furi_hal_nfc_low_power_mode_stop();
  34. FuriHalNfcError error = furi_hal_nfc_poller_field_on();
  35. if(error != FuriHalNfcErrorNone) {
  36. FURI_LOG_E(TAG, "Failed to turn on NFC field: %d", error);
  37. furi_hal_nfc_release();
  38. return false;
  39. }
  40. app->field_active = true;
  41. FURI_LOG_I(TAG, "NFC field activated");
  42. return true;
  43. }
  44. static void cyborg_detector_deactivate_nfc_field(CyborgDetectorApp* app) {
  45. FURI_LOG_I(TAG, "Deactivating NFC field");
  46. furi_hal_nfc_low_power_mode_start();
  47. furi_hal_nfc_release();
  48. app->field_active = false;
  49. FURI_LOG_I(TAG, "NFC field deactivated");
  50. }
  51. CyborgDetectorApp* cyborg_detector_app_alloc() {
  52. CyborgDetectorApp* app = malloc(sizeof(CyborgDetectorApp));
  53. app->gui = furi_record_open(RECORD_GUI);
  54. app->view_port = view_port_alloc();
  55. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  56. app->running = true;
  57. app->field_active = false;
  58. view_port_draw_callback_set(app->view_port, cyborg_detector_draw_callback, app);
  59. view_port_input_callback_set(app->view_port, cyborg_detector_input_callback, app);
  60. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  61. return app;
  62. }
  63. void cyborg_detector_app_free(CyborgDetectorApp* app) {
  64. furi_assert(app);
  65. gui_remove_view_port(app->gui, app->view_port);
  66. view_port_free(app->view_port);
  67. furi_message_queue_free(app->event_queue);
  68. furi_record_close(RECORD_GUI);
  69. free(app);
  70. }
  71. int32_t cyborg_detector_app(void* p) {
  72. UNUSED(p);
  73. FURI_LOG_I(TAG, "Starting Cyborg Detector app");
  74. CyborgDetectorApp* app = cyborg_detector_app_alloc();
  75. if(!cyborg_detector_activate_nfc_field(app)) {
  76. FURI_LOG_E(TAG, "Failed to activate NFC field, exiting");
  77. cyborg_detector_app_free(app);
  78. return 255;
  79. }
  80. InputEvent event;
  81. while(app->running) {
  82. if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
  83. if(event.type == InputTypeShort && event.key == InputKeyBack) {
  84. app->running = false;
  85. }
  86. }
  87. view_port_update(app->view_port);
  88. }
  89. cyborg_detector_deactivate_nfc_field(app);
  90. cyborg_detector_app_free(app);
  91. FURI_LOG_I(TAG, "Cyborg Detector app finished");
  92. return 0;
  93. }