@@ -43,34 +43,8 @@ bool CSE7766Component::check_byte_() {
43
43
uint8_t index = this ->raw_data_index_ ;
44
44
uint8_t byte = this ->raw_data_ [index ];
45
45
if (index == 0 ) {
46
- // Header - 0x55
47
- // These messages are verbose because the CSE7766
48
- // reports these when no load is attached. If the checksum doesn't match
49
- // though, then we should print a warning.
50
- if (byte == 0xAA ) {
51
- ESP_LOGV (TAG, " CSE7766 not calibrated!" );
52
- return false ;
53
- }
54
- if ((byte & 0xF0 ) == 0xF0 ) {
55
- ESP_LOGV (TAG, " CSE7766 reports abnormal hardware: (0x%02X)" , byte);
56
- if ((byte >> 3 ) & 1 ) {
57
- ESP_LOGV (TAG, " Voltage cycle exceeds range." );
58
- }
59
- if ((byte >> 2 ) & 1 ) {
60
- ESP_LOGV (TAG, " Current cycle exceeds range." );
61
- }
62
- if ((byte >> 1 ) & 1 ) {
63
- ESP_LOGV (TAG, " Power cycle exceeds range." );
64
- }
65
- if ((byte >> 0 ) & 1 ) {
66
- ESP_LOGV (TAG, " Coefficient storage area is abnormal." );
67
- }
68
- return false ;
69
- }
70
- if (byte != 0x55 ) {
71
- ESP_LOGV (TAG, " Invalid Header Start from CSE7766: 0x%02X!" , byte);
72
- return false ;
73
- }
46
+ // Header, usually 0x55, contains data about calibration etc.
47
+ // this is validated in parse_data_
74
48
return true ;
75
49
}
76
50
@@ -103,6 +77,18 @@ void CSE7766Component::parse_data_() {
103
77
ESP_LOGVV (TAG, " i=%u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)" , i, BYTE_TO_BINARY (this ->raw_data_ [i]),
104
78
this ->raw_data_ [i]);
105
79
}
80
+
81
+ uint8_t header1 = this ->raw_data_ [0 ];
82
+ if (header1 == 0xAA ) {
83
+ ESP_LOGW (TAG, " CSE7766 not calibrated!" );
84
+ return ;
85
+ }
86
+ if ((header1 & 0xF0 ) == 0xF0 && ((header1 >> 0 ) & 1 ) == 1 ) {
87
+ ESP_LOGW (TAG, " CSE7766 reports abnormal hardware: (0x%02X)" , header1);
88
+ ESP_LOGW (TAG, " Coefficient storage area is abnormal." );
89
+ return ;
90
+ }
91
+
106
92
const uint32_t now = micros ();
107
93
const float d = (now - this ->last_reading_ ) / 1000 .0f ;
108
94
this ->last_reading_ = now;
@@ -116,19 +102,25 @@ void CSE7766Component::parse_data_() {
116
102
117
103
uint8_t adj = this ->raw_data_ [20 ];
118
104
119
- if ((adj >> 6 != 0 ) && voltage_cycle != 0 ) {
105
+ if ((adj >> 6 != 0 ) && voltage_cycle != 0 &&
106
+ // voltage cycle exceeds range
107
+ ((header1 >> 3 ) & 1 ) == 0 ) {
120
108
// voltage cycle of serial port outputted is a complete cycle;
121
109
float voltage = voltage_calib / float (voltage_cycle);
122
110
this ->voltage_acc_ += voltage * d;
123
111
}
124
112
125
- if ((adj >> 5 != 0 ) && power_cycle != 0 && current_cycle != 0 ) {
113
+ if ((adj >> 5 != 0 ) && power_cycle != 0 && current_cycle != 0 &&
114
+ // current cycle exceeds range
115
+ ((header1 >> 2 ) & 1 ) == 0 ) {
126
116
// indicates current cycle of serial port outputted is a complete cycle;
127
117
float current = current_calib / float (current_cycle);
128
118
this ->current_acc_ += current * d;
129
119
}
130
120
131
- if ((adj >> 4 != 0 ) && power_cycle != 0 ) {
121
+ if ((adj >> 4 != 0 ) && power_cycle != 0 &&
122
+ // power cycle exceeds range
123
+ ((header1 >> 1 ) & 1 ) == 0 ) {
132
124
// power cycle of serial port outputted is a complete cycle;
133
125
float active_power = power_calib / float (power_cycle);
134
126
this ->power_acc_ += active_power * d;
0 commit comments