-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeed_relay_test.py
109 lines (85 loc) · 3.29 KB
/
seeed_relay_test.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
# from Seeed Studio Wiki
# http://wiki.seeed.cc/Raspberry_Pi_Relay_Board_v1.0/
# This Seeed's test app, it does NOT use the library. Use this program to
# test that the relay board is working when you're having trouble with the
# library
import signal
import sys
import smbus
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
class Relay:
global bus
def __init__(self):
self.DEVICE_ADDRESS = 0x20 # 7 bit address (will be left shifted to add the read write bit)
self.DEVICE_REG_MODE1 = 0x06
self.DEVICE_REG_DATA = 0xff
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ON_1(self):
print('ON_1...')
self.DEVICE_REG_DATA &= ~(0x1 << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ON_2(self):
print('ON_2...')
self.DEVICE_REG_DATA &= ~(0x1 << 1)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ON_3(self):
print('ON_3...')
self.DEVICE_REG_DATA &= ~(0x1 << 2)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ON_4(self):
print('ON_4...')
self.DEVICE_REG_DATA &= ~(0x1 << 3)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def OFF_1(self):
print('OFF_1...')
self.DEVICE_REG_DATA |= (0x1 << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def OFF_2(self):
print('OFF_2...')
self.DEVICE_REG_DATA |= (0x1 << 1)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def OFF_3(self):
print('OFF_3...')
self.DEVICE_REG_DATA |= (0x1 << 2)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def OFF_4(self):
print('OFF_4...')
self.DEVICE_REG_DATA |= (0x1 << 3)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ALLON(self):
print('ALL ON...')
self.DEVICE_REG_DATA &= ~(0xf << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def ALLOFF(self):
print('ALL OFF...')
self.DEVICE_REG_DATA |= (0xf << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
if __name__ == "__main__":
relay = Relay()
# Called on process interruption. Set all pins to "Input" default mode.
def endProcess(signalnum=None, handler=None):
relay.ALLOFF()
sys.exit()
signal.signal(signal.SIGINT, endProcess)
while True:
ct = raw_input("input: ")
if ct == '1on':
relay.ON_1()
elif ct == '2on':
relay.ON_2()
elif ct == '3on':
relay.ON_3()
elif ct == '4on':
relay.ON_4()
elif ct == '1off':
relay.OFF_1()
elif ct == '2off':
relay.OFF_2()
elif ct == '3off':
relay.OFF_3()
elif ct == '4off':
relay.OFF_4()
elif ct == 'allon':
relay.ALLON()
elif ct == 'alloff':
relay.ALLOFF()