dolphin.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "dolphin_i.h"
  2. #include <furi.h>
  3. #define DOLPHIN_LOCK_EVENT_FLAG (0x1)
  4. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  5. furi_assert(dolphin);
  6. DolphinEvent event;
  7. event.type = DolphinEventTypeDeed;
  8. event.deed = deed;
  9. dolphin_event_send_async(dolphin, &event);
  10. }
  11. DolphinStats dolphin_stats(Dolphin* dolphin) {
  12. furi_assert(dolphin);
  13. DolphinStats stats;
  14. DolphinEvent event;
  15. event.type = DolphinEventTypeStats;
  16. event.stats = &stats;
  17. dolphin_event_send_wait(dolphin, &event);
  18. return stats;
  19. }
  20. void dolphin_flush(Dolphin* dolphin) {
  21. furi_assert(dolphin);
  22. DolphinEvent event;
  23. event.type = DolphinEventTypeFlush;
  24. dolphin_event_send_wait(dolphin, &event);
  25. }
  26. Dolphin* dolphin_alloc() {
  27. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  28. dolphin->state = dolphin_state_alloc();
  29. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  30. return dolphin;
  31. }
  32. void dolphin_free(Dolphin* dolphin) {
  33. furi_assert(dolphin);
  34. dolphin_state_free(dolphin->state);
  35. osMessageQueueDelete(dolphin->event_queue);
  36. free(dolphin);
  37. }
  38. void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
  39. furi_assert(dolphin);
  40. furi_assert(event);
  41. event->flag = NULL;
  42. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  43. }
  44. void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
  45. furi_assert(dolphin);
  46. furi_assert(event);
  47. event->flag = osEventFlagsNew(NULL);
  48. furi_check(event->flag);
  49. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  50. furi_check(
  51. osEventFlagsWait(event->flag, DOLPHIN_LOCK_EVENT_FLAG, osFlagsWaitAny, osWaitForever) ==
  52. DOLPHIN_LOCK_EVENT_FLAG);
  53. furi_check(osEventFlagsDelete(event->flag) == osOK);
  54. }
  55. void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event) {
  56. if(event->flag) {
  57. osEventFlagsSet(event->flag, DOLPHIN_LOCK_EVENT_FLAG);
  58. }
  59. }
  60. int32_t dolphin_srv(void* p) {
  61. Dolphin* dolphin = dolphin_alloc();
  62. furi_record_create("dolphin", dolphin);
  63. dolphin_state_load(dolphin->state);
  64. DolphinEvent event;
  65. while(1) {
  66. if(osMessageQueueGet(dolphin->event_queue, &event, NULL, 60000) == osOK) {
  67. if(event.type == DolphinEventTypeDeed) {
  68. dolphin_state_on_deed(dolphin->state, event.deed);
  69. } else if(event.type == DolphinEventTypeStats) {
  70. event.stats->icounter = dolphin_state_get_icounter(dolphin->state);
  71. event.stats->butthurt = dolphin_state_get_butthurt(dolphin->state);
  72. } else if(event.type == DolphinEventTypeFlush) {
  73. dolphin_state_save(dolphin->state);
  74. }
  75. dolphin_event_release(dolphin, &event);
  76. } else {
  77. dolphin_state_save(dolphin->state);
  78. }
  79. }
  80. dolphin_free(dolphin);
  81. return 0;
  82. }