fun light game

This commit is contained in:
Daniel Flanagan 2023-11-29 21:35:15 -06:00
parent b34f557c64
commit 5f905156c2
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
6 changed files with 143 additions and 351 deletions

6
.gitignore vendored
View file

@ -6,3 +6,9 @@
# nix flake
/.direnv
# vendored libs
# TODO: script out retrieval of these?
/requirements
/lib
/examples

129
code.py
View file

@ -1,4 +1,127 @@
from adafruit_datetime import datetime, date, time
# 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
print("Hello, Mom!")
print("Cur datetime:", datetime.now())

View file

@ -1,62 +1,5 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nix-github-actions": {
"inputs": {
"nixpkgs": [
"poetry2nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1698974481,
"narHash": "sha256-yPncV9Ohdz1zPZxYHQf47S8S0VrnhV7nNhCawY46hDA=",
"owner": "nix-community",
"repo": "nix-github-actions",
"rev": "4bb5e752616262457bc7ca5882192a564c0472d2",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nix-github-actions",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1700794826,
@ -73,100 +16,9 @@
"type": "github"
}
},
"poetry2nix": {
"inputs": {
"flake-utils": "flake-utils_2",
"nix-github-actions": "nix-github-actions",
"nixpkgs": [
"nixpkgs"
],
"systems": "systems_3",
"treefmt-nix": "treefmt-nix"
},
"locked": {
"lastModified": 1700890240,
"narHash": "sha256-AKbWnuDzDuXaYSXHXSj0Sa1DSmUm1KaFNJhf2MEhoS0=",
"owner": "nix-community",
"repo": "poetry2nix",
"rev": "674fc0ef18bb62f3aea7684e09f20046a3cdfedf",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "poetry2nix",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"poetry2nix": "poetry2nix"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_3": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"id": "systems",
"type": "indirect"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"poetry2nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1699786194,
"narHash": "sha256-3h3EH1FXQkIeAuzaWB+nK0XK54uSD46pp+dMD3gAcB4=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "e82f32aa7f06bbbd56d7b12186d555223dc399d1",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
"nixpkgs": "nixpkgs"
}
}
},

View file

@ -1,34 +1,16 @@
{
description = "Application packaged using poetry2nix";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = {
self,
nixpkgs,
flake-utils,
poetry2nix,
}:
flake-utils.lib.eachDefaultSystem (system: let
# see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
inherit (poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication;
# lib = nixpkgs.lib;
in {
packages = {
myapp = mkPoetryApplication {projectDir = self;};
default = self.packages.${system}.myapp;
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [circup] ++ (with pkgs.python311Packages; [python-lsp-server]);
};
devShells.default = pkgs.mkShell {
inputsFrom = [self.packages.${system}.myapp];
packages = with pkgs; [poetry] ++ (with pkgs.python311Packages; [python-lsp-server]);
};
});
}

156
poetry.lock generated
View file

@ -1,156 +0,0 @@
# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand.
[[package]]
name = "adafruit-blinka"
version = "8.25.0"
description = "CircuitPython APIs for non-CircuitPython versions of Python such as CPython on Linux and MicroPython."
optional = false
python-versions = ">=3.7.0"
files = [
{file = "Adafruit-Blinka-8.25.0.tar.gz", hash = "sha256:4cae1655a60c341d97e59108c0fa4f3963e8373b6195381e3b7f76ce1f4c2a5b"},
{file = "Adafruit_Blinka-8.25.0-py3-none-any.whl", hash = "sha256:6ccb124143bfc1073af7225445c75781e5415ecb97484bd24a9e0d9dbfb90816"},
]
[package.dependencies]
adafruit-circuitpython-typing = "*"
Adafruit-PlatformDetect = ">=3.53.0"
Adafruit-PureIO = ">=1.1.7"
pyftdi = ">=0.40.0"
[[package]]
name = "adafruit-circuitpython-busdevice"
version = "5.2.6"
description = "CircuitPython bus device classes to manage bus sharing."
optional = false
python-versions = "*"
files = [
{file = "adafruit-circuitpython-busdevice-5.2.6.tar.gz", hash = "sha256:ed06f5552e5567b0c89589c5bc6ef3adcac67d59eb505ce9127af99f33c2bc90"},
{file = "adafruit_circuitpython_busdevice-5.2.6-py3-none-any.whl", hash = "sha256:9f25577843f0a338a0936a1b57436f4451f7783b38e3cf46160b6be78faeaa44"},
]
[package.dependencies]
Adafruit-Blinka = ">=7.0.0"
adafruit-circuitpython-typing = "*"
[[package]]
name = "adafruit-circuitpython-datetime"
version = "1.2.6"
description = "Subset of CPython datetime module"
optional = false
python-versions = "*"
files = [
{file = "adafruit-circuitpython-datetime-1.2.6.tar.gz", hash = "sha256:f54867c184d8ae8295c66085893870370470350afebe720a52f715124f556236"},
{file = "adafruit_circuitpython_datetime-1.2.6-py3-none-any.whl", hash = "sha256:47c70ac4ed63b070136b1a211d3c47e1cce7ae99a5fced4d03b804cbd6db08f1"},
]
[package.dependencies]
Adafruit-Blinka = "*"
[[package]]
name = "adafruit-circuitpython-requests"
version = "2.0.2"
description = "A requests-like library for web interfacing"
optional = false
python-versions = "*"
files = [
{file = "adafruit-circuitpython-requests-2.0.2.tar.gz", hash = "sha256:fec34d21be9d721a44bd1471e3651630eb7d394ce806de2fb39536dac73408aa"},
{file = "adafruit_circuitpython_requests-2.0.2-py3-none-any.whl", hash = "sha256:91e7634cd223ee3adf22ca3cf6e8f34713a56a3809031f37ec31b90a3a9f503f"},
]
[package.dependencies]
Adafruit-Blinka = "*"
[[package]]
name = "adafruit-circuitpython-typing"
version = "1.9.5"
description = "Types needed for type annotation that are not in `typing`"
optional = false
python-versions = "*"
files = [
{file = "adafruit-circuitpython-typing-1.9.5.tar.gz", hash = "sha256:6a2a7a4f60d54348f3c4aad8f4dd0d0f0aaf9c854ddd1761b20d5146440faa1d"},
{file = "adafruit_circuitpython_typing-1.9.5-py3-none-any.whl", hash = "sha256:02625de1f8fdeb42db5a88999d2dc7a905339b30c673f7a2d8608b840e8520cc"},
]
[package.dependencies]
Adafruit-Blinka = "*"
adafruit-circuitpython-busdevice = "*"
adafruit-circuitpython-requests = "*"
typing-extensions = ">=4.0,<5.0"
[[package]]
name = "adafruit-platformdetect"
version = "3.54.0"
description = "Platform detection for use by libraries like Adafruit-Blinka."
optional = false
python-versions = "*"
files = [
{file = "Adafruit-PlatformDetect-3.54.0.tar.gz", hash = "sha256:3fe7fb781a83d3f28812bcbff34edd4100afb68901d9c62ee22d07e824d82168"},
{file = "Adafruit_PlatformDetect-3.54.0-py3-none-any.whl", hash = "sha256:fecae7a6612bbcd32d1f40c2d2f1aa263c554be5e52cc6fa4eebaf8b80c33519"},
]
[[package]]
name = "adafruit-pureio"
version = "1.1.11"
description = "Pure python (i.e. no native extensions) access to Linux IO including I2C and SPI. Drop in replacement for smbus and spidev modules."
optional = false
python-versions = ">=3.5.0"
files = [
{file = "Adafruit_PureIO-1.1.11-py3-none-any.whl", hash = "sha256:281ab2099372cc0decc26326918996cbf21b8eed694ec4764d51eefa029d324e"},
{file = "Adafruit_PureIO-1.1.11.tar.gz", hash = "sha256:c4cfbb365731942d1f1092a116f47dfdae0aef18c5b27f1072b5824ad5ea8c7c"},
]
[[package]]
name = "pyftdi"
version = "0.55.0"
description = "FTDI device driver (pure Python)"
optional = false
python-versions = ">=3.8"
files = [
{file = "pyftdi-0.55.0-py3-none-any.whl", hash = "sha256:6af7b6c73da1256fd5140076fe77a616c13e44dd0779dd561dac1b5981dbd43f"},
{file = "pyftdi-0.55.0.tar.gz", hash = "sha256:a747bbbccc4eeea26cefa2c8bd3d2b8bef8c94ecb6969bb9c75a63640887519a"},
]
[package.dependencies]
pyserial = ">=3.0"
pyusb = ">=1.0.0,<1.2.0 || >1.2.0"
[[package]]
name = "pyserial"
version = "3.5"
description = "Python Serial Port Extension"
optional = false
python-versions = "*"
files = [
{file = "pyserial-3.5-py2.py3-none-any.whl", hash = "sha256:c4451db6ba391ca6ca299fb3ec7bae67a5c55dde170964c7a14ceefec02f2cf0"},
{file = "pyserial-3.5.tar.gz", hash = "sha256:3c77e014170dfffbd816e6ffc205e9842efb10be9f58ec16d3e8675b4925cddb"},
]
[package.extras]
cp2110 = ["hidapi"]
[[package]]
name = "pyusb"
version = "1.2.1"
description = "Python USB access module"
optional = false
python-versions = ">=3.6.0"
files = [
{file = "pyusb-1.2.1-py3-none-any.whl", hash = "sha256:2b4c7cb86dbadf044dfb9d3a4ff69fd217013dbe78a792177a3feb172449ea36"},
{file = "pyusb-1.2.1.tar.gz", hash = "sha256:a4cc7404a203144754164b8b40994e2849fde1cfff06b08492f12fff9d9de7b9"},
]
[[package]]
name = "typing-extensions"
version = "4.8.0"
description = "Backported and Experimental Type Hints for Python 3.8+"
optional = false
python-versions = ">=3.8"
files = [
{file = "typing_extensions-4.8.0-py3-none-any.whl", hash = "sha256:8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0"},
{file = "typing_extensions-4.8.0.tar.gz", hash = "sha256:df8e4339e9cb77357558cbdbceca33c303714cf861d1eef15e1070055ae8b7ef"},
]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "222a24d7a18347e2035b793fd793724c3457b8b2a897e2385917d6ee0b063c71"

View file

@ -1,15 +0,0 @@
[tool.poetry]
name = "macropad"
version = "1.0.0-alpha"
description = ""
authors = ["Daniel Flanagan <daniel@lyte.dev>"]
license = "GPL"
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
adafruit-circuitpython-datetime = "^1.2.6"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"