Skip to content

Commit 806a29e

Browse files
committed
Initial commit.
0 parents  commit 806a29e

File tree

63 files changed

+1628
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1628
-0
lines changed

02-PyBoard/blink.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Taken from the REPL based example.
2+
import pyb
3+
4+
5+
while True:
6+
pyb.LED(1).toggle()
7+
pyb.delay(500)

03-BBC_microbit/blink.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from microbit import display, sleep
2+
3+
4+
while True:
5+
display.set_pixel(2, 2, 9)
6+
sleep(500)
7+
display.set_pixel(2, 2, 0)
8+
sleep(500)
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Taken from the REPL based example.
2+
from board import D13
3+
import digitalio
4+
import time
5+
6+
led = digitalio.DigitalInOut(D13)
7+
led.switch_to_output()
8+
while True:
9+
led.value = not led.value
10+
time.sleep(0.5)

05-ESP8266-32/blink.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Taken from the REPL based example.
2+
from machine import Pin
3+
import time
4+
5+
6+
led = Pin(2, Pin.OUT)
7+
8+
9+
while True:
10+
led.value(not led.value())
11+
time.sleep(0.5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import neopixel
2+
import random
3+
import time
4+
from board import NEOPIXEL
5+
6+
7+
np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
8+
step = 32
9+
10+
11+
while True:
12+
for i in range(10):
13+
for j in range(10):
14+
np[j] = tuple((max(0, val - step) for val in np[j]))
15+
r = random.randint(0, 255)
16+
g = random.randint(0, 255)
17+
b = random.randint(0, 255)
18+
np[i] = (r, g, b)
19+
np.write()
20+
time.sleep(0.05)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Taken from the REPL based example.
2+
from board import D13
3+
import time
4+
import pulseio
5+
6+
7+
pin = pulseio.PWMOut(D13)
8+
9+
10+
while True:
11+
for i in range(16):
12+
pin.duty_cycle = 2 ** i
13+
time.sleep(0.1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from microbit import display, Image
2+
3+
4+
my_picture = Image(
5+
'33333:'
6+
'36663:'
7+
'36963:'
8+
'36663:'
9+
'33333:')
10+
11+
display.show(my_picture)

07-Visual_Feedback/microbit_happy.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Taken from the REPL based example.
2+
from microbit import display, Image
3+
4+
5+
display.show(Image.HAPPY)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Taken from the REPL based example
2+
from microbit import display
3+
4+
5+
display.scroll("Hello World!", delay=80, wait=False, loop=True, monospace=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from microbit import display, Image
2+
3+
buf = bytearray(x % 10 for x in range(100))
4+
i = Image(10, 10, buf)
5+
6+
display.show(i.crop(3, 4, 5, 5))

07-Visual_Feedback/microbit_wopr.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from microbit import *
2+
import random
3+
import array
4+
5+
6+
def animation():
7+
blinkenlights = array.array('b', [random.randint(0, 9) for i in range(25)])
8+
yield Image(5, 5, blinkenlights)
9+
10+
11+
while True:
12+
display.show(animation())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import lcd160cr
2+
3+
4+
lcd = lcd160cr.LCD160CR('X')
5+
lcd.erase()
6+
lcd.write('Hello, World!')
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pyb
2+
import lcd160cr
3+
from random import randint, choice, uniform
4+
5+
6+
lcd = lcd160cr.LCD160CR('X')
7+
8+
9+
MAX_DEPTH = 4
10+
RED = lcd.rgb(255, 0, 0)
11+
YELLOW = lcd.rgb(255, 255, 0)
12+
BLUE = lcd.rgb(0, 0, 255)
13+
WHITE = lcd.rgb(255, 255, 255)
14+
BLACK = lcd.rgb(0, 0, 0)
15+
COLOURS = [RED, YELLOW, BLUE, WHITE, WHITE, WHITE]
16+
17+
18+
class Node:
19+
"""
20+
A node in a tree representation of a Mondrian painting.
21+
"""
22+
23+
def __init__(self, depth=0):
24+
"""
25+
Choose the colour of the rectangle, work out the depth
26+
add child nodes if not too deep.
27+
"""
28+
self.colour = choice(COLOURS)
29+
self.depth = depth + 1
30+
self.children = []
31+
if self.depth <= MAX_DEPTH:
32+
self.direction = choice(['h', 'v'])
33+
self.divide = uniform(0.1, 0.9)
34+
self.children.append(Node(self.depth))
35+
self.children.append(Node(self.depth))
36+
37+
def draw(self, x, y, w, h):
38+
"""
39+
Recursively draw this node and its children.
40+
"""
41+
lcd.set_pen(BLACK, self.colour)
42+
lcd.rect(x, y, w, h)
43+
if self.children:
44+
if self.direction == 'h':
45+
self.children[0].draw(x, y, int(w * self.divide), h)
46+
self.children[1].draw(x + int(w * self.divide), y,
47+
int(w * (1.0 - self.divide)), h)
48+
else:
49+
self.children[0].draw(x, y, w, int(h * self.divide))
50+
self.children[1].draw(x, y + int(h * self.divide), w,
51+
int(h * (1.0 - self.divide)))
52+
53+
54+
while True:
55+
# Keep re-drawing new Mondrian pictures every few seconds.
56+
tree = Node()
57+
tree.draw(0, 0, lcd.w, lcd.h)
58+
pyb.delay(randint(4000, 8000))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import lcd160cr
2+
import random
3+
4+
5+
lcd = lcd160cr.LCD160CR('X')
6+
lcd.erase()
7+
8+
9+
while True:
10+
r = random.randint(0, 255)
11+
g = random.randint(0, 255)
12+
b = random.randint(0, 255)
13+
colour = lcd.rgb(r, g, b)
14+
x = random.randint(0, lcd.w)
15+
y = random.randint(0, lcd.h)
16+
lcd.set_pixel(x, y, colour)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Taken from REPL based example.
2+
import pyb
3+
4+
5+
blue = pyb.LED(4)
6+
i = 0
7+
while True:
8+
pyb.delay(5)
9+
i += 1
10+
if i > 255:
11+
i = 0
12+
blue.intensity(i)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import neopixel
2+
import audiobusio
3+
import digitalio
4+
import audioio
5+
import time
6+
from board import *
7+
8+
9+
def countdown(np):
10+
""" Uses the NeoPixels to display a countdown."""
11+
# Start from an "off" state.
12+
np.fill((0, 0, 0))
13+
np.write()
14+
for i in range(10):
15+
np[i] = (0, 20, 0)
16+
np.write()
17+
time.sleep(0.5)
18+
np.fill((0, 128, 0))
19+
np.write()
20+
21+
22+
def record():
23+
""" Returns a buffer of recorded sound."""
24+
buf = bytearray(8000)
25+
with audiobusio.PDMIn(MICROPHONE_CLOCK, MICROPHONE_DATA) as mic:
26+
mic.record(buf, len(buf))
27+
return buf
28+
29+
30+
def play(buf, freq):
31+
"""
32+
Play the referenced buffer of recorded sound at a certain
33+
frequency.
34+
"""
35+
# Set the speaker ready for output.
36+
speaker_enable = digitalio.DigitalInOut(SPEAKER_ENABLE)
37+
speaker_enable.switch_to_output(value = True)
38+
# Play the audio buffer through the speaker.
39+
with audioio.AudioOut(SPEAKER, buf) as speaker:
40+
speaker.frequency = freq
41+
speaker.play()
42+
# Block while the speaker is playing.
43+
while speaker.playing:
44+
pass
45+
46+
47+
neopixels = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
48+
button_a = digitalio.DigitalInOut(BUTTON_A)
49+
button_a.pull = digitalio.Pull.DOWN
50+
button_b = digitalio.DigitalInOut(BUTTON_B)
51+
button_b.pull = digitalio.Pull.DOWN
52+
53+
54+
countdown(neopixels)
55+
audio_buffer = record()
56+
neopixels.fill((0, 0, 0))
57+
neopixels.write()
58+
59+
60+
freq = 8000 # Default = normal speed.
61+
if button_a.value:
62+
freq = 12000 # Button A = chipmunk.
63+
elif button_b.value:
64+
freq = 6000 # Button B = Barry White.
65+
66+
67+
play(audio_buffer, freq)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import neopixel
2+
import time
3+
import digitalio
4+
from board import NEOPIXEL, BUTTON_A, BUTTON_B
5+
6+
7+
np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
8+
button_a = digitalio.DigitalInOut(BUTTON_A)
9+
button_a.pull = digitalio.Pull.DOWN
10+
button_b = digitalio.DigitalInOut(BUTTON_B)
11+
button_b.pull = digitalio.Pull.DOWN
12+
13+
14+
clockwise = True
15+
16+
17+
while True:
18+
time.sleep(0.05)
19+
if button_a.value:
20+
clockwise = True
21+
elif button_b.value:
22+
clockwise = False
23+
for i in range(10):
24+
if clockwise:
25+
i = 9 - i
26+
for j in range(10):
27+
np[j] = tuple((max(0, val - 64) for val in np[j]))
28+
np[i] = (0, 0, 254)
29+
np.write()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import neopixel
2+
import time
3+
import digitalio
4+
from board import NEOPIXEL, SLIDE_SWITCH
5+
6+
7+
np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
8+
switch = digitalio.DigitalInOut(SLIDE_SWITCH)
9+
switch.pull = digitalio.Pull.UP
10+
11+
12+
while True:
13+
time.sleep(0.05)
14+
for i in range(10):
15+
if switch.value:
16+
i = 9 - i
17+
for j in range(10):
18+
np[j] = tuple((max(0, val - 64) for val in np[j]))
19+
np[i] = (0, 0, 254)
20+
np.write()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import neopixel
2+
import touchio
3+
import digitalio
4+
from board import *
5+
6+
7+
# Stops the speaker crackling when touched.
8+
spkr = digitalio.DigitalInOut(SPEAKER_ENABLE)
9+
spkr.switch_to_output()
10+
spkr.value = False
11+
12+
13+
np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
14+
touch_a1 = touchio.TouchIn(A1)
15+
touch_a3 = touchio.TouchIn(A3)
16+
touch_a4 = touchio.TouchIn(A4)
17+
touch_a6 = touchio.TouchIn(A6)
18+
19+
20+
while True:
21+
if touch_a4.value:
22+
np[0] = (255, 0, 0)
23+
np[1] = (255, 0, 0)
24+
if touch_a6.value:
25+
np[3] = (0, 255, 0)
26+
np[4] = (0, 255, 0)
27+
if touch_a1.value:
28+
np[5] = (255, 255, 0)
29+
np[6] = (255, 255, 0)
30+
if touch_a3.value:
31+
np[8] = (0, 0, 255)
32+
np[9] = (0, 0, 255)
33+
for j in range(10):
34+
np[j] = tuple((max(0, val - 32) for val in np[j]))
35+
np.write()
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from machine import Pin
2+
3+
4+
led = Pin(2, Pin.OUT)
5+
button = Pin(14, Pin.IN, Pin.PULL_UP)
6+
7+
8+
while True:
9+
led.value(button.value())

0 commit comments

Comments
 (0)