Firmware SDK
twr_hc_sr04.h
1 #ifndef _TWR_HC_SR04_H
2 #define _TWR_HC_SR04_H
3 
4 #include <twr_tick.h>
5 #include <twr_gpio.h>
6 #include <twr_scheduler.h>
7 
11 
12 typedef enum
13 {
16 
19 
21 
22 typedef enum
23 {
24  TWR_HC_SR04_ECHO_P5 = TWR_GPIO_P5,
25  TWR_HC_SR04_ECHO_P8 = TWR_GPIO_P8,
26 
27 } twr_hc_sr04_echo_t;
28 
29 
31 
32 typedef struct twr_hc_sr04_t twr_hc_sr04_t;
33 
35 
36 struct twr_hc_sr04_t
37 {
38  twr_scheduler_task_id_t _task_id_interval;
39  twr_scheduler_task_id_t _task_id_notify;
40  void (*_event_handler)(twr_hc_sr04_t *, twr_hc_sr04_event_t, void *);
41  void *_event_param;
42  twr_tick_t _update_interval;
43  bool _measurement_active;
44  bool _measurement_valid;
45  uint16_t _echo_duration;
46  twr_hc_sr04_echo_t _echo;
47  twr_gpio_channel_t _trig;
48 };
49 
51 
54 
56 
61 
62 void twr_hc_sr04_init(twr_hc_sr04_t *self, twr_gpio_channel_t trig, twr_hc_sr04_echo_t echo);
63 
68 
69 void twr_hc_sr04_set_event_handler(twr_hc_sr04_t *self, void (*event_handler)(twr_hc_sr04_t *, twr_hc_sr04_event_t, void *), void *event_param);
70 
74 
76 
81 
83 
89 
90 bool twr_hc_sr04_get_distance_millimeter(twr_hc_sr04_t *self, float *millimeter);
91 
93 
94 #endif // _TWR_HC_SR04_H
twr_gpio_channel_t
GPIO channels.
Definition: twr_gpio.h:13
@ TWR_GPIO_P8
GPIO channel P8.
Definition: twr_gpio.h:39
@ TWR_GPIO_P5
GPIO channel P5, A5, DAC1.
Definition: twr_gpio.h:30
void twr_hc_sr04_init_sensor_module(twr_hc_sr04_t *self)
Initialize HC-SR04 for sensor module.
Definition: twr_hc_sr04.c:14
twr_hc_sr04_event_t
Definition: twr_hc_sr04.h:13
void twr_hc_sr04_set_update_interval(twr_hc_sr04_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_hc_sr04.c:98
void twr_hc_sr04_init(twr_hc_sr04_t *self, twr_gpio_channel_t trig, twr_hc_sr04_echo_t echo)
Initialize HC-SR04.
Definition: twr_hc_sr04.c:19
struct twr_hc_sr04_t twr_hc_sr04_t
HC-SR04 instance.
Definition: twr_hc_sr04.h:32
bool twr_hc_sr04_measure(twr_hc_sr04_t *self)
Start measurement manually.
Definition: twr_hc_sr04.c:114
bool twr_hc_sr04_get_distance_millimeter(twr_hc_sr04_t *self, float *millimeter)
Get measured distance in millimeters.
Definition: twr_hc_sr04.c:190
void twr_hc_sr04_set_event_handler(twr_hc_sr04_t *self, void(*event_handler)(twr_hc_sr04_t *, twr_hc_sr04_event_t, void *), void *event_param)
Set callback function.
Definition: twr_hc_sr04.c:92
@ TWR_HC_SR04_EVENT_ERROR
Error event.
Definition: twr_hc_sr04.h:15
@ TWR_HC_SR04_EVENT_UPDATE
Update event.
Definition: twr_hc_sr04.h:18
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16