Add more advanced and remote recipes

This commit is contained in:
Ben Nuttall
2017-07-17 02:03:29 +01:00
parent 957bb788ea
commit 220da280ba
16 changed files with 290 additions and 11 deletions

View File

@@ -0,0 +1,11 @@
from bluedot import BlueDot
from gpiozero import LED
bd = BlueDot()
led = LED(17)
while True:
bd.wait_for_press()
led.on()
bd.wait_for_release()
led.off()

View File

@@ -0,0 +1,22 @@
from bluedot import BlueDot
from gpiozero import Robot
from signal import pause
bd = BlueDot()
robot = Robot(left=(4, 14), right=(17, 18))
def move(pos):
if pos.top:
robot.forward(pos.distance)
elif pos.bottom:
robot.backward(pos.distance)
elif pos.left:
robot.left(pos.distance)
elif pos.right:
robot.right(pos.distance)
bd.when_pressed = move
bd.when_moved = move
bd.when_released = robot.stop
pause()

View File

@@ -0,0 +1,26 @@
from gpiozero import Robot
from bluedot import BlueDot
from signal import pause
def pos_to_values(x, y):
left = y if x > 0 else y + x
right = y if x < 0 else y - x
return (clamped(left), clamped(right))
def clamped(v):
return max(-1, min(1, v))
def drive():
while True:
if bd.is_pressed:
x, y = bd.position.x, bd.position.y
yield pos_to_values(x, y)
else:
yield (0, 0)
robot = Robot(left=(4, 14), right=(17, 18))
bd = BlueDot()
robot.source = drive()
pause()

View File

@@ -0,0 +1,9 @@
from gpiozero import LEDBarGraph, CPUTemperature
from signal import pause
cpu = CPUTemperature(min_temp=50, max_temp=90)
leds = LEDBarGraph(2, 3, 4, 5, 6, 7, 8, pwm=True)
leds.source = cpu.values
pause()

View File

@@ -0,0 +1,14 @@
from gpiozero import LED, PingServer
from gpiozero.tools import negated
from signal import pause
green = LED(17)
red = LED(18)
google = PingServer('google.com')
green.source = google.values
green.source_delay = 60
red.source = negated(green.values)
pause()

View File

@@ -1,12 +1,12 @@
from gpiozero import Buzzer, Button
from gpiozero.pins.pigpio import PiGPIOPin
from gpiozero import LEDBoard, MotionSensor
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6']
remote_pins = [PiGPIOPin(17, host=ip) for ip in ips]
remotes = [PiGPIOFactory(host=ip) for ip in ips]
button = Button(17) # button on this pi
buzzers = [Buzzer(pin) for pin in remote_pins] # buzzers on remote pins
buzzers = [Buzzer(pin, pin_factory=r) for r in remotes] # buzzers on remote pins
for buzzer in buzzers:
buzzer.source = button.values

View File

@@ -1,12 +1,12 @@
from gpiozero import LEDBoard, MotionSensor
from gpiozero.pins.pigpio import PiGPIOPin
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6']
remote_pins = [PiGPIOPin(17, host=ip) for ip in ips]
remotes = [PiGPIOFactory(host=ip) for ip in ips]
leds = LEDBoard(2, 3, 4, 5) # leds on this pi
sensors = [MotionSensor(pin) for pin in remote_pins] # motion sensors on other pis
sensors = [MotionSensor(17, pin_factory=r) for r in remotes] # remote sensors
for led, sensor in zip(leds, sensors):
led.source = sensor.values

View File

@@ -0,0 +1,26 @@
from gpiozero import Button, Robot
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
factory = PiGPIOFactory(host='192.168.1.17')
robot = Robot(left=(4, 14), right=(17, 18), pin_factory=factory) # remote pins
# local buttons
left = Button(26)
right = Button(16)
fw = Button(21)
bw = Button(20)
fw.when_pressed = robot.forward
fw.when_released = robot.stop
left.when_pressed = robot.left
left.when_released = robot.stop
right.when_pressed = robot.right
right.when_released = robot.stop
bw.when_pressed = robot.backward
bw.when_released = robot.stop
pause()

View File

@@ -0,0 +1,11 @@
from gpiozero import Robot, MCP3008
from signal import pause
robot = Robot(left=(4, 14), right=(17, 18))
left = MCP3008(0)
right = MCP3008(1)
robot.source = zip(left.values, right.values)
pause()

View File

@@ -0,0 +1,12 @@
from gpiozero import Robot, MCP3008
from gpiozero.tools import scaled
from signal import pause
robot = Robot(left=(4, 14), right=(17, 18))
left = MCP3008(0)
right = MCP3008(1)
robot.source = zip(scaled(left.values, -1, 1), scaled(right.values, -1, 1))
pause()

View File

@@ -0,0 +1,11 @@
from gpiozero import Energenie, TimeOfDay
from datetime import time
from signal import pause
lamp = Energenie(1)
daytime = TimeOfDay(time(8), time(20))
lamp.source = daytime.values
lamp.source_delay = 60
pause()

View File

@@ -0,0 +1,22 @@
from gpiozero import PingServer, LEDBoard
from gpiozero.tools import negated
from signal import pause
status = LEDBoard(
mum=LEDBoard(red=14, green=15),
dad=LEDBoard(red=17, green=18),
alice=LEDBoard(red=21, green=22)
)
statuses = {
PingServer('192.168.1.5'): status.mum,
PingServer('192.168.1.6'): status.dad,
PingServer('192.168.1.7'): status.alice,
}
for server, leds in statuses.items():
leds.green.source = server.values
leds.green.source_delay = 60
leds.red.source = negated(leds.green.values)
pause()

View File

@@ -0,0 +1,18 @@
from gpiozero import PingServer, StatusZero
from gpiozero.tools import negated
from signal import pause
status = StatusZero('mum', 'dad', 'alice')
statuses = {
PingServer('192.168.1.5'): status.mum,
PingServer('192.168.1.6'): status.dad,
PingServer('192.168.1.7'): status.alice,
}
for server, leds in statuses.items():
leds.green.source = server.values
leds.green.source_delay = 60
leds.red.source = negated(leds.green.values)
pause()