clock_app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <notification/notification_messages.h>
  6. #include <notification/notification_app.h>
  7. #include "clock_app.h"
  8. /*
  9. This is a modified version of the default clock app intended for use overnight
  10. Up / Down controls the displays brightness. Down at brightness 0 turns the notification LED on and off.
  11. */
  12. int brightness = 5;
  13. bool led = false;
  14. NotificationApp* notif = 0;
  15. int dspBrightnessBarFrames = 0;
  16. const int dspBrightnessBarDisplayFrames = 8;
  17. const NotificationMessage message_red_dim = {
  18. .type = NotificationMessageTypeLedRed,
  19. .data.led.value = 0xFF / 16,
  20. };
  21. const NotificationMessage message_red_off = {
  22. .type = NotificationMessageTypeLedRed,
  23. .data.led.value = 0x00,
  24. };
  25. static const NotificationSequence led_on = {
  26. &message_red_dim,
  27. &message_do_not_reset,
  28. NULL,
  29. };
  30. static const NotificationSequence led_off = {
  31. &message_red_off,
  32. &message_do_not_reset,
  33. NULL,
  34. };
  35. static const NotificationSequence led_reset = {
  36. &message_red_0,
  37. NULL,
  38. };
  39. void set_backlight_brightness(float brightness) {
  40. notif->settings.display_brightness = brightness;
  41. notification_message(notif, &sequence_display_backlight_on);
  42. }
  43. void handle_up() {
  44. dspBrightnessBarFrames = dspBrightnessBarDisplayFrames;
  45. if(brightness < 100) {
  46. led = false;
  47. notification_message(notif, &led_off);
  48. brightness += 5;
  49. }
  50. set_backlight_brightness((float)(brightness / 100.f));
  51. }
  52. void handle_down() {
  53. dspBrightnessBarFrames = dspBrightnessBarDisplayFrames;
  54. if(brightness > 0) {
  55. brightness -= 5;
  56. if(brightness == 0) { //trigger only on the first brightness 5 -> 0 transition
  57. led = true;
  58. notification_message(notif, &led_on);
  59. }
  60. } else if(brightness == 0) { //trigger on every down press afterwards
  61. led = !led;
  62. if(led) {
  63. notification_message(notif, &led_on);
  64. } else {
  65. notification_message(notif, &led_off);
  66. }
  67. }
  68. set_backlight_brightness((float)(brightness / 100.f));
  69. }
  70. static void clock_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  71. furi_assert(event_queue);
  72. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  73. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  74. }
  75. //do you are have stupid?
  76. void elements_progress_bar_vertical(
  77. Canvas* canvas,
  78. uint8_t x,
  79. uint8_t y,
  80. uint8_t height,
  81. float progress) {
  82. furi_assert(canvas);
  83. furi_assert((progress >= 0) && (progress <= 1.0));
  84. uint8_t width = 9;
  85. uint8_t progress_length = roundf((1.f - progress) * (height - 2));
  86. canvas_set_color(canvas, ColorBlack);
  87. canvas_draw_box(canvas, x + 1, y + 1, width - 2, height - 2);
  88. canvas_set_color(canvas, ColorWhite);
  89. canvas_draw_box(canvas, x + 1, y + 1, width - 2, progress_length);
  90. canvas_set_color(canvas, ColorBlack);
  91. canvas_draw_rframe(canvas, x, y, width, height, 3);
  92. }
  93. static void clock_render_callback(Canvas* const canvas, void* ctx) {
  94. //canvas_clear(canvas);
  95. //canvas_set_color(canvas, ColorBlack);
  96. //avoids a bug with the brightness being reverted after the backlight-off period
  97. set_backlight_brightness((float)(brightness / 100.f));
  98. if(dspBrightnessBarFrames > 0) {
  99. elements_progress_bar_vertical(canvas, 119, 1, 62, (float)(brightness / 100.f));
  100. dspBrightnessBarFrames--;
  101. }
  102. ClockState* state = ctx;
  103. if(furi_mutex_acquire(state->mutex, 200) != FuriStatusOk) {
  104. //FURI_LOG_D(TAG, "Can't obtain mutex, requeue render");
  105. PluginEvent event = {.type = EventTypeTick};
  106. furi_message_queue_put(state->event_queue, &event, 0);
  107. return;
  108. }
  109. DateTime curr_dt;
  110. furi_hal_rtc_get_datetime(&curr_dt);
  111. uint32_t curr_ts = datetime_datetime_to_timestamp(&curr_dt);
  112. char time_string[TIME_LEN];
  113. char date_string[DATE_LEN];
  114. char meridian_string[MERIDIAN_LEN];
  115. char timer_string[20];
  116. if(state->time_format == LocaleTimeFormat24h) {
  117. snprintf(
  118. time_string, TIME_LEN, CLOCK_TIME_FORMAT, curr_dt.hour, curr_dt.minute, curr_dt.second);
  119. } else {
  120. bool pm = curr_dt.hour > 12;
  121. bool pm12 = curr_dt.hour >= 12;
  122. snprintf(
  123. time_string,
  124. TIME_LEN,
  125. CLOCK_TIME_FORMAT,
  126. pm ? curr_dt.hour - 12 : curr_dt.hour,
  127. curr_dt.minute,
  128. curr_dt.second);
  129. snprintf(
  130. meridian_string,
  131. MERIDIAN_LEN,
  132. MERIDIAN_FORMAT,
  133. pm12 ? MERIDIAN_STRING_PM : MERIDIAN_STRING_AM);
  134. }
  135. if(state->date_format == LocaleDateFormatYMD) {
  136. snprintf(
  137. date_string, DATE_LEN, CLOCK_ISO_DATE_FORMAT, curr_dt.year, curr_dt.month, curr_dt.day);
  138. } else if(state->date_format == LocaleDateFormatMDY) {
  139. snprintf(
  140. date_string, DATE_LEN, CLOCK_RFC_DATE_FORMAT, curr_dt.month, curr_dt.day, curr_dt.year);
  141. } else {
  142. snprintf(
  143. date_string, DATE_LEN, CLOCK_RFC_DATE_FORMAT, curr_dt.day, curr_dt.month, curr_dt.year);
  144. }
  145. bool timer_running = state->timer_running;
  146. uint32_t timer_start_timestamp = state->timer_start_timestamp;
  147. uint32_t timer_stopped_seconds = state->timer_stopped_seconds;
  148. furi_mutex_release(state->mutex);
  149. canvas_set_font(canvas, FontBigNumbers);
  150. if(timer_start_timestamp != 0) {
  151. int32_t elapsed_secs = timer_running ? (curr_ts - timer_start_timestamp) :
  152. timer_stopped_seconds;
  153. snprintf(timer_string, 20, "%.2ld:%.2ld", elapsed_secs / 60, elapsed_secs % 60);
  154. canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, time_string); // DRAW TIME
  155. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, timer_string); // DRAW TIMER
  156. canvas_set_font(canvas, FontSecondary);
  157. canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignTop, date_string); // DRAW DATE
  158. elements_button_left(canvas, "Reset");
  159. } else {
  160. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, time_string);
  161. canvas_set_font(canvas, FontSecondary);
  162. canvas_draw_str_aligned(canvas, 65, 17, AlignCenter, AlignCenter, date_string);
  163. if(state->time_format == LocaleTimeFormat12h)
  164. canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignCenter, meridian_string);
  165. }
  166. if(timer_running) {
  167. elements_button_center(canvas, "Stop");
  168. } else if(timer_start_timestamp != 0 && !timer_running) {
  169. elements_button_center(canvas, "Start");
  170. }
  171. }
  172. static void clock_state_init(ClockState* const state) {
  173. state->time_format = locale_get_time_format();
  174. state->date_format = locale_get_date_format();
  175. //FURI_LOG_D(TAG, "Time format: %s", state->settings.time_format == H12 ? "12h" : "24h");
  176. //FURI_LOG_D(TAG, "Date format: %s", state->settings.date_format == Iso ? "ISO 8601" : "RFC 5322");
  177. //furi_hal_rtc_get_datetime(&state->datetime);
  178. }
  179. // Runs every 1000ms by default
  180. static void clock_tick(void* ctx) {
  181. furi_assert(ctx);
  182. FuriMessageQueue* event_queue = ctx;
  183. PluginEvent event = {.type = EventTypeTick};
  184. // It's OK to loose this event if system overloaded
  185. furi_message_queue_put(event_queue, &event, 0);
  186. }
  187. void timer_start_stop(ClockState* plugin_state) {
  188. // START/STOP TIMER
  189. uint32_t curr_ts = furi_hal_rtc_get_timestamp();
  190. if(plugin_state->timer_running) {
  191. // Update stopped seconds
  192. plugin_state->timer_stopped_seconds = curr_ts - plugin_state->timer_start_timestamp;
  193. } else {
  194. if(plugin_state->timer_start_timestamp == 0) {
  195. // Set starting timestamp if this is first time
  196. plugin_state->timer_start_timestamp = curr_ts;
  197. } else {
  198. // Timer was already running, need to slightly readjust so we don't
  199. // count the intervening time
  200. plugin_state->timer_start_timestamp = curr_ts - plugin_state->timer_stopped_seconds;
  201. }
  202. }
  203. plugin_state->timer_running = !plugin_state->timer_running;
  204. }
  205. void timer_reset_seconds(ClockState* plugin_state) {
  206. if(plugin_state->timer_start_timestamp != 0) {
  207. // Reset seconds
  208. plugin_state->timer_running = false;
  209. plugin_state->timer_start_timestamp = 0;
  210. plugin_state->timer_stopped_seconds = 0;
  211. }
  212. }
  213. int32_t clock_app(void* p) {
  214. UNUSED(p);
  215. ClockState* plugin_state = malloc(sizeof(ClockState));
  216. plugin_state->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  217. if(plugin_state->event_queue == NULL) {
  218. FURI_LOG_E(TAG, "Cannot create event queue");
  219. free(plugin_state);
  220. return 255;
  221. }
  222. //FURI_LOG_D(TAG, "Event queue created");
  223. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  224. if(plugin_state->mutex == NULL) {
  225. FURI_LOG_E(TAG, "Cannot create mutex");
  226. furi_message_queue_free(plugin_state->event_queue);
  227. free(plugin_state);
  228. return 255;
  229. }
  230. //FURI_LOG_D(TAG, "Mutex created");
  231. clock_state_init(plugin_state);
  232. notif = furi_record_open(RECORD_NOTIFICATION);
  233. float tmpBrightness = notif->settings.display_brightness;
  234. brightness = tmpBrightness * 100; // Keep current brightness by default
  235. notification_message(notif, &sequence_display_backlight_enforce_on);
  236. notification_message(notif, &led_off);
  237. // Set system callbacks
  238. ViewPort* view_port = view_port_alloc();
  239. view_port_draw_callback_set(view_port, clock_render_callback, plugin_state);
  240. view_port_input_callback_set(view_port, clock_input_callback, plugin_state->event_queue);
  241. FuriTimer* timer =
  242. furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, plugin_state->event_queue);
  243. if(timer == NULL) {
  244. FURI_LOG_E(TAG, "Cannot create timer");
  245. furi_mutex_free(plugin_state->mutex);
  246. furi_message_queue_free(plugin_state->event_queue);
  247. free(plugin_state);
  248. return 255;
  249. }
  250. //FURI_LOG_D(TAG, "Timer created");
  251. // Open GUI and register view_port
  252. Gui* gui = furi_record_open(RECORD_GUI);
  253. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  254. furi_timer_start(timer, furi_kernel_get_tick_frequency());
  255. //FURI_LOG_D(TAG, "Timer started");
  256. // Main loop
  257. PluginEvent event;
  258. for(bool processing = true; processing;) {
  259. FuriStatus event_status = furi_message_queue_get(plugin_state->event_queue, &event, 100);
  260. if(event_status != FuriStatusOk) continue;
  261. if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) != FuriStatusOk) continue;
  262. // press events
  263. if(event.type == EventTypeKey) {
  264. if(event.input.type == InputTypeShort) {
  265. switch(event.input.key) {
  266. case InputKeyLeft:
  267. // Reset seconds
  268. timer_reset_seconds(plugin_state);
  269. break;
  270. case InputKeyOk:
  271. // Toggle timer
  272. timer_start_stop(plugin_state);
  273. break;
  274. case InputKeyBack:
  275. // Exit the plugin
  276. processing = false;
  277. break;
  278. case InputKeyUp:
  279. handle_up();
  280. break;
  281. case InputKeyDown:
  282. handle_down();
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. } /*else if(event.type == EventTypeTick) {
  289. furi_hal_rtc_get_datetime(&plugin_state->datetime);
  290. }*/
  291. furi_mutex_release(plugin_state->mutex);
  292. view_port_update(view_port);
  293. }
  294. furi_timer_free(timer);
  295. view_port_enabled_set(view_port, false);
  296. gui_remove_view_port(gui, view_port);
  297. furi_record_close(RECORD_GUI);
  298. furi_record_close(RECORD_NOTIFICATION);
  299. view_port_free(view_port);
  300. furi_message_queue_free(plugin_state->event_queue);
  301. furi_mutex_free(plugin_state->mutex);
  302. free(plugin_state);
  303. set_backlight_brightness(tmpBrightness);
  304. notification_message(notif, &sequence_display_backlight_enforce_auto);
  305. notification_message(notif, &led_reset);
  306. return 0;
  307. }