ios_trigger.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "ios_trigger.h"
  2. __int32_t ios_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):
  46. app->shooting = !app->shooting;
  47. if(app->shooting){
  48. app->shots = 0;
  49. //Timer triggered every delay ms
  50. furi_timer_start(timer, app->delay * 1000);
  51. }else{
  52. //Timer triggered every delay ms
  53. furi_timer_stop(timer);
  54. }
  55. break;
  56. case(InputKeyUp):
  57. if(!app->shooting){
  58. app->delay++;
  59. }
  60. break;
  61. case(InputKeyDown):
  62. if(!app->shooting){
  63. app->delay--;
  64. }
  65. break;
  66. case(InputKeyLeft):
  67. break;
  68. case(InputKeyRight):
  69. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
  70. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
  71. notification_message(app->notifications, &sequence_blink_blue_100);
  72. app->shots++;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. break;
  79. case(EventTypeTick):
  80. if(app->shooting){
  81. //sending command to trigger via BT
  82. furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
  83. furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
  84. notification_message(app->notifications, &sequence_blink_blue_100);
  85. app->shots++;
  86. }
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. //Going back to serial mode BT
  93. bt_set_status_changed_callback(app->bt, NULL, NULL);
  94. bt_disconnect(app->bt);
  95. // Wait 2nd core to update nvm storage
  96. furi_delay_ms(200);
  97. bt_keys_storage_set_default_path(app->bt);
  98. if(!bt_set_profile(app->bt, BtProfileSerial)) {
  99. FURI_LOG_E(TAG, "Failed to switch to Serial profile");
  100. }
  101. //Freeing memory
  102. furi_message_queue_free(event_queue);
  103. //Freeing timer
  104. furi_timer_free(timer);
  105. cleanUpBeforeYouLeave(app);
  106. return 0;
  107. }
  108. //Callback display
  109. static void draw_callback(Canvas* canvas, void* ctx) {
  110. AppStruct* app = ctx;
  111. char chaine_photo[36];
  112. char chaine_delais[36];
  113. char chaine_shooting[36];
  114. snprintf(chaine_photo, sizeof(chaine_photo), "%i shots", app->shots);
  115. snprintf(chaine_delais, sizeof(chaine_delais), "%i", app->delay);
  116. if(app->shooting){
  117. snprintf(chaine_shooting, sizeof(chaine_shooting), "Press to stop");
  118. }else {
  119. snprintf(chaine_shooting, sizeof(chaine_shooting), "Press to start");
  120. }
  121. canvas_clear(canvas);
  122. canvas_draw_frame(canvas, 0, 0, 128, 64);
  123. canvas_set_font(canvas, FontPrimary);
  124. canvas_draw_str(canvas, 2, 10, "iOS Intervalometer");
  125. //Represent
  126. canvas_set_font(canvas, FontSecondary);
  127. canvas_draw_str(canvas, 79, 60, "By Nem0oo");
  128. if(app->connected){
  129. canvas_draw_icon(canvas, 111, 2, &I_Ble_connected_15x15);
  130. canvas_set_font(canvas, FontSecondary);
  131. canvas_draw_str(canvas, 3, 26, "Delay (in sec)");
  132. canvas_draw_icon(canvas, 67, 28, &I_ButtonDown_7x4);
  133. canvas_draw_icon(canvas, 67, 13, &I_ButtonUp_7x4);
  134. canvas_draw_str(canvas, 65, 26, chaine_delais);
  135. canvas_draw_icon(canvas, 2, 31, &I_Ok_btn_pressed_13x13);
  136. canvas_draw_str(canvas, 17, 41, chaine_shooting);
  137. canvas_draw_str(canvas, 17, 56, chaine_photo);
  138. canvas_draw_icon(canvas, 2, 47, &I_dir_10px);
  139. }else{
  140. canvas_draw_icon(canvas, 111, 2, &I_Ble_disconnected_15x15);
  141. canvas_draw_icon(canvas, 1, 21, &I_WarningDolphin_45x42);
  142. canvas_set_font(canvas, FontSecondary);
  143. canvas_draw_str(canvas, 48, 37, "Awaiting bluetooth");
  144. }
  145. }
  146. //Input callbacks
  147. static void input_callback(InputEvent* input_event, void* ctx) {
  148. furi_assert(ctx);
  149. //Getting our event queue
  150. FuriMessageQueue* event_queue = ctx;
  151. //Adding the event to our custom Struct
  152. IosTriggerEvent event = {.type = EventTypeInput, .input = *input_event};
  153. //Adding our event to the event queue
  154. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  155. }
  156. //Timer callback
  157. static void timer_callback(FuriMessageQueue* event_queue) {
  158. //check eventqueue is not null
  159. furi_assert(event_queue);
  160. //creating event and adding it to the event list
  161. IosTriggerEvent event = {.type = EventTypeTick};
  162. furi_message_queue_put(event_queue, &event, 0);
  163. }
  164. static void bt_hid_connection_status_changed_callback(BtStatus status, void* context) {
  165. furi_assert(context);
  166. AppStruct* app = context;
  167. app->connected = (status == BtStatusConnected);
  168. //if(app->connected) {
  169. // notification_message(app->notifications, &sequence_set_blue_255);
  170. //} else {
  171. // notification_message(app->notifications, &sequence_reset_blue);
  172. //}
  173. }
  174. AppStruct* appStructAlloc(){
  175. AppStruct* app = malloc(sizeof(AppStruct));
  176. //Init bluetooth
  177. app->bt = furi_record_open(RECORD_BT);
  178. //Drawing to be displayed
  179. app->gui = furi_record_open(RECORD_GUI);
  180. //Display
  181. app->view_port = view_port_alloc();
  182. //Init notifications (used for led blink)
  183. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  184. app->connected = false;
  185. app->running = true;
  186. return app;
  187. }
  188. void cleanUpBeforeYouLeave(AppStruct* app){
  189. furi_assert(app);
  190. //Freeing notifications
  191. furi_record_close(RECORD_NOTIFICATION);
  192. app->notifications = NULL;
  193. //Remove gui from display
  194. gui_remove_view_port(app->gui, app->view_port);
  195. //Freeing display
  196. view_port_free(app->view_port);
  197. furi_record_close(RECORD_GUI);
  198. app->gui = NULL;
  199. furi_record_close(RECORD_BT);
  200. app->bt = NULL;
  201. free(app);
  202. }