Skip to content

Commit 563febd

Browse files
authored
Merge pull request #24 from forderud/bit-field
Simplify PresentStatus handling with C++ bit-fields
2 parents ff5a41d + b90c690 commit 563febd

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

examples/UPS/UPS.ino

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const char STRING_SERIAL[] PROGMEM = "UPS10";
1717
const byte bDeviceChemistry = IDEVICECHEMISTRY;
1818
const byte bOEMVendor = IOEMVENDOR;
1919

20-
uint16_t iPresentStatus = 0, iPreviousStatus = 0;
20+
PresentStatus iPresentStatus = {}, iPreviousStatus = {};
2121

2222
byte bRechargable = 1;
2323
byte bCapacityMode = 2; // units are in %%
@@ -109,56 +109,44 @@ void loop() {
109109
iRemaining = (byte)(round((float)100*iBattSoc/1024));
110110
iRunTimeToEmpty = (uint16_t)round((float)iAvgTimeToEmpty*iRemaining/100);
111111

112-
// Charging
113-
if(bCharging)
114-
bitSet(iPresentStatus,PRESENTSTATUS_CHARGING);
115-
else
116-
bitClear(iPresentStatus,PRESENTSTATUS_CHARGING);
117-
if(bACPresent)
118-
bitSet(iPresentStatus,PRESENTSTATUS_ACPRESENT);
119-
else
120-
bitClear(iPresentStatus,PRESENTSTATUS_ACPRESENT);
121-
if(iRemaining == iFullChargeCapacity)
122-
bitSet(iPresentStatus,PRESENTSTATUS_FULLCHARGE);
123-
else
124-
bitClear(iPresentStatus,PRESENTSTATUS_FULLCHARGE);
112+
// Charging
113+
iPresentStatus.CHARGING = bCharging;
114+
iPresentStatus.ACPRESENT = bACPresent;
115+
iPresentStatus.FULLCHARGE = (iRemaining == iFullChargeCapacity);
125116

126117
// Discharging
127118
if(bDischarging) {
128-
bitSet(iPresentStatus,PRESENTSTATUS_DISCHARGING);
129-
// if(iRemaining < iRemnCapacityLimit) bitSet(iPresentStatus,PRESENTSTATUS_BELOWRCL);
119+
iPresentStatus.DISCHARGING = 1;
120+
// if(iRemaining < iRemnCapacityLimit) iPresentStatus.BELOWRCL = 1;
130121

131-
if(iRunTimeToEmpty < iRemainTimeLimit)
132-
bitSet(iPresentStatus, PRESENTSTATUS_RTLEXPIRED);
133-
else
134-
bitClear(iPresentStatus, PRESENTSTATUS_RTLEXPIRED);
122+
iPresentStatus.RTLEXPIRED = (iRunTimeToEmpty < iRemainTimeLimit);
135123

136124
}
137125
else {
138-
bitClear(iPresentStatus,PRESENTSTATUS_DISCHARGING);
139-
bitClear(iPresentStatus, PRESENTSTATUS_RTLEXPIRED);
126+
iPresentStatus.DISCHARGING = 0;
127+
iPresentStatus.RTLEXPIRED = 0;
140128
}
141129

142130
// Shutdown requested
143131
if(iDelayBe4ShutDown > 0 ) {
144-
bitSet(iPresentStatus, PRESENTSTATUS_SHUTDOWNREQ);
132+
iPresentStatus.SHUTDOWNREQ = 1;
145133
Serial.println("shutdown requested");
146134
}
147135
else
148-
bitClear(iPresentStatus, PRESENTSTATUS_SHUTDOWNREQ);
136+
iPresentStatus.SHUTDOWNREQ = 0;
149137

150138
// Shutdown imminent
151-
if((iPresentStatus & (1 << PRESENTSTATUS_SHUTDOWNREQ)) ||
152-
(iPresentStatus & (1 << PRESENTSTATUS_RTLEXPIRED))) {
153-
bitSet(iPresentStatus, PRESENTSTATUS_SHUTDOWNIMNT);
139+
if((iPresentStatus.SHUTDOWNREQ) ||
140+
(iPresentStatus.RTLEXPIRED)) {
141+
iPresentStatus.SHUTDOWNIMNT = 1;
154142
Serial.println("shutdown imminent");
155143
}
156144
else
157-
bitClear(iPresentStatus, PRESENTSTATUS_SHUTDOWNIMNT);
145+
iPresentStatus.SHUTDOWNIMNT = 0;
158146

159147

160148

161-
bitSet(iPresentStatus ,PRESENTSTATUS_BATTPRESENT);
149+
iPresentStatus.BATTPRESENT = 1;
162150

163151

164152

src/HIDPowerDevice.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,31 @@
7272
#define HID_PD_IOEMINFORMATION 0x20 // Feature
7373

7474

75-
// PresenStatus dynamic flags
76-
#define PRESENTSTATUS_CHARGING 0x00
77-
#define PRESENTSTATUS_DISCHARGING 0x01
78-
#define PRESENTSTATUS_ACPRESENT 0x02
79-
#define PRESENTSTATUS_BATTPRESENT 0x03
80-
#define PRESENTSTATUS_BELOWRCL 0x04
81-
#define PRESENTSTATUS_RTLEXPIRED 0x05
82-
#define PRESENTSTATUS_NEEDREPLACE 0x06
83-
#define PRESENTSTATUS_VOLTAGENR 0x07
84-
#define PRESENTSTATUS_FULLCHARGE 0x08
85-
#define PRESENTSTATUS_FULLDISCHARGE 0x09
86-
#define PRESENTSTATUS_SHUTDOWNREQ 0x0A
87-
#define PRESENTSTATUS_SHUTDOWNIMNT 0x0B
88-
#define PRESENTSTATUS_COMMLOST 0x0C
89-
#define PRESENTSTATUS_OVERLOAD 0x0D
75+
// PresentStatus dynamic flags
76+
struct PresentStatus {
77+
uint8_t CHARGING : 1; // bit 0x00
78+
uint8_t DISCHARGING : 1; // bit 0x01
79+
uint8_t ACPRESENT : 1; // bit 0x02
80+
uint8_t BATTPRESENT : 1; // bit 0x03
81+
uint8_t BELOWRCL : 1; // bit 0x04
82+
uint8_t RTLEXPIRED : 1; // bit 0x05
83+
uint8_t NEEDREPLACE : 1; // bit 0x06
84+
uint8_t VOLTAGENR : 1; // bit 0x07
85+
86+
uint8_t FULLCHARGE : 1; // bit 0x08
87+
uint8_t FULLDISCHARGE : 1;// bit 0x09
88+
uint8_t SHUTDOWNREQ : 1; // bit 0x0A
89+
uint8_t SHUTDOWNIMNT : 1; // bit 0x0B
90+
uint8_t COMMLOST : 1; // bit 0x0C
91+
uint8_t OVERLOAD : 1; // bit 0x0D
92+
uint8_t unused1 : 1;
93+
uint8_t unused2 : 1;
94+
95+
operator uint16_t () {
96+
return *(uint16_t*)(this); // switch to std::bit_cast after migrating to C++20
97+
}
98+
};
99+
static_assert(sizeof(PresentStatus) == sizeof(uint16_t));
90100

91101

92102

0 commit comments

Comments
 (0)