diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..389685b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,409 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "ioctl-rs" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7970510895cee30b3e9128319f2cefd4bde883a39f38baa279567ba3a7eb97d" +dependencies = [ + "libc", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "mac_address" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8836fae9d0d4be2c8b4efcdd79e828a2faa058a90d005abf42f91cac5493a08e" +dependencies = [ + "nix", + "winapi", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", + "memoffset", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "serial" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1237a96570fc377c13baa1b88c7589ab66edced652e43ffb17088f003db3e86" +dependencies = [ + "serial-core", + "serial-unix", + "serial-windows", +] + +[[package]] +name = "serial-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f46209b345401737ae2125fe5b19a77acce90cd53e1658cda928e4fe9a64581" +dependencies = [ + "libc", +] + +[[package]] +name = "serial-unix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f03fbca4c9d866e24a459cbca71283f545a37f8e3e002ad8c70593871453cab7" +dependencies = [ + "ioctl-rs", + "libc", + "serial-core", + "termios", +] + +[[package]] +name = "serial-windows" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c6d3b776267a75d31bbdfd5d36c0ca051251caafc285827052bc53bcdc8162" +dependencies = [ + "libc", + "serial-core", +] + +[[package]] +name = "termios" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5d9cf598a6d7ce700a4e6a9199da127e6819a61e64b68609683cc9a01b5683a" +dependencies = [ + "libc", +] + +[[package]] +name = "thekenlicht-daemon" +version = "0.1.0" +dependencies = [ + "env_logger", + "log", + "mac_address", + "serial", + "tiny-artnet", +] + +[[package]] +name = "tiny-artnet" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4059a7eec3c8a78aa7a9bb57bfe5ba15995eed5ced63debef99fbe19f4ae854c" +dependencies = [ + "nom", + "tiny-artnet-bytes-no-atomic", +] + +[[package]] +name = "tiny-artnet-bytes-no-atomic" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c26c57f0398a7fd68af1bee8759e7b3b915595cbf30862f5c61524a09ba48af3" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4adc757 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "thekenlicht-daemon" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.22" +env_logger = "0.11.5" +tiny-artnet = "0.1.3" +serial = "0.4.0" +mac_address = "1.1.7" diff --git a/arduino/thekenlicht.ino b/arduino/thekenlicht.ino new file mode 100644 index 0000000..51f5913 --- /dev/null +++ b/arduino/thekenlicht.ino @@ -0,0 +1,55 @@ + +#define RED_LED 6 +#define GREEN_LED 9 +#define BLUE_LED 5 + +int brightness = 255; +int rb = 0; +int gb = 0; +int bb = 0; +int fadeSpeed = 10; + +void setup() +{ + pinMode(RED_LED, OUTPUT); + pinMode(GREEN_LED, OUTPUT); + pinMode(BLUE_LED, OUTPUT); + + Serial.begin(9600); +} + +char* result[3]; + +void loop() +{ + if(Serial.available() > 1) + { + char input[12]; + byte size = Serial.readBytes(input, 11); + // Add the final 0 to end the C string + input[size] = 0; + + // Read each command pair + char* command = strtok(input, ";"); + int index = 0; + while (command != 0) + { + result[index] = command; + index += 1; + // Find the next command in input string + command = strtok(0, ";"); + } + + rb = atoi(result[0]); + gb = atoi(result[1]); + bb = atoi(result[2]); + + analogWrite(RED_LED, rb); + analogWrite(GREEN_LED, gb); + analogWrite(BLUE_LED, bb); + + String out = (String)rb + ";" + (String)gb + ";" + (String)bb; + Serial.println(out); + } +} + diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..9cf1981 --- /dev/null +++ b/flake.lock @@ -0,0 +1,121 @@ +{ + "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1724775741, + "narHash": "sha256-xuj7Ye3Y2EgunLiEEV5zYxUQuLTURV5mgbXDB1fA7h8=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "fe4d5979b34444815287d61bd2a4e193cebbc7a6", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1724537630, + "narHash": "sha256-gpqINM71zp3kw5XYwUXa84ZtPnCmLLnByuFoYesT1bY=", + "owner": "ipetkov", + "repo": "crane", + "rev": "3e08f4b1fc9aaede5dd511d8f5f4ef27501e49b0", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": [] + }, + "locked": { + "lastModified": 1724826636, + "narHash": "sha256-hz8Szf5J9oQg6EeMhHE/eKuexoHPiDbmOZTPvijYwyM=", + "owner": "nix-community", + "repo": "fenix", + "rev": "3454a665ff4dd29cf618e6a2e53065370876297f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1724748588, + "narHash": "sha256-NlpGA4+AIf1dKNq76ps90rxowlFXUsV9x7vK/mN37JM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a6292e34000dc93d43bccf78338770c1c5ec8a99", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", + "fenix": "fenix", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..e1f0a5b --- /dev/null +++ b/flake.nix @@ -0,0 +1,118 @@ +{ + description = "Daemon to convert ArtNet to Thekenlicht Commands"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.rust-analyzer-src.follows = ""; + }; + + flake-utils.url = "github:numtide/flake-utils"; + + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; + }; + + outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + inherit (pkgs) lib; + + craneLib = crane.mkLib pkgs; + src = craneLib.cleanCargoSource ./.; + + # Common arguments can be set here to avoid repeating them later + commonArgs = { + inherit src; + strictDeps = true; + + buildInputs = []; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + # artifacts from above. + thekenlicht-daemon = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in + { + checks = { + inherit thekenlicht-daemon; + + # Run clippy (and deny all warnings) on the crate source, + # again, reusing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + thekenlicht-daemon-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + thekenlicht-daemon-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + # Check formatting + thekenlicht-daemon-fmt = craneLib.cargoFmt { + inherit src; + }; + + # Audit dependencies + thekenlicht-daemon-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + # Audit licenses + #thekenlicht-daemon-deny = craneLib.cargoDeny { + # inherit src; + #}; + + # Run tests with cargo-nextest + # Consider setting `doCheck = false` on `my-crate` if you do not want + # the tests to run twice + thekenlicht-daemon-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + }; + + packages = { + default = thekenlicht-daemon; + }; + + apps.default = flake-utils.lib.mkApp { + drv = thekenlicht-daemon; + }; + + devShells.default = craneLib.devShell { + # Inherit inputs from checks. + checks = self.checks.${system}; + + # Additional dev-shell environment variables can be set directly + # MY_CUSTOM_DEVELOPMENT_VAR = "something else"; + + # Extra inputs can be added here; cargo and rustc are provided by default. + packages = [ + # pkgs.ripgrep + ]; + }; + + nixosModules.thekenlicht-daemon = ./module.nix; + }); +} diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..4c80b9e --- /dev/null +++ b/module.nix @@ -0,0 +1,21 @@ +{ self, config, pkgs, lib, ... }: +let + cfg = config.services.thekenlicht-daemon; +in +{ + options = { + services.thekenlicht-daemon = { + enable = lib.mkEnableOption "Enable Module"; + serialPort = lib.mkStringOption "Serial Port"; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.thekenlicht-daemon = { + description = "converts artnet to serial thekenlicht commands"; + wantedBy = [ "multi-user.target" ]; + serviceConfig.ExecStart = "${self.packages.thekenlicht-daemon}/bin/thekenlicht-daemon ${cfg.serialPort}"; + serviceConfig.Restart = "on-failure"; + }; + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c5061a4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,126 @@ +use std::{ + env, + io::Write, + net::{Ipv4Addr, UdpSocket}, +}; + +use log::{debug, error, info, warn}; +use serial::SerialPort; +use tiny_artnet::Art; + +const UNIVERSE: u8 = 0; +const START_ADDRESS: usize = 1; + +fn main() { + env_logger::init(); + + let Some(tty_port) = env::args().nth(1) else { + println!( + "usage: {} ", + env::args().next().unwrap_or_default() + ); + return; + }; + + let ip_address = Ipv4Addr::new(127, 0, 0, 2); + let port = tiny_artnet::PORT; + let Ok(socket) = UdpSocket::bind((ip_address, port)) else { + error!("failed to bind to {}:{}", ip_address, port); + return; + }; + + let Ok(mut serial) = serial::open(&tty_port) else { + error!("failed to open serial port"); + return; + }; + let conf_res = serial.reconfigure(&|settings| { + settings.set_baud_rate(serial::Baud9600)?; + settings.set_char_size(serial::Bits8); + settings.set_parity(serial::ParityNone); + settings.set_stop_bits(serial::Stop1); + settings.set_flow_control(serial::FlowNone); + Ok(()) + }); + if let Err(e) = conf_res { + error!("failed to configure serial port: {e}"); + return; + } + + let mac_address_bytes = mac_address::get_mac_address().unwrap().unwrap().bytes(); + + info!("Listening on {}:{}", ip_address, port); + + let mut buf = [0; 65_507]; + + loop { + let Ok((len, from_addr)) = socket.recv_from(&mut buf) else { + warn!("failed to receive artnet udp packet"); + continue; + }; + debug!("received {} bytes from {}", len, from_addr); + + match tiny_artnet::from_slice(&buf[..len]) { + Ok(Art::Dmx(dmx)) => { + debug!( + "received dmx packet for universe {}", + dmx.port_address.universe + ); + + if dmx.port_address.universe == UNIVERSE { + let mut payload = [b';'; 11]; + let commands = format!( + "{};{};{}", + dmx.data[START_ADDRESS - 1], + dmx.data[START_ADDRESS], + dmx.data[START_ADDRESS + 1] + ); + payload[..commands.as_bytes().len()].copy_from_slice(commands.as_bytes()); + if let Err(e) = serial.write_all(&payload) { + warn!("failed to send command over serial: {e}"); + } + } + } + Ok(Art::Sync) => { + debug!("received sync packet"); + } + Ok(Art::Poll(poll)) => { + debug!("received poll packet: {:?}", poll); + + let poll_reply = tiny_artnet::PollReply { + ip_address: &ip_address.octets(), + port, + firmware_version: 0x0001, + short_name: "Example Node", + long_name: "Tiny Artnet Example Node", + mac_address: &mac_address_bytes, + // This Node has one port + num_ports: 1, + // This node has one output channel + port_types: &[0b10000000, 0, 0, 0], + // Report that data is being output correctly + good_output_a: &[0b10000000, 0, 0, 0], + ..Default::default() + }; + + let msg_len = poll_reply.serialize(&mut buf); + socket.send_to(&buf[..msg_len], from_addr).unwrap(); + // let broadcast: UdpSocket = UdpSocket::bind("0.0.0.0:0").unwrap(); + // broadcast + // .set_read_timeout(Some(Duration::new(5, 0))) + // .unwrap(); + // broadcast.set_broadcast(true).unwrap(); + // broadcast + // .send_to(&buf[..msg_len], "255.255.255.255") + // .unwrap(); + + debug!("responded to {:?}: {:?}", from_addr, poll_reply); + } + Err(err) => { + warn!("artnet error: {:?}", err); + } + msg => { + info!("received unknown packet: {:?}", msg); + } + }; + } +} diff --git a/thekenlicht.ino b/thekenlicht.ino deleted file mode 100644 index d050d26..0000000 --- a/thekenlicht.ino +++ /dev/null @@ -1,38 +0,0 @@ -#include "DMXUSB.h" - -#define UNIVERSE 0 -#define ADDRESS 1 - -#define RED_PIN 6 -#define GREEN_PIN 9 -#define BLUE_PIN 5 - -#define DMXUSB_BAUDRATE 115200 - -void dmxCallback(int universe, char buffer[512]) { - if (universe == UNIVERSE) { - // DMX-Address starts at one, buffer at zero - analogWrite(RED_PIN, buffer[ADDRESS-1]); - analogWrite(GREEN_PIN, buffer[ADDRESS]); - analogWrite(BLUE_PIN, buffer[ADDRESS+1]); - } -} - -DMXUSB myDMXUsb( - Serial, - DMXUSB_BAUDRATE, - 0, - dmxCallback -); - -void setup() { - // setup pins - pinMode(RED_PIN, OUTPUT); - pinMode(GREEN_PIN, OUTPUT); - pinMode(BLUE_PIN, OUTPUT); - Serial.begin(DMXUSB_BAUDRATE); -} - -void loop() { - myDMXUsb.listen(); -}