hal.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <stdlib.h>
  4. #include <m-string.h>
  5. #include <stm32wbxx_ll_tim.h>
  6. #include "tama.h"
  7. #define TAG_HAL "TamaLIB"
  8. static void* tamagotchi_p1_hal_malloc(u32_t size) {
  9. return malloc(size);
  10. }
  11. static void tamagotchi_p1_hal_free(void* ptr) {
  12. free(ptr);
  13. }
  14. static void tamagotchi_p1_hal_halt(void) {
  15. g_ctx->halted = true;
  16. }
  17. static bool_t tamagotchi_p1_hal_is_log_enabled(log_level_t level) {
  18. switch(level) {
  19. case LOG_ERROR:
  20. return true;
  21. case LOG_INFO:
  22. return true;
  23. case LOG_MEMORY:
  24. return false;
  25. case LOG_CPU:
  26. return false;
  27. default:
  28. return false;
  29. }
  30. }
  31. static void tamagotchi_p1_hal_log(log_level_t level, char* buff, ...) {
  32. if(!tamagotchi_p1_hal_is_log_enabled(level)) return;
  33. string_t string;
  34. va_list args;
  35. va_start(args, buff);
  36. string_init_vprintf(string, buff, args);
  37. va_end(args);
  38. switch(level) {
  39. case LOG_ERROR:
  40. FURI_LOG_E(TAG_HAL, "%s", string_get_cstr(string));
  41. break;
  42. case LOG_INFO:
  43. FURI_LOG_I(TAG_HAL, "%s", string_get_cstr(string));
  44. break;
  45. case LOG_MEMORY:
  46. case LOG_CPU:
  47. default:
  48. FURI_LOG_D(TAG_HAL, "%s", string_get_cstr(string));
  49. break;
  50. }
  51. string_clear(string);
  52. }
  53. static void tamagotchi_p1_hal_sleep_until(timestamp_t ts) {
  54. while(true) {
  55. uint32_t count = LL_TIM_GetCounter(TIM2);
  56. uint32_t delay = ts - count;
  57. // FURI_LOG_D(TAG, "delay: %x", delay);
  58. // Stolen from furi_delay_until_tick
  59. if(delay != 0 && 0 == (delay >> (8 * sizeof(uint32_t) - 1))) {
  60. // Not the best place to release mutex, but this is the only place we know whether
  61. // we're ahead or behind, otherwise around the step call we'll always have to
  62. // delay a tick and run more and more behind.
  63. furi_mutex_release(g_state_mutex);
  64. furi_delay_tick(1);
  65. while(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk)
  66. furi_delay_tick(1);
  67. } else {
  68. break;
  69. }
  70. }
  71. }
  72. static timestamp_t tamagotchi_p1_hal_get_timestamp(void) {
  73. return LL_TIM_GetCounter(TIM2);
  74. }
  75. static void tamagotchi_p1_hal_update_screen(void) {
  76. // Do nothing, covered by main loop
  77. }
  78. static void tamagotchi_p1_hal_set_lcd_matrix(u8_t x, u8_t y, bool_t val) {
  79. if(val)
  80. g_ctx->framebuffer[y] |= 1 << x;
  81. else
  82. g_ctx->framebuffer[y] &= ~(1 << x);
  83. }
  84. static void tamagotchi_p1_hal_set_lcd_icon(u8_t icon, bool_t val) {
  85. if(val)
  86. g_ctx->icons |= 1 << icon;
  87. else
  88. g_ctx->icons &= ~(1 << icon);
  89. }
  90. static void tamagotchi_p1_hal_play_frequency(bool_t en) {
  91. if(en)
  92. furi_hal_speaker_start(g_ctx->frequency, 0.5f);
  93. else
  94. furi_hal_speaker_stop();
  95. g_ctx->buzzer_on = en;
  96. }
  97. static void tamagotchi_p1_hal_set_frequency(u32_t freq) {
  98. g_ctx->frequency = freq / 10.0F;
  99. if(g_ctx->buzzer_on) tamagotchi_p1_hal_play_frequency(true);
  100. }
  101. static int tamagotchi_p1_hal_handler(void) {
  102. // Do nothing
  103. return 0;
  104. }
  105. void tamagotchi_p1_hal_init(hal_t* hal) {
  106. hal->malloc = tamagotchi_p1_hal_malloc;
  107. hal->free = tamagotchi_p1_hal_free;
  108. hal->halt = tamagotchi_p1_hal_halt;
  109. hal->is_log_enabled = tamagotchi_p1_hal_is_log_enabled;
  110. hal->log = tamagotchi_p1_hal_log;
  111. hal->sleep_until = tamagotchi_p1_hal_sleep_until;
  112. hal->get_timestamp = tamagotchi_p1_hal_get_timestamp;
  113. hal->update_screen = tamagotchi_p1_hal_update_screen;
  114. hal->set_lcd_matrix = tamagotchi_p1_hal_set_lcd_matrix;
  115. hal->set_lcd_icon = tamagotchi_p1_hal_set_lcd_icon;
  116. hal->set_frequency = tamagotchi_p1_hal_set_frequency;
  117. hal->play_frequency = tamagotchi_p1_hal_play_frequency;
  118. hal->handler = tamagotchi_p1_hal_handler;
  119. }