desktop_view_debug.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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[] = {"FW Version info:", "Boot Version info:", "Dolphin info:"};
  23. canvas_set_color(canvas, ColorBlack);
  24. canvas_set_font(canvas, FontPrimary);
  25. canvas_draw_str(canvas, 2, 9 + STATUS_BAR_Y_SHIFT, headers[m->screen]);
  26. canvas_set_font(canvas, FontSecondary);
  27. if(m->screen != DesktopViewStatsMeta) {
  28. // Hardware version
  29. const char* my_name = furi_hal_version_get_name_ptr();
  30. snprintf(
  31. buffer,
  32. sizeof(buffer),
  33. "HW: %d.F%dB%dC%d %s",
  34. furi_hal_version_get_hw_version(),
  35. furi_hal_version_get_hw_target(),
  36. furi_hal_version_get_hw_body(),
  37. furi_hal_version_get_hw_connect(),
  38. my_name ? my_name : "Unknown");
  39. canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
  40. ver = m->screen == DesktopViewStatsBoot ? furi_hal_version_get_bootloader_version() :
  41. furi_hal_version_get_firmware_version();
  42. if(!ver) {
  43. canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, "No info");
  44. return;
  45. }
  46. snprintf(
  47. buffer,
  48. sizeof(buffer),
  49. "%s [%s]",
  50. version_get_version(ver),
  51. version_get_builddate(ver));
  52. canvas_draw_str(canvas, 5, 28 + STATUS_BAR_Y_SHIFT, buffer);
  53. snprintf(
  54. buffer,
  55. sizeof(buffer),
  56. "%s [%s]",
  57. version_get_githash(ver),
  58. version_get_gitbranchnum(ver));
  59. canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
  60. snprintf(
  61. buffer, sizeof(buffer), "[%d] %s", version_get_target(ver), version_get_gitbranch(ver));
  62. canvas_draw_str(canvas, 5, 50 + STATUS_BAR_Y_SHIFT, buffer);
  63. } else {
  64. char buffer[64];
  65. Dolphin* dolphin = furi_record_open("dolphin");
  66. DolphinStats stats = dolphin_stats(dolphin);
  67. furi_record_close("dolphin");
  68. uint32_t current_lvl = stats.level;
  69. uint32_t remaining = dolphin_state_xp_to_levelup(m->icounter);
  70. canvas_set_font(canvas, FontSecondary);
  71. snprintf(buffer, 64, "Icounter: %ld Butthurt %ld", m->icounter, m->butthurt);
  72. canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
  73. snprintf(
  74. buffer,
  75. 64,
  76. "Level: %ld To level up: %ld",
  77. current_lvl,
  78. (remaining == (uint32_t)(-1) ? remaining : 0));
  79. canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, buffer);
  80. snprintf(buffer, 64, "%s", asctime(localtime((const time_t*)&m->timestamp)));
  81. canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
  82. canvas_draw_str(canvas, 0, 49 + STATUS_BAR_Y_SHIFT, "[< >] icounter value [ok] save");
  83. }
  84. }
  85. View* desktop_debug_get_view(DesktopDebugView* debug_view) {
  86. furi_assert(debug_view);
  87. return debug_view->view;
  88. }
  89. bool desktop_debug_input(InputEvent* event, void* context) {
  90. furi_assert(event);
  91. furi_assert(context);
  92. DesktopDebugView* debug_view = context;
  93. if(event->type != InputTypeShort) return false;
  94. DesktopViewStatsScreens current = 0;
  95. with_view_model(
  96. debug_view->view, (DesktopDebugViewModel * model) {
  97. #if SRV_DOLPHIN_STATE_DEBUG == 1
  98. if(event->key == InputKeyDown) {
  99. model->screen = (model->screen + 1) % DesktopViewStatsTotalCount;
  100. } else if(event->key == InputKeyUp) {
  101. model->screen = ((model->screen - 1) + DesktopViewStatsTotalCount) %
  102. DesktopViewStatsTotalCount;
  103. }
  104. #else
  105. if((event->key == InputKeyDown) || (event->key == InputKeyUp)) {
  106. model->screen = !model->screen;
  107. }
  108. #endif
  109. current = model->screen;
  110. return true;
  111. });
  112. if(current == DesktopViewStatsMeta) {
  113. if(event->key == InputKeyLeft) {
  114. debug_view->callback(DesktopDebugEventWrongDeed, debug_view->context);
  115. } else if(event->key == InputKeyRight) {
  116. debug_view->callback(DesktopDebugEventDeed, debug_view->context);
  117. } else if(event->key == InputKeyOk) {
  118. debug_view->callback(DesktopDebugEventSaveState, debug_view->context);
  119. } else {
  120. return false;
  121. }
  122. }
  123. if(event->key == InputKeyBack) {
  124. debug_view->callback(DesktopDebugEventExit, debug_view->context);
  125. }
  126. return true;
  127. }
  128. DesktopDebugView* desktop_debug_alloc() {
  129. DesktopDebugView* debug_view = malloc(sizeof(DesktopDebugView));
  130. debug_view->view = view_alloc();
  131. view_allocate_model(debug_view->view, ViewModelTypeLocking, sizeof(DesktopDebugViewModel));
  132. view_set_context(debug_view->view, debug_view);
  133. view_set_draw_callback(debug_view->view, (ViewDrawCallback)desktop_debug_render);
  134. view_set_input_callback(debug_view->view, desktop_debug_input);
  135. return debug_view;
  136. }
  137. void desktop_debug_free(DesktopDebugView* debug_view) {
  138. furi_assert(debug_view);
  139. view_free(debug_view->view);
  140. free(debug_view);
  141. }
  142. void desktop_debug_get_dolphin_data(DesktopDebugView* debug_view) {
  143. Dolphin* dolphin = furi_record_open("dolphin");
  144. DolphinStats stats = dolphin_stats(dolphin);
  145. with_view_model(
  146. debug_view->view, (DesktopDebugViewModel * model) {
  147. model->icounter = stats.icounter;
  148. model->butthurt = stats.butthurt;
  149. model->timestamp = stats.timestamp;
  150. return true;
  151. });
  152. furi_record_close("dolphin");
  153. }
  154. void desktop_debug_reset_screen_idx(DesktopDebugView* debug_view) {
  155. with_view_model(
  156. debug_view->view, (DesktopDebugViewModel * model) {
  157. model->screen = 0;
  158. return true;
  159. });
  160. }