dolphin.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "dolphin_i.h"
  2. void dolphin_draw_callback(Canvas* canvas, void* context) {
  3. Dolphin* dolphin = context;
  4. canvas_clear(canvas);
  5. canvas_set_color(canvas, ColorBlack);
  6. if(dolphin->screen == DolphinScreenIdle) {
  7. dolphin_draw_idle(canvas, dolphin);
  8. } else if(dolphin->screen == DolphinScreenDebug) {
  9. dolphin_draw_debug(canvas, dolphin);
  10. } else if(dolphin->screen == DolphinScreenStats) {
  11. dolphin_draw_stats(canvas, dolphin);
  12. }
  13. }
  14. void dolphin_draw_idle(Canvas* canvas, Dolphin* dolphin) {
  15. canvas_draw_icon(canvas, 128 - 80, 0, dolphin->icon);
  16. canvas_set_font(canvas, FontSecondary);
  17. canvas_draw_str(canvas, 2, 10, "/\\: Stats");
  18. canvas_draw_str(canvas, 5, 32, "OK: Menu");
  19. canvas_draw_str(canvas, 2, 52, "\\/: Version");
  20. }
  21. void dolphin_draw_debug(Canvas* canvas, Dolphin* dolphin) {
  22. canvas_set_font(canvas, FontPrimary);
  23. canvas_draw_str(canvas, 2, 10, "Version info:");
  24. canvas_set_font(canvas, FontSecondary);
  25. canvas_draw_str(canvas, 5, 22, TARGET " " BUILD_DATE);
  26. canvas_draw_str(canvas, 5, 32, GIT_BRANCH);
  27. canvas_draw_str(canvas, 5, 42, GIT_BRANCH_NUM);
  28. canvas_draw_str(canvas, 5, 52, GIT_COMMIT);
  29. }
  30. void dolphin_draw_stats(Canvas* canvas, Dolphin* dolphin) {
  31. canvas_set_font(canvas, FontPrimary);
  32. canvas_draw_str(canvas, 2, 10, "Dolphin stats:");
  33. char buffer[64];
  34. canvas_set_font(canvas, FontSecondary);
  35. snprintf(buffer, 64, "Icounter: %ld", dolphin_state_get_icounter(dolphin->state));
  36. canvas_draw_str(canvas, 5, 22, buffer);
  37. snprintf(buffer, 64, "Butthurt: %ld", dolphin_state_get_butthurt(dolphin->state));
  38. canvas_draw_str(canvas, 5, 32, buffer);
  39. canvas_draw_str(canvas, 5, 40, "< > change icounter");
  40. }
  41. void dolphin_input_callback(InputEvent* event, void* context) {
  42. Dolphin* dolphin = context;
  43. if(!event->state) return;
  44. if(event->input == InputOk) {
  45. with_value_mutex(
  46. dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
  47. } else if(event->input == InputUp) {
  48. if(dolphin->screen != DolphinScreenStats) {
  49. dolphin->screen++;
  50. }
  51. } else if(event->input == InputDown) {
  52. if(dolphin->screen != DolphinScreenDebug) {
  53. dolphin->screen--;
  54. }
  55. } else if(event->input == InputBack) {
  56. dolphin->screen = DolphinScreenIdle;
  57. } else if(event->input == InputLeft) {
  58. dolphin_deed(dolphin, DolphinDeedIButtonEmulate);
  59. } else if(event->input == InputRight) {
  60. dolphin_deed(dolphin, DolphinDeedWrong);
  61. }
  62. widget_update(dolphin->widget);
  63. }
  64. Dolphin* dolphin_alloc() {
  65. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  66. dolphin->icon = assets_icons_get(I_Flipper_young_80x60);
  67. icon_start_animation(dolphin->icon);
  68. dolphin->widget = widget_alloc();
  69. widget_draw_callback_set(dolphin->widget, dolphin_draw_callback, dolphin);
  70. widget_input_callback_set(dolphin->widget, dolphin_input_callback, dolphin);
  71. dolphin->menu_vm = furi_open("menu");
  72. furi_check(dolphin->menu_vm);
  73. dolphin->state = dolphin_state_alloc();
  74. dolphin->screen = DolphinScreenIdle;
  75. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  76. furi_check(dolphin->event_queue);
  77. return dolphin;
  78. }
  79. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  80. DolphinEvent event;
  81. event.type = DolphinEventTypeDeed;
  82. event.deed = deed;
  83. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  84. }
  85. void dolphin_task() {
  86. Dolphin* dolphin = dolphin_alloc();
  87. Gui* gui = furi_open("gui");
  88. gui_add_widget(gui, dolphin->widget, GuiLayerNone);
  89. if(!furi_create("dolphin", dolphin)) {
  90. printf("[dolphin_task] cannot create the dolphin record\n");
  91. furiac_exit(NULL);
  92. }
  93. furiac_ready();
  94. DolphinEvent event;
  95. while(1) {
  96. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  97. if(event.type == DolphinEventTypeDeed) {
  98. dolphin_state_on_deed(dolphin->state, event.deed);
  99. }
  100. }
  101. }