-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeed_relay_v1.py
131 lines (115 loc) · 4.85 KB
/
seeed_relay_v1.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
# =========================================================
# Seeed Studio Raspberry Pi Relay Board Library
#
# by John M. Wargo (www.johnwargo.com)
#
# Modified from the sample code on the Seeed Studio Wiki
# http://wiki.seeed.cc/Raspberry_Pi_Relay_Board_v1.0/
# =========================================================
import smbus
# This value should never change since Seeed only makes 4 port boards,
# but I made it a constructor option anyway
# The number of relay ports on the relay board.
# NUM_RELAY_PORTS = 4
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
class Relay():
global bus
def __init__(self, device_address=0x20, num_relays=4, action_output=False):
print('Initializing relay board at {}'.format(device_address))
if action_output:
print('Enabling action_output mode')
self.DEVICE_ADDRESS = device_address
self.NUM_RELAY_PORTS = num_relays
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)
self.debug = action_output
def on(self, relay_num):
if isinstance(relay_num, int):
# do we have a valid relay number?
if 0 < relay_num <= self.NUM_RELAY_PORTS:
if self.debug:
print('Turning relay {} on'.format(relay_num))
self.DEVICE_REG_DATA &= ~(0x1 << (relay_num - 1))
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
else:
print('Invalid relay: #{}'.format(relay_num))
else:
print('Relay number must be an Integer value')
def off(self, relay_num):
if isinstance(relay_num, int):
# do we have a valid relay number?
if 0 < relay_num <= self.NUM_RELAY_PORTS:
if self.debug:
print('Turning relay {} off'.format(relay_num))
self.DEVICE_REG_DATA |= (0x1 << (relay_num - 1))
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
else:
print('Invalid relay: #{}'.format(relay_num))
else:
print('Relay number must be an Integer value')
def all_on(self):
if self.debug:
print('Turning all relays ON')
self.DEVICE_REG_DATA &= ~(0xf << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def all_off(self):
if self.debug:
print('Turning all relays OFF')
self.DEVICE_REG_DATA |= (0xf << 0)
bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, self.DEVICE_REG_DATA)
def toggle_port(self, relay_num):
if self.debug:
print('Toggling relay:', relay_num)
if self.get_port_status(relay_num):
# it's on, so turn it off
self.off(relay_num)
else:
# it's off, so turn it on
self.on(relay_num)
def get_port_status(self, relay_num):
# determines whether the specified port is ON/OFF
if self.debug:
print('Checking status of relay {}'.format(relay_num))
res = self.get_port_data(relay_num)
if res > 0:
mask = 1 << (relay_num - 1)
# return the specified bit status
# return (DEVICE_REG_DATA & mask) != 0
return (self.DEVICE_REG_DATA & mask) == 0
else:
# otherwise (invalid port), always return False
print("Relay port ({}) is invalid".format(relay_num))
return False
def get_port_data(self, relay_num):
# gets the current byte value stored in the relay board
if self.debug:
print('Reading relay status value for relay {}'.format(relay_num))
# do we have a valid port?
if 0 < relay_num <= self.NUM_RELAY_PORTS:
# read the memory location
self.DEVICE_REG_DATA = bus.read_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1)
# return the specified bit status
return self.DEVICE_REG_DATA
else:
# otherwise (invalid port), always return 0
print("Specified relay port is invalid")
return 0
def print_status_all(self):
output = "| "
for x in range(1, self.NUM_RELAY_PORTS+1):
status = self.get_port_status(x)
output += str(x)
if status:
output += ': On | '
else:
output += ': Off | '
print('Relay status: {}'.format(output))
def print_status(self, relay_num):
output = str(relay_num)
status = self.get_port_status(relay_num)
if status:
output += ': On '
else:
output += ': Off '
print('Relay {}'.format(output))