├── Chapter01 ├── 01_first.py └── 02_repl.py ├── Chapter02 ├── 01_pin_13.py ├── 02_brightness.py ├── 03_color.py ├── 04_hex.py ├── 05_color_by_name.py ├── 06_set_all.py ├── 07_set_range.py ├── 08_generating_random.py ├── 09_random_animation.py └── 10_rainbow_animation.py ├── Chapter03 ├── 01_beep_basic.py ├── 02_beep_loop.py ├── 03_note.py ├── 04_melody.py ├── 05_alarm.py ├── 06_wav.py ├── 07_convert.py ├── 08_tones.py └── hello.wav ├── Chapter04 ├── 01_push_button.py ├── 02_button_led.py ├── 03_slide_switch.py ├── 04_button_state_change.py ├── 05_moving_leds.py ├── 06_button_beep.py ├── 07_detect_touch.py ├── 08_touch_pad_raw.py └── 09_adjust_threshold.py ├── Chapter05 ├── 01_temperature.py ├── 02_light.py ├── 03_meter.py ├── 04_motion.py ├── 05_tap.py ├── 06_shake.py └── 07_beep_on_shake.py ├── Chapter06 ├── 01_detect_button.py ├── 02_button_module.py ├── 03_button_event_loop.py ├── 04_generator.py ├── 05_show_scores.py ├── 06_detect_winners.py ├── 07_score_event_loop.py └── game │ ├── button.py │ ├── colors.py │ ├── main.py │ ├── score.py │ ├── start.wav │ ├── win1.wav │ └── win2.wav ├── Chapter07 ├── 01_touch.py ├── 02_speaker.py ├── 03_play.py ├── 04_pixels.py ├── 05_handler_sounds.py ├── 06_handler_pixels.py ├── 07_event_loop.py └── fruity │ ├── hit.wav │ ├── main.py │ ├── piano.wav │ ├── tin.wav │ └── wood.wav ├── Chapter08 ├── 01_tuning.py ├── 02_actuation_range.py ├── 03_servo_angle.py ├── 04_servo_sweep.py ├── 05_servo_buttons.py ├── 06_multiple_servos.py ├── 07_dc_motor_on.py ├── 08_dc_motor_speed.py └── 09_dc_motor_buttons.py ├── Chapter09 ├── 01_mu_flash.py ├── 02_mu_repl.py ├── 03_display_character.py ├── 04_display_image.py ├── 05_display_scrolling.py ├── 06_show_button.py └── 07_countdown.py ├── Chapter10 ├── 01_repl.py ├── 02_scan.py ├── 03_ap.py ├── 04_station.py ├── 05_webrepl_web.py ├── 06_webrepl_cli.py └── 07_leds.py ├── Chapter11 ├── 01_remount.py ├── 02_list_files.py ├── 03_remove_files.py ├── 04_create_directories.py ├── 05_read_files.py ├── 06_write_files.py └── 07_disk_usage.py ├── Chapter12 ├── 01_dns.py ├── 02_check_network.py ├── 03_http_raw.py ├── 04_http_urequests.py ├── 05_fetch_json.py ├── 06_http_server.py ├── 07_web_handler.py ├── 08_web_leds.py ├── 09_api_leds.py └── extra │ ├── fix_urequests_warnings.py │ ├── netcheck.py │ └── web.py ├── Chapter13 ├── 01_buttons.py ├── 02_ssd1306.py ├── 03_fill.py ├── 04_pixels.py ├── 05_lines.py ├── 06_text.py ├── 07_invert.py └── extra │ └── fix_framebuf_import.py ├── Chapter14 ├── 01_get_weather_simple.py ├── 02_get_weather_func.py ├── 03_random_city.py ├── 04_screen.py ├── 05_show_weather.py ├── 06_visual_feedback.py ├── 07_show_random_weather.py ├── 08_button_event_loop.py └── extra │ └── screen.py ├── Chapter15 ├── 01_discover_i2c.py ├── 02_accelerometer.py ├── 03_flip.py ├── 04_brightness.py ├── 05_display_image.py ├── 06_list_images.py ├── 07_joke_machine.py └── images │ ├── joke_01_question.bmp │ ├── joke_01_response.bmp │ ├── joke_02_question.bmp │ ├── joke_02_response.bmp │ ├── joke_03_question.bmp │ ├── joke_03_response.bmp │ ├── joke_04_question.bmp │ └── joke_04_response.bmp ├── LICENSE └── README.md /Chapter01/01_first.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | cpx.pixels[0] = (255, 0, 0) # set first NeoPixel to the color red 5 | time.sleep(60) 6 | -------------------------------------------------------------------------------- /Chapter01/02_repl.py: -------------------------------------------------------------------------------- 1 | def add(a, b): 2 | return a + b 3 | 4 | 5 | add(2, 2) 6 | 7 | 8 | 2**100 + 2**101 9 | -------------------------------------------------------------------------------- /Chapter02/01_pin_13.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | cpx.red_led = True 3 | cpx.red_led = False 4 | -------------------------------------------------------------------------------- /Chapter02/02_brightness.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | cpx.pixels.brightness = 1.0 4 | cpx.pixels[0] = (255, 0, 0) 5 | 6 | cpx.pixels.brightness = 0.5 7 | cpx.pixels.brightness = 0.10 8 | -------------------------------------------------------------------------------- /Chapter02/03_color.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | cpx.pixels[0] = (255, 0, 0) 4 | 5 | cpx.pixels[0] = (0, 255, 0) 6 | 7 | cpx.pixels[0] = (0, 0, 255) 8 | 9 | cpx.pixels[0] = (0, 0, 0) 10 | 11 | cpx.pixels[1] = (255, 0, 0) 12 | -------------------------------------------------------------------------------- /Chapter02/04_hex.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | cpx.pixels[0] = 0xFF0000 4 | 5 | cpx.pixels[1] = 0x00FF00 6 | cpx.pixels[2] = 0x0000FF 7 | 8 | cpx.pixels[3] = 0xFFFF00 9 | 10 | cpx.pixels[4] = 255 11 | -------------------------------------------------------------------------------- /Chapter02/05_color_by_name.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | RGB = dict(black=0x000000, blue=0x0000FF, green=0x00FF00, cyan=0x00FFFF, 4 | red=0xFF0000, magenta=0xFF00FF, yellow=0xFFFF00, white=0xFFFFFF) 5 | cpx.pixels[0] = RGB['red'] 6 | 7 | for i, name in enumerate(sorted(RGB)): 8 | cpx.pixels[i] = RGB[name] 9 | -------------------------------------------------------------------------------- /Chapter02/06_set_all.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | cpx.pixels.fill(0x0000FF) 3 | 4 | cpx.pixels.fill(0x000000) 5 | -------------------------------------------------------------------------------- /Chapter02/07_set_range.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | cpx.pixels[0:2] = [0xFF0000, 0xFF0000] 3 | 4 | cpx.pixels[2:5] = [0x00FF00] * 3 5 | cpx.pixels[5:10] = [0x0000FF] * 5 6 | -------------------------------------------------------------------------------- /Chapter02/08_generating_random.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | from random import randint 3 | 4 | randint(0, 255) 5 | 6 | 7 | def get_random_color(): 8 | return (randint(0, 255), randint(0, 255), randint(0, 255)) 9 | 10 | 11 | get_random_color() 12 | 13 | 14 | cpx.pixels[0] = get_random_color() 15 | 16 | 17 | cpx.pixels.fill(get_random_color()) 18 | -------------------------------------------------------------------------------- /Chapter02/09_random_animation.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | from random import randint 3 | import time 4 | 5 | 6 | def get_random_color(): 7 | return (randint(0, 255), randint(0, 255), randint(0, 255)) 8 | 9 | 10 | for i in range(10): 11 | cpx.pixels[i] = get_random_color() 12 | time.sleep(1) 13 | 14 | 15 | cpx.pixels.fill(0x000000) 16 | for cycle in range(3): 17 | for i in range(10): 18 | cpx.pixels[i] = get_random_color() 19 | time.sleep(1) 20 | 21 | 22 | cpx.pixels.fill(0x000000) 23 | for i in range(5): 24 | cpx.pixels.fill(get_random_color()) 25 | time.sleep(1) 26 | -------------------------------------------------------------------------------- /Chapter02/10_rainbow_animation.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | RAINBOW = [ 5 | 0xFF0000, # red 6 | 0xFFA500, # orange 7 | 0xFFFF00, # yellow 8 | 0x00FF00, # green 9 | 0x0000FF, # blue 10 | 0x4b0082, # indigo 11 | 0xEE82EE, # violet 12 | ] 13 | 14 | cpx.pixels.brightness = 0.10 15 | cpx.pixels.fill(0x000000) 16 | while True: 17 | for i, color in enumerate(RAINBOW): 18 | cpx.pixels[i] = color 19 | time.sleep(0.2) 20 | for i in range(len(RAINBOW)): 21 | cpx.pixels[i] = 0x000000 22 | time.sleep(0.2) 23 | -------------------------------------------------------------------------------- /Chapter03/01_beep_basic.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | cpx.play_tone(900, 0.2) 3 | 4 | cpx.play_tone(500, 0.4) 5 | -------------------------------------------------------------------------------- /Chapter03/02_beep_loop.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | for i in range(500, 1000, 100): 3 | print(i) 4 | cpx.play_tone(i, 0.2) 5 | 6 | 7 | for i in range(200, 500, 100): 8 | print(i) 9 | cpx.play_tone(i, i / 1000) 10 | -------------------------------------------------------------------------------- /Chapter03/03_note.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | E5 = 659 4 | C5 = 523 5 | G5 = 784 6 | 7 | cpx.play_tone(E5, 0.15) 8 | -------------------------------------------------------------------------------- /Chapter03/04_melody.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | 4 | E5 = 659 5 | C5 = 523 6 | G5 = 784 7 | 8 | MELODY = (E5, E5, 0, E5, 0, C5, E5, 0, G5) 9 | 10 | 11 | def play_note(note, duration=0.15): 12 | if note == 0: 13 | time.sleep(duration) 14 | else: 15 | cpx.play_tone(note, duration) 16 | 17 | for note in MELODY: 18 | play_note(note) 19 | -------------------------------------------------------------------------------- /Chapter03/05_alarm.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | BEEP_HIGH = 960 4 | BEEP_LOW = 800 5 | 6 | for i in range(3): 7 | cpx.play_tone(BEEP_HIGH, 0.5) 8 | cpx.play_tone(BEEP_LOW, 0.5) 9 | -------------------------------------------------------------------------------- /Chapter03/06_wav.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | cpx.play_file('hello.wav') 3 | -------------------------------------------------------------------------------- /Chapter03/07_convert.py: -------------------------------------------------------------------------------- 1 | # no python code for this recipe 2 | -------------------------------------------------------------------------------- /Chapter03/08_tones.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | BEEP_HIGH = 960 5 | BEEP_LOW = 800 6 | 7 | cpx.pixels.brightness = 0.10 8 | 9 | cpx.start_tone(BEEP_HIGH) 10 | for i in range(10): 11 | cpx.pixels[i] = 0xFF0000 12 | time.sleep(0.1) 13 | cpx.stop_tone() 14 | 15 | cpx.start_tone(BEEP_LOW) 16 | for i in range(10): 17 | cpx.pixels[i] = 0x000000 18 | time.sleep(0.1) 19 | cpx.stop_tone() 20 | -------------------------------------------------------------------------------- /Chapter03/hello.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter03/hello.wav -------------------------------------------------------------------------------- /Chapter04/01_push_button.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | print(cpx.button_a) 6 | time.sleep(0.05) 7 | -------------------------------------------------------------------------------- /Chapter04/02_button_led.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | BLACK = 0x000000 4 | GREEN = 0x00FF00 5 | 6 | cpx.pixels.brightness = 0.10 7 | while True: 8 | cpx.pixels[2] = GREEN if cpx.button_a else BLACK 9 | cpx.pixels[7] = GREEN if cpx.button_b else BLACK 10 | -------------------------------------------------------------------------------- /Chapter04/03_slide_switch.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | print(cpx.switch) 6 | time.sleep(0.05) 7 | -------------------------------------------------------------------------------- /Chapter04/04_button_state_change.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | 4 | def button_change(pressed): 5 | print('pressed:', pressed) 6 | 7 | 8 | last = cpx.button_a 9 | while True: 10 | if cpx.button_a != last: 11 | button_change(cpx.button_a) 12 | last = cpx.button_a 13 | -------------------------------------------------------------------------------- /Chapter04/05_moving_leds.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | BLACK = 0x000000 5 | BLUE = 0x0000FF 6 | 7 | cpx.pixels.brightness = 0.10 8 | i = 0 9 | direction = 1 10 | while True: 11 | if cpx.button_a: 12 | direction = 1 13 | if cpx.button_b: 14 | direction = -1 15 | i += direction 16 | i = i % 10 17 | cpx.pixels.fill(BLACK) 18 | cpx.pixels[i] = BLUE 19 | time.sleep(0.05) 20 | -------------------------------------------------------------------------------- /Chapter04/06_button_beep.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | while True: 4 | if cpx.button_a: 5 | cpx.play_tone(500, 0.2) 6 | if cpx.button_b: 7 | cpx.play_tone(900, 0.2) 8 | -------------------------------------------------------------------------------- /Chapter04/07_detect_touch.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | if cpx.touch_A1: 6 | print('detected touch') 7 | time.sleep(0.05) 8 | -------------------------------------------------------------------------------- /Chapter04/08_touch_pad_raw.py: -------------------------------------------------------------------------------- 1 | import time 2 | import touchio 3 | import board 4 | 5 | a1 = touchio.TouchIn(board.A1) 6 | while True: 7 | touch = a1.raw_value > a1.threshold 8 | print('raw:', a1.raw_value, 'threshold:', a1.threshold, 'touch:', touch) 9 | time.sleep(0.5) 10 | -------------------------------------------------------------------------------- /Chapter04/09_adjust_threshold.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | cpx.adjust_touch_threshold(200) 5 | while True: 6 | if cpx.touch_A1: 7 | print('detected touch') 8 | time.sleep(0.5) 9 | -------------------------------------------------------------------------------- /Chapter05/01_temperature.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | start = time.monotonic() 5 | while True: 6 | elapsed = time.monotonic() - start 7 | temp = cpx.temperature 8 | print('{elapsed:.2f}\t{temp}'.format(**locals())) 9 | time.sleep(0.1) 10 | -------------------------------------------------------------------------------- /Chapter05/02_light.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | print(cpx.light) 6 | time.sleep(0.1) 7 | -------------------------------------------------------------------------------- /Chapter05/03_meter.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | BLACK = 0x000000 5 | BLUE = 0x0000FF 6 | MAX_LUX = 330 7 | cpx.pixels.brightness = 0.10 8 | 9 | 10 | def gauge(level): 11 | cpx.pixels[0:level] = [BLUE] * level 12 | 13 | 14 | last = 0 15 | while True: 16 | level = int((cpx.light / MAX_LUX) * 10) 17 | if level != last: 18 | cpx.pixels.fill(BLACK) 19 | gauge(level) 20 | last = level 21 | time.sleep(0.05) 22 | -------------------------------------------------------------------------------- /Chapter05/04_motion.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | x, y, z = cpx.acceleration 6 | print('x: {x:.2f} y: {y:.2f} z: {z:.2f}'.format(**locals())) 7 | time.sleep(0.1) 8 | -------------------------------------------------------------------------------- /Chapter05/05_tap.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | cpx.detect_taps = 1 5 | while True: 6 | print('tap detected:', cpx.tapped) 7 | time.sleep(0.1) 8 | -------------------------------------------------------------------------------- /Chapter05/06_shake.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | print('shake detected:', cpx.shake()) 6 | time.sleep(0.1) 7 | -------------------------------------------------------------------------------- /Chapter05/07_beep_on_shake.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | import time 3 | 4 | while True: 5 | if cpx.shake(20): 6 | cpx.play_tone(900, 0.2) 7 | time.sleep(0.1) 8 | -------------------------------------------------------------------------------- /Chapter06/01_detect_button.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | 4 | class ButtonEvent: 5 | def __init__(self, name): 6 | self.name = name 7 | self.last = False 8 | 9 | def is_pressed(self): 10 | pressed = getattr(cpx, self.name) 11 | changed = (pressed != self.last) 12 | self.last = pressed 13 | return (pressed and changed) 14 | 15 | 16 | button = ButtonEvent('button_a') 17 | while True: 18 | if button.is_pressed(): 19 | print('button A pressed') 20 | -------------------------------------------------------------------------------- /Chapter06/02_button_module.py: -------------------------------------------------------------------------------- 1 | from button import ButtonEvent 2 | 3 | button = ButtonEvent('button_a') 4 | while True: 5 | if button.is_pressed(): 6 | print('button A pressed') 7 | -------------------------------------------------------------------------------- /Chapter06/03_button_event_loop.py: -------------------------------------------------------------------------------- 1 | from button import ButtonEvent 2 | 3 | 4 | def main(): 5 | buttons = {1: ButtonEvent('button_a'), 2: ButtonEvent('button_b')} 6 | while True: 7 | for player, button in buttons.items(): 8 | if button.is_pressed(): 9 | print('button pressed for player', player) 10 | 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /Chapter06/04_generator.py: -------------------------------------------------------------------------------- 1 | BLACK = 0x000000 2 | SEQUENCE = [ 3 | 0xFFFF00, # Yellow 4 | 0xFF8C00, # DarkOrange 5 | 0xFF0000, # Red 6 | 0xFF00FF, # Magenta 7 | ] 8 | PLAYER_PIXELS1 = [0, 1, 2, 3, 4] 9 | PLAYER_PIXELS2 = [9, 8, 7, 6, 5] 10 | 11 | 12 | def generate_colors(positions): 13 | yield 0, BLACK 14 | for i in positions: 15 | for color in SEQUENCE: 16 | yield i, color 17 | 18 | 19 | COLORS = dict() 20 | COLORS[1] = list(generate_colors(PLAYER_PIXELS1)) 21 | COLORS[2] = list(generate_colors(PLAYER_PIXELS2)) 22 | -------------------------------------------------------------------------------- /Chapter06/05_show_scores.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | from colors import COLORS 3 | 4 | 5 | class ScoreBoard: 6 | def __init__(self): 7 | self.score = {1: 0, 2: 0} 8 | cpx.pixels.brightness = 0.02 9 | cpx.play_file('start.wav') 10 | 11 | def show(self, player): 12 | score = self.score[player] 13 | pos, color = COLORS[player][score] 14 | cpx.pixels[pos] = color 15 | -------------------------------------------------------------------------------- /Chapter06/06_detect_winners.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | from colors import COLORS 3 | 4 | 5 | class ScoreBoard: 6 | def __init__(self): 7 | self.score = {1: 0, 2: 0} 8 | cpx.pixels.brightness = 0.02 9 | cpx.play_file('start.wav') 10 | 11 | def scored(self, player): 12 | self.score[player] += 1 13 | self.show(player) 14 | if self.score[player] == 20: 15 | cpx.play_file('win%s.wav' % player) 16 | 17 | def show(self, player): 18 | score = self.score[player] 19 | pos, color = COLORS[player][score] 20 | cpx.pixels[pos] = color 21 | -------------------------------------------------------------------------------- /Chapter06/07_score_event_loop.py: -------------------------------------------------------------------------------- 1 | from button import ButtonEvent 2 | from score import ScoreBoard 3 | 4 | 5 | def main(): 6 | buttons = {1: ButtonEvent('button_a'), 2: ButtonEvent('button_b')} 7 | board = ScoreBoard() 8 | while True: 9 | for player, button in buttons.items(): 10 | if button.is_pressed(): 11 | board.scored(player) 12 | 13 | 14 | main() 15 | -------------------------------------------------------------------------------- /Chapter06/game/button.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | 3 | 4 | class ButtonEvent: 5 | def __init__(self, name): 6 | self.name = name 7 | self.last = False 8 | 9 | def is_pressed(self): 10 | pressed = getattr(cpx, self.name) 11 | changed = (pressed != self.last) 12 | self.last = pressed 13 | return (pressed and changed) 14 | -------------------------------------------------------------------------------- /Chapter06/game/colors.py: -------------------------------------------------------------------------------- 1 | BLACK = 0x000000 2 | SEQUENCE = [ 3 | 0xFFFF00, # Yellow 4 | 0xFF8C00, # DarkOrange 5 | 0xFF0000, # Red 6 | 0xFF00FF, # Magenta 7 | ] 8 | PLAYER_PIXELS1 = [0, 1, 2, 3, 4] 9 | PLAYER_PIXELS2 = [9, 8, 7, 6, 5] 10 | 11 | 12 | def generate_colors(positions): 13 | yield 0, BLACK 14 | for i in positions: 15 | for color in SEQUENCE: 16 | yield i, color 17 | 18 | 19 | COLORS = dict() 20 | COLORS[1] = list(generate_colors(PLAYER_PIXELS1)) 21 | COLORS[2] = list(generate_colors(PLAYER_PIXELS2)) 22 | -------------------------------------------------------------------------------- /Chapter06/game/main.py: -------------------------------------------------------------------------------- 1 | from button import ButtonEvent 2 | from score import ScoreBoard 3 | 4 | 5 | def main(): 6 | buttons = {1: ButtonEvent('button_a'), 2: ButtonEvent('button_b')} 7 | board = ScoreBoard() 8 | while True: 9 | for player, button in buttons.items(): 10 | if button.is_pressed(): 11 | board.scored(player) 12 | 13 | 14 | main() 15 | -------------------------------------------------------------------------------- /Chapter06/game/score.py: -------------------------------------------------------------------------------- 1 | from adafruit_circuitplayground.express import cpx 2 | from colors import COLORS 3 | 4 | 5 | class ScoreBoard: 6 | def __init__(self): 7 | self.score = {1: 0, 2: 0} 8 | cpx.pixels.brightness = 0.02 9 | cpx.play_file('start.wav') 10 | 11 | def scored(self, player): 12 | self.score[player] += 1 13 | self.show(player) 14 | if self.score[player] == 20: 15 | cpx.play_file('win%s.wav' % player) 16 | 17 | def show(self, player): 18 | score = self.score[player] 19 | pos, color = COLORS[player][score] 20 | cpx.pixels[pos] = color 21 | -------------------------------------------------------------------------------- /Chapter06/game/start.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter06/game/start.wav -------------------------------------------------------------------------------- /Chapter06/game/win1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter06/game/win1.wav -------------------------------------------------------------------------------- /Chapter06/game/win2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter06/game/win2.wav -------------------------------------------------------------------------------- /Chapter07/01_touch.py: -------------------------------------------------------------------------------- 1 | from touchio import TouchIn 2 | import board 3 | 4 | 5 | class TouchEvent: 6 | THRESHOLD_ADJUSTMENT = 400 7 | 8 | def __init__(self, name, onchange): 9 | self.name = name 10 | self.last = False 11 | self.onchange = onchange 12 | pin = getattr(board, name) 13 | self.touch = TouchIn(pin) 14 | self.touch.threshold += self.THRESHOLD_ADJUSTMENT 15 | 16 | def process(self): 17 | current = self.touch.value 18 | if current != self.last: 19 | self.onchange(self.name, current) 20 | self.last = current 21 | 22 | 23 | def handle(name, current): 24 | print(name, current) 25 | 26 | 27 | event = TouchEvent('A1', handle) 28 | while True: 29 | event.process() 30 | -------------------------------------------------------------------------------- /Chapter07/02_speaker.py: -------------------------------------------------------------------------------- 1 | from digitalio import DigitalInOut 2 | import board 3 | 4 | 5 | def enable_speakers(): 6 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 7 | speaker_control.switch_to_output(value=True) 8 | 9 | 10 | enable_speakers() 11 | -------------------------------------------------------------------------------- /Chapter07/03_play.py: -------------------------------------------------------------------------------- 1 | from digitalio import DigitalInOut 2 | from audioio import WaveFile, AudioOut 3 | import board 4 | import time 5 | 6 | 7 | def play_file(speaker, path): 8 | file = open(path, "rb") 9 | audio = WaveFile(file) 10 | speaker.play(audio) 11 | 12 | 13 | def enable_speakers(): 14 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 15 | speaker_control.switch_to_output(value=True) 16 | 17 | 18 | enable_speakers() 19 | speaker = AudioOut(board.SPEAKER) 20 | play_file(speaker, 'piano.wav') 21 | time.sleep(100) 22 | -------------------------------------------------------------------------------- /Chapter07/04_pixels.py: -------------------------------------------------------------------------------- 1 | from neopixel import NeoPixel 2 | import board 3 | 4 | PIXEL_COUNT = 10 5 | RGB = dict( 6 | black=0x000000, 7 | white=0xFFFFFF, 8 | green=0x00FF00, 9 | red=0xFF0000, 10 | yellow=0xFFFF00, 11 | ) 12 | 13 | pixels = NeoPixel(board.NEOPIXEL, PIXEL_COUNT) 14 | pixels.brightness = 0.05 15 | pixels[0] = RGB['red'] 16 | pixels[1] = RGB['green'] 17 | 18 | while True: 19 | pass 20 | -------------------------------------------------------------------------------- /Chapter07/05_handler_sounds.py: -------------------------------------------------------------------------------- 1 | from touchio import TouchIn 2 | from digitalio import DigitalInOut 3 | from audioio import WaveFile, AudioOut 4 | import board 5 | 6 | 7 | def enable_speakers(): 8 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 9 | speaker_control.switch_to_output(value=True) 10 | 11 | 12 | def play_file(speaker, path): 13 | file = open(path, "rb") 14 | audio = WaveFile(file) 15 | speaker.play(audio) 16 | 17 | 18 | class Handler: 19 | def __init__(self, speaker): 20 | self.speaker = speaker 21 | 22 | def handle(self, name, state): 23 | if state: 24 | play_file(self.speaker, 'piano.wav') 25 | 26 | 27 | class TouchEvent: 28 | THRESHOLD_ADJUSTMENT = 400 29 | 30 | def __init__(self, name, onchange): 31 | self.name = name 32 | self.last = False 33 | self.onchange = onchange 34 | pin = getattr(board, name) 35 | self.touch = TouchIn(pin) 36 | self.touch.threshold += self.THRESHOLD_ADJUSTMENT 37 | 38 | def process(self): 39 | current = self.touch.value 40 | if current != self.last: 41 | self.onchange(self.name, current) 42 | self.last = current 43 | 44 | 45 | enable_speakers() 46 | speaker = AudioOut(board.SPEAKER) 47 | handler = Handler(speaker) 48 | event = TouchEvent('A1', handler.handle) 49 | while True: 50 | event.process() 51 | -------------------------------------------------------------------------------- /Chapter07/06_handler_pixels.py: -------------------------------------------------------------------------------- 1 | from touchio import TouchIn 2 | from digitalio import DigitalInOut 3 | from audioio import WaveFile, AudioOut 4 | from neopixel import NeoPixel 5 | import board 6 | 7 | PIXEL_COUNT = 10 8 | 9 | 10 | def enable_speakers(): 11 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 12 | speaker_control.switch_to_output(value=True) 13 | 14 | 15 | def play_file(speaker, path): 16 | file = open(path, "rb") 17 | audio = WaveFile(file) 18 | speaker.play(audio) 19 | 20 | 21 | class Handler: 22 | def __init__(self, speaker, pixels): 23 | self.speaker = speaker 24 | self.pixels = pixels 25 | 26 | def handle(self, name, state): 27 | if state: 28 | play_file(self.speaker, 'piano.wav') 29 | self.pixels[0] = 0xFF0000 30 | else: 31 | self.pixels[0] = 0x000000 32 | 33 | 34 | class TouchEvent: 35 | THRESHOLD_ADJUSTMENT = 400 36 | 37 | def __init__(self, name, onchange): 38 | self.name = name 39 | self.last = False 40 | self.onchange = onchange 41 | pin = getattr(board, name) 42 | self.touch = TouchIn(pin) 43 | self.touch.threshold += self.THRESHOLD_ADJUSTMENT 44 | 45 | def process(self): 46 | current = self.touch.value 47 | if current != self.last: 48 | self.onchange(self.name, current) 49 | self.last = current 50 | 51 | 52 | enable_speakers() 53 | speaker = AudioOut(board.SPEAKER) 54 | pixels = NeoPixel(board.NEOPIXEL, PIXEL_COUNT) 55 | pixels.brightness = 0.05 56 | handler = Handler(speaker, pixels) 57 | event = TouchEvent('A1', handler.handle) 58 | while True: 59 | event.process() 60 | -------------------------------------------------------------------------------- /Chapter07/07_event_loop.py: -------------------------------------------------------------------------------- 1 | from touchio import TouchIn 2 | from digitalio import DigitalInOut 3 | from audioio import WaveFile, AudioOut 4 | from neopixel import NeoPixel 5 | import board 6 | 7 | PIXEL_COUNT = 10 8 | TOUCH_PADS = ['A1', 'A2', 'A5', 'A6'] 9 | SOUND = dict( 10 | A1='hit.wav', 11 | A2='piano.wav', 12 | A5='tin.wav', 13 | A6='wood.wav', 14 | ) 15 | RGB = dict( 16 | black=0x000000, 17 | white=0xFFFFFF, 18 | green=0x00FF00, 19 | red=0xFF0000, 20 | yellow=0xFFFF00, 21 | ) 22 | PIXELS = dict( 23 | A1=(6, RGB['white']), 24 | A2=(8, RGB['red']), 25 | A5=(1, RGB['yellow']), 26 | A6=(3, RGB['green']), 27 | ) 28 | 29 | 30 | def play_file(speaker, path): 31 | file = open(path, "rb") 32 | audio = WaveFile(file) 33 | speaker.play(audio) 34 | 35 | 36 | def enable_speakers(): 37 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 38 | speaker_control.switch_to_output(value=True) 39 | 40 | 41 | class Handler: 42 | def __init__(self, speaker, pixels): 43 | self.speaker = speaker 44 | self.pixels = pixels 45 | 46 | def handle(self, name, state): 47 | pos, color = PIXELS[name] 48 | if state: 49 | play_file(self.speaker, SOUND[name]) 50 | self.pixels[pos] = color 51 | else: 52 | self.pixels[pos] = RGB['black'] 53 | 54 | 55 | class TouchEvent: 56 | THRESHOLD_ADJUSTMENT = 400 57 | 58 | def __init__(self, name, onchange): 59 | self.name = name 60 | self.last = False 61 | self.onchange = onchange 62 | pin = getattr(board, name) 63 | self.touch = TouchIn(pin) 64 | self.touch.threshold += self.THRESHOLD_ADJUSTMENT 65 | 66 | def process(self): 67 | current = self.touch.value 68 | if current != self.last: 69 | self.onchange(self.name, current) 70 | self.last = current 71 | 72 | 73 | def main(): 74 | enable_speakers() 75 | speaker = AudioOut(board.SPEAKER) 76 | pixels = NeoPixel(board.NEOPIXEL, PIXEL_COUNT) 77 | pixels.brightness = 0.05 78 | handler = Handler(speaker, pixels) 79 | events = [TouchEvent(i, handler.handle) for i in TOUCH_PADS] 80 | while True: 81 | for event in events: 82 | event.process() 83 | 84 | 85 | main() 86 | -------------------------------------------------------------------------------- /Chapter07/fruity/hit.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter07/fruity/hit.wav -------------------------------------------------------------------------------- /Chapter07/fruity/main.py: -------------------------------------------------------------------------------- 1 | from touchio import TouchIn 2 | from digitalio import DigitalInOut 3 | from audioio import WaveFile, AudioOut 4 | from neopixel import NeoPixel 5 | import board 6 | 7 | PIXEL_COUNT = 10 8 | TOUCH_PADS = ['A1', 'A2', 'A5', 'A6'] 9 | SOUND = dict( 10 | A1='hit.wav', 11 | A2='piano.wav', 12 | A5='tin.wav', 13 | A6='wood.wav', 14 | ) 15 | RGB = dict( 16 | black=0x000000, 17 | white=0xFFFFFF, 18 | green=0x00FF00, 19 | red=0xFF0000, 20 | yellow=0xFFFF00, 21 | ) 22 | PIXELS = dict( 23 | A1=(6, RGB['white']), 24 | A2=(8, RGB['red']), 25 | A5=(1, RGB['yellow']), 26 | A6=(3, RGB['green']), 27 | ) 28 | 29 | 30 | def play_file(speaker, path): 31 | file = open(path, "rb") 32 | audio = WaveFile(file) 33 | speaker.play(audio) 34 | 35 | 36 | def enable_speakers(): 37 | speaker_control = DigitalInOut(board.SPEAKER_ENABLE) 38 | speaker_control.switch_to_output(value=True) 39 | 40 | 41 | class Handler: 42 | def __init__(self, speaker, pixels): 43 | self.speaker = speaker 44 | self.pixels = pixels 45 | 46 | def handle(self, name, state): 47 | pos, color = PIXELS[name] 48 | if state: 49 | play_file(self.speaker, SOUND[name]) 50 | self.pixels[pos] = color 51 | else: 52 | self.pixels[pos] = RGB['black'] 53 | 54 | 55 | class TouchEvent: 56 | THRESHOLD_ADJUSTMENT = 400 57 | 58 | def __init__(self, name, onchange): 59 | self.name = name 60 | self.last = False 61 | self.onchange = onchange 62 | pin = getattr(board, name) 63 | self.touch = TouchIn(pin) 64 | self.touch.threshold += self.THRESHOLD_ADJUSTMENT 65 | 66 | def process(self): 67 | current = self.touch.value 68 | if current != self.last: 69 | self.onchange(self.name, current) 70 | self.last = current 71 | 72 | 73 | def main(): 74 | enable_speakers() 75 | speaker = AudioOut(board.SPEAKER) 76 | pixels = NeoPixel(board.NEOPIXEL, PIXEL_COUNT) 77 | pixels.brightness = 0.05 78 | handler = Handler(speaker, pixels) 79 | events = [TouchEvent(i, handler.handle) for i in TOUCH_PADS] 80 | while True: 81 | for event in events: 82 | event.process() 83 | 84 | 85 | main() 86 | -------------------------------------------------------------------------------- /Chapter07/fruity/piano.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter07/fruity/piano.wav -------------------------------------------------------------------------------- /Chapter07/fruity/tin.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter07/fruity/tin.wav -------------------------------------------------------------------------------- /Chapter07/fruity/wood.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MicroPython-Cookbook/dd9b98fae1c44e807c565358fc126b6dc982f2ca/Chapter07/fruity/wood.wav -------------------------------------------------------------------------------- /Chapter08/01_tuning.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | 8 | crickit.servo_1.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 9 | crickit.servo_1.angle = 0 10 | time.sleep(3) 11 | crickit.servo_1.angle = 90 12 | time.sleep(60) 13 | -------------------------------------------------------------------------------- /Chapter08/02_actuation_range.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | 8 | crickit.servo_1.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 9 | crickit.servo_1.angle = 0 10 | time.sleep(3) 11 | crickit.servo_1.actuation_range = 160 12 | crickit.servo_1.angle = 160 13 | time.sleep(60) 14 | -------------------------------------------------------------------------------- /Chapter08/03_servo_angle.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | 8 | crickit.servo_1.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 9 | crickit.servo_1.angle = 0 10 | crickit.servo_1.actuation_range = 160 11 | 12 | crickit.servo_1.angle = 0 13 | time.sleep(3) 14 | 15 | crickit.servo_1.angle = 45 16 | time.sleep(3) 17 | 18 | crickit.servo_1.angle = 90 19 | time.sleep(3) 20 | 21 | crickit.servo_1.angle = 160 22 | time.sleep(3) 23 | -------------------------------------------------------------------------------- /Chapter08/04_servo_sweep.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | MAX_ANGLE = 160 8 | STEP = 10 9 | DELAY = 0.1 10 | 11 | 12 | def init(servo): 13 | servo.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 14 | servo.angle = 0 15 | servo.actuation_range = MAX_ANGLE 16 | 17 | 18 | def sweep(servo, direction): 19 | angle = int(servo.angle) 20 | while 0 <= angle <= MAX_ANGLE: 21 | print(angle) 22 | servo.angle = angle 23 | time.sleep(DELAY) 24 | angle += STEP * direction 25 | 26 | 27 | def main(): 28 | init(crickit.servo_1) 29 | while True: 30 | sweep(crickit.servo_1, 1) 31 | sweep(crickit.servo_1, -1) 32 | 33 | 34 | main() 35 | -------------------------------------------------------------------------------- /Chapter08/05_servo_buttons.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | MAX_ANGLE = 160 8 | STEP = 10 9 | DELAY = 0.1 10 | 11 | 12 | def init(servo): 13 | servo.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 14 | servo.angle = 0 15 | servo.actuation_range = MAX_ANGLE 16 | 17 | 18 | def move(servo, angle, direction): 19 | new = angle + STEP * direction 20 | if 0 <= new <= MAX_ANGLE: 21 | angle = new 22 | print(angle) 23 | servo.angle = angle 24 | return angle 25 | 26 | 27 | def main(): 28 | init(crickit.servo_1) 29 | angle = 0 30 | while True: 31 | if cpx.button_a: 32 | angle = move(crickit.servo_1, angle, 1) 33 | if cpx.button_b: 34 | angle = move(crickit.servo_1, angle, -1) 35 | time.sleep(DELAY) 36 | 37 | 38 | main() 39 | -------------------------------------------------------------------------------- /Chapter08/06_multiple_servos.py: -------------------------------------------------------------------------------- 1 | import time 2 | from adafruit_circuitplayground.express import cpx 3 | from adafruit_crickit import crickit 4 | 5 | MIN_PULSE = 750 6 | MAX_PULSE = 2250 7 | MAX_ANGLE = 160 8 | STEP = 10 9 | DELAY = 0.1 10 | 11 | 12 | def init(servo): 13 | servo.set_pulse_width_range(MIN_PULSE, MAX_PULSE) 14 | servo.angle = 0 15 | servo.actuation_range = MAX_ANGLE 16 | 17 | 18 | def move(servo, angle, direction): 19 | new = angle + STEP * direction 20 | if 0 <= new <= MAX_ANGLE: 21 | angle = new 22 | print(angle) 23 | servo.angle = angle 24 | return angle 25 | 26 | 27 | def main(): 28 | servos = [crickit.servo_1, crickit.servo_4] 29 | angles = [0, 0] 30 | init(servos[0]) 31 | init(servos[1]) 32 | while True: 33 | switch = int(cpx.switch) 34 | if cpx.button_a: 35 | angles[switch] = move(servos[switch], angles[switch], 1) 36 | if cpx.button_b: 37 | angles[switch] = move(servos[switch], angles[switch], -1) 38 | time.sleep(DELAY) 39 | 40 | 41 | main() 42 | -------------------------------------------------------------------------------- /Chapter08/07_dc_motor_on.py: -------------------------------------------------------------------------------- 1 | from adafruit_crickit import crickit 2 | import time 3 | 4 | while True: 5 | crickit.dc_motor_1.throttle = 1 6 | time.sleep(1) 7 | crickit.dc_motor_1.throttle = 0 8 | time.sleep(1) 9 | -------------------------------------------------------------------------------- /Chapter08/08_dc_motor_speed.py: -------------------------------------------------------------------------------- 1 | from adafruit_crickit import crickit 2 | import time 3 | 4 | DELAY = 0.1 5 | 6 | 7 | def change_throttle(motor, start, increment): 8 | throttle = start 9 | for i in range(21): 10 | print(throttle) 11 | motor.throttle = throttle 12 | throttle += increment 13 | throttle = round(throttle, 1) 14 | time.sleep(DELAY) 15 | 16 | 17 | def main(): 18 | while True: 19 | change_throttle(crickit.dc_motor_1, -1.0, 0.1) 20 | change_throttle(crickit.dc_motor_1, 1.0, -0.1) 21 | 22 | 23 | main() 24 | -------------------------------------------------------------------------------- /Chapter08/09_dc_motor_buttons.py: -------------------------------------------------------------------------------- 1 | from adafruit_crickit import crickit 2 | from adafruit_circuitplayground.express import cpx 3 | import time 4 | 5 | STEP = 0.1 6 | DELAY = 0.1 7 | MIN_THROTTLE = -1 8 | MAX_THROTTLE = 1 9 | 10 | 11 | def move(motor, throttle, direction): 12 | new = throttle + STEP * direction 13 | if MIN_THROTTLE <= new <= MAX_THROTTLE: 14 | throttle = round(new, 1) 15 | print(throttle) 16 | motor.throttle = throttle 17 | return throttle 18 | 19 | 20 | def main(): 21 | throttle = 0 22 | while True: 23 | if cpx.button_a: 24 | throttle = move(crickit.dc_motor_1, throttle, 1) 25 | if cpx.button_b: 26 | throttle = move(crickit.dc_motor_1, throttle, -1) 27 | time.sleep(DELAY) 28 | 29 | 30 | main() 31 | -------------------------------------------------------------------------------- /Chapter09/01_mu_flash.py: -------------------------------------------------------------------------------- 1 | from microbit import display 2 | display.show('x') 3 | -------------------------------------------------------------------------------- /Chapter09/02_mu_repl.py: -------------------------------------------------------------------------------- 1 | import microbit 2 | microbit 3 | -------------------------------------------------------------------------------- /Chapter09/03_display_character.py: -------------------------------------------------------------------------------- 1 | from microbit import display 2 | import time 3 | 4 | for i in range(3): 5 | display.show(i) 6 | time.sleep(1) 7 | -------------------------------------------------------------------------------- /Chapter09/04_display_image.py: -------------------------------------------------------------------------------- 1 | from microbit import display, Image 2 | import time 3 | 4 | CLOCK = [getattr(Image, 'CLOCK%s' % i) for i in range(1, 13)] 5 | while True: 6 | for image in CLOCK: 7 | display.show(image) 8 | time.sleep(0.1) 9 | -------------------------------------------------------------------------------- /Chapter09/05_display_scrolling.py: -------------------------------------------------------------------------------- 1 | from microbit import display 2 | 3 | TEXT = [ 4 | ('slow', 300), 5 | ('normal', 150), 6 | ('fast', 75), 7 | 8 | ] 9 | 10 | for text, delay in TEXT: 11 | display.scroll(text, delay=delay) 12 | -------------------------------------------------------------------------------- /Chapter09/06_show_button.py: -------------------------------------------------------------------------------- 1 | from microbit import display, button_a, button_b 2 | 3 | while True: 4 | if button_a.is_pressed(): 5 | display.show('a') 6 | elif button_b.is_pressed(): 7 | display.show('b') 8 | else: 9 | display.clear() 10 | -------------------------------------------------------------------------------- /Chapter09/07_countdown.py: -------------------------------------------------------------------------------- 1 | from microbit import display, button_a 2 | import time 3 | 4 | NUMBERS = list(range(9, 0, -1)) 5 | 6 | 7 | def countdown(): 8 | for i in NUMBERS: 9 | display.show(i) 10 | time.sleep(1) 11 | display.clear() 12 | 13 | 14 | while True: 15 | if button_a.is_pressed(): 16 | countdown() 17 | -------------------------------------------------------------------------------- /Chapter10/01_repl.py: -------------------------------------------------------------------------------- 1 | import math 2 | math.pow(8, 2) 3 | 4 | 8**2 5 | -------------------------------------------------------------------------------- /Chapter10/02_scan.py: -------------------------------------------------------------------------------- 1 | import network 2 | station = network.WLAN(network.STA_IF) 3 | 4 | station.active(True) 5 | 6 | networks = station.scan() 7 | 8 | len(networks) 9 | names = [i[0] for i in networks] 10 | -------------------------------------------------------------------------------- /Chapter10/03_ap.py: -------------------------------------------------------------------------------- 1 | import network 2 | ap = network.WLAN(network.AP_IF) 3 | ap.config(essid='PyWifi', password='12345678') 4 | ap.active(True) 5 | ap.ifconfig() 6 | -------------------------------------------------------------------------------- /Chapter10/04_station.py: -------------------------------------------------------------------------------- 1 | import network 2 | station = network.WLAN(network.STA_IF) 3 | station.active(True) 4 | networks = station.scan() 5 | names = [i[0] for i in networks] 6 | 7 | station.connect('MyAmazingWiFi', 'MyAmazingPassword') 8 | station.isconnected() 9 | station.ifconfig() 10 | station.active(False) 11 | -------------------------------------------------------------------------------- /Chapter10/05_webrepl_web.py: -------------------------------------------------------------------------------- 1 | import webrepl_setup 2 | -------------------------------------------------------------------------------- /Chapter10/06_webrepl_cli.py: -------------------------------------------------------------------------------- 1 | import machine 2 | machine.reset() 3 | -------------------------------------------------------------------------------- /Chapter10/07_leds.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import time 3 | 4 | red = Pin(0, Pin.OUT) 5 | blue = Pin(2, Pin.OUT) 6 | while True: 7 | blue.value(0) 8 | red.value(1) 9 | time.sleep(1) 10 | blue.value(1) 11 | red.value(0) 12 | time.sleep(1) 13 | -------------------------------------------------------------------------------- /Chapter11/01_remount.py: -------------------------------------------------------------------------------- 1 | import storage 2 | storage.remount('/', False) 3 | -------------------------------------------------------------------------------- /Chapter11/02_list_files.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | FILE_CODE = 0x8000 4 | 5 | 6 | def isfile(path): 7 | return os.stat(path)[0] == FILE_CODE 8 | 9 | 10 | def main(): 11 | files = [i for i in sorted(os.listdir()) if isfile(i)] 12 | print('files:', files) 13 | dirs = [i for i in sorted(os.listdir()) if not isfile(i)] 14 | print('dirs:', dirs) 15 | 16 | 17 | main() 18 | -------------------------------------------------------------------------------- /Chapter11/03_remove_files.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | FILE_CODE = 0x8000 4 | 5 | 6 | def isfile(path): 7 | return os.stat(path)[0] == FILE_CODE 8 | 9 | 10 | def any_remove(path): 11 | func = os.remove if isfile(path) else os.rmdir 12 | func(path) 13 | -------------------------------------------------------------------------------- /Chapter11/04_create_directories.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def exists(path): 5 | try: 6 | os.stat(path) 7 | except OSError: 8 | return False 9 | return True 10 | 11 | 12 | def mkdir_safe(path): 13 | if not exists(path): 14 | os.mkdir(path) 15 | 16 | 17 | def makedirs(path): 18 | items = path.strip('/').split('/') 19 | count = len(items) 20 | paths = ['/' + '/'.join(items[0:i + 1]) for i in range(count)] 21 | for path in paths: 22 | os.mkdir(path) 23 | -------------------------------------------------------------------------------- /Chapter11/05_read_files.py: -------------------------------------------------------------------------------- 1 | data = open('hi.txt').read() 2 | 3 | data = open('hi.txt', 'rb').read() 4 | -------------------------------------------------------------------------------- /Chapter11/06_write_files.py: -------------------------------------------------------------------------------- 1 | class Path: 2 | def __init__(self, path): 3 | self._path = path 4 | 5 | def __repr__(self): 6 | return "Path(%r)" % self._path 7 | 8 | def write_bytes(self, data): 9 | with open(self._path, 'wb') as f: 10 | return f.write(data) 11 | 12 | def write_text(self, data): 13 | with open(self._path, 'w') as f: 14 | return f.write(data) 15 | 16 | 17 | Path('hi.txt').write_text('hi there') 18 | -------------------------------------------------------------------------------- /Chapter11/07_disk_usage.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def get_disk_stats(): 5 | stats = os.statvfs('/') 6 | block_size = stats[0] 7 | total_blocks = stats[2] 8 | free_blocks = stats[3] 9 | stats = dict() 10 | stats['free'] = block_size * free_blocks 11 | stats['total'] = block_size * total_blocks 12 | stats['used'] = stats['total'] - stats['free'] 13 | return stats 14 | 15 | 16 | def format_size(val): 17 | val = int(val / 1024) # convert bytes to KiB 18 | val = '{:,}'.format(val) # add thousand separator 19 | val = '{0: >6} KiB'.format(val) # right align amounts 20 | return val 21 | 22 | 23 | def print_stats(): 24 | stats = get_disk_stats() 25 | print('free space: ', format_size(stats['free'])) 26 | print('used space: ', format_size(stats['used'])) 27 | print('total space:', format_size(stats['total'])) 28 | 29 | 30 | print_stats() 31 | -------------------------------------------------------------------------------- /Chapter12/01_dns.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import time 3 | 4 | BOOTUP_WIFI_DELAY = 5 5 | 6 | 7 | def get_ip(host, port=80): 8 | addr_info = socket.getaddrinfo(host, port) 9 | return addr_info[0][-1][0] 10 | 11 | 12 | def main(): 13 | print('applying wifi delay...') 14 | time.sleep(BOOTUP_WIFI_DELAY) 15 | print('performing DNS lookup...') 16 | hosts = ['python.org', 'micropython.org', 'pypi.org'] 17 | for host in hosts: 18 | print(host, get_ip(host)) 19 | 20 | 21 | main() 22 | -------------------------------------------------------------------------------- /Chapter12/02_check_network.py: -------------------------------------------------------------------------------- 1 | from netcheck import wait_for_networking 2 | 3 | 4 | def main(): 5 | ip = wait_for_networking() 6 | print('main started') 7 | print('device ip:', ip) 8 | 9 | 10 | main() 11 | -------------------------------------------------------------------------------- /Chapter12/03_http_raw.py: -------------------------------------------------------------------------------- 1 | from netcheck import wait_for_networking 2 | import socket 3 | 4 | HTTP_REQUEST = 'GET /{path} HTTP/1.0\r\nHost: {host}\r\n\r\n' 5 | HTTP_PORT = 80 6 | BUFFER_SIZE = 1024 7 | 8 | 9 | def parse_url(url): 10 | return url.replace('http://', '').split('/', 1) 11 | 12 | 13 | def get_ip(host, port=HTTP_PORT): 14 | addr_info = socket.getaddrinfo(host, port) 15 | return addr_info[0][-1][0] 16 | 17 | 18 | def fetch(url): 19 | host, path = parse_url(url) 20 | ip = get_ip(host) 21 | sock = socket.socket() 22 | sock.connect((ip, 80)) 23 | request = HTTP_REQUEST.format(host=host, path=path) 24 | sock.send(bytes(request, 'utf8')) 25 | response = b'' 26 | while True: 27 | chunk = sock.recv(BUFFER_SIZE) 28 | if not chunk: 29 | break 30 | response += chunk 31 | sock.close() 32 | body = response.split(b'\r\n\r\n', 1)[1] 33 | return str(body, 'utf8') 34 | 35 | 36 | def main(): 37 | wait_for_networking() 38 | html = fetch('http://micropython.org/ks/test.html') 39 | print(html) 40 | 41 | 42 | main() 43 | -------------------------------------------------------------------------------- /Chapter12/04_http_urequests.py: -------------------------------------------------------------------------------- 1 | from netcheck import wait_for_networking 2 | import urequests 3 | 4 | 5 | def main(): 6 | wait_for_networking() 7 | url = 'http://micropython.org/ks/test.html' 8 | html = urequests.get(url).text 9 | print(html) 10 | 11 | 12 | main() 13 | -------------------------------------------------------------------------------- /Chapter12/05_fetch_json.py: -------------------------------------------------------------------------------- 1 | from netcheck import wait_for_networking 2 | import urequests 3 | import time 4 | 5 | ISS_API_URL = 'http://api.open-notify.org/iss-now.json' 6 | 7 | 8 | def track_space_station(): 9 | for i in range(10): 10 | data = urequests.get(ISS_API_URL).json() 11 | position = data['iss_position'] 12 | print(i, 'lat: {latitude} long: {longitude}'.format(**position)) 13 | time.sleep(1) 14 | 15 | 16 | def main(): 17 | wait_for_networking() 18 | track_space_station() 19 | 20 | main() 21 | -------------------------------------------------------------------------------- /Chapter12/06_http_server.py: -------------------------------------------------------------------------------- 1 | from netcheck import wait_for_networking 2 | import socket 3 | import time 4 | 5 | HTTP_PORT = 80 6 | TCP_BACKLOG = 0 7 | TEMPLATE = """\ 8 | 9 | 10 |
11 |