desktop_view_debug.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <toolbox/version.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <dolphin/helpers/dolphin_state.h>
  5. #include <dolphin/dolphin.h>
  6. #include "../desktop_i.h"
  7. #include "desktop_view_debug.h"
  8. void desktop_debug_set_callback(
  9. DesktopDebugView* debug_view,
  10. DesktopDebugViewCallback callback,
  11. void* context) {
  12. furi_assert(debug_view);
  13. furi_assert(callback);
  14. debug_view->callback = callback;
  15. debug_view->context = context;
  16. }
  17. void desktop_debug_render(Canvas* canvas, void* model) {
  18. canvas_clear(canvas);
  19. DesktopDebugViewModel* m = model;
  20. const Version* ver;
  21. char buffer[64];
  22. static const char* headers[] = {"Device Info:", "Dolphin Info:"};
  23. canvas_set_color(canvas, ColorBlack);
  24. canvas_set_font(canvas, FontPrimary);
  25. canvas_draw_str_aligned(
  26. canvas, 64, 1 + STATUS_BAR_Y_SHIFT, AlignCenter, AlignTop, headers[m->screen]);
  27. canvas_set_font(canvas, FontSecondary);
  28. if(m->screen != DesktopViewStatsMeta) {
  29. // Hardware version
  30. const char* my_name = furi_hal_version_get_name_ptr();
  31. snprintf(
  32. buffer,
  33. sizeof(buffer),
  34. "%d.F%dB%dC%d %s:%s %s",
  35. furi_hal_version_get_hw_version(),
  36. furi_hal_version_get_hw_target(),
  37. furi_hal_version_get_hw_body(),
  38. furi_hal_version_get_hw_connect(),
  39. furi_hal_version_get_hw_region_name(),
  40. furi_hal_region_get_name(),
  41. my_name ? my_name : "Unknown");
  42. canvas_draw_str(canvas, 0, 19 + STATUS_BAR_Y_SHIFT, buffer);
  43. ver = furi_hal_version_get_firmware_version();
  44. const BleGlueC2Info* c2_ver = NULL;
  45. #ifdef SRV_BT
  46. c2_ver = ble_glue_get_c2_info();
  47. #endif
  48. if(!ver) { //-V1051
  49. canvas_draw_str(canvas, 0, 30 + STATUS_BAR_Y_SHIFT, "No info");
  50. return;
  51. }
  52. snprintf(
  53. buffer,
  54. sizeof(buffer),
  55. "%s [%s]",
  56. version_get_version(ver),
  57. version_get_builddate(ver));
  58. canvas_draw_str(canvas, 0, 30 + STATUS_BAR_Y_SHIFT, buffer);
  59. uint16_t api_major, api_minor;
  60. furi_hal_info_get_api_version(&api_major, &api_minor);
  61. snprintf(
  62. buffer,
  63. sizeof(buffer),
  64. "%s%s [%d.%d] %s",
  65. version_get_dirty_flag(ver) ? "[!] " : "",
  66. version_get_githash(ver),
  67. api_major,
  68. api_minor,
  69. c2_ver ? c2_ver->StackTypeString : "<none>");
  70. canvas_draw_str(canvas, 0, 40 + STATUS_BAR_Y_SHIFT, buffer);
  71. snprintf(
  72. buffer, sizeof(buffer), "[%d] %s", version_get_target(ver), version_get_gitbranch(ver));
  73. canvas_draw_str(canvas, 0, 50 + STATUS_BAR_Y_SHIFT, buffer);
  74. } else {
  75. Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
  76. DolphinStats stats = dolphin_stats(dolphin);
  77. furi_record_close(RECORD_DOLPHIN);
  78. uint32_t current_lvl = stats.level;
  79. uint32_t remaining = dolphin_state_xp_to_levelup(m->icounter);
  80. canvas_set_font(canvas, FontSecondary);
  81. snprintf(buffer, sizeof(buffer), "Icounter: %lu Butthurt %lu", m->icounter, m->butthurt);
  82. canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
  83. snprintf(
  84. buffer,
  85. sizeof(buffer),
  86. "Level: %lu To level up: %lu",
  87. current_lvl,
  88. (remaining == (uint32_t)(-1) ? remaining : 0));
  89. canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, buffer);
  90. // even if timestamp is uint64_t, it's safe to cast it to uint32_t, because furi_hal_rtc_datetime_to_timestamp only returns uint32_t
  91. snprintf(buffer, sizeof(buffer), "%lu", (uint32_t)m->timestamp);
  92. canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
  93. canvas_draw_str(canvas, 0, 49 + STATUS_BAR_Y_SHIFT, "[< >] icounter value [ok] save");
  94. }
  95. }
  96. View* desktop_debug_get_view(DesktopDebugView* debug_view) {
  97. furi_assert(debug_view);
  98. return debug_view->view;
  99. }
  100. bool desktop_debug_input(InputEvent* event, void* context) {
  101. furi_assert(event);
  102. furi_assert(context);
  103. DesktopDebugView* debug_view = context;
  104. if(event->type != InputTypeShort && event->type != InputTypeRepeat) {
  105. return false;
  106. }
  107. DesktopViewStatsScreens current = 0;
  108. with_view_model(
  109. debug_view->view,
  110. DesktopDebugViewModel * model,
  111. {
  112. #ifdef SRV_DOLPHIN_STATE_DEBUG
  113. if((event->key == InputKeyDown) || (event->key == InputKeyUp)) {
  114. model->screen = !model->screen;
  115. }
  116. #endif
  117. current = model->screen;
  118. },
  119. true);
  120. size_t count = (event->type == InputTypeRepeat) ? 10 : 1;
  121. if(current == DesktopViewStatsMeta) {
  122. if(event->key == InputKeyLeft) {
  123. while(count-- > 0) {
  124. debug_view->callback(DesktopDebugEventWrongDeed, debug_view->context);
  125. }
  126. } else if(event->key == InputKeyRight) {
  127. while(count-- > 0) {
  128. debug_view->callback(DesktopDebugEventDeed, debug_view->context);
  129. }
  130. } else if(event->key == InputKeyOk) {
  131. debug_view->callback(DesktopDebugEventSaveState, debug_view->context);
  132. } else {
  133. return false;
  134. }
  135. }
  136. if(event->key == InputKeyBack) {
  137. debug_view->callback(DesktopDebugEventExit, debug_view->context);
  138. }
  139. return true;
  140. }
  141. DesktopDebugView* desktop_debug_alloc() {
  142. DesktopDebugView* debug_view = malloc(sizeof(DesktopDebugView));
  143. debug_view->view = view_alloc();
  144. view_allocate_model(debug_view->view, ViewModelTypeLocking, sizeof(DesktopDebugViewModel));
  145. view_set_context(debug_view->view, debug_view);
  146. view_set_draw_callback(debug_view->view, (ViewDrawCallback)desktop_debug_render);
  147. view_set_input_callback(debug_view->view, desktop_debug_input);
  148. return debug_view;
  149. }
  150. void desktop_debug_free(DesktopDebugView* debug_view) {
  151. furi_assert(debug_view);
  152. view_free(debug_view->view);
  153. free(debug_view);
  154. }
  155. void desktop_debug_get_dolphin_data(DesktopDebugView* debug_view) {
  156. Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
  157. DolphinStats stats = dolphin_stats(dolphin);
  158. with_view_model(
  159. debug_view->view,
  160. DesktopDebugViewModel * model,
  161. {
  162. model->icounter = stats.icounter;
  163. model->butthurt = stats.butthurt;
  164. model->timestamp = stats.timestamp;
  165. },
  166. true);
  167. furi_record_close(RECORD_DOLPHIN);
  168. }
  169. void desktop_debug_reset_screen_idx(DesktopDebugView* debug_view) {
  170. with_view_model(
  171. debug_view->view, DesktopDebugViewModel * model, { model->screen = 0; }, true);
  172. }