Page 176
from gpiozero import LED
from time import sleep
class TriLED():
def __init__(self, pin1=None, pin2=None, pin3=None):
self.LEDs = (LED(pin1), LED(pin2), LED(pin3))
def AllOn(self):
self.LEDs[0].on()
self.LEDs[1].on()
self.LEDs[2].on()
def AllOff(self):
self.LEDs[0].off()
self.LEDs[1].off()
self.LEDs[2].off()
Page 177
class TriLED(CompositeDevice):
def __init__(self, pin1=None, pin2=None, pin3=None):
super(TriLED, self).__init__(LED(pin1), LED(pin2), LED(pin3))
def AllOn(self):
self[0].on()
self[1].on()
self[2].on()
def AllOff(self):
self[0].off()
self[1].off()
self[2].off()
Page 192
from gpiozero import DigitalOutputDevice, CompositeDevice
class StepperBi4(CompositeDevice):
def __init__(self, A=None, Am=None, B=None, Bm=None):
if not all(p is not None for p in [A, Am, B, Bm]):
raise GPIOPinMissing(
'Four GPIO pins must be provided'
)
super(StepperUni4, self).__init__(DigitalOutputDevice(A), DigitalOutputDevice(
B), DigitalOutputDevice(Am), DigitalOutputDevice(Bm))
self.phase = 0
self.halfstepSeq = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]
]
def setPhase(self, phase):
self.phase = phase
for gpio, state in zip(self, self.halfstepSeq[phase]):
gpio.pin._set_state(state)
def stepForward(self):
self.phase = (self.phase+1) % 8
self.setPhase(self.phase)
def stepReverse(self):
self.phase = (self.phase-1) % 8
self.setPhase(self.phase)
Page 194
from gpiozero import DigitalOutputDevice, CompositeDevice
from time import sleep
from gpiozero.threads import GPIOThread
class StepperBi4(CompositeDevice):
def __init__(self, A=None, Am=None, B=None, Bm=None):
if not all(p is not None for p in [A, Am, B, Bm]):
raise GPIOPinMissing('Four GPIO pins must be provided')
super(StepperUni4, self).__init__(DigitalOutputDevice(A), DigitalOutputDevice(
B), DigitalOutputDevice(Am), DigitalOutputDevice(Bm))
self.phase = 0
self._rotate_thread = None
self.halfstepSeq = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]
]
def setPhase(self, phase):
self.phase = phase
for gpio, state in zip(self, self.halfstepSeq[phase]):
gpio.pin._set_state(state)
def stepForward(self):
self.phase = (self.phase+1) % 8
self.setPhase(self.phase)
def stepReverse(self):
self.phase = (self.phase-1) % 8
self.setPhase(self.phase)
def forward(self, speed=0):
self._stop_rotate()
if speed == 0:
return
self._rotate_thread = GPIOThread(
target=self._rotate, args=(+1, speed))
self._rotate_thread.start()
def reverse(self, speed=1):
self._stop_rotate()
if speed == 0:
return
self._rotate_thread = GPIOThread(
target=self._rotate, args=(-1, speed))
self._rotate_thread.start()
def _rotate(self, dir, speed):
delay = 60/(speed*400)-0.001
while True:
if dir == 1:
self.stepForward()
if dir == -1:
self.stepReverse()
if self._rotate_thread.stopping.wait(delay):
break
def _stop_rotate(self):
if getattr(self, '_rotate_thread', None):
self._rotate_thread.stop()
self._rotate_thread = None