LibDriver HLW8032
Loading...
Searching...
No Matches
driver_hlw8032_read_test.c
Go to the documentation of this file.
1
36
38
39static hlw8032_handle_t gs_handle;
40
50uint8_t hlw8032_read_test(hlw8032_mode_t mode, uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 hlw8032_info_t info;
55
56 /* link interface function */
64
65 /* get hlw8032 information */
66 res = hlw8032_info(&info);
67 if (res != 0)
68 {
69 hlw8032_interface_debug_print("hlw8032: get info failed.\n");
70
71 return 1;
72 }
73 else
74 {
75 /* print chip information */
76 hlw8032_interface_debug_print("hlw8032: chip is %s.\n", info.chip_name);
77 hlw8032_interface_debug_print("hlw8032: manufacturer is %s.\n", info.manufacturer_name);
78 hlw8032_interface_debug_print("hlw8032: interface is %s.\n", info.interface);
79 hlw8032_interface_debug_print("hlw8032: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
80 hlw8032_interface_debug_print("hlw8032: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
81 hlw8032_interface_debug_print("hlw8032: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
82 hlw8032_interface_debug_print("hlw8032: max current is %0.2fmA.\n", info.max_current_ma);
83 hlw8032_interface_debug_print("hlw8032: max temperature is %0.1fC.\n", info.temperature_max);
84 hlw8032_interface_debug_print("hlw8032: min temperature is %0.1fC.\n", info.temperature_min);
85 }
86
87 /* hlw8032 init */
88 res = hlw8032_init(&gs_handle);
89 if (res != 0)
90 {
91 hlw8032_interface_debug_print("hlw8032: init failed.\n");
92
93 return 1;
94 }
95
96 /* set mode */
97 res = hlw8032_set_mode(&gs_handle, mode);
98 if (res != 0)
99 {
100 hlw8032_interface_debug_print("hlw8032: set mode failed.\n");
101 (void)hlw8032_deinit(&gs_handle);
102
103 return 1;
104 }
105
106 /* start read test */
107 hlw8032_interface_debug_print("hlw8032: start read test.\n");
108
109 for (i = 0; i < times; i++)
110 {
111 int64_t quantity_electricity_counter;
112 hlw8032_data_t data;
113
114 /* delay 2000ms */
116
117 /* read */
118 res = hlw8032_read(&gs_handle, &data);
119 if (res != 0)
120 {
121 hlw8032_interface_debug_print("hlw8032: read failed.\n");
122 (void)hlw8032_deinit(&gs_handle);
123
124 return 1;
125 }
126
127 /* quantity electricity export */
128 res = hlw8032_quantity_electricity_export(&gs_handle, &quantity_electricity_counter);
129 if (res != 0)
130 {
131 hlw8032_interface_debug_print("hlw8032: quantity electricity export failed.\n");
132 (void)hlw8032_deinit(&gs_handle);
133
134 return 1;
135 }
136
137 /* output */
138 hlw8032_interface_debug_print("hlw8032: voltage parameter raw is 0x%08X.\n", data.voltage_parameter_raw);
139 hlw8032_interface_debug_print("hlw8032: voltage raw is 0x%08X.\n", data.voltage_raw);
140 hlw8032_interface_debug_print("hlw8032: current parameter raw is 0x%08X.\n", data.current_parameter_raw);
141 hlw8032_interface_debug_print("hlw8032: current raw is 0x%08X.\n", data.current_raw);
142 hlw8032_interface_debug_print("hlw8032: power parameter raw is 0x%08X.\n", data.power_parameter_raw);
143 hlw8032_interface_debug_print("hlw8032: power raw is 0x%08X.\n", data.power_raw);
144 hlw8032_interface_debug_print("hlw8032: update status is 0x%02X.\n", data.update_status);
145 hlw8032_interface_debug_print("hlw8032: status is 0x%02X.\n", data.status);
146 hlw8032_interface_debug_print("hlw8032: pf counter is %d.\n", data.pf_counter);
147 hlw8032_interface_debug_print("hlw8032: effective voltage is %0.2fV.\n", data.effective_voltage_v);
148 hlw8032_interface_debug_print("hlw8032: effective current is %0.2fA.\n", data.effective_current_a);
149 hlw8032_interface_debug_print("hlw8032: active power is %0.2fW.\n", data.active_power_w);
150 hlw8032_interface_debug_print("hlw8032: apparent power is %0.2fW.\n", data.apparent_power_w);
151 hlw8032_interface_debug_print("hlw8032: power factor is %0.2f.\n", data.power_factor);
152 hlw8032_interface_debug_print("hlw8032: quantity electricity is %0.2fKW·h.\n", data.quantity_electricity_kwh);
153 hlw8032_interface_debug_print("hlw8032: quantity electricity counter is %lld.\n", quantity_electricity_counter);
154 }
155
156 /* finish read test */
157 hlw8032_interface_debug_print("hlw8032: finish read test.\n");
158 (void)hlw8032_deinit(&gs_handle);
159
160 return 0;
161}
driver hlw8032 read test header file
uint8_t hlw8032_read(hlw8032_handle_t *handle, hlw8032_data_t *data)
read the data
uint8_t hlw8032_deinit(hlw8032_handle_t *handle)
close the chip
uint8_t hlw8032_init(hlw8032_handle_t *handle)
initialize the chip
uint8_t hlw8032_set_mode(hlw8032_handle_t *handle, hlw8032_mode_t mode)
set mode
struct hlw8032_info_s hlw8032_info_t
hlw8032 information structure definition
uint8_t hlw8032_info(hlw8032_info_t *info)
get chip's information
uint8_t hlw8032_quantity_electricity_export(hlw8032_handle_t *handle, int64_t *quantity_electricity_counter)
quantity electricity export
struct hlw8032_handle_s hlw8032_handle_t
hlw8032 handle structure definition
hlw8032_mode_t
hlw8032 mode enumeration definition
struct hlw8032_data_s hlw8032_data_t
hlw8032 data structure definition
void hlw8032_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t hlw8032_interface_uart_flush(void)
interface uart flush
void hlw8032_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t hlw8032_interface_uart_init(void)
interface uart init
uint8_t hlw8032_interface_uart_deinit(void)
interface uart deinit
uint16_t hlw8032_interface_uart_read(uint8_t *buf, uint16_t len)
interface uart read
uint8_t hlw8032_read_test(hlw8032_mode_t mode, uint32_t times)
read test
float quantity_electricity_kwh
uint32_t voltage_parameter_raw
uint32_t current_parameter_raw
uint32_t power_parameter_raw
uint32_t driver_version
char manufacturer_name[32]