hc_sr04.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // insired by
  2. // https://github.com/esphome/esphome/blob/ac0d921413c3884752193fe568fa82853f0f99e9/esphome/components/ultrasonic/ultrasonic_sensor.cpp
  3. // Ported and modified by @xMasterX
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. #include <furi_hal_power.h>
  7. #include <gui/gui.h>
  8. #include <input/input.h>
  9. #include <stdlib.h>
  10. #include <gui/elements.h>
  11. #include <notification/notification.h>
  12. #include <notification/notification_messages.h>
  13. #include <expansion/expansion.h>
  14. typedef enum {
  15. EventTypeTick,
  16. EventTypeKey,
  17. } EventType;
  18. typedef struct {
  19. EventType type;
  20. InputEvent input;
  21. } PluginEvent;
  22. typedef struct {
  23. FuriMutex* mutex;
  24. NotificationApp* notification;
  25. bool have_5v;
  26. bool measurement_made;
  27. uint32_t echo; // us
  28. float distance; // meters
  29. } PluginState;
  30. const NotificationSequence sequence_done = {
  31. &message_display_backlight_on,
  32. &message_green_255,
  33. &message_note_c5,
  34. &message_delay_50,
  35. &message_sound_off,
  36. NULL,
  37. };
  38. static void render_callback(Canvas* const canvas, void* ctx) {
  39. furi_assert(ctx);
  40. const PluginState* plugin_state = ctx;
  41. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  42. // border around the edge of the screen
  43. // canvas_draw_frame(canvas, 0, 0, 128, 64);
  44. canvas_set_font(canvas, FontPrimary);
  45. elements_multiline_text_aligned(
  46. canvas, 64, 2, AlignCenter, AlignTop, "HC-SR04 Ultrasonic\nDistance Sensor");
  47. canvas_set_font(canvas, FontSecondary);
  48. if(!plugin_state->have_5v) {
  49. elements_multiline_text_aligned(
  50. canvas,
  51. 4,
  52. 28,
  53. AlignLeft,
  54. AlignTop,
  55. "5V on GPIO must be\nenabled, or USB must\nbe connected.");
  56. } else {
  57. if(!plugin_state->measurement_made) {
  58. elements_multiline_text_aligned(
  59. canvas, 64, 28, AlignCenter, AlignTop, "Press OK button to measure");
  60. elements_multiline_text_aligned(
  61. canvas, 64, 40, AlignCenter, AlignTop, "13/TX -> Trig\n14/RX -> Echo");
  62. } else {
  63. elements_multiline_text_aligned(canvas, 4, 28, AlignLeft, AlignTop, "Readout:");
  64. FuriString* str_buf;
  65. str_buf = furi_string_alloc();
  66. furi_string_printf(str_buf, "Echo: %ld us", plugin_state->echo);
  67. canvas_draw_str_aligned(
  68. canvas, 8, 38, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
  69. furi_string_printf(str_buf, "Distance: %02f m", (double)plugin_state->distance);
  70. canvas_draw_str_aligned(
  71. canvas, 8, 48, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
  72. furi_string_free(str_buf);
  73. }
  74. }
  75. furi_mutex_release(plugin_state->mutex);
  76. }
  77. static void input_callback(InputEvent* input_event, void* ctx) {
  78. furi_assert(ctx);
  79. FuriMessageQueue* event_queue = ctx;
  80. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  81. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  82. }
  83. static void hc_sr04_state_init(PluginState* const plugin_state) {
  84. plugin_state->echo = -1;
  85. plugin_state->distance = -1;
  86. plugin_state->measurement_made = false;
  87. furi_hal_power_suppress_charge_enter();
  88. plugin_state->have_5v = false;
  89. if(furi_hal_power_is_otg_enabled() || furi_hal_power_is_charging()) {
  90. plugin_state->have_5v = true;
  91. } else {
  92. furi_hal_power_enable_otg();
  93. plugin_state->have_5v = true;
  94. }
  95. }
  96. float hc_sr04_us_to_m(uint32_t us) {
  97. //speed of sound for 20°C, 50% relative humidity
  98. //331.3 + 20 * 0.606 + 50 * 0.0124 = 0.034404
  99. const float speed_sound_m_per_s = 344.04f;
  100. const float time_s = us / 1e6f;
  101. const float total_dist = time_s * speed_sound_m_per_s;
  102. return total_dist / 2.0f;
  103. }
  104. static void hc_sr04_measure(PluginState* const plugin_state) {
  105. //plugin_state->echo = 1;
  106. //return;
  107. if(!plugin_state->have_5v) {
  108. if(furi_hal_power_is_otg_enabled() || furi_hal_power_is_charging()) {
  109. plugin_state->have_5v = true;
  110. } else {
  111. return;
  112. }
  113. }
  114. //furi_hal_light_set(LightRed, 0xFF);
  115. notification_message(plugin_state->notification, &sequence_blink_start_yellow);
  116. const uint32_t timeout_ms = 2000;
  117. // Pin 13 / TX -> Trig
  118. furi_hal_gpio_write(&gpio_usart_tx, false);
  119. furi_hal_gpio_init(&gpio_usart_tx, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  120. // Pin 14 / RX -> Echo
  121. furi_hal_gpio_write(&gpio_usart_rx, false);
  122. furi_hal_gpio_init(&gpio_usart_rx, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
  123. //FURI_CRITICAL_ENTER();
  124. // 10 ms pulse on TX
  125. furi_hal_gpio_write(&gpio_usart_tx, true);
  126. furi_delay_ms(10);
  127. furi_hal_gpio_write(&gpio_usart_tx, false);
  128. const uint32_t start = furi_get_tick();
  129. while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
  130. ;
  131. while(furi_get_tick() - start < timeout_ms && !furi_hal_gpio_read(&gpio_usart_rx))
  132. ;
  133. const uint32_t pulse_start = DWT->CYCCNT;
  134. while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
  135. ;
  136. const uint32_t pulse_end = DWT->CYCCNT;
  137. //FURI_CRITICAL_EXIT();
  138. plugin_state->echo =
  139. (pulse_end - pulse_start) / furi_hal_cortex_instructions_per_microsecond();
  140. plugin_state->distance = hc_sr04_us_to_m(plugin_state->echo);
  141. plugin_state->measurement_made = true;
  142. //furi_hal_light_set(LightRed, 0x00);
  143. notification_message(plugin_state->notification, &sequence_blink_stop);
  144. notification_message(plugin_state->notification, &sequence_done);
  145. }
  146. int32_t hc_sr04_app() {
  147. // Disable expansion protocol to avoid interference with UART Handle
  148. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  149. expansion_disable(expansion);
  150. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  151. PluginState* plugin_state = malloc(sizeof(PluginState));
  152. hc_sr04_state_init(plugin_state);
  153. FuriHalSerialHandle* serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  154. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  155. if(!plugin_state->mutex) {
  156. FURI_LOG_E("hc_sr04", "cannot create mutex\r\n");
  157. if(furi_hal_power_is_otg_enabled()) {
  158. furi_hal_power_disable_otg();
  159. }
  160. furi_hal_serial_control_release(serial_handle);
  161. furi_hal_power_suppress_charge_exit();
  162. furi_message_queue_free(event_queue);
  163. free(plugin_state);
  164. // Return previous state of expansion
  165. expansion_enable(expansion);
  166. furi_record_close(RECORD_EXPANSION);
  167. return 255;
  168. }
  169. plugin_state->notification = furi_record_open(RECORD_NOTIFICATION);
  170. // Set system callbacks
  171. ViewPort* view_port = view_port_alloc();
  172. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  173. view_port_input_callback_set(view_port, input_callback, event_queue);
  174. // Open GUI and register view_port
  175. Gui* gui = furi_record_open(RECORD_GUI);
  176. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  177. PluginEvent event;
  178. for(bool processing = true; processing;) {
  179. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  180. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  181. if(event_status == FuriStatusOk) {
  182. // press events
  183. if(event.type == EventTypeKey) {
  184. if(event.input.type == InputTypePress) {
  185. switch(event.input.key) {
  186. case InputKeyUp:
  187. case InputKeyDown:
  188. case InputKeyRight:
  189. case InputKeyLeft:
  190. break;
  191. case InputKeyOk:
  192. hc_sr04_measure(plugin_state);
  193. break;
  194. case InputKeyBack:
  195. processing = false;
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. furi_mutex_release(plugin_state->mutex);
  204. view_port_update(view_port);
  205. }
  206. if(furi_hal_power_is_otg_enabled()) {
  207. furi_hal_power_disable_otg();
  208. }
  209. furi_hal_power_suppress_charge_exit();
  210. // Return TX / RX back to usart mode
  211. furi_hal_gpio_init_ex(
  212. &gpio_usart_tx,
  213. GpioModeAltFunctionPushPull,
  214. GpioPullUp,
  215. GpioSpeedVeryHigh,
  216. GpioAltFn7USART1);
  217. furi_hal_gpio_init_ex(
  218. &gpio_usart_rx,
  219. GpioModeAltFunctionPushPull,
  220. GpioPullUp,
  221. GpioSpeedVeryHigh,
  222. GpioAltFn7USART1);
  223. furi_hal_serial_control_release(serial_handle);
  224. view_port_enabled_set(view_port, false);
  225. gui_remove_view_port(gui, view_port);
  226. furi_record_close(RECORD_GUI);
  227. furi_record_close(RECORD_NOTIFICATION);
  228. view_port_free(view_port);
  229. furi_message_queue_free(event_queue);
  230. furi_mutex_free(plugin_state->mutex);
  231. free(plugin_state);
  232. // Return previous state of expansion
  233. expansion_enable(expansion);
  234. furi_record_close(RECORD_EXPANSION);
  235. return 0;
  236. }