LibDriver HCSR04
Loading...
Searching...
No Matches
driver_hcsr04_read_test.c
Go to the documentation of this file.
1
37
39
40static hcsr04_handle_t gs_handle;
41
50uint8_t hcsr04_read_test(uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 hcsr04_info_t info;
55
56 /* link interface function */
68
69 /* get info */
70 res = hcsr04_info(&info);
71 if (res != 0)
72 {
73 hcsr04_interface_debug_print("hcsr04: get info failed.\n");
74
75 return 1;
76 }
77 else
78 {
79 /* print chip information */
80 hcsr04_interface_debug_print("hcsr04: chip is %s.\n", info.chip_name);
81 hcsr04_interface_debug_print("hcsr04: manufacturer is %s.\n", info.manufacturer_name);
82 hcsr04_interface_debug_print("hcsr04: interface is %s.\n", info.interface);
83 hcsr04_interface_debug_print("hcsr04: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
84 hcsr04_interface_debug_print("hcsr04: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
85 hcsr04_interface_debug_print("hcsr04: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
86 hcsr04_interface_debug_print("hcsr04: max current is %0.2fmA.\n", info.max_current_ma);
87 hcsr04_interface_debug_print("hcsr04: max temperature is %0.1fC.\n", info.temperature_max);
88 hcsr04_interface_debug_print("hcsr04: min temperature is %0.1fC.\n", info.temperature_min);
89 }
90
91 /* hscr04 init */
92 res = hcsr04_init(&gs_handle);
93 if (res != 0)
94 {
95 hcsr04_interface_debug_print("hcsr04: init failed.\n");
96
97 return 1;
98 }
99
100 /* start read test */
101 hcsr04_interface_debug_print("hcsr04: start read test.\n");
102 for (i = 0; i < times; i++)
103 {
104 uint32_t time_us;
105 float m;
106
107 /* read distance */
108 res = hcsr04_read(&gs_handle, (uint32_t *)&time_us, (float *)&m);
109 if (res != 0)
110 {
111 hcsr04_interface_debug_print("hcsr04: read failed.\n");
112 (void)hcsr04_deinit(&gs_handle);
113
114 return 1;
115 }
116 m *= 100.0f;
117 hcsr04_interface_debug_print("hcsr04: distance is %fcm.\n", m);
119 }
120
121 /* finish read test */
122 hcsr04_interface_debug_print("hcsr04: finish read test.\n");
123 (void)hcsr04_deinit(&gs_handle);
124
125 return 0;
126}
driver hcsr04 read test header file
uint8_t hcsr04_init(hcsr04_handle_t *handle)
initialize the chip
struct hcsr04_handle_s hcsr04_handle_t
hcsr04 handle structure definition
uint8_t hcsr04_read(hcsr04_handle_t *handle, uint32_t *time_us, float *m)
read the distance
uint8_t hcsr04_info(hcsr04_info_t *info)
get chip's information
uint8_t hcsr04_deinit(hcsr04_handle_t *handle)
close the chip
struct hcsr04_info_s hcsr04_info_t
hcsr04 information structure definition
uint8_t hcsr04_interface_echo_deinit(void)
interface echo deinit
uint8_t hcsr04_interface_timestamp_read(hcsr04_time_t *t)
interface timestamp read
void hcsr04_interface_delay_us(uint32_t us)
interface delay us
uint8_t hcsr04_interface_echo_read(uint8_t *value)
interface echo read
uint8_t hcsr04_interface_echo_init(void)
interface echo init
uint8_t hcsr04_interface_trig_init(void)
interface trig init
void hcsr04_interface_delay_ms(uint32_t ms)
interface delay ms
void hcsr04_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t hcsr04_interface_trig_write(uint8_t value)
interface trig write
uint8_t hcsr04_interface_trig_deinit(void)
interface trig deinit
uint8_t hcsr04_read_test(uint32_t times)
read test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]