shipping demo for metro m4

This commit is contained in:
ladyada 2019-03-28 16:42:12 -04:00
parent 02637176d5
commit 9ecf2b15ca
25 changed files with 216 additions and 0 deletions

View file

@ -0,0 +1,63 @@
Welcome to CircuitPython!
#############################
Visit the Metro M4 product page here for more info:
https://adafruit.com/product/3382
To get started with CircuitPython, which comes built into your Metro, visit:
https://learn.adafruit.com/welcome-to-circuitpython
#############################
The Metro has a small disk drive so we have disabled Mac OS X indexing
which could take up that valuable space.
So *please* do not remove the empty .fseventsd/no_log, .metadata_never_index
or .Trashes files!
#############################
The pre-loaded demo shows off what your Metro M0 can do with CircuitPython:
* The built in NeoPixel LED can show any color, it will swirl through the rainbow
* Pin A0 is a true analog output, when buttons on D3 or D4 are pressed to
ground audio files will play from the onboard storage to A0 (You'll need to
wire up a speaker or headphones to hear it!
* Pin A1 is an analog input, the REPL will display the voltage on this pin
(0-3.3V is the max range)
* Pin D2, D3 and D4 are digital inputs with pull-ups, you can touch them to
GND to activate button (or wire up a tactile button or switch!)
- If you update code.py to uncomment the relevant lines, D2 will act as a
mini keyboard and emulate an 'a' key-press whenever D2 is grounded.
- When D3 and D4 are grounded, audio files will play out of A0
* Pin D5 can be connected to a servo which will sweep back and forth. Power
the servo from 5V and GND.
* Pin D6 is a NeoPixel output, you can wire up a strip of NeoPixels to this
pin (power from USB and GND). The first 16 NeoPixel will rainbow swirl
For more details on how to use CircuitPython, visit
https://adafruit.com/product/3382
and check out all the tutorials we have!
#############################
CircuitPython Quick Start:
Changing the code is as easy as editing code.py in your favorite text editor.
Our recommended editor is Mu, which is great for simple projects, and comes
with a built in REPL serial viewer! It is available for Mac, Windows & Linux
https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor
After the file is saved, CircuitPython will automatically reload the latest
code. Try enabling the single-button keyboard!
(HINT: look for the "# optional! uncomment below..." text)
Connecting to the serial port will give you access to sensor information,
better error messages and an interactive CircuitPython (known as the REPL).
On Windows we recommend Mu, Tera Term or PuTTY.
On Mac OSX and Linux, use Mu or 'screen' can be used from a terminal.

View file

@ -0,0 +1,123 @@
# Metro M4 IO demo
# Welcome to CircuitPython 3.1 :)
import time
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
from adafruit_motor import servo
import neopixel
import audioio
import pulseio
import simpleio
# keyboard support
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# One pixel connected internally!
dot = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Analog audio output on A0, using two audio files
audio = audioio.AudioOut(board.A0)
audiofiles = ["rimshot.wav", "laugh.wav"]
# Analog input on A1
analog1in = AnalogIn(board.A1)
# Digital input with pullup on D2, D3, and D4
buttons = []
for p in [board.D2, board.D3, board.D4]:
button = DigitalInOut(p)
button.direction = Direction.INPUT
button.pull = Pull.UP
buttons.append(button)
# Servo on D5
# create a PWMOut object on Pin D5
pwm = pulseio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50)
servo = servo.Servo(pwm)
# NeoPixel strip (of 16 LEDs) connected on D6
NUMPIXELS = 16
neopixels = neopixel.NeoPixel(board.D6, NUMPIXELS, brightness=0.2, auto_write=False)
# Used if we do HID output, see below
kbd = Keyboard()
######################### HELPERS ##############################
# Helper to convert analog input to voltage
def getVoltage(pin):
return (pin.value * 3.3) / 65536
# Helper to give us a nice color swirl
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0):
return [0, 0, 0]
if (pos > 255):
return [0, 0, 0]
if (pos < 85):
return [int(pos * 3), int(255 - (pos*3)), 0]
elif (pos < 170):
pos -= 85
return [int(255 - pos*3), 0, int(pos*3)]
else:
pos -= 170
return [0, int(pos*3), int(255 - pos*3)]
def play_file(filename):
print("")
print("----------------------------------")
print("playing file "+filename)
with open(filename, "rb") as wave_file:
wave = audioio.WaveFile(wave_file)
audio.play(wave)
while audio.playing:
pass
print("finished")
print("----------------------------------")
######################### MAIN LOOP ##############################
i = 0
while True:
# spin internal LED around! autoshow is on
dot[0] = wheel(i & 255)
# also make the neopixels swirl around
for p in range(NUMPIXELS):
idx = int ((p * 256 / NUMPIXELS) + i)
neopixels[p] = wheel(idx & 255)
neopixels.show()
# Read analog voltage on A1
print("A1: %0.2f" % getVoltage(analog1in), end="\t")
if not buttons[0].value:
print("Button D2 pressed!", end ="\t")
# optional! uncomment below & save to have it sent a keypress
#kbd.press(Keycode.A)
#kbd.release_all()
if not buttons[1].value:
print("Button D3 pressed!", end ="\t")
play_file(audiofiles[0])
if not buttons[2].value:
print("Button D4 pressed!", end ="\t")
play_file(audiofiles[1])
# sweep a servo from 0-180 degrees (map from 0-255)
servo.angle = simpleio.map_range(i, 0, 255, 0, 180)
i = (i+1) % 256 # run from 0 to 255
#time.sleep(0.01) # make bigger to slow down
print("")

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,30 @@
# The MIT License (MIT)
#
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_hid`
====================================================
This driver simulates USB HID devices. Currently keyboard and mouse are implemented.
* Author(s): Scott Shawcroft, Dan Halbert
"""

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.