LibDriver APM2000
Loading...
Searching...
No Matches
driver_apm2000_read_test.c
Go to the documentation of this file.
1
36
38
39static apm2000_handle_t gs_handle;
40
50uint8_t apm2000_read_test(apm2000_interface_t interface, uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 apm2000_info_t info;
55 apm2000_pm_t pm;
56
57 /* link functions */
70
71 /* get information */
72 res = apm2000_info(&info);
73 if (res != 0)
74 {
75 apm2000_interface_debug_print("apm2000: get info failed.\n");
76
77 return 1;
78 }
79 else
80 {
81 /* print chip info */
82 apm2000_interface_debug_print("apm2000: chip is %s.\n", info.chip_name);
83 apm2000_interface_debug_print("apm2000: manufacturer is %s.\n", info.manufacturer_name);
84 apm2000_interface_debug_print("apm2000: interface is %s.\n", info.interface);
85 apm2000_interface_debug_print("apm2000: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
86 apm2000_interface_debug_print("apm2000: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
87 apm2000_interface_debug_print("apm2000: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
88 apm2000_interface_debug_print("apm2000: max current is %0.2fmA.\n", info.max_current_ma);
89 apm2000_interface_debug_print("apm2000: max temperature is %0.1fC.\n", info.temperature_max);
90 apm2000_interface_debug_print("apm2000: min temperature is %0.1fC.\n", info.temperature_min);
91 }
92
93 /* start read test */
94 apm2000_interface_debug_print("apm2000: start read test.\n");
95
96 /* set the interface */
97 res = apm2000_set_interface(&gs_handle, interface);
98 if (res != 0)
99 {
100 apm2000_interface_debug_print("apm2000: set interface failed.\n");
101
102 return 1;
103 }
104
105 /* init the chip */
106 res = apm2000_init(&gs_handle);
107 if (res != 0)
108 {
109 apm2000_interface_debug_print("apm2000: init failed.\n");
110
111 return 1;
112 }
113
114 if (interface == APM2000_INTERFACE_IIC)
115 {
116 /* start measurement */
117 res = apm2000_start_measurement(&gs_handle);
118 if (res != 0)
119 {
120 apm2000_interface_debug_print("apm2000: start measurement failed.\n");
121 (void)apm2000_deinit(&gs_handle);
122
123 return 1;
124 }
125 }
126
127 /* delay 2000 ms */
129
130 for (i = 0; i < times; i++)
131 {
132 res = apm2000_read(&gs_handle, &pm);
133 if (res != 0)
134 {
135 apm2000_interface_debug_print("apm2000: read failed.\n");
136 (void)apm2000_deinit(&gs_handle);
137
138 return 1;
139 }
140 apm2000_interface_debug_print("apm2000: pm1.0 is %d ug/m3.\n", pm.pm1p0_ug_m3);
141 apm2000_interface_debug_print("apm2000: pm2.5 is %d ug/m3.\n", pm.pm2p5_ug_m3);
142 apm2000_interface_debug_print("apm2000: pm10.0 is %d ug/m3.\n", pm.pm10_ug_m3);
143
144 /* delay 2000 ms */
146 }
147
148 /* uart */
149 if (interface == APM2000_INTERFACE_UART)
150 {
151 /* uart read pm2p5 test */
152 apm2000_interface_debug_print("apm2000: uart read pm2p5 test.\n");
153
154 /* loop */
155 for (i = 0; i < times; i++)
156 {
157 uint16_t pm2p5_ug_m3;
158
159 res = apm2000_read_pm2p5(&gs_handle, &pm2p5_ug_m3);
160 if (res != 0)
161 {
162 apm2000_interface_debug_print("apm2000: read pm2p5 failed.\n");
163 (void)apm2000_deinit(&gs_handle);
164
165 return 1;
166 }
167 apm2000_interface_debug_print("apm2000: pm2.5 is %d ug/m3.\n", pm2p5_ug_m3);
168
169 /* delay 2000 ms */
171 }
172 }
173
174 if (interface == APM2000_INTERFACE_IIC)
175 {
176 /* stop measurement */
177 res = apm2000_stop_measurement(&gs_handle);
178 if (res != 0)
179 {
180 return 1;
181 }
182 }
183
184 /* finish read test */
185 apm2000_interface_debug_print("apm2000: finish read test.\n");
186 (void)apm2000_deinit(&gs_handle);
187
188 return 0;
189}
driver apm2000 read test header file
uint8_t apm2000_deinit(apm2000_handle_t *handle)
close the chip
struct apm2000_pm_s apm2000_pm_t
apm2000 pm structure definition
uint8_t apm2000_read_pm2p5(apm2000_handle_t *handle, uint16_t *pm2p5_ug_m3)
read pm2.5
struct apm2000_info_s apm2000_info_t
apm2000 information structure definition
uint8_t apm2000_set_interface(apm2000_handle_t *handle, apm2000_interface_t interface)
set the chip interface
uint8_t apm2000_stop_measurement(apm2000_handle_t *handle)
stop the measurement
struct apm2000_handle_s apm2000_handle_t
apm2000 handle structure definition
uint8_t apm2000_init(apm2000_handle_t *handle)
initialize the chip
apm2000_interface_t
apm2000 interface enumeration definition
uint8_t apm2000_start_measurement(apm2000_handle_t *handle)
start the measurement
uint8_t apm2000_read(apm2000_handle_t *handle, apm2000_pm_t *pm)
read the result
uint8_t apm2000_info(apm2000_info_t *info)
get chip information
@ APM2000_INTERFACE_IIC
@ APM2000_INTERFACE_UART
uint8_t apm2000_interface_iic_read_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus read with 16 bits register address
void apm2000_interface_debug_print(const char *const fmt,...)
interface print format data
uint16_t apm2000_interface_uart_read(uint8_t *buf, uint16_t len)
interface uart read
uint8_t apm2000_interface_iic_deinit(void)
interface iic bus deinit
uint8_t apm2000_interface_uart_write(uint8_t *buf, uint16_t len)
interface uart write
uint8_t apm2000_interface_uart_flush(void)
interface uart flush
uint8_t apm2000_interface_iic_init(void)
interface iic bus init
uint8_t apm2000_interface_uart_deinit(void)
interface uart deinit
uint8_t apm2000_interface_iic_write_address16(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
interface iic bus write with 16 bits register address
void apm2000_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t apm2000_interface_uart_init(void)
interface uart init
uint8_t apm2000_read_test(apm2000_interface_t interface, uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]
uint16_t pm10_ug_m3
uint16_t pm2p5_ug_m3
uint16_t pm1p0_ug_m3