dolphin.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "dolphin_i.h"
  2. #include <furi.h>
  3. bool dolphin_load(Dolphin* dolphin) {
  4. furi_assert(dolphin);
  5. return dolphin_state_load(dolphin->state);
  6. }
  7. void dolphin_save(Dolphin* dolphin) {
  8. furi_assert(dolphin);
  9. DolphinEvent event;
  10. event.type = DolphinEventTypeSave;
  11. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  12. }
  13. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  14. furi_assert(dolphin);
  15. DolphinEvent event;
  16. event.type = DolphinEventTypeDeed;
  17. event.deed = deed;
  18. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  19. }
  20. DolphinDeedWeight dolphin_stats(Dolphin* dolphin) {
  21. DolphinDeedWeight stats;
  22. stats.butthurt = dolphin_state_get_butthurt(dolphin->state);
  23. stats.icounter = dolphin_state_get_icounter(dolphin->state);
  24. return stats;
  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. int32_t dolphin_srv(void* p) {
  39. Dolphin* dolphin = dolphin_alloc();
  40. furi_record_create("dolphin", dolphin);
  41. DolphinEvent event;
  42. while(1) {
  43. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  44. switch(event.type) {
  45. case DolphinEventTypeDeed:
  46. dolphin_state_on_deed(dolphin->state, event.deed);
  47. break;
  48. case DolphinEventTypeSave:
  49. dolphin_state_save(dolphin->state);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. dolphin_free(dolphin);
  56. return 0;
  57. }