Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll committed Sep 13, 2017
0 parents commit 806a29e
Show file tree
Hide file tree
Showing 63 changed files with 1,628 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 02-PyBoard/blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Taken from the REPL based example.
import pyb


while True:
pyb.LED(1).toggle()
pyb.delay(500)
8 changes: 8 additions & 0 deletions 03-BBC_microbit/blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from microbit import display, sleep


while True:
display.set_pixel(2, 2, 9)
sleep(500)
display.set_pixel(2, 2, 0)
sleep(500)
10 changes: 10 additions & 0 deletions 04-Adafruit_Circuit_Playground/blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Taken from the REPL based example.
from board import D13
import digitalio
import time

led = digitalio.DigitalInOut(D13)
led.switch_to_output()
while True:
led.value = not led.value
time.sleep(0.5)
11 changes: 11 additions & 0 deletions 05-ESP8266-32/blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Taken from the REPL based example.
from machine import Pin
import time


led = Pin(2, Pin.OUT)


while True:
led.value(not led.value())
time.sleep(0.5)
20 changes: 20 additions & 0 deletions 07-Visual_Feedback/circuit_playground_neopixels_alive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import neopixel
import random
import time
from board import NEOPIXEL


np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
step = 32


while True:
for i in range(10):
for j in range(10):
np[j] = tuple((max(0, val - step) for val in np[j]))
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
np[i] = (r, g, b)
np.write()
time.sleep(0.05)
13 changes: 13 additions & 0 deletions 07-Visual_Feedback/circuit_playground_pwm_brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Taken from the REPL based example.
from board import D13
import time
import pulseio


pin = pulseio.PWMOut(D13)


while True:
for i in range(16):
pin.duty_cycle = 2 ** i
time.sleep(0.1)
11 changes: 11 additions & 0 deletions 07-Visual_Feedback/microbit_custom_brightness_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from microbit import display, Image


my_picture = Image(
'33333:'
'36663:'
'36963:'
'36663:'
'33333:')

display.show(my_picture)
5 changes: 5 additions & 0 deletions 07-Visual_Feedback/microbit_happy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Taken from the REPL based example.
from microbit import display, Image


display.show(Image.HAPPY)
5 changes: 5 additions & 0 deletions 07-Visual_Feedback/microbit_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Taken from the REPL based example
from microbit import display


display.scroll("Hello World!", delay=80, wait=False, loop=True, monospace=True)
6 changes: 6 additions & 0 deletions 07-Visual_Feedback/microbit_image_from_bytearray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from microbit import display, Image

buf = bytearray(x % 10 for x in range(100))
i = Image(10, 10, buf)

display.show(i.crop(3, 4, 5, 5))
12 changes: 12 additions & 0 deletions 07-Visual_Feedback/microbit_wopr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from microbit import *
import random
import array


def animation():
blinkenlights = array.array('b', [random.randint(0, 9) for i in range(25)])
yield Image(5, 5, blinkenlights)


while True:
display.show(animation())
6 changes: 6 additions & 0 deletions 07-Visual_Feedback/pyboard_lcd_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import lcd160cr


lcd = lcd160cr.LCD160CR('X')
lcd.erase()
lcd.write('Hello, World!')
58 changes: 58 additions & 0 deletions 07-Visual_Feedback/pyboard_lcd_modrian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pyb
import lcd160cr
from random import randint, choice, uniform


lcd = lcd160cr.LCD160CR('X')


MAX_DEPTH = 4
RED = lcd.rgb(255, 0, 0)
YELLOW = lcd.rgb(255, 255, 0)
BLUE = lcd.rgb(0, 0, 255)
WHITE = lcd.rgb(255, 255, 255)
BLACK = lcd.rgb(0, 0, 0)
COLOURS = [RED, YELLOW, BLUE, WHITE, WHITE, WHITE]


class Node:
"""
A node in a tree representation of a Mondrian painting.
"""

def __init__(self, depth=0):
"""
Choose the colour of the rectangle, work out the depth
add child nodes if not too deep.
"""
self.colour = choice(COLOURS)
self.depth = depth + 1
self.children = []
if self.depth <= MAX_DEPTH:
self.direction = choice(['h', 'v'])
self.divide = uniform(0.1, 0.9)
self.children.append(Node(self.depth))
self.children.append(Node(self.depth))

def draw(self, x, y, w, h):
"""
Recursively draw this node and its children.
"""
lcd.set_pen(BLACK, self.colour)
lcd.rect(x, y, w, h)
if self.children:
if self.direction == 'h':
self.children[0].draw(x, y, int(w * self.divide), h)
self.children[1].draw(x + int(w * self.divide), y,
int(w * (1.0 - self.divide)), h)
else:
self.children[0].draw(x, y, w, int(h * self.divide))
self.children[1].draw(x, y + int(h * self.divide), w,
int(h * (1.0 - self.divide)))


while True:
# Keep re-drawing new Mondrian pictures every few seconds.
tree = Node()
tree.draw(0, 0, lcd.w, lcd.h)
pyb.delay(randint(4000, 8000))
16 changes: 16 additions & 0 deletions 07-Visual_Feedback/pyboard_lcd_snow_crash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import lcd160cr
import random


lcd = lcd160cr.LCD160CR('X')
lcd.erase()


while True:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
colour = lcd.rgb(r, g, b)
x = random.randint(0, lcd.w)
y = random.randint(0, lcd.h)
lcd.set_pixel(x, y, colour)
12 changes: 12 additions & 0 deletions 07-Visual_Feedback/pyboard_pwm_brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Taken from REPL based example.
import pyb


blue = pyb.LED(4)
i = 0
while True:
pyb.delay(5)
i += 1
if i > 255:
i = 0
blue.intensity(i)
67 changes: 67 additions & 0 deletions 08-Input_and_Sensing/circuit_playground_audio_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import neopixel
import audiobusio
import digitalio
import audioio
import time
from board import *


def countdown(np):
""" Uses the NeoPixels to display a countdown."""
# Start from an "off" state.
np.fill((0, 0, 0))
np.write()
for i in range(10):
np[i] = (0, 20, 0)
np.write()
time.sleep(0.5)
np.fill((0, 128, 0))
np.write()


def record():
""" Returns a buffer of recorded sound."""
buf = bytearray(8000)
with audiobusio.PDMIn(MICROPHONE_CLOCK, MICROPHONE_DATA) as mic:
mic.record(buf, len(buf))
return buf


def play(buf, freq):
"""
Play the referenced buffer of recorded sound at a certain
frequency.
"""
# Set the speaker ready for output.
speaker_enable = digitalio.DigitalInOut(SPEAKER_ENABLE)
speaker_enable.switch_to_output(value = True)
# Play the audio buffer through the speaker.
with audioio.AudioOut(SPEAKER, buf) as speaker:
speaker.frequency = freq
speaker.play()
# Block while the speaker is playing.
while speaker.playing:
pass


neopixels = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
button_a = digitalio.DigitalInOut(BUTTON_A)
button_a.pull = digitalio.Pull.DOWN
button_b = digitalio.DigitalInOut(BUTTON_B)
button_b.pull = digitalio.Pull.DOWN


countdown(neopixels)
audio_buffer = record()
neopixels.fill((0, 0, 0))
neopixels.write()


freq = 8000 # Default = normal speed.
if button_a.value:
freq = 12000 # Button A = chipmunk.
elif button_b.value:
freq = 6000 # Button B = Barry White.


play(audio_buffer, freq)
29 changes: 29 additions & 0 deletions 08-Input_and_Sensing/circuit_playground_buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import neopixel
import time
import digitalio
from board import NEOPIXEL, BUTTON_A, BUTTON_B


np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
button_a = digitalio.DigitalInOut(BUTTON_A)
button_a.pull = digitalio.Pull.DOWN
button_b = digitalio.DigitalInOut(BUTTON_B)
button_b.pull = digitalio.Pull.DOWN


clockwise = True


while True:
time.sleep(0.05)
if button_a.value:
clockwise = True
elif button_b.value:
clockwise = False
for i in range(10):
if clockwise:
i = 9 - i
for j in range(10):
np[j] = tuple((max(0, val - 64) for val in np[j]))
np[i] = (0, 0, 254)
np.write()
20 changes: 20 additions & 0 deletions 08-Input_and_Sensing/circuit_playground_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import neopixel
import time
import digitalio
from board import NEOPIXEL, SLIDE_SWITCH


np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
switch = digitalio.DigitalInOut(SLIDE_SWITCH)
switch.pull = digitalio.Pull.UP


while True:
time.sleep(0.05)
for i in range(10):
if switch.value:
i = 9 - i
for j in range(10):
np[j] = tuple((max(0, val - 64) for val in np[j]))
np[i] = (0, 0, 254)
np.write()
35 changes: 35 additions & 0 deletions 08-Input_and_Sensing/circuit_playground_touch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import neopixel
import touchio
import digitalio
from board import *


# Stops the speaker crackling when touched.
spkr = digitalio.DigitalInOut(SPEAKER_ENABLE)
spkr.switch_to_output()
spkr.value = False


np = neopixel.NeoPixel(NEOPIXEL, 10, auto_write=False)
touch_a1 = touchio.TouchIn(A1)
touch_a3 = touchio.TouchIn(A3)
touch_a4 = touchio.TouchIn(A4)
touch_a6 = touchio.TouchIn(A6)


while True:
if touch_a4.value:
np[0] = (255, 0, 0)
np[1] = (255, 0, 0)
if touch_a6.value:
np[3] = (0, 255, 0)
np[4] = (0, 255, 0)
if touch_a1.value:
np[5] = (255, 255, 0)
np[6] = (255, 255, 0)
if touch_a3.value:
np[8] = (0, 0, 255)
np[9] = (0, 0, 255)
for j in range(10):
np[j] = tuple((max(0, val - 32) for val in np[j]))
np.write()
9 changes: 9 additions & 0 deletions 08-Input_and_Sensing/esp8266_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from machine import Pin


led = Pin(2, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_UP)


while True:
led.value(button.value())
Loading

0 comments on commit 806a29e

Please sign in to comment.