|
1 | 1 | #include <HIDPowerDevice.h>
|
2 | 2 |
|
| 3 | +#define MINUPDATEINTERVAL 26 |
| 4 | + |
| 5 | +int iIntTimer=0; |
| 6 | + |
| 7 | + |
| 8 | +// String constants |
| 9 | +const char STRING_DEVICECHEMISTRY[] PROGMEM = "PbAc"; |
| 10 | +const char STRING_OEMVENDOR[] PROGMEM = "MyCoolUPS"; |
| 11 | +const char STRING_SERIAL[] PROGMEM = "UPS10"; |
| 12 | + |
| 13 | +const byte bDeviceChemistry = IDEVICECHEMISTRY; |
| 14 | +const byte bOEMVendor = IOEMVENDOR; |
| 15 | + |
| 16 | +uint16_t iPresentStatus = 0, iPreviousStatus = 0; |
| 17 | + |
| 18 | +byte bRechargable = 1; |
| 19 | +byte bCapacityMode = 2; // units are in %% |
| 20 | + |
| 21 | +// Physical parameters |
| 22 | +const uint16_t iConfigVoltage = 1380; |
| 23 | +uint16_t iVoltage =1300, iPrevVoltage = 0; |
| 24 | +uint16_t iRunTimeToEmpty = 0, iPrevRunTimeToEmpty = 0; |
| 25 | +uint16_t iAvgTimeToFull = 7200; |
| 26 | +uint16_t iAvgTimeToEmpty = 7200; |
| 27 | +uint16_t iRemainTimeLimit = 600; |
| 28 | +int16_t iDelayBe4Reboot = -1; |
| 29 | +int16_t iDelayBe4ShutDown = -1; |
| 30 | + |
| 31 | +byte iAudibleAlarmCtrl = 2; // 1 - Disabled, 2 - Enabled, 3 - Muted |
| 32 | + |
| 33 | + |
| 34 | +// Parameters for ACPI compliancy |
| 35 | +const byte iDesignCapacity = 100; |
| 36 | +byte iWarnCapacityLimit = 10; // warning at 10% |
| 37 | +byte iRemnCapacityLimit = 5; // low at 5% |
| 38 | +const byte bCapacityGranularity1 = 1; |
| 39 | +const byte bCapacityGranularity2 = 1; |
| 40 | +byte iFullChargeCapacity = 100; |
| 41 | + |
| 42 | +byte iRemaining =0, iPrevRemaining=0; |
| 43 | + |
| 44 | +int iRes=0; |
| 45 | + |
| 46 | + |
3 | 47 | void setup() {
|
4 |
| - // put your setup code here, to run once: |
| 48 | + |
| 49 | + Serial.begin(57600); |
| 50 | + |
5 | 51 | PowerDevice.begin();
|
6 |
| - pinMode(4, INPUT_PULLUP); |
| 52 | + |
| 53 | + // Serial No is set in a special way as it forms Arduino port name |
| 54 | + PowerDevice.setSerial(STRING_SERIAL); |
| 55 | + |
| 56 | + // Used for debugging purposes. |
| 57 | + PowerDevice.setOutput(Serial); |
| 58 | + |
| 59 | + pinMode(4, INPUT_PULLUP); // ground this pin to simulate power failure. |
| 60 | + pinMode(5, OUTPUT); // output flushing 1 sec indicating that the arduino cycle is running. |
| 61 | + pinMode(10, OUTPUT); // output is on once commuication is lost with the host, otherwise off. |
7 | 62 |
|
8 |
| - Serial.begin(9600); |
| 63 | + |
| 64 | + PowerDevice.setFeature(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus)); |
| 65 | + |
| 66 | + PowerDevice.setFeature(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty)); |
| 67 | + PowerDevice.setFeature(HID_PD_AVERAGETIME2FULL, &iAvgTimeToFull, sizeof(iAvgTimeToFull)); |
| 68 | + PowerDevice.setFeature(HID_PD_AVERAGETIME2EMPTY, &iAvgTimeToEmpty, sizeof(iAvgTimeToEmpty)); |
| 69 | + PowerDevice.setFeature(HID_PD_REMAINTIMELIMIT, &iRemainTimeLimit, sizeof(iRemainTimeLimit)); |
| 70 | + PowerDevice.setFeature(HID_PD_DELAYBE4REBOOT, &iDelayBe4Reboot, sizeof(iDelayBe4Reboot)); |
| 71 | + PowerDevice.setFeature(HID_PD_DELAYBE4SHUTDOWN, &iDelayBe4ShutDown, sizeof(iDelayBe4ShutDown)); |
9 | 72 |
|
| 73 | + PowerDevice.setFeature(HID_PD_RECHARGEABLE, &bRechargable, sizeof(bRechargable)); |
| 74 | + PowerDevice.setFeature(HID_PD_CAPACITYMODE, &bCapacityMode, sizeof(bCapacityMode)); |
| 75 | + PowerDevice.setFeature(HID_PD_CONFIGVOLTAGE, &iConfigVoltage, sizeof(iConfigVoltage)); |
| 76 | + PowerDevice.setFeature(HID_PD_VOLTAGE, &iVoltage, sizeof(iVoltage)); |
| 77 | + |
| 78 | + PowerDevice.setStringFeature(HID_PD_IDEVICECHEMISTRY, &bDeviceChemistry, STRING_DEVICECHEMISTRY); |
| 79 | + PowerDevice.setStringFeature(HID_PD_IOEMINFORMATION, &bOEMVendor, STRING_OEMVENDOR); |
| 80 | + |
| 81 | + PowerDevice.setFeature(HID_PD_AUDIBLEALARMCTRL, &iAudibleAlarmCtrl, sizeof(iAudibleAlarmCtrl)); |
| 82 | + |
| 83 | + PowerDevice.setFeature(HID_PD_DESIGNCAPACITY, &iDesignCapacity, sizeof(iDesignCapacity)); |
| 84 | + PowerDevice.setFeature(HID_PD_FULLCHRGECAPACITY, &iFullChargeCapacity, sizeof(iFullChargeCapacity)); |
| 85 | + PowerDevice.setFeature(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining)); |
| 86 | + PowerDevice.setFeature(HID_PD_WARNCAPACITYLIMIT, &iWarnCapacityLimit, sizeof(iWarnCapacityLimit)); |
| 87 | + PowerDevice.setFeature(HID_PD_REMNCAPACITYLIMIT, &iRemnCapacityLimit, sizeof(iRemnCapacityLimit)); |
| 88 | + PowerDevice.setFeature(HID_PD_CPCTYGRANULARITY1, &bCapacityGranularity1, sizeof(bCapacityGranularity1)); |
| 89 | + PowerDevice.setFeature(HID_PD_CPCTYGRANULARITY2, &bCapacityGranularity2, sizeof(bCapacityGranularity2)); |
| 90 | + |
10 | 91 | }
|
11 | 92 |
|
12 | 93 | void loop() {
|
13 |
| - //put your main code here, to run repeatedly: |
| 94 | + |
| 95 | + |
| 96 | + //*********** Measurements Unit **************************** |
| 97 | + bool bCharging = digitalRead(4); |
| 98 | + bool bACPresent = bCharging; // TODO - replace with sensor |
| 99 | + bool bDischarging = !bCharging; // TODO - replace with sensor |
| 100 | + int iA7 = analogRead(A7); // TODO - this is for debug only. Replace with charge estimation |
14 | 101 |
|
15 |
| - delay(2000); |
| 102 | + iRemaining = (byte)(round((float)100*iA7/1024)); |
| 103 | + iRunTimeToEmpty = (uint16_t)round((float)iAvgTimeToEmpty*iRemaining/100); |
16 | 104 |
|
17 |
| - uint8_t raw[USB_EP_SIZE]={0}; |
18 |
| - // Serial.println(USB_Available(HID_RX)); |
19 |
| - if(Serial.available()) { |
| 105 | + // Charging |
| 106 | + if(bCharging) |
| 107 | + bitSet(iPresentStatus,PRESENTSTATUS_CHARGING); |
| 108 | + else |
| 109 | + bitClear(iPresentStatus,PRESENTSTATUS_CHARGING); |
| 110 | + if(bACPresent) |
| 111 | + bitSet(iPresentStatus,PRESENTSTATUS_ACPRESENT); |
| 112 | + else |
| 113 | + bitClear(iPresentStatus,PRESENTSTATUS_ACPRESENT); |
| 114 | + if(iRemaining == iFullChargeCapacity) |
| 115 | + bitSet(iPresentStatus,PRESENTSTATUS_FULLCHARGE); |
| 116 | + else |
| 117 | + bitClear(iPresentStatus,PRESENTSTATUS_FULLCHARGE); |
20 | 118 |
|
21 |
| - int incomingByte = Serial.read(); |
22 |
| - Serial.println(incomingByte, DEC); |
23 |
| - } |
| 119 | + // Discharging |
| 120 | + if(bDischarging) { |
| 121 | + bitSet(iPresentStatus,PRESENTSTATUS_DISCHARGING); |
| 122 | + // if(iRemaining < iRemnCapacityLimit) bitSet(iPresentStatus,PRESENTSTATUS_BELOWRCL); |
24 | 123 |
|
25 |
| - if(USB_Available(HID_RX)) { |
26 |
| - Serial.print("USB_Available\t"); Serial.println(USB_Available(HID_RX)); |
| 124 | + if(iRunTimeToEmpty < iRemainTimeLimit) |
| 125 | + bitSet(iPresentStatus, PRESENTSTATUS_RTLEXPIRED); |
| 126 | + else |
| 127 | + bitClear(iPresentStatus, PRESENTSTATUS_RTLEXPIRED); |
| 128 | + |
| 129 | + } |
| 130 | + else { |
| 131 | + bitClear(iPresentStatus,PRESENTSTATUS_DISCHARGING); |
| 132 | + bitClear(iPresentStatus, PRESENTSTATUS_RTLEXPIRED); |
27 | 133 | }
|
28 | 134 |
|
29 |
| - // return; |
30 |
| - |
31 |
| - PowerDevice.sendByte(HID_PD_IPRODUCT, IPRODUCT); |
32 |
| - PowerDevice.sendByte(HID_PD_MANUFACTURER, IMANUFACTURER); |
33 |
| - PowerDevice.sendByte(HID_PD_SERIAL, ISERIAL); |
| 135 | + // Shutdown requested |
| 136 | + if(iDelayBe4ShutDown > 0 ) { |
| 137 | + bitSet(iPresentStatus, PRESENTSTATUS_SHUTDOWNREQ); |
| 138 | + Serial.println("shutdown requested"); |
| 139 | + } |
| 140 | + else |
| 141 | + bitClear(iPresentStatus, PRESENTSTATUS_SHUTDOWNREQ); |
| 142 | + |
| 143 | + // Shutdown imminent |
| 144 | + if((iPresentStatus & (1 << PRESENTSTATUS_SHUTDOWNREQ)) || |
| 145 | + (iPresentStatus & (1 << PRESENTSTATUS_RTLEXPIRED))) { |
| 146 | + bitSet(iPresentStatus, PRESENTSTATUS_SHUTDOWNIMNT); |
| 147 | + Serial.println("shutdown imminent"); |
| 148 | + } |
| 149 | + else |
| 150 | + bitClear(iPresentStatus, PRESENTSTATUS_SHUTDOWNIMNT); |
| 151 | + |
| 152 | + |
34 | 153 |
|
35 |
| - PowerDevice.sendByte(HID_PD_RECHARGEABLE, 1); // should be 1 (Rechargable). Equivalent to "Battery Technology" in ACPI. |
36 |
| - PowerDevice.sendByte(HID_PD_CAPACITYMODE, 0); |
37 |
| - PowerDevice.sendInt32(HID_PD_FULLCHRGECAPACITY, 43200); //12 Ah (in Asec) |
38 |
| - PowerDevice.sendInt32(HID_PD_DESIGNCAPACITY, 43200); //12 Ah (in Asec) |
39 |
| - PowerDevice.sendInt16(HID_PD_CONFIGVOLTAGE, 13800); //13.8V |
40 |
| - PowerDevice.sendInt32(HID_PD_REMNCAPACITYLIMIT, 2160); //5% is set to threshold |
41 |
| - PowerDevice.sendByte(HID_PD_CPCTYGRANULARITY1, 100); |
42 |
| - PowerDevice.sendByte(HID_PD_CPCTYGRANULARITY2, 50); |
43 |
| - |
44 |
| - byte bRemaining = 75; |
| 154 | + bitSet(iPresentStatus ,PRESENTSTATUS_BATTPRESENT); |
| 155 | + |
45 | 156 |
|
46 |
| - PowerDevice.sendByte(HID_PD_REMAININGCAPACITY, bRemaining); |
47 | 157 |
|
48 |
| - int iPresentStatus = 0; |
| 158 | + //************ Delay **************************************** |
| 159 | + delay(1000); |
| 160 | + iIntTimer++; |
| 161 | + digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level); |
| 162 | + delay(1000); |
| 163 | + iIntTimer++; |
| 164 | + digitalWrite(5, LOW); // turn the LED off; |
| 165 | + |
| 166 | + //************ Check if we are still online ****************** |
| 167 | + |
49 | 168 |
|
50 |
| - // Charging |
51 |
| - bool bCharging = digitalRead(4); |
52 |
| - if(bCharging) { |
53 |
| - bitSet(iPresentStatus,0); |
54 |
| - // Fully Charged |
55 |
| - if(bRemaining == 100) bitSet(iPresentStatus, 12); |
56 | 169 |
|
57 |
| - } |
58 |
| - // Dischargig |
59 |
| - else { |
60 |
| - bitSet(iPresentStatus,1); |
61 |
| - // Fully Discharged |
62 |
| - if(bRemaining = 1) bitSet(iPresentStatus, 13); |
63 |
| - } |
| 170 | + //************ Bulk send or interrupt *********************** |
64 | 171 |
|
65 |
| - // Need Replacement |
66 |
| - bitSet(iPresentStatus, 9); |
67 |
| - // Overload |
68 |
| - bitSet(iPresentStatus, 10); |
| 172 | + if((iPresentStatus != iPreviousStatus) || (iRemaining != iPrevRemaining) || (iRunTimeToEmpty != iPrevRunTimeToEmpty) || (iIntTimer>MINUPDATEINTERVAL) ) { |
69 | 173 |
|
| 174 | + PowerDevice.sendReport(HID_PD_REMAININGCAPACITY, &iRemaining, sizeof(iRemaining)); |
| 175 | + if(bDischarging) PowerDevice.sendReport(HID_PD_RUNTIMETOEMPTY, &iRunTimeToEmpty, sizeof(iRunTimeToEmpty)); |
| 176 | + iRes = PowerDevice.sendReport(HID_PD_PRESENTSTATUS, &iPresentStatus, sizeof(iPresentStatus)); |
70 | 177 |
|
71 |
| - PowerDevice.sendInt16(HID_PD_PRESENTSTATUS, iPresentStatus); |
72 |
| - if(bCharging) { |
73 |
| - PowerDevice.sendInt16(HID_PD_CURRENT, 500); |
74 |
| - } |
75 |
| - else { |
76 |
| - PowerDevice.sendInt16(HID_PD_CURRENT, -500); |
77 |
| - PowerDevice.sendInt16(HID_PD_RUNTIMETOEMPTY, 3600); |
| 178 | + if(iRes <0 ) { |
| 179 | + digitalWrite(10, HIGH); |
| 180 | + } |
| 181 | + else |
| 182 | + digitalWrite(10, LOW); |
| 183 | + |
| 184 | + iIntTimer = 0; |
| 185 | + iPreviousStatus = iPresentStatus; |
| 186 | + iPrevRemaining = iRemaining; |
| 187 | + iPrevRunTimeToEmpty = iRunTimeToEmpty; |
78 | 188 | }
|
79 |
| - |
80 |
| - PowerDevice.sendInt16(HID_PD_VOLTAGE, 12000); |
| 189 | + |
| 190 | + |
| 191 | + Serial.println(iRemaining); |
| 192 | + Serial.println(iRunTimeToEmpty); |
| 193 | + Serial.println(iRes); |
81 | 194 |
|
82 | 195 | }
|
0 commit comments