Skip to content

Commit 1cf240b

Browse files
committed
Change HIDPowerDevice_ to inherit from HID_ instead of aggregating it
Preparation to better support multi-battery setups where a global HID singleton no longer makes sense. This also simplifies the HIDPowerDevice_ class by removing wrapper methods that just forwards the call to the underlying HID_ class.
1 parent cef7b0d commit 1cf240b

File tree

5 files changed

+32
-68
lines changed

5 files changed

+32
-68
lines changed

examples/UPS/UPS.ino

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,36 @@ void setup() {
6565
pinMode(COMMLOSTPIN, OUTPUT); // output is on once communication is lost with the host, otherwise off.
6666

6767

68-
PowerDevice.setFeature(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
68+
PowerDevice.SetFeature(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
6969

70-
PowerDevice.setFeature(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty));
71-
PowerDevice.setFeature(HID_PD_AVERAGETIME2FULL, &iAvgTimeToFull, sizeof(iAvgTimeToFull));
72-
PowerDevice.setFeature(HID_PD_AVERAGETIME2EMPTY, &iAvgTimeToEmpty, sizeof(iAvgTimeToEmpty));
73-
PowerDevice.setFeature(HID_PD_REMAINTIMELIMIT, &iRemainTimeLimit, sizeof(iRemainTimeLimit));
74-
PowerDevice.setFeature(HID_PD_DELAYBE4REBOOT, &iDelayBe4Reboot, sizeof(iDelayBe4Reboot));
75-
PowerDevice.setFeature(HID_PD_DELAYBE4SHUTDOWN, &iDelayBe4ShutDown, sizeof(iDelayBe4ShutDown));
70+
PowerDevice.SetFeature(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty));
71+
PowerDevice.SetFeature(HID_PD_AVERAGETIME2FULL, &iAvgTimeToFull, sizeof(iAvgTimeToFull));
72+
PowerDevice.SetFeature(HID_PD_AVERAGETIME2EMPTY, &iAvgTimeToEmpty, sizeof(iAvgTimeToEmpty));
73+
PowerDevice.SetFeature(HID_PD_REMAINTIMELIMIT, &iRemainTimeLimit, sizeof(iRemainTimeLimit));
74+
PowerDevice.SetFeature(HID_PD_DELAYBE4REBOOT, &iDelayBe4Reboot, sizeof(iDelayBe4Reboot));
75+
PowerDevice.SetFeature(HID_PD_DELAYBE4SHUTDOWN, &iDelayBe4ShutDown, sizeof(iDelayBe4ShutDown));
7676

77-
PowerDevice.setFeature(HID_PD_RECHARGEABLE, &bRechargable, sizeof(bRechargable));
78-
PowerDevice.setFeature(HID_PD_CAPACITYMODE, &bCapacityMode, sizeof(bCapacityMode));
79-
PowerDevice.setFeature(HID_PD_CONFIGVOLTAGE, &iConfigVoltage, sizeof(iConfigVoltage));
80-
PowerDevice.setFeature(HID_PD_VOLTAGE, &iVoltage, sizeof(iVoltage));
77+
PowerDevice.SetFeature(HID_PD_RECHARGEABLE, &bRechargable, sizeof(bRechargable));
78+
PowerDevice.SetFeature(HID_PD_CAPACITYMODE, &bCapacityMode, sizeof(bCapacityMode));
79+
PowerDevice.SetFeature(HID_PD_CONFIGVOLTAGE, &iConfigVoltage, sizeof(iConfigVoltage));
80+
PowerDevice.SetFeature(HID_PD_VOLTAGE, &iVoltage, sizeof(iVoltage));
8181

8282
PowerDevice.setStringFeature(HID_PD_IDEVICECHEMISTRY, &bDeviceChemistry, STRING_DEVICECHEMISTRY);
8383
PowerDevice.setStringFeature(HID_PD_IOEMINFORMATION, &bOEMVendor, STRING_OEMVENDOR);
8484

85-
PowerDevice.setFeature(HID_PD_AUDIBLEALARMCTRL, &iAudibleAlarmCtrl, sizeof(iAudibleAlarmCtrl));
85+
PowerDevice.SetFeature(HID_PD_AUDIBLEALARMCTRL, &iAudibleAlarmCtrl, sizeof(iAudibleAlarmCtrl));
8686

87-
PowerDevice.setFeature(HID_PD_DESIGNCAPACITY, &iDesignCapacity, sizeof(iDesignCapacity));
88-
PowerDevice.setFeature(HID_PD_FULLCHRGECAPACITY, &iFullChargeCapacity, sizeof(iFullChargeCapacity));
89-
PowerDevice.setFeature(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
90-
PowerDevice.setFeature(HID_PD_WARNCAPACITYLIMIT, &iWarnCapacityLimit, sizeof(iWarnCapacityLimit));
91-
PowerDevice.setFeature(HID_PD_REMNCAPACITYLIMIT, &iRemnCapacityLimit, sizeof(iRemnCapacityLimit));
92-
PowerDevice.setFeature(HID_PD_CPCTYGRANULARITY1, &bCapacityGranularity1, sizeof(bCapacityGranularity1));
93-
PowerDevice.setFeature(HID_PD_CPCTYGRANULARITY2, &bCapacityGranularity2, sizeof(bCapacityGranularity2));
87+
PowerDevice.SetFeature(HID_PD_DESIGNCAPACITY, &iDesignCapacity, sizeof(iDesignCapacity));
88+
PowerDevice.SetFeature(HID_PD_FULLCHRGECAPACITY, &iFullChargeCapacity, sizeof(iFullChargeCapacity));
89+
PowerDevice.SetFeature(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
90+
PowerDevice.SetFeature(HID_PD_WARNCAPACITYLIMIT, &iWarnCapacityLimit, sizeof(iWarnCapacityLimit));
91+
PowerDevice.SetFeature(HID_PD_REMNCAPACITYLIMIT, &iRemnCapacityLimit, sizeof(iRemnCapacityLimit));
92+
PowerDevice.SetFeature(HID_PD_CPCTYGRANULARITY1, &bCapacityGranularity1, sizeof(bCapacityGranularity1));
93+
PowerDevice.SetFeature(HID_PD_CPCTYGRANULARITY2, &bCapacityGranularity2, sizeof(bCapacityGranularity2));
9494

9595
uint16_t year = 2024, month = 10, day = 12;
9696
iManufacturerDate = (year - 1980)*512 + month*32 + day; // from 4.2.6 Battery Settings in "Universal Serial Bus Usage Tables for HID Power Devices"
97-
PowerDevice.setFeature(HID_PD_MANUFACTUREDATE, &iManufacturerDate, sizeof(iManufacturerDate));
97+
PowerDevice.SetFeature(HID_PD_MANUFACTUREDATE, &iManufacturerDate, sizeof(iManufacturerDate));
9898
}
9999

100100
void loop() {
@@ -166,9 +166,9 @@ void loop() {
166166

167167
if((iPresentStatus != iPreviousStatus) || (iRemaining != iPrevRemaining) || (iRunTimeToEmpty != iPrevRunTimeToEmpty) || (iIntTimer>MINUPDATEINTERVAL) ) {
168168

169-
PowerDevice.sendReport(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
170-
if(bDischarging) PowerDevice.sendReport(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty));
171-
iRes = PowerDevice.sendReport(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
169+
PowerDevice.SendReport(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
170+
if(bDischarging) PowerDevice.SendReport(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty));
171+
iRes = PowerDevice.SendReport(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
172172

173173
if(iRes <0 ) {
174174
digitalWrite(COMMLOSTPIN, HIGH);

src/HID/HID.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121

2222
#if defined(USBCON)
2323

24-
HID_& HID()
25-
{
26-
static HID_ obj;
27-
return obj;
28-
}
29-
3024
int HID_::getInterface(uint8_t* interfaceCount)
3125
{
3226
*interfaceCount += 1; // uses 1

src/HID/HID.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ class HID_ : public PluggableUSBModule
157157

158158
};
159159

160-
// Replacement for global singleton.
161-
// This function prevents static-initialization-order-fiasco
162-
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
163-
HID_& HID();
164-
165160
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0x21, 1, 0x22, lowByte(length), highByte(length) }
166161

167162
#endif // USBCON

src/HIDPowerDevice.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,51 +224,35 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
224224
HIDPowerDevice_::HIDPowerDevice_(void) {
225225
static HIDSubDescriptor node(_hidReportDescriptor, sizeof (_hidReportDescriptor));
226226

227-
HID().AppendDescriptor(&node);
227+
AppendDescriptor(&node);
228228
}
229229

230230
void HIDPowerDevice_::begin(void) {
231-
HID().begin();
231+
HID_::begin();
232232

233233
// set string ID here
234234

235-
HID().SetFeature(HID_PD_IPRODUCT, &bProduct, sizeof(bProduct));
236-
HID().SetFeature(HID_PD_SERIAL, &bSerial, sizeof(bSerial));
237-
HID().SetFeature(HID_PD_MANUFACTURER, &bManufacturer, sizeof(bManufacturer));
235+
SetFeature(HID_PD_IPRODUCT, &bProduct, sizeof(bProduct));
236+
SetFeature(HID_PD_SERIAL, &bSerial, sizeof(bSerial));
237+
SetFeature(HID_PD_MANUFACTURER, &bManufacturer, sizeof(bManufacturer));
238238

239239
}
240240

241-
void HIDPowerDevice_::setOutput(Serial_& out) {
242-
HID().setOutput(out);
243-
}
244-
245-
void HIDPowerDevice_::setSerial(const char* s) {
246-
HID().setSerial(s);
247-
}
248-
249241
void HIDPowerDevice_::end(void) {
250242
}
251243

252244
int HIDPowerDevice_::sendDate(uint16_t id, uint16_t year, uint8_t month, uint8_t day) {
253245
uint16_t bval = (year - 1980)*512 + month * 32 + day;
254-
return HID().SendReport(id, &bval, sizeof (bval));
255-
}
256-
257-
int HIDPowerDevice_::sendReport(uint16_t id, const void* bval, int len) {
258-
return HID().SendReport(id, bval, len);
259-
}
260-
261-
int HIDPowerDevice_::setFeature(uint16_t id, const void *data, int len) {
262-
return HID().SetFeature(id, data, len);
246+
return SendReport(id, &bval, sizeof (bval));
263247
}
264248

265249
int HIDPowerDevice_::setStringFeature(uint8_t id, const uint8_t* index, const char* data) {
266250

267-
int res = HID().SetFeature(id, index, 1);
251+
int res = SetFeature(id, index, 1);
268252

269253
if(res == 0) return 0;
270254

271-
res += HID().SetFeature(0xFF00 | *index , data, strlen_P(data));
255+
res += SetFeature(0xFF00 | *index , data, strlen_P(data));
272256

273257
return res;
274258
}

src/HIDPowerDevice.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static_assert(sizeof(PresentStatus) == sizeof(uint16_t));
102102

103103

104104

105-
class HIDPowerDevice_ {
105+
class HIDPowerDevice_ : public HID_ {
106106

107107
private:
108108

@@ -114,20 +114,11 @@ class HIDPowerDevice_ {
114114
HIDPowerDevice_(void);
115115
void begin(void);
116116

117-
void setOutput(Serial_&);
118-
119-
void setSerial(const char*);
120-
121-
122117
void end(void);
123118

124119
int sendDate(uint16_t id, uint16_t year, uint8_t month, uint8_t day);
125-
int sendReport(uint16_t id, const void* bval, int len);
126-
127-
int setFeature(uint16_t id, const void* data, int len);
128120

129121
int setStringFeature(uint8_t id, const uint8_t* index, const char* data);
130-
131122
};
132123

133124
extern HIDPowerDevice_ PowerDevice;

0 commit comments

Comments
 (0)