wire_tester.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <notification/notification.h>
  6. #include <notification/notification_messages.h>
  7. #include <stdlib.h>
  8. #include "wire_tester_icons.h"
  9. //#define TAG "wire_tester"
  10. static const uint32_t EVENT_PERIOD_MS = 10; // check for input changes often
  11. static const float BEEP_FREQ = 2400.0f; // louder than other frequencies
  12. static const float BEEP_VOL = 0.8f;
  13. static const GpioPin* const INPUT_PIN = &gpio_ext_pb2; // pin 6
  14. static void start_feedback(NotificationApp* notifications) {
  15. // set LED to green
  16. notification_message_block(notifications, &sequence_set_only_green_255);
  17. // start beep
  18. if (furi_hal_speaker_acquire(1000)) {
  19. furi_hal_speaker_start(BEEP_FREQ, BEEP_VOL);
  20. }
  21. }
  22. static void stop_feedback(NotificationApp* notifications) {
  23. // clear LED
  24. notification_message_block(notifications, &sequence_reset_rgb);
  25. // stop beep
  26. if (furi_hal_speaker_is_mine()) {
  27. furi_hal_speaker_stop();
  28. furi_hal_speaker_release();
  29. }
  30. }
  31. static void draw_callback(Canvas* canvas, void* ctx) {
  32. furi_assert(ctx);
  33. canvas_clear(canvas);
  34. canvas_draw_icon(canvas, 0, 0, &I_background_128x64);
  35. }
  36. static void input_callback(InputEvent* input_event, void* ctx) {
  37. furi_assert(ctx);
  38. FuriMessageQueue* event_queue = ctx;
  39. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  40. }
  41. int32_t app_main(void* p) {
  42. UNUSED(p);
  43. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  44. // force backlight on because our hands will be busy with wires
  45. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  46. notification_message_block(notifications, &sequence_display_backlight_enforce_on);
  47. ViewPort* view_port = view_port_alloc();
  48. view_port_draw_callback_set(view_port, draw_callback, view_port);
  49. view_port_input_callback_set(view_port, input_callback, event_queue);
  50. Gui* gui = furi_record_open(RECORD_GUI);
  51. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  52. view_port_update(view_port);
  53. stop_feedback(notifications);
  54. // set input to be normally high; it will be low when shorted to ground
  55. furi_hal_gpio_init(INPUT_PIN, GpioModeInput, GpioPullUp, GpioSpeedLow);
  56. bool alarming = false;
  57. bool running = true;
  58. while (running) {
  59. // start and stop feedback on the transition
  60. bool continuous = !furi_hal_gpio_read(INPUT_PIN);
  61. if (continuous && !alarming) {
  62. start_feedback(notifications);
  63. } else if (!continuous && alarming) {
  64. stop_feedback(notifications);
  65. }
  66. alarming = continuous;
  67. // exit on back key
  68. InputEvent event;
  69. if (furi_message_queue_get(event_queue, &event, EVENT_PERIOD_MS) == FuriStatusOk) {
  70. if ((event.type == InputTypePress || event.type == InputTypeRepeat)
  71. && event.key == InputKeyBack) {
  72. running = false;
  73. }
  74. }
  75. view_port_update(view_port);
  76. }
  77. // return control of the LED, beeper, and backlight
  78. stop_feedback(notifications);
  79. notification_message_block(notifications, &sequence_display_backlight_enforce_auto);
  80. view_port_enabled_set(view_port, false);
  81. gui_remove_view_port(gui, view_port);
  82. view_port_free(view_port);
  83. furi_message_queue_free(event_queue);
  84. furi_record_close(RECORD_GUI);
  85. furi_record_close(RECORD_NOTIFICATION);
  86. return 0;
  87. }