3 from .common import OnoffDevice, ST_ACTIVE
5 def _write_file_value(path, content):
6 with open(path, "w") as f:
9 class OnoffGPIO(OnoffDevice):
11 def __init__(self, gpionumber, active_low=False):
14 @param gpionumber: a Linux GPIO number. That includes a controller
16 @type active_low: bool
17 @param active_low: whether the GPIO should be inverted
19 OnoffDevice.__init__(self)
20 self.current_state = 0
21 basedir = "/sys/class/gpio"
22 self.gpiodir = "%s/gpio%d" % (basedir, gpionumber)
23 if not os.path.isdir(self.gpiodir):
24 _write_file_value("%s/export" % basedir, "%d" % gpionumber)
25 _write_file_value("%s/direction" % self.gpiodir, "out")
26 _write_file_value("%s/active_low" % self.gpiodir, "%d" % active_low)
28 def _set_state(self, state):
29 self.current_state = state
30 _write_file_value("%s/value" % self.gpiodir,
31 "1" if self.current_state == ST_ACTIVE else "0")
32 self.changestate(self.current_state)
36 return self.current_state
39 self._set_state(ST_ACTIVE)