bt_trigger.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "bt_trigger.h"
  2. __int32_t bt_trigger_app(void* p) {
  3. //Fake using p to compile
  4. UNUSED(p);
  5. AppStruct* app = appStructAlloc();
  6. //bt_disconnect(app->bt);
  7. // Wait 2nd core to update nvm storage
  8. //furi_delay_ms(200);
  9. bt_keys_storage_set_storage_path(app->bt, HID_BT_KEYS_STORAGE_PATH);
  10. if(!bt_set_profile(app->bt, BtProfileHidKeyboard)) {
  11. FURI_LOG_E(TAG, "Failed to switch to HID profile");
  12. }
  13. furi_hal_bt_start_advertising();
  14. bt_set_status_changed_callback(app->bt, bt_hid_connection_status_changed_callback, app);
  15. dolphin_deed(DolphinDeedPluginStart);
  16. //An event
  17. IosTriggerEvent event;
  18. //List of 8 events
  19. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(IosTriggerEvent));
  20. //A timer
  21. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  22. //Callback for the display
  23. view_port_draw_callback_set(app->view_port, draw_callback, app);
  24. //Callback for the inputs passing the list as param
  25. view_port_input_callback_set(app->view_port, input_callback, event_queue);
  26. //Linking the drawin on the display
  27. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  28. //Main loop
  29. while(app->running) {
  30. //Geting new event from the envent list in the event variable
  31. //waiting forever if the list is empty
  32. //checking status as ok
  33. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  34. //Dealing with events one by one
  35. switch(event.type) {
  36. case(EventTypeInput):
  37. //On ne considère que les appuies courts
  38. if(event.input.type == InputTypeShort) {
  39. switch(event.input.key) {
  40. case(InputKeyBack):
  41. //Breaking main loop if the back key is pressed
  42. app->shooting = false;
  43. app->running = false;
  44. break;
  45. case(InputKeyOk): //Take a shot and start intervalometer
  46. if(app->delay > 0) {
  47. app->shooting = !app->shooting;
  48. if(app->shooting) {
  49. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
  50. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
  51. notification_message(app->notifications, &sequence_blink_blue_100);
  52. app->shots++;
  53. //Timer triggered every delay ms
  54. furi_timer_start(timer, app->delay * 1000);
  55. } else {
  56. //Timer triggered every delay ms
  57. furi_timer_stop(timer);
  58. }
  59. }
  60. break;
  61. case(InputKeyUp): //Increase delay
  62. if(!app->shooting) {
  63. app->delay++;
  64. }
  65. break;
  66. case(InputKeyDown): //Decrease delay
  67. if(!app->shooting && app->delay > 1) {
  68. app->delay--;
  69. }
  70. break;
  71. case(InputKeyLeft): //Reset shots counter
  72. if(!app->shooting) {
  73. app->shots = 0;
  74. }
  75. break;
  76. case(InputKeyRight): //Take a shot
  77. if(!app->shooting) {
  78. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
  79. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
  80. notification_message(app->notifications, &sequence_blink_blue_100);
  81. app->shots++;
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. view_port_update(app->view_port);
  89. break;
  90. case(EventTypeTick):
  91. if(app->shooting) {
  92. //sending command to trigger via BT
  93. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
  94. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
  95. notification_message(app->notifications, &sequence_blink_blue_100);
  96. app->shots++;
  97. }
  98. view_port_update(app->view_port);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. //Going back to serial mode BT
  105. bt_set_status_changed_callback(app->bt, NULL, NULL);
  106. bt_disconnect(app->bt);
  107. // Wait 2nd core to update nvm storage
  108. furi_delay_ms(200);
  109. bt_keys_storage_set_default_path(app->bt);
  110. if(!bt_set_profile(app->bt, BtProfileSerial)) {
  111. FURI_LOG_E(TAG, "Failed to switch to Serial profile");
  112. }
  113. //Freeing memory
  114. furi_message_queue_free(event_queue);
  115. //Freeing timer
  116. furi_timer_free(timer);
  117. cleanUpBeforeYouLeave(app);
  118. return 0;
  119. }
  120. //Callback display
  121. static void draw_callback(Canvas* canvas, void* ctx) {
  122. AppStruct* app = ctx;
  123. char chaine_photo[36];
  124. char chaine_delais[36];
  125. char chaine_shooting[36];
  126. snprintf(chaine_photo, sizeof(chaine_photo), "%i shots", app->shots);
  127. snprintf(chaine_delais, sizeof(chaine_delais), "%i", app->delay);
  128. if(app->shooting) {
  129. snprintf(chaine_shooting, sizeof(chaine_shooting), "Press to stop");
  130. } else {
  131. snprintf(chaine_shooting, sizeof(chaine_shooting), "Press to start");
  132. }
  133. canvas_clear(canvas);
  134. canvas_draw_frame(canvas, 0, 0, 128, 64);
  135. canvas_set_font(canvas, FontPrimary);
  136. canvas_draw_str(canvas, 2, 10, "iOS Intervalometer");
  137. //Represent
  138. canvas_set_font(canvas, FontSecondary);
  139. canvas_draw_str(canvas, 92, 62, "Nem0oo");
  140. if(app->connected) {
  141. canvas_draw_icon(canvas, 111, 2, &I_Ble_connected_15x15);
  142. canvas_set_font(canvas, FontSecondary);
  143. //Delay line
  144. canvas_draw_icon(canvas, 3, 19, &I_ButtonDown_7x4);
  145. canvas_draw_icon(canvas, 3, 14, &I_ButtonUp_7x4);
  146. canvas_draw_str(canvas, 13, 22, "Delay (in sec)");
  147. canvas_draw_str(canvas, 71, 22, chaine_delais);
  148. //Start/stop line
  149. canvas_draw_icon(canvas, 2, 25, &I_Ok_btn_9x9);
  150. canvas_draw_str(canvas, 13, 33, chaine_shooting);
  151. //Single shot line
  152. canvas_draw_icon(canvas, 6, 36, &I_ButtonRight_4x7);
  153. canvas_draw_str(canvas, 13, 43, "Single shot");
  154. //Reset shot count line
  155. canvas_draw_icon(canvas, 3, 45, &I_ButtonLeft_4x7);
  156. canvas_draw_str(canvas, 13, 52, "Reset shot count");
  157. //Shots number line
  158. canvas_draw_icon(canvas, 2, 53, &I_dir_10px);
  159. canvas_draw_str(canvas, 14, 62, chaine_photo);
  160. } else {
  161. canvas_draw_icon(canvas, 111, 2, &I_Ble_disconnected_15x15);
  162. canvas_draw_icon(canvas, 1, 21, &I_WarningDolphin_45x42);
  163. canvas_set_font(canvas, FontSecondary);
  164. canvas_draw_str(canvas, 48, 37, "Awaiting bluetooth");
  165. }
  166. }
  167. //Input callbacks
  168. static void input_callback(InputEvent* input_event, void* ctx) {
  169. furi_assert(ctx);
  170. //Getting our event queue
  171. FuriMessageQueue* event_queue = ctx;
  172. //Adding the event to our custom Struct
  173. IosTriggerEvent event = {.type = EventTypeInput, .input = *input_event};
  174. //Adding our event to the event queue
  175. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  176. }
  177. //Timer callback
  178. static void timer_callback(FuriMessageQueue* event_queue) {
  179. //check eventqueue is not null
  180. furi_assert(event_queue);
  181. //creating event and adding it to the event list
  182. IosTriggerEvent event = {.type = EventTypeTick};
  183. furi_message_queue_put(event_queue, &event, 0);
  184. }
  185. static void bt_hid_connection_status_changed_callback(BtStatus status, void* context) {
  186. furi_assert(context);
  187. AppStruct* app = context;
  188. app->connected = (status == BtStatusConnected);
  189. view_port_update(app->view_port);
  190. }
  191. AppStruct* appStructAlloc() {
  192. AppStruct* app = malloc(sizeof(AppStruct));
  193. //Init bluetooth
  194. app->bt = furi_record_open(RECORD_BT);
  195. //Drawing to be displayed
  196. app->gui = furi_record_open(RECORD_GUI);
  197. //Display
  198. app->view_port = view_port_alloc();
  199. //Init notifications (used for led blink)
  200. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  201. app->connected = false;
  202. app->running = true;
  203. app->delay = 1;
  204. return app;
  205. }
  206. void cleanUpBeforeYouLeave(AppStruct* app) {
  207. furi_assert(app);
  208. //Freeing notifications
  209. furi_record_close(RECORD_NOTIFICATION);
  210. app->notifications = NULL;
  211. //Remove gui from display
  212. gui_remove_view_port(app->gui, app->view_port);
  213. //Freeing display
  214. view_port_free(app->view_port);
  215. furi_record_close(RECORD_GUI);
  216. app->gui = NULL;
  217. furi_record_close(RECORD_BT);
  218. app->bt = NULL;
  219. free(app);
  220. }