clock_app.c 13 KB

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