zeitraffer.c 11 KB

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