-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpowerpack.py
133 lines (104 loc) · 3.23 KB
/
powerpack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from smbus import SMBus
import curses
import time
def toS16(val):
if val > 32767:
val -= 65536
return val
# Configure charger cell voltage and current
def setCharger(bus, volt, curr):
bus.write_word_data(0x09, 0x14, curr)
bus.write_word_data(0x09, 0x15, volt)
bus.write_word_data(0x09, 0x12, 0x9912)
def configureINA219(bus):
bus.write_word_data(0x40, 0x05, 0x0010) # Calibration
bus.write_word_data(0x40, 0x00, 0xD739) # Power on
def getBatteryStatus(bus):
try:
batt_volt = (bus.read_word_data(0x0b, 0x09)) / 1000.0
batt_curr = toS16(bus.read_word_data(0x0b, 0x0a)) / 1000.0
batt_pcent = bus.read_word_data(0x0b, 0x0d)
except IOError:
return 0, 0, 0
return batt_volt, batt_curr, batt_pcent
def getINA219Status(bus):
try:
volt1 = bus.read_word_data(0x40, 0x02)
curr1 = bus.read_word_data(0x40, 0x01)
except IOError:
return 0, 0
# Endianness swap and multiply with scaling factor
volt = ((volt1 >> 8) | ((volt1 & 0xFF) << 8)) * 0.0005
curr = toS16((curr1 >> 8) | ((curr1 & 0xFF) << 8)) * 0.0001
return (volt, curr)
def main(window):
batt_det = 1
chrg_det = 1
chg_conn = 0
ina_det = 1
counter = 0
# Initialize bus
bus = SMBus(1)
window.addstr(0, 0, 'Smarter Power Pack v1.0 Trial');
try:
batt_volt, batt_curr, percent = getBatteryStatus(bus)
window.addstr(2, 2, 'Battery detected')
except IOError,err:
window.addstr(2, 2, 'Battery not detected!')
batt_det = 0
try:
chrg_volt = bus.read_word_data(0x09, 0x15)
window.addstr(3, 2, 'Charger detected')
except IOError,err:
window.addstr(3, 2, 'Charger not detected!')
chrg_det = 0
try:
configureINA219(bus)
window.addstr(4, 2, 'INA219 detected')
except IOError,err:
window.addstr(4, 2, 'INA219 not detected!')
ina_det = 0
window.addstr(6, 0, 'Battery Status')
window.addstr(10, 0, 'INA219 Status')
while (batt_det | chrg_det | ina_det) != 0:
try:
batt_volt, batt_curr, percent = getBatteryStatus(bus)
ina_volt, ina_curr = getINA219Status(bus)
window.move(3, 0)
if chrg_det == 1:
window.clrtoeol()
chg_stat = bus.read_word_data(0x09, 0x12)
if (chg_stat & 0x0010 == 0x0010):
window.addstr(3, 2, 'Charger connected')
if chg_conn == 0 & bus.read_word_data(0x09, 0x14) == 0:
setCharger(bus, 12600, 4000)
chg_conn = 1
else:
window.addstr(3, 2, 'Charger disconnected')
if chg_conn == 1:
setCharger(bus, 0, 0)
chg_conn = 0
window.move(8, 0)
window.clrtoeol()
counter = counter + 1
timeto = 0
if counter >= 10:
counter = 0
# Ask battery about time to empty / time to full
# curr = toS16(chrg_curr = bus.read_word_data(0x0B, 0x0a))
# Clear pending errors
window.move(14, 0)
window.clrtoeol()
window.addstr(8, 2, '{0:.3f} V ({2:d}% charged), {1:.3f} A'.format(batt_volt, batt_curr, percent))
window.move(12, 0)
if ((ina_volt != 0) | (ina_curr != 0)):
window.clrtoeol()
window.addstr(12, 2, '{0:.3f} V, {1:.3f} A'.format(ina_volt, ina_curr))
window.refresh()
time.sleep(1)
except RuntimeError, err:
# Display error message
window.addstr(14, 1, 'Error' * err)
pass
if __name__ == "__main__":
curses.wrapper(main)