Skip to content

Commit acbe731

Browse files
authored
Merge pull request adafruit#1262 from FoamyGuy/multitasking_with_circuitpython
adding multitasking with circuitpython files
2 parents 1a4ae4d + 73d1c11 commit acbe731

File tree

8 files changed

+455
-0
lines changed

8 files changed

+455
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""
2+
This example script shows the usage of servos, LEDs, and buttons all
3+
used simultaneously without interrupting each other.
4+
"""
5+
6+
import board
7+
import digitalio
8+
import time
9+
import neopixel
10+
import pulseio
11+
from adafruit_motor import servo
12+
from digitalio import DigitalInOut, Direction, Pull
13+
14+
btn = DigitalInOut(board.SWITCH)
15+
btn.direction = Direction.INPUT
16+
btn.pull = Pull.UP
17+
18+
prev_state = btn.value
19+
20+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
21+
pixels[0] = (0, 0, 0)
22+
23+
BLINK_LIST = [
24+
{
25+
"ON": 0.5,
26+
"OFF": 0.5,
27+
"PREV_TIME": -1,
28+
"PIN": board.D5,
29+
},
30+
{
31+
"ON": 0.5,
32+
"OFF": 0.5,
33+
"PREV_TIME": -1,
34+
"PIN": board.D6,
35+
},
36+
{
37+
"ON": 0.5,
38+
"OFF": 0.5,
39+
"PREV_TIME": -1,
40+
"PIN": board.D9,
41+
},
42+
{
43+
"ON": 0.5,
44+
"OFF": 0.5,
45+
"PREV_TIME": -1,
46+
"PIN": board.D10,
47+
}
48+
]
49+
50+
SERVO_LIST = [
51+
{
52+
"MAX_ANGLE": 180,
53+
"MIN_ANGLE": 0,
54+
"PREV_TIME": -1,
55+
"PIN": board.A1,
56+
"DELAY_BETWEEN": 0.05,
57+
"SERVO": None,
58+
"MOVE_BY": 5
59+
},
60+
{
61+
"MAX_ANGLE": 90,
62+
"MIN_ANGLE": 0,
63+
"PREV_TIME": -1,
64+
"PIN": board.A3,
65+
"DELAY_BETWEEN": 0.02,
66+
"SERVO": None,
67+
"MOVE_BY": 2
68+
}
69+
]
70+
71+
for cur_servo in SERVO_LIST:
72+
pwm = pulseio.PWMOut(cur_servo["PIN"], duty_cycle=2 ** 15, frequency=50)
73+
# Create a servo object.
74+
cur_servo["SERVO"] = servo.Servo(pwm)
75+
76+
77+
for led in BLINK_LIST:
78+
led["PIN"] = digitalio.DigitalInOut(led["PIN"])
79+
led["PIN"].direction = digitalio.Direction.OUTPUT
80+
81+
disabled_leds = []
82+
# temporarily remove first two from the blink list
83+
disabled_leds.append(BLINK_LIST.pop(0))
84+
disabled_leds.append(BLINK_LIST.pop(0))
85+
while True:
86+
# Store the current time to refer to later.
87+
now = time.monotonic()
88+
89+
cur_state = btn.value
90+
if cur_state != prev_state:
91+
if not cur_state:
92+
print("BTN is down")
93+
94+
# swap the LED Blink patterns to the opposite pairs of LEDs
95+
temp = []
96+
temp.append(BLINK_LIST.pop(0))
97+
temp.append(BLINK_LIST.pop(0))
98+
99+
BLINK_LIST.append(disabled_leds.pop(0))
100+
BLINK_LIST.append(disabled_leds.pop(0))
101+
102+
disabled_leds.append(temp.pop(0))
103+
disabled_leds.append(temp.pop(0))
104+
105+
else:
106+
print("BTN is up")
107+
108+
prev_state = cur_state
109+
110+
for led in BLINK_LIST:
111+
if led["PIN"].value == False:
112+
if now >= led["PREV_TIME"] + led["OFF"]:
113+
led["PREV_TIME"] = now
114+
led["PIN"].value = True
115+
if led["PIN"].value == True:
116+
if now >= led["PREV_TIME"] + led["ON"]:
117+
led["PREV_TIME"] = now
118+
led["PIN"].value = False
119+
120+
for servo in SERVO_LIST:
121+
if now >= servo["PREV_TIME"] + servo["DELAY_BETWEEN"]:
122+
try:
123+
servo["SERVO"].angle += servo["MOVE_BY"]
124+
except ValueError as e:
125+
126+
if servo["MOVE_BY"] > 0:
127+
servo["SERVO"].angle = servo["MAX_ANGLE"]
128+
else:
129+
servo["SERVO"].angle = servo["MIN_ANGLE"]
130+
131+
if servo["SERVO"].angle >= servo["MAX_ANGLE"] or \
132+
servo["SERVO"].angle <= servo["MIN_ANGLE"]:
133+
134+
servo["MOVE_BY"] = -servo["MOVE_BY"]
135+
136+
servo["PREV_TIME"] = now
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
This example script shows the usage of servos, and LEDs
3+
used simultaneously without interrupting each other.
4+
"""
5+
6+
import board
7+
import digitalio
8+
import time
9+
import neopixel
10+
import pulseio
11+
from adafruit_motor import servo
12+
13+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
14+
pixels[0] = (0, 0, 0)
15+
16+
BLINK_LIST = [
17+
{
18+
"ON": 0.25,
19+
"OFF": 0.25,
20+
"PREV_TIME": -1,
21+
"PIN": board.D5,
22+
},
23+
{
24+
"ON": 0.5,
25+
"OFF": 0.5,
26+
"PREV_TIME": -1,
27+
"PIN": board.D6,
28+
},
29+
{
30+
"ON": 1.0,
31+
"OFF": 1.0,
32+
"PREV_TIME": -1,
33+
"PIN": board.D9,
34+
},
35+
{
36+
"ON": 2.0,
37+
"OFF": 2.0,
38+
"PREV_TIME": -1,
39+
"PIN": board.D10,
40+
}
41+
]
42+
43+
SERVO_LIST = [
44+
{
45+
"MAX_ANGLE": 180,
46+
"MIN_ANGLE": 0,
47+
"PREV_TIME": -1,
48+
"PIN": board.A1,
49+
"DELAY_BETWEEN": 0.05,
50+
"SERVO": None,
51+
"MOVE_BY": 5
52+
}
53+
]
54+
55+
for cur_servo in SERVO_LIST:
56+
pwm = pulseio.PWMOut(cur_servo["PIN"], duty_cycle=2 ** 15, frequency=50)
57+
# Create a servo object.
58+
cur_servo["SERVO"] = servo.Servo(pwm)
59+
60+
61+
for led in BLINK_LIST:
62+
led["PIN"] = digitalio.DigitalInOut(led["PIN"])
63+
led["PIN"].direction = digitalio.Direction.OUTPUT
64+
65+
while True:
66+
# Store the current time to refer to later.
67+
now = time.monotonic()
68+
69+
for led in BLINK_LIST:
70+
if led["PIN"].value == False:
71+
if now >= led["PREV_TIME"] + led["OFF"]:
72+
led["PREV_TIME"] = now
73+
led["PIN"].value = True
74+
if led["PIN"].value == True:
75+
if now >= led["PREV_TIME"] + led["ON"]:
76+
led["PREV_TIME"] = now
77+
led["PIN"].value = False
78+
79+
for servo in SERVO_LIST:
80+
if now >= servo["PREV_TIME"] + servo["DELAY_BETWEEN"]:
81+
try:
82+
servo["SERVO"].angle += servo["MOVE_BY"]
83+
except ValueError as e:
84+
85+
if servo["MOVE_BY"] > 0:
86+
servo["SERVO"].angle = servo["MAX_ANGLE"]
87+
else:
88+
servo["SERVO"].angle = servo["MIN_ANGLE"]
89+
90+
91+
if servo["SERVO"].angle >= servo["MAX_ANGLE"] or \
92+
servo["SERVO"].angle <= servo["MIN_ANGLE"]:
93+
94+
servo["MOVE_BY"] = -servo["MOVE_BY"]
95+
96+
servo["PREV_TIME"] = now
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Using time.monotonic() to blink the built-in LED.
3+
4+
Instead of "wait until" think "Is it time yet?"
5+
"""
6+
import time
7+
import digitalio
8+
import board
9+
10+
# How long we want the LED to stay on
11+
BLINK_ON_DURATION = 0.5
12+
13+
# How long we want the LED to stay off
14+
BLINK_OFF_DURATION = 0.25
15+
16+
# When we last changed the LED state
17+
LAST_BLINK_TIME = -1
18+
19+
# Setup the LED pin.
20+
led = digitalio.DigitalInOut(board.D13)
21+
led.direction = digitalio.Direction.OUTPUT
22+
23+
while True:
24+
# Store the current time to refer to later.
25+
now = time.monotonic()
26+
if not led.value:
27+
# Is it time to turn on?
28+
if now >= LAST_BLINK_TIME + BLINK_OFF_DURATION:
29+
led.value = True
30+
LAST_BLINK_TIME = now
31+
if led.value:
32+
# Is it time to turn off?
33+
if now >= LAST_BLINK_TIME + BLINK_ON_DURATION:
34+
led.value = False
35+
LAST_BLINK_TIME = now
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
This example script shows how to read button state with
3+
debouncing that does not rely on time.sleep().
4+
"""
5+
6+
import board
7+
from digitalio import DigitalInOut, Direction, Pull
8+
9+
btn = DigitalInOut(board.SWITCH)
10+
btn.direction = Direction.INPUT
11+
btn.pull = Pull.UP
12+
13+
prev_state = btn.value
14+
15+
while True:
16+
cur_state = btn.value
17+
if cur_state != prev_state:
18+
if not cur_state:
19+
print("BTN is down")
20+
else:
21+
print("BTN is up")
22+
23+
prev_state = cur_state
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
This example script shows how to blink multiple LEDs at different
3+
rates simultaneously without each affecting the others.
4+
"""
5+
6+
import board
7+
import digitalio
8+
import time
9+
10+
BLINK_LIST = [
11+
{
12+
"ON": 0.25,
13+
"OFF": 0.25,
14+
"PREV_TIME": -1,
15+
"PIN": board.D5,
16+
},
17+
{
18+
"ON": 0.5,
19+
"OFF": 0.5,
20+
"PREV_TIME": -1,
21+
"PIN": board.D6,
22+
},
23+
{
24+
"ON": 1.0,
25+
"OFF": 1.0,
26+
"PREV_TIME": -1,
27+
"PIN": board.D9,
28+
},
29+
{
30+
"ON": 2.0,
31+
"OFF": 2.0,
32+
"PREV_TIME": -1,
33+
"PIN": board.D10,
34+
}
35+
]
36+
37+
for led in BLINK_LIST:
38+
led["PIN"] = digitalio.DigitalInOut(led["PIN"])
39+
led["PIN"].direction = digitalio.Direction.OUTPUT
40+
41+
while True:
42+
# Store the current time to refer to later.
43+
now = time.monotonic()
44+
45+
for led in BLINK_LIST:
46+
if led["PIN"].value == False:
47+
if now >= led["PREV_TIME"] + led["OFF"]:
48+
led["PREV_TIME"] = now
49+
led["PIN"].value = True
50+
if led["PIN"].value == True:
51+
if now >= led["PREV_TIME"] + led["ON"]:
52+
led["PREV_TIME"] = now
53+
led["PIN"].value = False

0 commit comments

Comments
 (0)