usping.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // inspired by
  2. // https://github.com/esphome/esphome/blob/ac0d921413c3884752193fe568fa82853f0f99e9/esphome/components/ultrasonic/ultrasonic_sensor.cpp
  3. // Ported and modified by @xMasterX
  4. // Modified by privet971 using PING))) sensor from Parallax (3 wires) 3/23/2023
  5. // use only gpio 13 for TX & RX to SIG pin
  6. #include <furi.h>
  7. #include <furi_hal.h>
  8. #include <furi_hal_power.h>
  9. #include <gui/gui.h>
  10. #include <input/input.h>
  11. #include <stdlib.h>
  12. #include <gui/elements.h>
  13. #include <notification/notification.h>
  14. #include <notification/notification_messages.h>
  15. typedef enum {
  16. EventTypeTick,
  17. EventTypeKey,
  18. } EventType;
  19. typedef struct {
  20. EventType type;
  21. InputEvent input;
  22. } PluginEvent;
  23. typedef struct {
  24. FuriMutex* mutex;
  25. NotificationApp* notification;
  26. bool have_5v;
  27. bool measurement_made;
  28. uint32_t echo; // us
  29. float distance; // meters
  30. } PluginState;
  31. const NotificationSequence sequence_done = {
  32. &message_display_backlight_on,
  33. &message_green_255,
  34. &message_note_c5,
  35. &message_delay_50,
  36. &message_sound_off,
  37. NULL,
  38. };
  39. static void render_callback(Canvas* const canvas, void* ctx) {
  40. furi_assert(ctx);
  41. const PluginState* plugin_state = ctx;
  42. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  43. // border around the edge of the screen
  44. // canvas_draw_frame(canvas, 0, 0, 128, 64);
  45. canvas_set_font(canvas, FontPrimary);
  46. elements_multiline_text_aligned(
  47. canvas, 64, 2, AlignCenter, AlignTop, "PING Ultrasonic\nDistance Sensor");
  48. canvas_set_font(canvas, FontSecondary);
  49. if(!plugin_state->have_5v) {
  50. elements_multiline_text_aligned(
  51. canvas,
  52. 4,
  53. 28,
  54. AlignLeft,
  55. AlignTop,
  56. "5V on GPIO must be\nenabled, or USB must\nbe connected.");
  57. } else {
  58. if(!plugin_state->measurement_made) {
  59. elements_multiline_text_aligned(
  60. canvas, 64, 28, AlignCenter, AlignTop, "Press OK button to measure");
  61. elements_multiline_text_aligned(
  62. canvas, 64, 40, AlignCenter, AlignTop, "13/TX -> Sig pin\n");
  63. } else {
  64. elements_multiline_text_aligned(canvas, 4, 28, AlignLeft, AlignTop, "Readout:");
  65. FuriString* str_buf;
  66. str_buf = furi_string_alloc();
  67. furi_string_printf(str_buf, "Echo: %ld us", plugin_state->echo);
  68. canvas_draw_str_aligned(
  69. canvas, 8, 38, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
  70. furi_string_printf(str_buf, "Distance: %02f m", (double)plugin_state->distance);
  71. canvas_draw_str_aligned(
  72. canvas, 8, 48, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
  73. furi_string_free(str_buf);
  74. }
  75. }
  76. furi_mutex_release(plugin_state->mutex);
  77. }
  78. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  79. furi_assert(event_queue);
  80. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  81. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  82. }
  83. static void usping_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 usping_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 usping_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. //FURI_CRITICAL_ENTER();
  121. // 10 ms pulse on TX
  122. furi_hal_gpio_write(&gpio_usart_tx, true);
  123. furi_delay_ms(10);
  124. furi_hal_gpio_write(&gpio_usart_tx, false);
  125. // change TX > RX
  126. furi_hal_gpio_write(&gpio_usart_tx, false);
  127. furi_hal_gpio_init(&gpio_usart_tx, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
  128. const uint32_t start = furi_get_tick();
  129. while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_tx))
  130. ;
  131. while(furi_get_tick() - start < timeout_ms && !furi_hal_gpio_read(&gpio_usart_tx))
  132. ;
  133. const uint32_t pulse_start = DWT->CYCCNT;
  134. while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_tx))
  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 = usping_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 usping_app() {
  147. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  148. PluginState* plugin_state = malloc(sizeof(PluginState));
  149. usping_state_init(plugin_state);
  150. FuriHalSerialHandle* serial_handle = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
  151. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  152. if(!plugin_state->mutex) {
  153. FURI_LOG_E("usping", "cannot create mutex\r\n");
  154. if(furi_hal_power_is_otg_enabled()) {
  155. furi_hal_power_disable_otg();
  156. }
  157. furi_hal_serial_control_release(serial_handle);
  158. furi_hal_power_suppress_charge_exit();
  159. furi_message_queue_free(event_queue);
  160. free(plugin_state);
  161. return 255;
  162. }
  163. plugin_state->notification = furi_record_open(RECORD_NOTIFICATION);
  164. // Set system callbacks
  165. ViewPort* view_port = view_port_alloc();
  166. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  167. view_port_input_callback_set(view_port, input_callback, event_queue);
  168. // Open GUI and register view_port
  169. Gui* gui = furi_record_open(RECORD_GUI);
  170. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  171. PluginEvent event;
  172. for(bool processing = true; processing;) {
  173. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  174. furi_mutex_acquire(plugin_state->mutex, FuriWaitForever);
  175. if(event_status == FuriStatusOk) {
  176. // press events
  177. if(event.type == EventTypeKey) {
  178. if(event.input.type == InputTypePress) {
  179. switch(event.input.key) {
  180. case InputKeyUp:
  181. case InputKeyDown:
  182. case InputKeyRight:
  183. case InputKeyLeft:
  184. break;
  185. case InputKeyOk:
  186. usping_measure(plugin_state);
  187. break;
  188. case InputKeyBack:
  189. processing = false;
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. furi_mutex_release(plugin_state->mutex);
  198. view_port_update(view_port);
  199. }
  200. if(furi_hal_power_is_otg_enabled()) {
  201. furi_hal_power_disable_otg();
  202. }
  203. furi_hal_power_suppress_charge_exit();
  204. // Return TX / RX back to usart mode
  205. furi_hal_gpio_init_ex(
  206. &gpio_usart_tx,
  207. GpioModeAltFunctionPushPull,
  208. GpioPullUp,
  209. GpioSpeedVeryHigh,
  210. GpioAltFn7USART1);
  211. furi_hal_serial_control_release(serial_handle);
  212. view_port_enabled_set(view_port, false);
  213. gui_remove_view_port(gui, view_port);
  214. furi_record_close(RECORD_GUI);
  215. furi_record_close(RECORD_NOTIFICATION);
  216. view_port_free(view_port);
  217. furi_message_queue_free(event_queue);
  218. furi_mutex_free(plugin_state->mutex);
  219. free(plugin_state);
  220. return 0;
  221. }