rename PZEM enums

This commit is contained in:
King Kévin 2016-09-12 21:42:37 +02:00
parent 353dfe295f
commit 79d76ee0fd
3 changed files with 16 additions and 16 deletions

View File

@ -79,7 +79,7 @@ void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurem
if (tx_i!=0) { // transmission is ongoing
return;
}
if (type>=MAX) { // invalid type
if (type>=SENSOR_PZEM_MAX) { // invalid type
return;
}
tx_buffer[0] = 0xB0+type; // set request nibble and type nibble
@ -107,7 +107,7 @@ struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
if ((rx_buffer[0]&0xf0)!=0xa0) { // not a response received
return measurement;
}
if ((rx_buffer[0]&0x0f)>=MAX) { // not a valid response type received (actually 4 and 5 are valid, but should not happen when using this code
if ((rx_buffer[0]&0x0f)>=SENSOR_PZEM_MAX) { // not a valid response type received (actually 4 and 5 are valid, but should not happen when using this code
return measurement;
}
uint8_t checksum = 0; // calculate checksum (sum of all other bytes)
@ -120,16 +120,16 @@ struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
measurement.valid = true; // all checks passed
measurement.type = rx_buffer[0]&0x0f; // save type
switch (measurement.type) { // decode value depending on type
case VOLTAGE:
case SENSOR_PZEM_VOLTAGE:
measurement.value.voltage = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2]+rx_buffer[3]*0.1;
break;
case CURRENT:
case SENSOR_PZEM_CURRENT:
measurement.value.current = rx_buffer[2]+rx_buffer[3]/100;
break;
case POWER:
case SENSOR_PZEM_POWER:
measurement.value.power = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2];
break;
case ENERGY:
case SENSOR_PZEM_ENERGY:
measurement.value.power = ((uint32_t)rx_buffer[1]<<16)+((uint16_t)rx_buffer[2]<<8)+rx_buffer[3];
break;
default:

View File

@ -25,11 +25,11 @@ extern volatile bool sensor_pzem_measurement_received;
/** measurements offered by electricity meter */
enum sensor_pzem_measurement_type_t {
VOLTAGE = 0,
CURRENT = 1,
POWER = 2,
ENERGY = 3,
MAX
SENSOR_PZEM_VOLTAGE = 0,
SENSOR_PZEM_CURRENT = 1,
SENSOR_PZEM_POWER = 2,
SENSOR_PZEM_ENERGY = 3,
SENSOR_PZEM_MAX
};
/** measurement returned by electricity meter */

10
main.c
View File

@ -170,7 +170,7 @@ void main(void)
sensor_pzem_setup(); // setup PZEM electricity meter
printf("OK\n");
sensor_pzem_measurement_request(0xc0a80101, VOLTAGE);
sensor_pzem_measurement_request(0xc0a80101, SENSOR_PZEM_VOLTAGE);
// main loop
printf("command input: ready\n");
@ -212,16 +212,16 @@ void main(void)
struct sensor_pzem_measurement_t measurement = sensor_pzem_measurement_decode(); // decode measurement
if (measurement.valid) { // only show valid measurement
switch (measurement.type) {
case VOLTAGE:
case SENSOR_PZEM_VOLTAGE:
printf("voltage: %.01f V\n", measurement.value.voltage);
break;
case CURRENT:
case SENSOR_PZEM_CURRENT:
printf("current: %.02f A\n", measurement.value.current);
break;
case POWER:
case SENSOR_PZEM_POWER:
printf("power: %.00f W\n", measurement.value.power);
break;
case ENERGY:
case SENSOR_PZEM_ENERGY:
printf("energy: %lu Wh\n", measurement.value.energy);
break;
default: