hc_sr04.c 8.6 KB

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