zeitraffer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <../../applications/main/gpio/gpio_item.h>
  4. #include <gui/gui.h>
  5. #include <input/input.h>
  6. #include <notification/notification_messages.h>
  7. // Часть кода покрадена из https://github.com/zmactep/flipperzero-hello-world
  8. int Time = 10;
  9. int Count = 10;
  10. int WorkTime = 0;
  11. int WorkCount = 0;
  12. bool InfiniteShot = false;
  13. bool Bulb = false;
  14. bool Backlight = true;
  15. const NotificationSequence sequence_click = {
  16. &message_note_c7,
  17. &message_delay_50,
  18. &message_sound_off,
  19. NULL,
  20. };
  21. typedef enum {
  22. EventTypeTick,
  23. EventTypeInput,
  24. } EventType;
  25. typedef struct {
  26. EventType type;
  27. InputEvent input;
  28. } ZeitrafferEvent;
  29. static void draw_callback(Canvas* canvas, void* ctx) {
  30. UNUSED(ctx);
  31. char temp_str[36];
  32. canvas_clear(canvas);
  33. canvas_set_font(canvas, FontPrimary);
  34. switch (Count) {
  35. case -1:
  36. snprintf(temp_str,sizeof(temp_str),"Set: BULB %i sec",Time);
  37. break;
  38. case 0:
  39. snprintf(temp_str,sizeof(temp_str),"Set: infinite, %i sec",Time);
  40. break;
  41. default:
  42. snprintf(temp_str,sizeof(temp_str),"Set: %i frames, %i sec",Count,Time);
  43. }
  44. canvas_draw_str(canvas, 3, 15, temp_str);
  45. snprintf(temp_str,sizeof(temp_str),"Left: %i frames, %i sec",WorkCount,WorkTime);
  46. canvas_draw_str(canvas, 3, 35, temp_str);
  47. snprintf(temp_str,sizeof(temp_str),"Backlight: %i",Backlight);
  48. canvas_draw_str(canvas, 3, 55, temp_str);
  49. }
  50. static void input_callback(InputEvent* input_event, void* ctx) {
  51. // Проверяем, что контекст не нулевой
  52. furi_assert(ctx);
  53. FuriMessageQueue* event_queue = ctx;
  54. ZeitrafferEvent event = {.type = EventTypeInput, .input = *input_event};
  55. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  56. }
  57. static void timer_callback(FuriMessageQueue* event_queue) {
  58. // Проверяем, что контекст не нулевой
  59. furi_assert(event_queue);
  60. ZeitrafferEvent event = {.type = EventTypeTick};
  61. furi_message_queue_put(event_queue, &event, 0);
  62. }
  63. int32_t zeitraffer_app(void* p) {
  64. UNUSED(p);
  65. // Текущее событие типа кастомного типа ZeitrafferEvent
  66. ZeitrafferEvent event;
  67. // Очередь событий на 8 элементов размера ZeitrafferEvent
  68. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(ZeitrafferEvent));
  69. // Создаем новый view port
  70. ViewPort* view_port = view_port_alloc();
  71. // Создаем callback отрисовки, без контекста
  72. view_port_draw_callback_set(view_port, draw_callback, NULL);
  73. // Создаем callback нажатий на клавиши, в качестве контекста передаем
  74. // нашу очередь сообщений, чтоб запихивать в неё эти события
  75. view_port_input_callback_set(view_port, input_callback, event_queue);
  76. // Создаем GUI приложения
  77. Gui* gui = furi_record_open(RECORD_GUI);
  78. // Подключаем view port к GUI в полноэкранном режиме
  79. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  80. // Конфигурим пины
  81. gpio_item_configure_all_pins(GpioModeOutputPushPull);
  82. // Создаем периодический таймер с коллбэком, куда в качестве
  83. // контекста будет передаваться наша очередь событий
  84. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, event_queue);
  85. // Запускаем таймер
  86. //furi_timer_start(timer, 1500);
  87. // Включаем нотификации
  88. NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
  89. // Бесконечный цикл обработки очереди событий
  90. while(1) {
  91. // Выбираем событие из очереди в переменную event (ждем бесконечно долго, если очередь пуста)
  92. // и проверяем, что у нас получилось это сделать
  93. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  94. // Наше событие — это нажатие кнопки
  95. if(event.type == EventTypeInput) {
  96. if(event.input.type == InputTypeShort) { // Короткие нажатия
  97. if(event.input.key == InputKeyBack) {
  98. if(furi_timer_is_running(timer)) { // Если таймер запущен - нефиг мацать кнопки!
  99. notification_message(notifications, &sequence_error);
  100. }
  101. else {
  102. WorkCount = Count;
  103. WorkTime = 3;
  104. if (Count == 0) {
  105. InfiniteShot = true;
  106. WorkCount = 1;
  107. }
  108. else
  109. InfiniteShot = false;
  110. notification_message(notifications, &sequence_success);
  111. }
  112. }
  113. if(event.input.key == InputKeyRight) {
  114. if(furi_timer_is_running(timer)) {
  115. notification_message(notifications, &sequence_error);
  116. }
  117. else {
  118. Count++;
  119. notification_message(notifications, &sequence_click);
  120. }
  121. }
  122. if(event.input.key == InputKeyLeft) {
  123. if(furi_timer_is_running(timer)) {
  124. notification_message(notifications, &sequence_error);
  125. }
  126. else {
  127. Count--;
  128. notification_message(notifications, &sequence_click);
  129. }
  130. }
  131. if(event.input.key == InputKeyUp) {
  132. if(furi_timer_is_running(timer)) {
  133. notification_message(notifications, &sequence_error);
  134. }
  135. else {
  136. Time++;
  137. notification_message(notifications, &sequence_click);
  138. }
  139. }
  140. if(event.input.key == InputKeyDown) {
  141. if(furi_timer_is_running(timer)) {
  142. notification_message(notifications, &sequence_error);
  143. }
  144. else {
  145. Time--;
  146. notification_message(notifications, &sequence_click);
  147. }
  148. }
  149. if(event.input.key == InputKeyOk) {
  150. if(furi_timer_is_running(timer)) {
  151. notification_message(notifications, &sequence_click);
  152. furi_timer_stop(timer);
  153. }
  154. else {
  155. furi_timer_start(timer, 1000);
  156. if (WorkCount == 0)
  157. WorkCount = Count;
  158. if (WorkTime == 0)
  159. WorkTime = 3;
  160. if (Count == 0) {
  161. InfiniteShot = true;
  162. WorkCount = 1;
  163. }
  164. else
  165. InfiniteShot = false;
  166. if (Count == -1) {
  167. gpio_item_set_pin(4, true);
  168. gpio_item_set_pin(5, true);
  169. Bulb = true;
  170. WorkCount = 1;
  171. WorkTime = Time;
  172. }
  173. else
  174. Bulb = false;
  175. notification_message(notifications, &sequence_success);
  176. }
  177. }
  178. }
  179. if(event.input.type == InputTypeLong) { // Длинные нажатия
  180. // Если нажата кнопка "назад", то выходим из цикла, а следовательно и из приложения
  181. if(event.input.key == InputKeyBack) {
  182. if(furi_timer_is_running(timer)) { // А если работает таймер - не выходим :D
  183. notification_message(notifications, &sequence_error);
  184. }
  185. else {
  186. notification_message(notifications, &sequence_click);
  187. gpio_item_set_all_pins(false);
  188. furi_timer_stop(timer);
  189. break;
  190. }
  191. }
  192. if(event.input.key == InputKeyOk) {
  193. Backlight = !Backlight; // Нам ваша подсветка и нахой не нужна! Или нужна.
  194. }
  195. }
  196. if(event.input.type == InputTypeRepeat) { // Зажатые кнопки
  197. if(event.input.key == InputKeyRight) {
  198. if(furi_timer_is_running(timer)) {
  199. notification_message(notifications, &sequence_error);
  200. }
  201. else {
  202. Count = Count+10;
  203. }
  204. }
  205. if(event.input.key == InputKeyLeft) {
  206. if(furi_timer_is_running(timer)) {
  207. notification_message(notifications, &sequence_error);
  208. }
  209. else {
  210. Count = Count-10;
  211. }
  212. }
  213. if(event.input.key == InputKeyUp) {
  214. if(furi_timer_is_running(timer)) {
  215. notification_message(notifications, &sequence_error);
  216. }
  217. else {
  218. Time = Time+10;
  219. }
  220. }
  221. if(event.input.key == InputKeyDown) {
  222. if(furi_timer_is_running(timer)) {
  223. notification_message(notifications, &sequence_error);
  224. }
  225. else {
  226. Time = Time-10;
  227. }
  228. }
  229. }
  230. }
  231. // Наше событие — это сработавший таймер
  232. else if(event.type == EventTypeTick) {
  233. WorkTime--;
  234. // Отправляем нотификацию мигания синим светодиодом
  235. notification_message(notifications, &sequence_blink_blue_100);
  236. if (Backlight) { // чо по подсветке?
  237. notification_message(notifications, &sequence_display_backlight_on);
  238. }
  239. else {
  240. notification_message(notifications, &sequence_display_backlight_off);
  241. }
  242. if( WorkTime < 1 ) { // фоткаем
  243. if (Bulb) {
  244. gpio_item_set_all_pins(false); WorkCount = 0;
  245. }
  246. else {
  247. WorkCount--;
  248. view_port_update(view_port);
  249. notification_message(notifications, &sequence_click);
  250. // Дрыгаем ногами
  251. //gpio_item_set_all_pins(true);
  252. gpio_item_set_pin(4, true);
  253. gpio_item_set_pin(5, true);
  254. furi_delay_ms(400); // На короткие нажатия фотик плохо реагирует
  255. gpio_item_set_pin(4, false);
  256. gpio_item_set_pin(5, false);
  257. //gpio_item_set_all_pins(false);
  258. if (InfiniteShot) WorkCount++;
  259. WorkTime = Time;
  260. view_port_update(view_port);
  261. }
  262. }
  263. if( WorkCount < 1 ) { // закончили
  264. gpio_item_set_all_pins(false);
  265. furi_timer_stop(timer);
  266. notification_message(notifications, &sequence_audiovisual_alert);
  267. WorkTime = 3;
  268. WorkCount = 0;
  269. }
  270. }
  271. if (Time < 1) Time = 1; // Не даём открутить таймер меньше единицы
  272. if (Count < -1) Count = 0; // А тут даём, бо 0 кадров это бесконечная съёмка, а -1 кадров - BULB
  273. }
  274. // Очищаем таймер
  275. furi_timer_free(timer);
  276. // Специальная очистка памяти, занимаемой очередью
  277. furi_message_queue_free(event_queue);
  278. // Чистим созданные объекты, связанные с интерфейсом
  279. gui_remove_view_port(gui, view_port);
  280. view_port_free(view_port);
  281. furi_record_close(RECORD_GUI);
  282. // Очищаем нотификации
  283. furi_record_close(RECORD_NOTIFICATION);
  284. return 0;
  285. }