-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.py
38 lines (30 loc) · 836 Bytes
/
switch.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
import logging
import RPi.GPIO as GPIO
class Switch(object):
"""A Switch class"""
def __init__(self, pin, name, status=0):
# pin number
self.pin = pin
# component name
self.name = name
# status (bool)
self.status = 0
logging.debug('Switch created - pin: %s, name: %s, status: %s', pin, name, status)
# Setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, 0)
self.set_status(status)
# todo: this should be private
def set_status(self, status):
self.status = status
GPIO.output(self.pin, status)
logging.debug('switch %s - set_status %s', self.name, self.get_status())
def get_status(self):
# convert to boolean
return not (not self.status)
def on(self):
"""Turn on"""
self.set_status(1)
def off(self):
"""Turn off"""
self.set_status(0)