-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte2c.py
69 lines (52 loc) · 1.78 KB
/
byte2c.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
import smbus
import time
bus = smbus.SMBus(1)
class byte2c:
def __init__(self):
self.addr = 0x24
self.regwrite = 0x14
self.iodir = 0x00
self.clock = 0
self.outbit = 0
self.outbyte = 0
self.onbit = 0
self.bus = smbus.SMBus(1)
#self.bus.write_byte_data(self.addr,0x05,0x00)
#self.bus.write_byte_data(self.addr,0x0a,0x10)
self.bus.write_byte_data(self.addr,self.iodir,0x00)
self.bus.write_byte_data(self.addr,self.iodir+1,0x00)
self.bus.write_byte_data(self.addr,self.regwrite,0x00)
self.bus.write_byte_data(self.addr,self.regwrite+1,0x00)
def set_out_bit(self):
if(self.onbit>=0):
self.bus.write_byte_data(self.addr,self.regwrite,1<<self.onbit)
else:
self.bus.write_byte_data(self.addr,self.regwrite,0)
def clock_up(self):
if(self.clock == 1):
return
self.clock = 1
self.onbit -= 1
self.write_control()
def clock_down(self):
if(self.clock == 0):
return
self.clock = 0
self.write_control()
if(self.onbit == 0):
self.onbit = -1
self.set_out_bit()
def set_write_byte(self,byte):
self.outbyte = byte
self.onbit = 8
def write_control(self):
if(self.onbit>=0):
self.outbit = (self.outbyte >> self.onbit) & 1
else:
self.outbit = 1
self.bus.write_byte_data(self.addr,self.regwrite+1,self.outbit*128 + self.clock*64)
self.set_out_bit()
def reset(self):
self.bus.write_byte_data(self.addr,self.regwrite,0x00)
self.bus.write_byte_data(self.addr,self.regwrite+1,0x00)
self.outbit = -1