Rev Author Line No. Line
4261 kakl 1 #!/usr/bin/python
2  
3 # Python example of use pymlab with LIONCELL MLAB module
4  
5 import time
6 import sys
7 from pymlab import config
8  
9 import pandas as pd # data parsing library
10 import struct
11  
12 filename = './guage.csv'
13  
14 df = pd.read_csv(filename,delimiter=',')
15  
16 def ReadFlash(ID, offset, name, typ, units, default):
17 flash = guage.ReadFlashBlock(ID, offset // 32)
18 print ID, '\t', offset, '\t', '"', '{:32s}'.format(name), '"', '\t', typ, '\t',
19 # if (offset // 32) > 0 :
20 offset = offset - 32 * (offset // 32)
21 if typ == 'U1':
22 print '{:10d}'.format(flash[offset]),
23 if typ == 'I1':
24 if flash[offset] > 127:
25 ble = (256-flash[offset]) * (-1)
26 else:
27 ble = flash[offset]
28 print '{:10d}'.format(ble),
29 if typ == 'I2':
30 ble = (flash[offset]<<8)+flash[offset+1]
31 if ble > 32767:
32 ble = (65536-ble) * (-1)
33 print '{:10d}'.format(ble),
34 if typ == 'U2':
35 print '{:10d}'.format(abs((flash[offset]<<8)+flash[offset+1])),
36 if typ == 'F4':
37 print '{:10s}'.format(hex(0x1000000*flash[offset]+0x10000*flash[(offset+1)]+0x100*flash[(offset+2)]+flash[(offset+3)])),
38 #print '{:10f}'.format(struct.unpack('@f', chr(flash[offset])+chr(flash[offset+1])+chr(flash[offset+2])+chr(flash[offset+3]))[0]),
39 if typ == 'H1':
40 print '{:10s}'.format(hex(flash[(offset)])),
41 if typ == 'H2':
42 print '{:10s}'.format(hex(0x100*flash[(offset)]+flash[(offset+1)])),
43 if typ == 'H4':
44 print '{:10s}'.format(hex(0x1000000*flash[offset]+0x10000*flash[(offset+1)]+0x100*flash[(offset+2)]+flash[(offset+3)])),
45 if typ == 'S9':
46 string = ''
47 for n in range(9):
48 string = string + chr(flash[offset+n+1])
49 print '{:12s}'.format(string+'\0'),
50 if typ == 'S12':
51 string = ''
52 for n in range(9):
53 string = string + chr(flash[offset+n+1])
54 print '{:12s}'.format(string+'...\0'),
55 if typ == 'S5':
56 string = ''
57 for n in range(5):
58 string = string + chr(flash[offset+n+1])
59 print '{:12s}'.format(string+'\0'),
60 print '{:10s}'.format('['+units+']'), '\t', default, '{:10s}'.format('['+units+']')
61  
62  
63 #### Sensor Configuration ###########################################
64 cfg = config.Config(
65 i2c = {
66 "port": 0, # I2C bus number
67 },
68  
69 bus = [
70 {
71 "type": "i2chub",
72 "address": 0x73,
73  
74 "children": [
75 {"name": "guage", "type": "lioncell", "channel": 7, },
76 ],
77 },
78 ],
79 )
80  
81  
82 cfg.initialize()
83 guage = cfg.get_device("guage")
84  
85 #flash = guage.ReadFlashBlock(48, 2)
86 #print " ".join([hex(i) for i in flash])
87  
88 #print hex(guage.PackConfiguration())
89  
90 #guage.WriteFlashByte(48, 0, 21, 0x0D) # Design Capacity 3350 mAh
91 #guage.WriteFlashByte(48, 0, 22, 0x16) #
92 #guage.WriteFlashByte(48, 0, 23, 0x5E) # Design Energy 24120 mWh
93 #guage.WriteFlashByte(48, 0, 24, 0x38) #
94 #guage.WriteFlashByte(64, 0, 0, 0x9) # External Voltage Measurement
95 #guage.WriteFlashByte(64, 0, 7, 0x2) # Two Cells
96 #guage.WriteFlashByte(64, 0, 4, 0x74) # 8 LED (1+7), Shift Register
97 #guage.WriteFlashByte(104, 0, 14, 0x28) # Voltage Measurement Range 10240 mV
98 #guage.WriteFlashByte(104, 0, 15, 0x00) #
99 #guage.reset() # Reset Guage
100  
101 #flash = guage.ReadFlashBlock(112, 0)
102 #print "112 - ",
103 #print " ".join([hex(i) for i in flash])
104  
105  
106 #ReadFlash(2, 0, 'OT Chg', 'I2', '0.1 degC')
107 #ReadFlash(112, 8, 'Authen Key3', 'H4', '-')
108  
109 for i in range(len(df)):
110 ReadFlash(df['SubclassID'][i], df['Offset'][i], df['Name'][i], df['DataType'][i], df['Units'][i], df['DefaultValue'][i])
111  
112  
113 for i in range(4):
114 # Battery status readout
115 print "NominalAvailableCapacity =", guage.NominalAvailableCapacity(), "mAh, FullAvailabeCapacity =", guage.FullAvailabeCapacity(), "mAh, AvailableEnergy =", guage.AvailableEnergy(), "* 10 mWh"
116 print "Temp =", guage.getTemp(), "degC, RemainCapacity =", guage.getRemainingCapacity(), "mAh, cap =", guage.FullChargeCapacity(), "mAh, U =", guage.Voltage(), "mV, I =", guage.AverageCurrent(), "mA, charge =", guage.StateOfCharge(), "%"
117 time.sleep(3)
118  
119