dolphin.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "dolphin_i.h"
  2. bool dolphin_view_first_start_input(InputEvent* event, void* context) {
  3. furi_assert(event);
  4. furi_assert(context);
  5. Dolphin* dolphin = context;
  6. if(event->state) {
  7. if(event->input == InputRight) {
  8. uint32_t page;
  9. with_view_model(
  10. dolphin->idle_view_first_start,
  11. (DolphinViewFirstStartModel * model) { page = ++model->page; });
  12. if(page > 8) {
  13. dolphin_save(dolphin);
  14. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  15. }
  16. }
  17. }
  18. // All events consumed
  19. return true;
  20. }
  21. bool dolphin_view_idle_main_input(InputEvent* event, void* context) {
  22. furi_assert(event);
  23. furi_assert(context);
  24. Dolphin* dolphin = context;
  25. if(event->state) {
  26. if(event->input == InputOk) {
  27. with_value_mutex(
  28. dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
  29. } else if(event->input == InputUp) {
  30. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleStats);
  31. } else if(event->input == InputDown) {
  32. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleDebug);
  33. }
  34. }
  35. // All events consumed
  36. return true;
  37. }
  38. bool dolphin_view_idle_stats_input(InputEvent* event, void* context) {
  39. furi_assert(event);
  40. furi_assert(context);
  41. Dolphin* dolphin = context;
  42. if(!event->state) return false;
  43. if(event->input == InputLeft) {
  44. dolphin_deed(dolphin, DolphinDeedWrong);
  45. } else if(event->input == InputRight) {
  46. dolphin_deed(dolphin, DolphinDeedIButtonRead);
  47. } else if(event->input == InputOk) {
  48. dolphin_save(dolphin);
  49. } else {
  50. return false;
  51. }
  52. return true;
  53. }
  54. Dolphin* dolphin_alloc() {
  55. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  56. // Message queue
  57. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  58. furi_check(dolphin->event_queue);
  59. // State
  60. dolphin->state = dolphin_state_alloc();
  61. // Menu
  62. dolphin->menu_vm = furi_open("menu");
  63. furi_check(dolphin->menu_vm);
  64. // GUI
  65. dolphin->idle_view_dispatcher = view_dispatcher_alloc();
  66. // First start View
  67. dolphin->idle_view_first_start = view_alloc();
  68. view_allocate_model(
  69. dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
  70. view_set_context(dolphin->idle_view_first_start, dolphin);
  71. view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
  72. view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
  73. view_dispatcher_add_view(
  74. dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
  75. // Main Idle View
  76. dolphin->idle_view_main = view_alloc();
  77. view_set_context(dolphin->idle_view_main, dolphin);
  78. view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
  79. view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
  80. view_dispatcher_add_view(
  81. dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
  82. // Stats Idle View
  83. dolphin->idle_view_stats = view_alloc();
  84. view_set_context(dolphin->idle_view_stats, dolphin);
  85. view_allocate_model(
  86. dolphin->idle_view_stats, ViewModelTypeLockFree, sizeof(DolphinViewIdleStatsModel));
  87. view_set_draw_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_draw);
  88. view_set_input_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_input);
  89. view_set_previous_callback(dolphin->idle_view_stats, dolphin_view_idle_back);
  90. view_dispatcher_add_view(
  91. dolphin->idle_view_dispatcher, DolphinViewIdleStats, dolphin->idle_view_stats);
  92. // Debug Idle View
  93. dolphin->idle_view_debug = view_alloc();
  94. view_set_draw_callback(dolphin->idle_view_debug, dolphin_view_idle_debug_draw);
  95. view_set_previous_callback(dolphin->idle_view_debug, dolphin_view_idle_back);
  96. view_dispatcher_add_view(
  97. dolphin->idle_view_dispatcher, DolphinViewIdleDebug, dolphin->idle_view_debug);
  98. return dolphin;
  99. }
  100. void dolphin_save(Dolphin* dolphin) {
  101. furi_assert(dolphin);
  102. DolphinEvent event;
  103. event.type = DolphinEventTypeSave;
  104. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  105. }
  106. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  107. furi_assert(dolphin);
  108. DolphinEvent event;
  109. event.type = DolphinEventTypeDeed;
  110. event.deed = deed;
  111. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  112. }
  113. void dolphin_task() {
  114. Dolphin* dolphin = dolphin_alloc();
  115. Gui* gui = furi_open("gui");
  116. view_dispatcher_attach_to_gui(dolphin->idle_view_dispatcher, gui, ViewDispatcherTypeWindow);
  117. if(dolphin_state_load(dolphin->state)) {
  118. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  119. } else {
  120. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
  121. }
  122. with_view_model(
  123. dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
  124. model->icounter = dolphin_state_get_icounter(dolphin->state);
  125. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  126. });
  127. if(!furi_create("dolphin", dolphin)) {
  128. printf("[dolphin_task] cannot create the dolphin record\n");
  129. furiac_exit(NULL);
  130. }
  131. furiac_ready();
  132. DolphinEvent event;
  133. while(1) {
  134. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  135. if(event.type == DolphinEventTypeDeed) {
  136. dolphin_state_on_deed(dolphin->state, event.deed);
  137. with_view_model(
  138. dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
  139. model->icounter = dolphin_state_get_icounter(dolphin->state);
  140. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  141. });
  142. } else if(event.type == DolphinEventTypeSave) {
  143. dolphin_state_save(dolphin->state);
  144. }
  145. }
  146. }