clock_app.c 11 KB

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