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