dolphin.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "dolphin_i.h"
  2. #include <furi.h>
  3. #define DOLPHIN_TIMEGATE 86400 // one day
  4. #define DOLPHIN_LOCK_EVENT_FLAG (0x1)
  5. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  6. furi_assert(dolphin);
  7. DolphinEvent event;
  8. event.type = DolphinEventTypeDeed;
  9. event.deed = deed;
  10. dolphin_event_send_async(dolphin, &event);
  11. }
  12. DolphinStats dolphin_stats(Dolphin* dolphin) {
  13. furi_assert(dolphin);
  14. DolphinStats stats;
  15. DolphinEvent event;
  16. event.type = DolphinEventTypeStats;
  17. event.stats = &stats;
  18. dolphin_event_send_wait(dolphin, &event);
  19. return stats;
  20. }
  21. void dolphin_flush(Dolphin* dolphin) {
  22. furi_assert(dolphin);
  23. DolphinEvent event;
  24. event.type = DolphinEventTypeFlush;
  25. dolphin_event_send_wait(dolphin, &event);
  26. }
  27. Dolphin* dolphin_alloc() {
  28. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  29. dolphin->state = dolphin_state_alloc();
  30. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  31. return dolphin;
  32. }
  33. void dolphin_free(Dolphin* dolphin) {
  34. furi_assert(dolphin);
  35. dolphin_state_free(dolphin->state);
  36. osMessageQueueDelete(dolphin->event_queue);
  37. free(dolphin);
  38. }
  39. void dolphin_event_send_async(Dolphin* dolphin, DolphinEvent* event) {
  40. furi_assert(dolphin);
  41. furi_assert(event);
  42. event->flag = NULL;
  43. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  44. }
  45. void dolphin_event_send_wait(Dolphin* dolphin, DolphinEvent* event) {
  46. furi_assert(dolphin);
  47. furi_assert(event);
  48. event->flag = osEventFlagsNew(NULL);
  49. furi_check(event->flag);
  50. furi_check(osMessageQueuePut(dolphin->event_queue, event, 0, osWaitForever) == osOK);
  51. furi_check(
  52. osEventFlagsWait(event->flag, DOLPHIN_LOCK_EVENT_FLAG, osFlagsWaitAny, osWaitForever) ==
  53. DOLPHIN_LOCK_EVENT_FLAG);
  54. furi_check(osEventFlagsDelete(event->flag) == osOK);
  55. }
  56. void dolphin_event_release(Dolphin* dolphin, DolphinEvent* event) {
  57. if(event->flag) {
  58. osEventFlagsSet(event->flag, DOLPHIN_LOCK_EVENT_FLAG);
  59. }
  60. }
  61. static void dolphin_check_butthurt(DolphinState* state) {
  62. furi_assert(state);
  63. float diff_time = difftime(dolphin_state_get_timestamp(state), dolphin_state_timestamp());
  64. #if 0
  65. FURI_LOG_I("dolphin-state", "Butthurt check, time since deed %.0f", fabs(diff_time));
  66. #endif
  67. if((fabs(diff_time)) > DOLPHIN_TIMEGATE) {
  68. // increase butthurt
  69. FURI_LOG_I("dolphin-state", "Increasing butthurt");
  70. dolphin_state_butthurted(state);
  71. }
  72. }
  73. int32_t dolphin_srv(void* p) {
  74. Dolphin* dolphin = dolphin_alloc();
  75. furi_record_create("dolphin", dolphin);
  76. dolphin_state_load(dolphin->state);
  77. DolphinEvent event;
  78. while(1) {
  79. if(osMessageQueueGet(dolphin->event_queue, &event, NULL, 60000) == osOK) {
  80. if(event.type == DolphinEventTypeDeed) {
  81. dolphin_state_on_deed(dolphin->state, event.deed);
  82. } else if(event.type == DolphinEventTypeStats) {
  83. event.stats->icounter = dolphin_state_get_icounter(dolphin->state);
  84. event.stats->butthurt = dolphin_state_get_butthurt(dolphin->state);
  85. event.stats->timestamp = dolphin_state_get_timestamp(dolphin->state);
  86. } else if(event.type == DolphinEventTypeFlush) {
  87. dolphin_state_save(dolphin->state);
  88. }
  89. dolphin_event_release(dolphin, &event);
  90. } else {
  91. dolphin_check_butthurt(dolphin->state);
  92. dolphin_state_save(dolphin->state);
  93. }
  94. }
  95. dolphin_free(dolphin);
  96. return 0;
  97. }