macropad/code.py

128 lines
3.9 KiB
Python
Raw Normal View History

2023-11-29 21:35:15 -06:00
# import rtc
# import time
# import board
# import digitalio
# import time
# import neopixel
# from adafruit_datetime import datetime
# from board import *
# import time
# pixels = neopixel.NeoPixel(NEOPIXEL, 12)
# RED = 0x100000
# while True:
# for i in range(len(pixels)):
# pixels[i] = RED
# tz = "America/Chicago"
# rtc.datetime = time.struct_time((2017, 10, 29, 15, 14, 15, 0, -1, -1))
# TODO: for proper datetime reporting, may need to regularly be updated with tzdata?
# TODO: set all LEDs (neopixels?) to off (board.NEOPIXEL, board.LED)
# TODO: clear OLED (sh1106)
# TODO: mute speaker (board.SPEAKER)
# TODO: update clock every half-second or so (for blinking colon?)
# TODO: check out the repl over the serial console! (^C)
# then try help() and help("modules")
# import board; dir(board)
# print("Hello, Mom!")
# print("Cur datetime:", datetime.now())
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""Keypad and rotary encoder example for Adafruit MacroPad"""
import board
import digitalio
import rotaryio
import neopixel
import keypad
import random
import math
import displayio
import array
import time
try:
from audiocore import RawSample
except ImportError:
from audioio import RawSample
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass # not always supported by every board!
# palette = displayio.Palette(3)
# palette[0] = 0x000000
# palette[1] = 0x888888
# palette[2] = 0xFFFFFF
# bitmap = displayio.Bitmap(128, 64, 3)
# bitmap[23, 42] = 2
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.direction = digitalio.Direction.OUTPUT
speaker_enable.value = False
wave_freqs = [440 + (x * 2) for x in range(13)]
waves = []
wave_samples = []
SAMPLERATE = 8000
for i in range(len(wave_freqs)):
length = int(SAMPLERATE // wave_freqs[i])
waves.append(array.array("H", [0] * length))
print(wave_freqs[i], length)
for j in range(length):
waves[i][j] = int(math.sin(math.pi * 2 * j / length) * (2 ** 15) + 2 ** 15)
wave_samples.append(RawSample(waves[i]))
audio = AudioOut(board.SPEAKER)
# A single sine wave sample is hundredths of a second long. If you set loop=False, it will play
# a single instance of the sample (a quick burst of sound) and then silence for the rest of the
# duration of the time.sleep(). If loop=True, it will play the single instance of the sample
# continuously for the duration of the time.sleep().
from rainbowio import colorwheel
key_pins = (board.KEY1, board.KEY2, board.KEY3, board.KEY4, board.KEY5, board.KEY6, board.KEY7, board.KEY8, board.KEY9, board.KEY10, board.KEY11, board.KEY12)
keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True)
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=1.0)
for n in range(len(pixels)):
pixels[n] = 0x101010
init = random.randrange(0, 255)
last_position = 0
color_value = (init + (last_position * 6)) % 255
pixels[4] = colorwheel(color_value)
while True:
if not button.value:
pixels.brightness = 0.0
for i in range(len(pixels)):
pixels[i] = 0x000000
else:
pixels.brightness = 1.0
position = encoder.position
if position != last_position:
print("Rotary:", position)
last_position = position
color_value = (init + (last_position * 6)) % 255
event = keys.events.get()
if event:
print(event)
if event.pressed:
pixels[event.key_number] = colorwheel(color_value)
audio.play(wave_samples[event.key_number], loop=True) # Play the single sine_wave sample continuously...
else:
audio.stop() # and then stop.
# pixels[event.key_number] = 0
2023-11-25 17:17:36 -06:00