dolphin.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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->type == InputTypeShort) {
  7. if(event->key == InputKeyLeft) {
  8. with_view_model(
  9. dolphin->idle_view_first_start, (DolphinViewFirstStartModel * model) {
  10. if(model->page > 0) model->page--;
  11. return true;
  12. });
  13. } else if(event->key == InputKeyRight) {
  14. uint32_t page;
  15. with_view_model(
  16. dolphin->idle_view_first_start, (DolphinViewFirstStartModel * model) {
  17. page = ++model->page;
  18. return true;
  19. });
  20. if(page > 8) {
  21. dolphin_save(dolphin);
  22. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  23. }
  24. }
  25. }
  26. // All events consumed
  27. return true;
  28. }
  29. bool dolphin_view_idle_main_input(InputEvent* event, void* context) {
  30. furi_assert(event);
  31. furi_assert(context);
  32. Dolphin* dolphin = context;
  33. if(event->type == InputTypeShort) {
  34. if(event->key == InputKeyOk) {
  35. with_value_mutex(
  36. dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
  37. } else if(event->key == InputKeyUp) {
  38. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleUp);
  39. } else if(event->key == InputKeyDown) {
  40. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleDown);
  41. }
  42. }
  43. // All events consumed
  44. return true;
  45. }
  46. bool dolphin_view_idle_up_input(InputEvent* event, void* context) {
  47. furi_assert(event);
  48. furi_assert(context);
  49. Dolphin* dolphin = context;
  50. if(event->type != InputTypeShort) return false;
  51. if(event->key == InputKeyLeft) {
  52. dolphin_deed(dolphin, DolphinDeedWrong);
  53. } else if(event->key == InputKeyRight) {
  54. dolphin_deed(dolphin, DolphinDeedIButtonRead);
  55. } else if(event->key == InputKeyOk) {
  56. dolphin_save(dolphin);
  57. } else {
  58. return false;
  59. }
  60. return true;
  61. }
  62. Dolphin* dolphin_alloc() {
  63. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  64. // Message queue
  65. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  66. furi_check(dolphin->event_queue);
  67. // State
  68. dolphin->state = dolphin_state_alloc();
  69. // Menu
  70. dolphin->menu_vm = furi_record_open("menu");
  71. // GUI
  72. dolphin->idle_view_dispatcher = view_dispatcher_alloc();
  73. // First start View
  74. dolphin->idle_view_first_start = view_alloc();
  75. view_allocate_model(
  76. dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
  77. view_set_context(dolphin->idle_view_first_start, dolphin);
  78. view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
  79. view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
  80. view_dispatcher_add_view(
  81. dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
  82. // Main Idle View
  83. dolphin->idle_view_main = view_alloc();
  84. view_set_context(dolphin->idle_view_main, dolphin);
  85. view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
  86. view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
  87. view_dispatcher_add_view(
  88. dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
  89. // Stats Idle View
  90. dolphin->idle_view_up = view_alloc();
  91. view_set_context(dolphin->idle_view_up, dolphin);
  92. view_allocate_model(
  93. dolphin->idle_view_up, ViewModelTypeLockFree, sizeof(DolphinViewIdleUpModel));
  94. view_set_draw_callback(dolphin->idle_view_up, dolphin_view_idle_up_draw);
  95. view_set_input_callback(dolphin->idle_view_up, dolphin_view_idle_up_input);
  96. view_set_previous_callback(dolphin->idle_view_up, dolphin_view_idle_back);
  97. view_dispatcher_add_view(
  98. dolphin->idle_view_dispatcher, DolphinViewIdleUp, dolphin->idle_view_up);
  99. // Down Idle View
  100. dolphin->idle_view_down = view_alloc();
  101. view_set_draw_callback(dolphin->idle_view_down, dolphin_view_idle_down_draw);
  102. view_set_previous_callback(dolphin->idle_view_down, dolphin_view_idle_back);
  103. view_dispatcher_add_view(
  104. dolphin->idle_view_dispatcher, DolphinViewIdleDown, dolphin->idle_view_down);
  105. // HW Mismatch
  106. dolphin->view_hw_mismatch = view_alloc();
  107. view_set_draw_callback(dolphin->view_hw_mismatch, dolphin_view_hw_mismatch_draw);
  108. view_set_previous_callback(dolphin->view_hw_mismatch, dolphin_view_idle_back);
  109. view_dispatcher_add_view(
  110. dolphin->idle_view_dispatcher, DolphinViewHwMismatch, dolphin->view_hw_mismatch);
  111. return dolphin;
  112. }
  113. void dolphin_save(Dolphin* dolphin) {
  114. furi_assert(dolphin);
  115. DolphinEvent event;
  116. event.type = DolphinEventTypeSave;
  117. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  118. }
  119. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  120. furi_assert(dolphin);
  121. DolphinEvent event;
  122. event.type = DolphinEventTypeDeed;
  123. event.deed = deed;
  124. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  125. }
  126. int32_t dolphin_task() {
  127. Dolphin* dolphin = dolphin_alloc();
  128. Gui* gui = furi_record_open("gui");
  129. view_dispatcher_attach_to_gui(dolphin->idle_view_dispatcher, gui, ViewDispatcherTypeWindow);
  130. if(dolphin_state_load(dolphin->state)) {
  131. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  132. } else {
  133. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
  134. }
  135. with_view_model(
  136. dolphin->idle_view_up, (DolphinViewIdleUpModel * model) {
  137. model->icounter = dolphin_state_get_icounter(dolphin->state);
  138. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  139. return true;
  140. });
  141. furi_record_create("dolphin", dolphin);
  142. if(!api_hal_version_do_i_belong_here()) {
  143. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewHwMismatch);
  144. }
  145. DolphinEvent event;
  146. while(1) {
  147. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  148. if(event.type == DolphinEventTypeDeed) {
  149. dolphin_state_on_deed(dolphin->state, event.deed);
  150. with_view_model(
  151. dolphin->idle_view_up, (DolphinViewIdleUpModel * model) {
  152. model->icounter = dolphin_state_get_icounter(dolphin->state);
  153. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  154. return true;
  155. });
  156. } else if(event.type == DolphinEventTypeSave) {
  157. dolphin_state_save(dolphin->state);
  158. }
  159. }
  160. return 0;
  161. }