Skip to content

Commit dc1495c

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 some boilerplate code.
1 parent 4747184 commit dc1495c

File tree

5 files changed

+14
-50
lines changed

5 files changed

+14
-50
lines changed

examples/UPS/UPS.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ void setup() {
3737
pinMode(COMMLOSTPIN, OUTPUT); // output is on once communication is lost with the host, otherwise off.
3838

3939

40-
PowerDevice.setFeature(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
40+
PowerDevice.SetFeature(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
4141

42-
PowerDevice.setFeature(HID_PD_CAPACITYMODE, &bCapacityMode, sizeof(bCapacityMode));
43-
PowerDevice.setFeature(HID_PD_VOLTAGE, &iVoltage, sizeof(iVoltage));
42+
PowerDevice.SetFeature(HID_PD_CAPACITYMODE, &bCapacityMode, sizeof(bCapacityMode));
43+
PowerDevice.SetFeature(HID_PD_VOLTAGE, &iVoltage, sizeof(iVoltage));
4444

45-
PowerDevice.setFeature(HID_PD_DESIGNCAPACITY, &iDesignCapacity, sizeof(iDesignCapacity));
46-
PowerDevice.setFeature(HID_PD_FULLCHRGECAPACITY, &iFullChargeCapacity, sizeof(iFullChargeCapacity));
47-
PowerDevice.setFeature(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
45+
PowerDevice.SetFeature(HID_PD_DESIGNCAPACITY, &iDesignCapacity, sizeof(iDesignCapacity));
46+
PowerDevice.SetFeature(HID_PD_FULLCHRGECAPACITY, &iFullChargeCapacity, sizeof(iFullChargeCapacity));
47+
PowerDevice.SetFeature(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
4848
}
4949

5050
void loop() {
@@ -86,8 +86,8 @@ void loop() {
8686

8787
if((iPresentStatus != iPreviousStatus) || (iRemaining != iPrevRemaining) || (iIntTimer>MINUPDATEINTERVAL) ) {
8888

89-
PowerDevice.sendReport(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
90-
iRes = PowerDevice.sendReport(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
89+
PowerDevice.SendReport(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining));
90+
iRes = PowerDevice.SendReport(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus));
9191

9292
if(iRes <0 ) {
9393
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
@@ -147,11 +147,6 @@ class HID_ : public PluggableUSBModule
147147

148148
};
149149

150-
// Replacement for global singleton.
151-
// This function prevents static-initialization-order-fiasco
152-
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
153-
HID_& HID();
154-
155150
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0x21, 1, 0x22, lowByte(length), highByte(length) }
156151

157152
#endif // USBCON

src/HIDPowerDevice.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,42 +94,25 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
9494
HIDPowerDevice_::HIDPowerDevice_(void) {
9595
static HIDSubDescriptor node(_hidReportDescriptor, sizeof (_hidReportDescriptor));
9696

97-
HID().AppendDescriptor(&node);
98-
}
99-
100-
void HIDPowerDevice_::setOutput(Serial_& out) {
101-
HID().setOutput(out);
102-
}
103-
104-
void HIDPowerDevice_::setSerial(const char* s) {
105-
HID().setSerial(s);
97+
AppendDescriptor(&node);
10698
}
10799

108100
int HIDPowerDevice_::sendDate(uint16_t id, uint16_t year, uint8_t month, uint8_t day) {
109101
uint16_t bval = (year - 1980)*512 + month * 32 + day;
110-
return HID().SendReport(id, &bval, sizeof (bval));
111-
}
112-
113-
int HIDPowerDevice_::sendReport(uint16_t id, const void* bval, int len) {
114-
return HID().SendReport(id, bval, len);
115-
}
116-
117-
int HIDPowerDevice_::setFeature(uint16_t id, const void *data, int len) {
118-
return HID().SetFeature(id, data, len);
102+
return SendReport(id, &bval, sizeof (bval));
119103
}
120104

121105
int HIDPowerDevice_::setStringFeature(uint8_t id, const uint8_t* index, const char* data) {
122106

123-
int res = HID().SetFeature(id, index, 1);
107+
int res = SetFeature(id, index, 1);
124108

125109
if(res == 0) return 0;
126110

127-
res += HID().SetFeature(0xFF00 | *index , data, strlen_P(data));
111+
res += SetFeature(0xFF00 | *index , data, strlen_P(data));
128112

129113
return res;
130114
}
131115

132116
HIDPowerDevice_ PowerDevice;
133117

134118
#endif
135-

src/HIDPowerDevice.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,13 @@ static_assert(sizeof(PresentStatus) == sizeof(uint8_t));
6262

6363

6464

65-
class HIDPowerDevice_ {
65+
class HIDPowerDevice_ : public HID_ {
6666
public:
6767
HIDPowerDevice_(void);
68-
69-
void setOutput(Serial_&);
70-
71-
void setSerial(const char*);
72-
68+
7369
int sendDate(uint16_t id, uint16_t year, uint8_t month, uint8_t day);
74-
int sendReport(uint16_t id, const void* bval, int len);
75-
76-
int setFeature(uint16_t id, const void* data, int len);
7770

7871
int setStringFeature(uint8_t id, const uint8_t* index, const char* data);
79-
8072
};
8173

8274
extern HIDPowerDevice_ PowerDevice;

0 commit comments

Comments
 (0)