mirror of
				https://github.com/KevinMidboe/python-gpiozero.git
				synced 2025-10-29 17:50:37 +00:00 
			
		
		
		
	Merge pull request #411 from waveform80/include-examples
Convert recipe examples to includes
This commit is contained in:
		| @@ -74,6 +74,7 @@ autodoc_member_order = 'groupwise' | |||||||
|  |  | ||||||
| intersphinx_mapping = { | intersphinx_mapping = { | ||||||
|     'python': ('http://docs.python.org/3.4', None), |     'python': ('http://docs.python.org/3.4', None), | ||||||
|  |     'picamera': ('http://picamera.readthedocs.io/en/latest', None), | ||||||
|     } |     } | ||||||
|  |  | ||||||
| # -- Options for HTML output ---------------------------------------------- | # -- Options for HTML output ---------------------------------------------- | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								docs/examples/all_on_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/all_on_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import FishDish | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | fish = FishDish() | ||||||
|  |  | ||||||
|  | fish.button.when_pressed = fish.on | ||||||
|  | fish.button.when_released = fish.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										9
									
								
								docs/examples/all_on_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/all_on_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import TrafficHat | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | th = TrafficHat() | ||||||
|  |  | ||||||
|  | th.button.when_pressed = th.on | ||||||
|  | th.button.when_released = th.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										23
									
								
								docs/examples/all_on_3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								docs/examples/all_on_3.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | from gpiozero import LED, Buzzer, Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  | buzzer = Buzzer(3) | ||||||
|  | red = LED(4) | ||||||
|  | amber = LED(5) | ||||||
|  | green = LED(6) | ||||||
|  |  | ||||||
|  | things = [red, amber, green, buzzer] | ||||||
|  |  | ||||||
|  | def things_on(): | ||||||
|  |     for thing in things: | ||||||
|  |         thing.on() | ||||||
|  |  | ||||||
|  | def things_off(): | ||||||
|  |     for thing in things: | ||||||
|  |         thing.off() | ||||||
|  |  | ||||||
|  | button.when_pressed = things_on | ||||||
|  | button.when_released = things_off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										9
									
								
								docs/examples/button_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/button_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     if button.is_pressed: | ||||||
|  |         print("Button is pressed") | ||||||
|  |     else: | ||||||
|  |         print("Button is not pressed") | ||||||
							
								
								
									
										6
									
								
								docs/examples/button_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								docs/examples/button_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | button.wait_for_press() | ||||||
|  | print("Button was pressed") | ||||||
							
								
								
									
										11
									
								
								docs/examples/button_3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/button_3.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | def say_hello(): | ||||||
|  |     print("Hello!") | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | button.when_pressed = say_hello | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										15
									
								
								docs/examples/button_4.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docs/examples/button_4.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | def say_hello(): | ||||||
|  |     print("Hello!") | ||||||
|  |  | ||||||
|  | def say_goodbye(): | ||||||
|  |     print("Goodbye!") | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | button.when_pressed = say_hello | ||||||
|  | button.when_released = say_goodbye | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										15
									
								
								docs/examples/button_camera_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docs/examples/button_camera_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from picamera import PiCamera | ||||||
|  | from datetime import datetime | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  | camera = PiCamera() | ||||||
|  |  | ||||||
|  | def capture(): | ||||||
|  |     datetime = datetime.now().isoformat() | ||||||
|  |     camera.capture('/home/pi/%s.jpg' % datetime) | ||||||
|  |  | ||||||
|  | button.when_pressed = capture | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										18
									
								
								docs/examples/button_camera_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								docs/examples/button_camera_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from picamera import PiCamera | ||||||
|  | from datetime import datetime | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | left_button = Button(2) | ||||||
|  | right_button = Button(3) | ||||||
|  | camera = PiCamera() | ||||||
|  |  | ||||||
|  | def capture(): | ||||||
|  |     datetime = datetime.now().isoformat() | ||||||
|  |     camera.capture('/home/pi/%s.jpg' % datetime) | ||||||
|  |  | ||||||
|  | left_button.when_pressed = camera.start_preview | ||||||
|  | left_button.when_released = camera.stop_preview | ||||||
|  | right_button.when_pressed = capture | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										10
									
								
								docs/examples/button_led_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/button_led_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import LED, Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | led = LED(17) | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | button.when_pressed = led.on | ||||||
|  | button.when_released = led.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										9
									
								
								docs/examples/button_led_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/button_led_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import LED, Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | led = LED(17) | ||||||
|  | button = Button(2) | ||||||
|  |  | ||||||
|  | led.source = button.values | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										11
									
								
								docs/examples/button_shutdown.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/button_shutdown.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from subprocess import check_call | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | def shutdown(): | ||||||
|  |     check_call(['sudo', 'poweroff']) | ||||||
|  |  | ||||||
|  | shutdown_btn = Button(17, hold_time=2) | ||||||
|  | shutdown_btn.when_held = shutdown | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										12
									
								
								docs/examples/button_stop_motion.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								docs/examples/button_stop_motion.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | from picamera import PiCamera | ||||||
|  |  | ||||||
|  | button = Button(2) | ||||||
|  | camera = PiCamera() | ||||||
|  |  | ||||||
|  | camera.start_preview() | ||||||
|  | frame = 1 | ||||||
|  | while True: | ||||||
|  |     button.wait_for_press() | ||||||
|  |     camera.capture('/home/pi/frame%03d.jpg' % frame) | ||||||
|  |     frame += 1 | ||||||
							
								
								
									
										8
									
								
								docs/examples/distance_sensor_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/examples/distance_sensor_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | from gpiozero import DistanceSensor | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | sensor = DistanceSensor(23, 24) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     print('Distance to nearest object is', sensor.distance, 'm') | ||||||
|  |     sleep(1) | ||||||
							
								
								
									
										10
									
								
								docs/examples/distance_sensor_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/distance_sensor_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import DistanceSensor, LED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) | ||||||
|  | led = LED(16) | ||||||
|  |  | ||||||
|  | sensor.when_in_range = led.on | ||||||
|  | sensor.when_out_of_range = led.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										10
									
								
								docs/examples/led_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/led_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import LED | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | red = LED(17) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     red.on() | ||||||
|  |     sleep(1) | ||||||
|  |     red.off() | ||||||
|  |     sleep(1) | ||||||
							
								
								
									
										8
									
								
								docs/examples/led_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/examples/led_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | from gpiozero import LED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | red = LED(17) | ||||||
|  |  | ||||||
|  | red.blink() | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										15
									
								
								docs/examples/led_bargraph_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docs/examples/led_bargraph_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | from gpiozero import LEDBarGraph | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | graph = LEDBarGraph(5, 6, 13, 19, 26, 20) | ||||||
|  |  | ||||||
|  | graph.value = 1  # (1, 1, 1, 1, 1, 1) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = 1/2  # (1, 1, 1, 0, 0, 0) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = -1/2  # (0, 0, 0, 1, 1, 1) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = 1/4  # (1, 0, 0, 0, 0, 0) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = -1  # (1, 1, 1, 1, 1, 1) | ||||||
|  | sleep(1) | ||||||
							
								
								
									
										15
									
								
								docs/examples/led_bargraph_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docs/examples/led_bargraph_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | from gpiozero import LEDBarGraph | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) | ||||||
|  |  | ||||||
|  | graph.value = 1/10  # (0.5, 0, 0, 0, 0) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = 3/10  # (1, 0.5, 0, 0, 0) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = -3/10  # (0, 0, 0, 0.5, 1) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = 9/10  # (1, 1, 1, 1, 0.5) | ||||||
|  | sleep(1) | ||||||
|  | graph.value = 95/100  # (1, 1, 1, 1, 0.75) | ||||||
|  | sleep(1) | ||||||
							
								
								
									
										15
									
								
								docs/examples/led_board_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docs/examples/led_board_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | from gpiozero import LEDBoard | ||||||
|  | from time import sleep | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | leds = LEDBoard(5, 6, 13, 19, 26) | ||||||
|  |  | ||||||
|  | leds.on() | ||||||
|  | sleep(1) | ||||||
|  | leds.off() | ||||||
|  | sleep(1) | ||||||
|  | leds.value = (1, 0, 1, 0, 1) | ||||||
|  | sleep(1) | ||||||
|  | leds.blink() | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										5
									
								
								docs/examples/led_board_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								docs/examples/led_board_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | from gpiozero import LEDBoard | ||||||
|  |  | ||||||
|  | leds = LEDBoard(5, 6, 13, 19, 26, pwm=True) | ||||||
|  |  | ||||||
|  | leds.value = (0.2, 0.4, 0.6, 0.8, 1.0) | ||||||
							
								
								
									
										9
									
								
								docs/examples/led_builtin.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/led_builtin.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import LED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | power = LED(35) # /sys/class/leds/led1 | ||||||
|  | activity = LED(47) # /sys/class/leds/led0 | ||||||
|  |  | ||||||
|  | activity.blink() | ||||||
|  | power.blink() | ||||||
|  | pause() | ||||||
							
								
								
									
										8
									
								
								docs/examples/led_pulse.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/examples/led_pulse.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | from gpiozero import PWMLED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | led = PWMLED(17) | ||||||
|  |  | ||||||
|  | led.pulse() | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										19
									
								
								docs/examples/led_travis.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								docs/examples/led_travis.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | from travispy import TravisPy | ||||||
|  | from gpiozero import LED | ||||||
|  | from gpiozero.tools import negated | ||||||
|  | from time import sleep | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | def build_passed(repo='RPi-Distro/python-gpiozero', delay=3600): | ||||||
|  |     t = TravisPy() | ||||||
|  |     r = t.repo(repo) | ||||||
|  |     while True: | ||||||
|  |         yield r.last_build_state == 'passed' | ||||||
|  |         sleep(delay) # Sleep an hour before hitting travis again | ||||||
|  |  | ||||||
|  | red = LED(12) | ||||||
|  | green = LED(16) | ||||||
|  |  | ||||||
|  | red.source = negated(green.values) | ||||||
|  | green.source = build_passed() | ||||||
|  | pause() | ||||||
							
								
								
									
										12
									
								
								docs/examples/led_variable_brightness.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								docs/examples/led_variable_brightness.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | from gpiozero import PWMLED | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | led = PWMLED(17) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     led.value = 0  # off | ||||||
|  |     sleep(1) | ||||||
|  |     led.value = 0.5  # half brightness | ||||||
|  |     sleep(1) | ||||||
|  |     led.value = 1  # full brightness | ||||||
|  |     sleep(1) | ||||||
							
								
								
									
										9
									
								
								docs/examples/light_sensor_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/light_sensor_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import LightSensor | ||||||
|  |  | ||||||
|  | sensor = LightSensor(18) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     sensor.wait_for_light() | ||||||
|  |     print("It's light! :)") | ||||||
|  |     sensor.wait_for_dark() | ||||||
|  |     print("It's dark :(") | ||||||
							
								
								
									
										10
									
								
								docs/examples/light_sensor_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/light_sensor_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import LightSensor, LED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | sensor = LightSensor(18) | ||||||
|  | led = LED(16) | ||||||
|  |  | ||||||
|  | sensor.when_dark = led.on | ||||||
|  | sensor.when_light = led.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										9
									
								
								docs/examples/light_sensor_3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/light_sensor_3.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import LightSensor, PWMLED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | sensor = LightSensor(18) | ||||||
|  | led = PWMLED(16) | ||||||
|  |  | ||||||
|  | led.source = sensor.values | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										10
									
								
								docs/examples/motion_sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/motion_sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import MotionSensor, LED | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | pir = MotionSensor(4) | ||||||
|  | led = LED(16) | ||||||
|  |  | ||||||
|  | pir.when_motion = led.on | ||||||
|  | pir.when_no_motion = led.off | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										10
									
								
								docs/examples/motor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/motor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import Motor | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | motor = Motor(forward=4, backward=14) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     motor.forward() | ||||||
|  |     sleep(5) | ||||||
|  |     motor.backward() | ||||||
|  |     sleep(5) | ||||||
							
								
								
									
										18
									
								
								docs/examples/music_box.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								docs/examples/music_box.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | from gpiozero import Button | ||||||
|  | import pygame.mixer | ||||||
|  | from pygame.mixer import Sound | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | pygame.mixer.init() | ||||||
|  |  | ||||||
|  | sound_pins = { | ||||||
|  |     2: Sound("samples/drum_tom_mid_hard.wav"), | ||||||
|  |     3: Sound("samples/drum_cymbal_open.wav"), | ||||||
|  | } | ||||||
|  |  | ||||||
|  | buttons = [Button(pin) for pin in sound_pins] | ||||||
|  | for button in buttons: | ||||||
|  |     sound = sound_pins[button.pin.number] | ||||||
|  |     button.when_pressed = sound.play | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										6
									
								
								docs/examples/pot_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								docs/examples/pot_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | from gpiozero import MCP3008 | ||||||
|  |  | ||||||
|  | pot = MCP3008(channel=0) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     print(pot.value) | ||||||
							
								
								
									
										7
									
								
								docs/examples/pot_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								docs/examples/pot_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | from gpiozero import LEDBarGraph, MCP3008 | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) | ||||||
|  | pot = MCP3008(channel=0) | ||||||
|  | graph.source = pot.values | ||||||
|  | pause() | ||||||
							
								
								
									
										22
									
								
								docs/examples/reaction_game.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								docs/examples/reaction_game.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | from gpiozero import Button, LED | ||||||
|  | from time import sleep | ||||||
|  | import random | ||||||
|  |  | ||||||
|  | led = LED(17) | ||||||
|  |  | ||||||
|  | player_1 = Button(2) | ||||||
|  | player_2 = Button(3) | ||||||
|  |  | ||||||
|  | time = random.uniform(5, 10) | ||||||
|  | sleep(time) | ||||||
|  | led.on() | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     if player_1.is_pressed: | ||||||
|  |         print("Player 1 wins!") | ||||||
|  |         break | ||||||
|  |     if player_2.is_pressed: | ||||||
|  |         print("Player 2 wins!") | ||||||
|  |         break | ||||||
|  |  | ||||||
|  | led.off() | ||||||
							
								
								
									
										28
									
								
								docs/examples/rgbled.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								docs/examples/rgbled.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | |||||||
|  | from gpiozero import RGBLED | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | led = RGBLED(red=9, green=10, blue=11) | ||||||
|  |  | ||||||
|  | led.red = 1  # full red | ||||||
|  | sleep(1) | ||||||
|  | led.red = 0.5  # half red | ||||||
|  | sleep(1) | ||||||
|  |  | ||||||
|  | led.color = (0, 1, 0)  # full green | ||||||
|  | sleep(1) | ||||||
|  | led.color = (1, 0, 1)  # magenta | ||||||
|  | sleep(1) | ||||||
|  | led.color = (1, 1, 0)  # yellow | ||||||
|  | sleep(1) | ||||||
|  | led.color = (0, 1, 1)  # cyan | ||||||
|  | sleep(1) | ||||||
|  | led.color = (1, 1, 1)  # white | ||||||
|  | sleep(1) | ||||||
|  |  | ||||||
|  | led.color = (0, 0, 0)  # off | ||||||
|  | sleep(1) | ||||||
|  |  | ||||||
|  | # slowly increase intensity of blue | ||||||
|  | for n in range(100): | ||||||
|  |     led.blue = n/100 | ||||||
|  |     sleep(0.1) | ||||||
							
								
								
									
										11
									
								
								docs/examples/rgbled_pot_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/rgbled_pot_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | from gpiozero import RGBLED, MCP3008 | ||||||
|  |  | ||||||
|  | led = RGBLED(red=2, green=3, blue=4) | ||||||
|  | red_pot = MCP3008(channel=0) | ||||||
|  | green_pot = MCP3008(channel=1) | ||||||
|  | blue_pot = MCP3008(channel=2) | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     led.red = red_pot.value | ||||||
|  |     led.green = green_pot.value | ||||||
|  |     led.blue = blue_pot.value | ||||||
							
								
								
									
										11
									
								
								docs/examples/rgbled_pot_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/rgbled_pot_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | from gpiozero import RGBLED, MCP3008 | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | led = RGBLED(2, 3, 4) | ||||||
|  | red_pot = MCP3008(0) | ||||||
|  | green_pot = MCP3008(1) | ||||||
|  | blue_pot = MCP3008(2) | ||||||
|  |  | ||||||
|  | led.source = zip(red_pot.values, green_pot.values, blue_pot.values) | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										10
									
								
								docs/examples/robot_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/robot_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import Robot | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  |  | ||||||
|  | for i in range(4): | ||||||
|  |     robot.forward() | ||||||
|  |     sleep(10) | ||||||
|  |     robot.right() | ||||||
|  |     sleep(1) | ||||||
							
								
								
									
										9
									
								
								docs/examples/robot_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/robot_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import Robot, DistanceSensor | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  |  | ||||||
|  | sensor.when_in_range = robot.backward | ||||||
|  | sensor.when_out_of_range = robot.stop | ||||||
|  | pause() | ||||||
							
								
								
									
										23
									
								
								docs/examples/robot_buttons.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								docs/examples/robot_buttons.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | from gpiozero import Robot, Button | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  |  | ||||||
|  | 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() | ||||||
							
								
								
									
										34
									
								
								docs/examples/robot_keyboard_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								docs/examples/robot_keyboard_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | import curses | ||||||
|  | from gpiozero import Robot | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  |  | ||||||
|  | actions = { | ||||||
|  |     curses.KEY_UP:    robot.forward, | ||||||
|  |     curses.KEY_DOWN:  robot.backward, | ||||||
|  |     curses.KEY_LEFT:  robot.left, | ||||||
|  |     curses.KEY_RIGHT: robot.right, | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | def main(window): | ||||||
|  |     next_key = None | ||||||
|  |     while True: | ||||||
|  |         curses.halfdelay(1) | ||||||
|  |         if next_key is None: | ||||||
|  |             key = window.getch() | ||||||
|  |         else: | ||||||
|  |             key = next_key | ||||||
|  |             next_key = None | ||||||
|  |         if key != -1: | ||||||
|  |             # KEY DOWN | ||||||
|  |             curses.halfdelay(3) | ||||||
|  |             action = actions.get(key) | ||||||
|  |             if action is not None: | ||||||
|  |                 action() | ||||||
|  |             next_key = key | ||||||
|  |             while next_key == key: | ||||||
|  |                 next_key = window.getch() | ||||||
|  |             # KEY UP | ||||||
|  |             robot.stop() | ||||||
|  |  | ||||||
|  | curses.wrapper(main) | ||||||
							
								
								
									
										21
									
								
								docs/examples/robot_keyboard_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								docs/examples/robot_keyboard_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | from gpiozero import Robot | ||||||
|  | from evdev import InputDevice, list_devices, ecodes | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  |  | ||||||
|  | devices = [InputDevice(device) for device in list_devices()] | ||||||
|  | keyboard = devices[0]  # this may vary | ||||||
|  |  | ||||||
|  | keypress_actions = { | ||||||
|  |     ecodes.KEY_UP: robot.forward, | ||||||
|  |     ecodes.KEY_DOWN: robot.backward, | ||||||
|  |     ecodes.KEY_LEFT: robot.left, | ||||||
|  |     ecodes.KEY_RIGHT: robot.right, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | for event in keyboard.read_loop(): | ||||||
|  |     if event.type == ecodes.EV_KEY: | ||||||
|  |         if event.value == 1:  # key down | ||||||
|  |             keypress_actions[event.code]() | ||||||
|  |         if event.value == 0:  # key up | ||||||
|  |             robot.stop() | ||||||
							
								
								
									
										10
									
								
								docs/examples/robot_motion_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								docs/examples/robot_motion_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | from gpiozero import Robot, MotionSensor | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  | pir = MotionSensor(5) | ||||||
|  |  | ||||||
|  | pir.when_motion = robot.forward | ||||||
|  | pir.when_no_motion = robot.stop | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										9
									
								
								docs/examples/robot_motion_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/robot_motion_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | from gpiozero import Robot, MotionSensor | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | robot = Robot(left=(4, 14), right=(17, 18)) | ||||||
|  | pir = MotionSensor(5) | ||||||
|  |  | ||||||
|  | robot.source = zip(pir.values, pir.values) | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										12
									
								
								docs/examples/thermometer.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								docs/examples/thermometer.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | from gpiozero import MCP3008 | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | def convert_temp(gen): | ||||||
|  |     for value in gen: | ||||||
|  |         yield (value * 3.3 - 0.5) * 100 | ||||||
|  |  | ||||||
|  | adc = MCP3008(channel=0) | ||||||
|  |  | ||||||
|  | for temp in convert_temp(adc.values): | ||||||
|  |     print('The temperature is', temp, 'C') | ||||||
|  |     sleep(1) | ||||||
							
								
								
									
										20
									
								
								docs/examples/traffic_lights_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								docs/examples/traffic_lights_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | from gpiozero import TrafficLights | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | lights = TrafficLights(2, 3, 4) | ||||||
|  |  | ||||||
|  | lights.green.on() | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     sleep(10) | ||||||
|  |     lights.green.off() | ||||||
|  |     lights.amber.on() | ||||||
|  |     sleep(1) | ||||||
|  |     lights.amber.off() | ||||||
|  |     lights.red.on() | ||||||
|  |     sleep(10) | ||||||
|  |     lights.amber.on() | ||||||
|  |     sleep(1) | ||||||
|  |     lights.green.on() | ||||||
|  |     lights.amber.off() | ||||||
|  |     lights.red.off() | ||||||
							
								
								
									
										20
									
								
								docs/examples/traffic_lights_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								docs/examples/traffic_lights_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | from gpiozero import TrafficLights | ||||||
|  | from time import sleep | ||||||
|  | from signal import pause | ||||||
|  |  | ||||||
|  | lights = TrafficLights(2, 3, 4) | ||||||
|  |  | ||||||
|  | def traffic_light_sequence(): | ||||||
|  |     while True: | ||||||
|  |         yield (0, 0, 1) # green | ||||||
|  |         sleep(10) | ||||||
|  |         yield (0, 1, 0) # amber | ||||||
|  |         sleep(1) | ||||||
|  |         yield (1, 0, 0) # red | ||||||
|  |         sleep(10) | ||||||
|  |         yield (1, 1, 0) # red+amber | ||||||
|  |         sleep(1) | ||||||
|  |  | ||||||
|  | lights.source = traffic_light_sequence() | ||||||
|  |  | ||||||
|  | pause() | ||||||
							
								
								
									
										24
									
								
								docs/examples/traffic_lights_3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								docs/examples/traffic_lights_3.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | from gpiozero import LED | ||||||
|  | from time import sleep | ||||||
|  |  | ||||||
|  | red = LED(2) | ||||||
|  | amber = LED(3) | ||||||
|  | green = LED(4) | ||||||
|  |  | ||||||
|  | green.on() | ||||||
|  | amber.off() | ||||||
|  | red.off() | ||||||
|  |  | ||||||
|  | while True: | ||||||
|  |     sleep(10) | ||||||
|  |     green.off() | ||||||
|  |     amber.on() | ||||||
|  |     sleep(1) | ||||||
|  |     amber.off() | ||||||
|  |     red.on() | ||||||
|  |     sleep(10) | ||||||
|  |     amber.on() | ||||||
|  |     sleep(1) | ||||||
|  |     green.on() | ||||||
|  |     amber.off() | ||||||
|  |     red.off() | ||||||
							
								
								
									
										802
									
								
								docs/recipes.rst
									
									
									
									
									
								
							
							
						
						
									
										802
									
								
								docs/recipes.rst
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user