usping.c 9.0 KB

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