From 2bb1814e8df814a3ae170431a28363242dd82b91 Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 28 Sep 2017 21:30:17 -0400 Subject: [PATCH] demos --- Introducing_Gemma_M0/Gemma_AnalogIn.py | 24 +++++++++ Introducing_Gemma_M0/Gemma_AnalogOut.py | 13 +++++ Introducing_Gemma_M0/Gemma_CapTouch.py | 18 +++++++ Introducing_Gemma_M0/Gemma_DotStar.py | 51 ++++++++++++++++++ Introducing_Gemma_M0/Gemma_HIDkeyboard.py | 58 +++++++++++++++++++++ Introducing_Gemma_M0/Gemma_I2CScan.py | 15 ++++++ Introducing_Gemma_M0/Gemma_I2Csi7021.py | 19 +++++++ Introducing_Gemma_M0/Gemma_NeoPixel.py | 54 +++++++++++++++++++ Introducing_Gemma_M0/Gemma_Touch_DotStar.py | 35 +++++++++++++ Introducing_Gemma_M0/Gemma_UART.py | 23 ++++++++ 10 files changed, 310 insertions(+) create mode 100644 Introducing_Gemma_M0/Gemma_AnalogIn.py create mode 100644 Introducing_Gemma_M0/Gemma_AnalogOut.py create mode 100644 Introducing_Gemma_M0/Gemma_CapTouch.py create mode 100644 Introducing_Gemma_M0/Gemma_DotStar.py create mode 100644 Introducing_Gemma_M0/Gemma_HIDkeyboard.py create mode 100644 Introducing_Gemma_M0/Gemma_I2CScan.py create mode 100644 Introducing_Gemma_M0/Gemma_I2Csi7021.py create mode 100644 Introducing_Gemma_M0/Gemma_NeoPixel.py create mode 100644 Introducing_Gemma_M0/Gemma_Touch_DotStar.py create mode 100644 Introducing_Gemma_M0/Gemma_UART.py diff --git a/Introducing_Gemma_M0/Gemma_AnalogIn.py b/Introducing_Gemma_M0/Gemma_AnalogIn.py new file mode 100644 index 000000000..bee72d1e1 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_AnalogIn.py @@ -0,0 +1,24 @@ +# Gemma IO demo - analog inputs + +from digitalio import DigitalInOut, Direction +from analogio import AnalogIn +import board +import time + +led = DigitalInOut(board.L) +led.direction = Direction.OUTPUT + +analog0in = AnalogIn(board.A0) +analog1in = AnalogIn(board.A1) +analog2in = AnalogIn(board.A2) + +def getVoltage(pin): + return (pin.value * 3.3) / 65536 + +while True: + print("A0: %f \t\t A1: %f \t\t A2: %f" % + (getVoltage(analog0in), + getVoltage(analog1in), + getVoltage(analog2in))) + + time.sleep(0.1) diff --git a/Introducing_Gemma_M0/Gemma_AnalogOut.py b/Introducing_Gemma_M0/Gemma_AnalogOut.py new file mode 100644 index 000000000..fd0fa41cd --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_AnalogOut.py @@ -0,0 +1,13 @@ +# Gemma IO demo - analog output + +from analogio import AnalogOut +import board +import time + +aout = AnalogOut(board.A0) + +while True: + # Count up from 0 to 65535, with 64 increment + # which ends up corresponding to the DAC's 10-bit range + for i in range (0,65535,64): + aout.value = i diff --git a/Introducing_Gemma_M0/Gemma_CapTouch.py b/Introducing_Gemma_M0/Gemma_CapTouch.py new file mode 100644 index 000000000..0f4f7dcd2 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_CapTouch.py @@ -0,0 +1,18 @@ +# Gemma IO demo - captouch + +import touchio +import board +import time + +touch0 = touchio.TouchIn(board.A0) +touch1 = touchio.TouchIn(board.A1) +touch2 = touchio.TouchIn(board.A2) + +while True: + if touch0.value: + print("A0 touched!") + if touch1.value: + print("A1 touched!") + if touch2.value: + print("A2 touched!") + time.sleep(0.01) diff --git a/Introducing_Gemma_M0/Gemma_DotStar.py b/Introducing_Gemma_M0/Gemma_DotStar.py new file mode 100644 index 000000000..dcc38a110 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_DotStar.py @@ -0,0 +1,51 @@ +# Gemma IO demo - Dotstar + +from digitalio import * +from board import * +import dotstar +import time + +numpix = 64 +strip = dotstar.DotStar(D2, D0, numpix, brightness=0.2) + +led = DigitalInOut(D13) +led.direction = Direction.OUTPUT + +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 rainbow_cycle(wait): + for j in range(255): + for i in range(len(strip)): + idx = int ((i * 256 / len(strip)) + j) + strip[i] = wheel(idx & 255) + strip.write() + time.sleep(wait) + +while True: + strip.fill([255, 0, 0]) + strip.write() + time.sleep(1) + + strip.fill([0, 255, 0]) + strip.write() + time.sleep(1) + + strip.fill([0, 0, 255]) + strip.write() + time.sleep(1) + + rainbow_cycle(0) diff --git a/Introducing_Gemma_M0/Gemma_HIDkeyboard.py b/Introducing_Gemma_M0/Gemma_HIDkeyboard.py new file mode 100644 index 000000000..a15f4d696 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_HIDkeyboard.py @@ -0,0 +1,58 @@ +# Gemma IO demo - Keyboard emu + +import digitalio +import touchio +from board import * +import time +from adafruit_hid.keyboard import Keyboard +from adafruit_hid.keycode import Keycode + +# A simple neat keyboard demo in circuitpython + +# The button pins we'll use, each will have an internal pullup +buttonpins = [D2, D0] +# our array of button objects +buttons = [] +# One pin will be capacitive touch +ptcbutton = touchio.TouchIn(D1) + +# The keycode sent for each button, will be paired with a control key +buttonkeys = [Keycode.A, Keycode.B, Keycode.C] +controlkey = Keycode.SHIFT + +# the keyboard object! +kbd = Keyboard() + +# make all pin objects, make them inputs w/pullups +for pin in buttonpins: + button = digitalio.DigitalInOut(pin) + button.direction = digitalio.Direction.INPUT + button.pull = digitalio.Pull.UP + buttons.append(button) + +led = digitalio.DigitalInOut(D13) +led.direction = digitalio.Direction.OUTPUT + +print("Waiting for button presses") + +while True: + # check each button + for button in buttons: + if (not button.value): # pressed? + i = buttons.index(button) + print("Button #%d Pressed" % i) + + # turn on the LED + led.value = True + + while (not button.value): + pass # wait for it to be released! + # type the keycode! + k = buttonkeys[i] # get the corresp. keycode + kbd.press(controlkey, k) + kbd.release_all() + + # turn off the LED + led.value = False + + time.sleep(0.01) diff --git a/Introducing_Gemma_M0/Gemma_I2CScan.py b/Introducing_Gemma_M0/Gemma_I2CScan.py new file mode 100644 index 000000000..78d34209a --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_I2CScan.py @@ -0,0 +1,15 @@ +# Gemma IO demo - I2C scan + +from digitalio import * +from board import * +import busio +import time + +led = DigitalInOut(D13) +led.direction = Direction.OUTPUT + +i2c = busio.I2C(D2, D0) + +while True: + print("I2C addresses found:", [hex(i) for i in i2c.scan()]) + time.sleep(2) diff --git a/Introducing_Gemma_M0/Gemma_I2Csi7021.py b/Introducing_Gemma_M0/Gemma_I2Csi7021.py new file mode 100644 index 000000000..17a20b071 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_I2Csi7021.py @@ -0,0 +1,19 @@ +# Gemma M0 IO Demo - I2C demo + +from digitalio import * +from board import * +import busio +import adafruit_si7021 +import time + +led = DigitalInOut(D13) +led.direction = Direction.OUTPUT + +i2c = busio.I2C(D2, D0) +print("I2C addresses found:", [hex(i) for i in i2c.scan()]) + +si7021 = adafruit_si7021.SI7021(i2c) + +while True: + print("Temp: %0.2F *C Humidity: %0.1F %%" % (si7021.temperature, si7021.relative_humidity)) + time.sleep(1) diff --git a/Introducing_Gemma_M0/Gemma_NeoPixel.py b/Introducing_Gemma_M0/Gemma_NeoPixel.py new file mode 100644 index 000000000..2c33e7cdd --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_NeoPixel.py @@ -0,0 +1,54 @@ +# Gemma IO demo - NeoPixel + +from digitalio import * +from board import * +import neopixel +import time + +pixpin = D1 +numpix = 10 + +led = DigitalInOut(D13) +led.direction = Direction.OUTPUT + +strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.3) + + +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 rainbow_cycle(wait): + for j in range(255): + for i in range(len(strip)): + idx = int ((i * 256 / len(strip)) + j) + strip[i] = wheel(idx & 255) + strip.write() + time.sleep(wait) + +while True: + strip.fill([255, 0, 0]) + strip.write() + time.sleep(0.2) + + strip.fill([0, 255, 0]) + strip.write() + time.sleep(0.2) + + strip.fill([0, 0, 255]) + strip.write() + time.sleep(0.2) + + rainbow_cycle(0.001) diff --git a/Introducing_Gemma_M0/Gemma_Touch_DotStar.py b/Introducing_Gemma_M0/Gemma_Touch_DotStar.py new file mode 100644 index 000000000..7d8b7436b --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_Touch_DotStar.py @@ -0,0 +1,35 @@ +# Gemma IO demo - captouch to dotstar + +import touchio +import busio +import board +import time + +dotstar = busio.SPI(board.APA102_SCK, board.APA102_MOSI) +touch0 = touchio.TouchIn(board.A0) +touch1 = touchio.TouchIn(board.A1) +touch2 = touchio.TouchIn(board.A2) + +r = g = b = 0 + +def setPixel(red, green, blue): + if not dotstar.try_lock(): + return + print("setting pixel to: %d %d %d" % (red, green, blue)) + + data = bytearray([0x00, 0x00, 0x00, 0x00, + 0xff, blue, green, red, + 0xff, 0xff, 0xff, 0xff]) + dotstar.write(data) + dotstar.unlock() + time.sleep(0.01) + +while True: + if touch0.value: + r = (r+1) % 256 + if touch1.value: + g = (g+1) % 256 + if touch2.value: + b = (b+1) % 256 + + setPixel(r, g, b) diff --git a/Introducing_Gemma_M0/Gemma_UART.py b/Introducing_Gemma_M0/Gemma_UART.py new file mode 100644 index 000000000..2dce92bf9 --- /dev/null +++ b/Introducing_Gemma_M0/Gemma_UART.py @@ -0,0 +1,23 @@ +# Gemma IO demo - USB/Serial echo + +from digitalio import * +from board import * +import busio +import time + +led = DigitalInOut(D13) +led.direction = Direction.OUTPUT + +uart = busio.UART(D0, D2, baudrate=9600) + +while True: + data = uart.read(32) # read up to 32 bytes + #print(data) # this is a bytearray type + + if data != None: + led.value = True + + datastr = ''.join([chr(b) for b in data]) # convert bytearray to string + print(datastr, end="") + + led.value = False