From 1872a35e26822effbbf3a4ab4c4572380a9ca33a Mon Sep 17 00:00:00 2001 From: Piotr Gaczkowski Date: Tue, 12 Nov 2024 20:44:21 +0100 Subject: [PATCH] feat: Initial commit --- .envrc | 8 ++ .github/workflows/check.yaml | 19 +++ .gitignore | 135 +++++++++++++++++++++ LICENSE | 7 ++ flake.lock | 224 +++++++++++++++++++++++++++++++++++ flake.nix | 141 ++++++++++++++++++++++ src/code.py | 123 +++++++++++++++++++ src/keyboard.py | 31 +++++ src/requirements.txt | 3 + 9 files changed, 691 insertions(+) create mode 100644 .envrc create mode 100644 .github/workflows/check.yaml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/code.py create mode 100644 src/keyboard.py create mode 100644 src/requirements.txt diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..6aebd4a --- /dev/null +++ b/.envrc @@ -0,0 +1,8 @@ +#!/bin/bash + +# This is a better (faster) alternative to the built-in Nix support +if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4=" +fi + +use flake diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 0000000..88c1eaa --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,19 @@ +name: Validate Nix Flake +on: + workflow_dispatch: + push: +jobs: + check-flake: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install Lix + uses: DeterminateSystems/nix-installer-action@main + with: + source-url: https://install.lix.systems/lix/lix-installer-x86_64-linux + logger: pretty + - name: Nix Magic Cache + uses: DeterminateSystems/magic-nix-cache-action@main + - name: Check Flake + run: nix flake check diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db03374 --- /dev/null +++ b/.gitignore @@ -0,0 +1,135 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Generated by git-commit.nix +.pre-commit-config.yaml + +# Used by direnv +.direnv diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..12eb6c3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2024 Hackerspace Trójmiasto + +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. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..bf60e2f --- /dev/null +++ b/flake.lock @@ -0,0 +1,224 @@ +{ + "nodes": { + "crane": { + "inputs": { + "nixpkgs": [ + "flake-checker", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1717383740, + "narHash": "sha256-559HbY4uhNeoYvK3H6AMZAtVfmR3y8plXZ1x6ON/cWU=", + "rev": "b65673fce97d277934488a451724be94cc62499a", + "revCount": 580, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/ipetkov/crane/0.17.3/018fdc0e-176b-7a0f-92ce-cc2d0db7b735/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/ipetkov/crane/0.17.%2A" + } + }, + "flake-checker": { + "inputs": { + "crane": "crane", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1729609536, + "narHash": "sha256-qFDvEKKIJfF5WndMCB2nDxBQPyW/J5pAvf0eaEaLAyE=", + "owner": "DeterminateSystems", + "repo": "flake-checker", + "rev": "9d532f92777a7ce6e0cab264867219fdf0a03d30", + "type": "github" + }, + "original": { + "owner": "DeterminateSystems", + "repo": "flake-checker", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1730504689, + "narHash": "sha256-hgmguH29K2fvs9szpq2r3pz2/8cJd2LPS+b4tfNFCwE=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "506278e768c2a08bec68eb62932193e341f55c90", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": [], + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": [] + }, + "locked": { + "lastModified": 1730302582, + "narHash": "sha256-W1MIJpADXQCgosJZT8qBYLRuZls2KSiKdpnTVdKBuvU=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "af8a16fe5c264f5e9e18bcee2859b40a656876cf", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1729307008, + "narHash": "sha256-QUvb6epgKi9pCu9CttRQW4y5NqJ+snKr1FZpG/x3Wtc=", + "rev": "a9b86fc2290b69375c5542b622088eb6eca2a7c3", + "revCount": 636009, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2405.636009%2Brev-a9b86fc2290b69375c5542b622088eb6eca2a7c3/0192adf6-8ea6-72ef-aeee-39631619cc73/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/NixOS/nixpkgs/0.2405.%2A" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1730651795, + "narHash": "sha256-XGYmN3WdyGU8FasWLPjL1Yvm9L9GJ0h62fMgCOPyvo0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5012ef7926747f739c65bd2e1ceff96da30fb3b8", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-checker": "flake-checker", + "flake-parts": "flake-parts", + "flake-utils": "flake-utils", + "git-hooks": "git-hooks", + "nixpkgs": "nixpkgs_2", + "treefmt-nix": "treefmt-nix" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "flake-checker", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1729477859, + "narHash": "sha256-r0VyeJxy4O4CgTB/PNtfQft9fPfN1VuGvnZiCxDArvg=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "ada8266712449c4c0e6ee6fcbc442b3c217c79e1", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1730321837, + "narHash": "sha256-vK+a09qq19QNu2MlLcvN4qcRctJbqWkX7ahgPZ/+maI=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "746901bb8dba96d154b66492a29f5db0693dbfcc", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..db8cb37 --- /dev/null +++ b/flake.nix @@ -0,0 +1,141 @@ +{ + description = "L’esprit de l’escalier"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05-small"; + flake-utils.url = "github:numtide/flake-utils"; + + flake-parts = { + type = "github"; + owner = "hercules-ci"; + repo = "flake-parts"; + inputs.nixpkgs-lib.follows = "nixpkgs"; + }; + + # this adds pre commit hooks via nix to our repo + git-hooks = { + type = "github"; + owner = "cachix"; + repo = "git-hooks.nix"; + + inputs = { + nixpkgs.follows = "nixpkgs"; + nixpkgs-stable.follows = ""; + flake-compat.follows = ""; + }; + }; + + flake-checker = { + type = "github"; + owner = "DeterminateSystems"; + repo = "flake-checker"; + }; + + # a tree-wide formatter + treefmt-nix = { + type = "github"; + owner = "numtide"; + repo = "treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + outputs = + inputs@{ self, ... }: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + imports = with inputs; [ + git-hooks.flakeModule + treefmt-nix.flakeModule + ]; + + perSystem = + { + pkgs, + lib, + config, + system, + ... + }: + let + # + # don't check these + excludes = [ "flake.lock" ]; + + mkHook = + prev: + lib.attrsets.recursiveUpdate { + inherit excludes; + enable = true; + fail_fast = true; + verbose = true; + } prev; + in + with pkgs; + { + devShells.default = mkShell { + inherit (self.checks.${system}.pre-commit-check) shellHook; + buildInputs = [ + self.checks.${system}.pre-commit-check.enabledPackages + circup + ]; + }; + + checks = { + pre-commit-check = inputs.git-hooks.lib.${system}.run { + src = ./.; + hooks = { + # make sure our nix code is of good quality before we commit + statix = mkHook { }; + deadnix = mkHook { }; + flake-checker = mkHook { package = inputs.flake-checker.packages.${system}.flake-checker; }; + + # ensure we have nice formatting + treefmt = mkHook { package = config.treefmt.build.wrapper; }; + + # Git police + check-merge-conflicts = mkHook { }; + commitizen = mkHook { }; + + # Various Artists + check-added-large-files = mkHook { }; + check-case-conflicts = mkHook { }; + detect-private-keys = mkHook { }; + fix-byte-order-marker = mkHook { }; + mixed-line-endings = mkHook { }; + }; + }; + }; + treefmt = { + projectRootFile = "flake.nix"; + + programs = { + actionlint = { + enable = true; + }; + ruff-format = { + enable = true; + }; + ruff-check = { + enable = true; + }; + deadnix = { + enable = true; + }; + dos2unix = { + enable = true; + }; + mdformat = { + enable = true; + }; + nixfmt = { + enable = true; + }; + }; + }; + }; + }; +} diff --git a/src/code.py b/src/code.py new file mode 100644 index 0000000..a6ac66b --- /dev/null +++ b/src/code.py @@ -0,0 +1,123 @@ +import board +import busio +import usb_midi + +import adafruit_midi +import adafruit_vl53l0x +from adafruit_midi.note_on import NoteOn +from adafruit_midi.note_off import NoteOff +import neopixel + +from keyboard import white_midi_notes, black_midi_notes + +midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) + +i2c = None +while i2c is None: + try: + i2c = busio.I2C(scl=board.GP1, sda=board.GP0) + except Exception: + pass + +pixels = neopixel.NeoPixel(board.A0, 300) + +step_size = 20 + +step_offset = 10 + +num_sensors = 1 + +black_key_threshold = 400 +white_key_threshold = 800 + +debug = True + + +class Key: + def __init__(self, note): + self._note = note + self._playing = False + + def stop_note(self): + if self._playing: + print("Shutting up {0}".format(self._note)) + midi.send(NoteOff(self._note)) + self._playing = False + + def play_note(self): + if not self._playing: + print("Playing {0}".format(self._note)) + midi.send(NoteOn(self._note)) + self._playing = True + + +class Step: + def __init__(self, sensor, address, white_note, black_note, pixels): + self._sensor = sensor + self._address = address + self._white_key = Key(white_note) + self._black_key = Key(black_note) + self._pixels = pixels + + def bling(self, color): + self.pixels = [color] * step_size + + def tick(self): + if self._sensor.range < black_key_threshold: + self._white_key.stop_note() + self._black_key.play_note() + self.bling((255, 0, 0)) + elif self._sensor.range < white_key_threshold: + self._black_key.stop_note() + self._white_key.play_note() + self.bling((0, 255, 0)) + else: + self._black_key.stop_note() + self._white_key.stop_note() + self.bling((0, 0, 0)) + if debug: + print("Sensor range: {0}mm".format(self._sensor.range)) + + +class Piano: + def __init__(self): + self._steps = [] + + def add_step(self, step): + self._steps.append(step) + print("Added sensor with address {0}".format(step._address)) + + def initialize_sensors(self): + for i in range(num_sensors): + sensor = None + address = i + 0x30 + print("Waiting for sensor {0}".format(i)) + while sensor is None: + try: + sensor = adafruit_vl53l0x.VL53L0X(i2c) + except Exception as _: + pass + # print(".") + print(sensor) + sensor.set_address(address) + step = Step( + sensor, + address, + white_midi_notes[i], + black_midi_notes[i], + pixels[i * step_size + (i + 1) * step_size + step_offset], + ) + self.add_step(step) + + def tick(self): + for i in range(num_sensors): + self._steps[i].tick() + if debug: + print() + + +piano = Piano() +piano.initialize_sensors() + +while True: + piano.tick() diff --git a/src/keyboard.py b/src/keyboard.py new file mode 100644 index 0000000..cefec32 --- /dev/null +++ b/src/keyboard.py @@ -0,0 +1,31 @@ +white_midi_notes = [ + 55, + 43, + 45, + 48, + 50, + 60, + 62, + 64, + 65, + 67, + 69, + 71, + 72, +] + +black_midi_notes = [ + 56, + 44, + 46, + 49, + 51, + 61, + 63, + 64, + 66, + 68, + 70, + 71, + 73, +] diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..51b90ce --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1,3 @@ +adafruit_vl53l0x +adafruit_midi +neopixel