clock_app.c 14 KB

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