commit ce75a8f8901063fa642427bcb823bbb78c9527e9 Author: puckoprutt Date: Sun Jul 12 03:12:42 2026 +0200 first version, first embedded lib, please help. diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..41ecf0b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1013 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "ascii-canvas" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1e3e699d84ab1b0911a1010c5c106aa34ae89aeac103be5ce0c3859db1e891" +dependencies = [ + "term", +] + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "bitfield" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac" + +[[package]] +name = "bitflags" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "cortex-m" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9" +dependencies = [ + "bare-metal", + "bitfield 0.13.2", + "embedded-hal 0.2.7", + "volatile-register", +] + +[[package]] +name = "cortex-m-rt" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d4dec46b34c299ccf6b036717ae0fce602faa4f4fe816d9013b9a7c9f5ba6" +dependencies = [ + "cortex-m-rt-macros", +] + +[[package]] +name = "cortex-m-rt-macros" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37549a379a9e0e6e576fd208ee60394ccb8be963889eebba3ffe0980364f472" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "either" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" + +[[package]] +name = "embedded-dma" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "994f7e5b5cb23521c22304927195f236813053eb9c065dd2226a32ba64695446" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-async" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884" +dependencies = [ + "embedded-hal 1.0.0", +] + +[[package]] +name = "embedded-hal-nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" +dependencies = [ + "embedded-hal 1.0.0", + "nb 1.1.0", +] + +[[package]] +name = "embedded-io" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eb1aa714776b75c7e67e1da744b81a129b3ff919c8712b5e1b32252c1f07cc7" + +[[package]] +name = "ena" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabffdaee24bd1bf95c5ef7cec31260444317e72ea56c4c91750e8b7ee58d5f1" +dependencies = [ + "log", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "frunk" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28aef0f9aa070bce60767c12ba9cb41efeaf1a2bc6427f87b7d83f11239a16d7" +dependencies = [ + "frunk_core 0.4.4", + "frunk_derives", +] + +[[package]] +name = "frunk_core" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "476eeaa382e3462b84da5d6ba3da97b5786823c2d0d3a0d04ef088d073da225c" + +[[package]] +name = "frunk_core" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bd3c9ba2e323e8b19e77f15873f60974a7d82f89b80e50c53be44b8b92927c1" + +[[package]] +name = "frunk_derives" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0b4095fc99e1d858e5b8c7125d2638372ec85aa0fe6c807105cf10b0265ca6c" +dependencies = [ + "frunk_proc_macro_helpers", + "quote", + "syn", +] + +[[package]] +name = "frunk_proc_macro_helpers" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b70229a1347a20d4af9c06116cc452acef34f798668c6b69e97dd5c8a88052bd" +dependencies = [ + "frunk_core 0.5.0", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "fugit" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e639847d312d9a82d2e75b0edcc1e934efcc64e6cb7aa94f0b1fbec0bc231d6" +dependencies = [ + "gcd", +] + +[[package]] +name = "gcd" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "stable_deref_trait", +] + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "keccak" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb26cec98cce3a3d96cbb7bced3c4b16e3d13f27ec56dbd62cbc8f39cfb9d653" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "lalrpop" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba4ebbd48ce411c1d10fb35185f5a51a7bfa3d8b24b4e330d30c9e3a34129501" +dependencies = [ + "ascii-canvas", + "bit-set", + "ena", + "itertools 0.14.0", + "lalrpop-util", + "petgraph", + "pico-args", + "regex", + "regex-syntax", + "sha3", + "string_cache", + "term", + "unicode-xid", + "walkdir", +] + +[[package]] +name = "lalrpop-util" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5baa5e9ff84f1aefd264e6869907646538a52147a755d494517a8007fb48733" +dependencies = [ + "regex-automata", + "rustversion", +] + +[[package]] +name = "led-button" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "embedded-hal 1.0.0", + "rp235x-hal", + "semihosting", +] + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "nb" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" + +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + +[[package]] +name = "num_enum" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0bca838442ec211fa11de3a8b0e0e8f3a4522575b5c4c06ed722e005036f26" +dependencies = [ + "num_enum_derive", + "rustversion", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "petgraph" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + +[[package]] +name = "pio" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0ba4153cee9585abc451271aa437d9e8defdea8b468d48ba6b8f098cbe03d7f" +dependencies = [ + "pio-core", + "pio-proc", +] + +[[package]] +name = "pio-core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61d90fddc3d67f21bbf93683bc461b05d6a29c708caf3ffb79947d7ff7095406" +dependencies = [ + "arrayvec", + "num_enum", + "paste", +] + +[[package]] +name = "pio-parser" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "825266c1eaddf54f636d06eefa4bf3c99d774c14ec46a4a6c6e5128a0f10d205" +dependencies = [ + "lalrpop", + "lalrpop-util", + "pio-core", +] + +[[package]] +name = "pio-proc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed4a76571f5fe51af43cc80ac870fe0c79cc0cdd686b9002a6c4c84bfdd0176b" +dependencies = [ + "codespan-reporting", + "lalrpop-util", + "pio-core", + "pio-parser", + "proc-macro-error2", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0e75113e14dc5acb068cd0786884f214f1312650a3d36d269f5c4f3cdee8a2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f388202e4b80542a0921078cc23b6333bcf1409c1e3f86404cae4766a6131db" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "riscv" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f5c1b8bf41ea746266cdee443d1d1e9125c86ce1447e1a2615abd34330d33a9" +dependencies = [ + "critical-section", + "embedded-hal 1.0.0", +] + +[[package]] +name = "riscv-rt" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0d35e32cf1383183e8885d8a9aa4402a087fd094dc34c2cb6df6687d0229dfe" +dependencies = [ + "riscv", + "riscv-rt-macros", +] + +[[package]] +name = "riscv-rt-macros" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30f19a85fe107b65031e0ba8ec60c34c2494069fe910d6c297f5e7cb5a6f76d0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "rp-binary-info" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f582261945fa215d40e2988b4df595d11c0908c0fff97a0fe23df766d117b790" + +[[package]] +name = "rp-hal-common" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8288358786b1458fb2caac8c4b40fb529ef4200d6c46467e2695b7a8ba573ae8" +dependencies = [ + "fugit", +] + +[[package]] +name = "rp235x-hal" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871a18f1bdad479deb080891d65b4d61d000f06f8e4227071f8ca5aa39c25d3a" +dependencies = [ + "bitfield 0.14.0", + "cortex-m", + "cortex-m-rt", + "critical-section", + "embedded-dma", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-async", + "embedded-hal-nb", + "embedded-io", + "frunk", + "fugit", + "gcd", + "itertools 0.13.0", + "nb 1.1.0", + "paste", + "pio", + "rand_core", + "riscv", + "riscv-rt", + "rp-binary-info", + "rp-hal-common", + "rp235x-hal-macros", + "rp235x-pac", + "sha2-const-stable", + "usb-device", + "vcell", + "void", +] + +[[package]] +name = "rp235x-hal-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74edd7a5979e9763bbb98e9746e711bac7464ee3397af7288e6c288ff0d3c764" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "rp235x-pac" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3f1227561239cdfbcea69f6f1c66c78cc8197d5b4103db394eacf20aedf74fa" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "vcell", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "semihosting" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8e4abf97879f4e80db69a9fba7bd64998e9bdad25f58ef045a778e191172fd4" + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "sha2-const-stable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f179d4e11094a893b82fff208f74d448a7512f99f5a0acbd5c679b705f83ed9" + +[[package]] +name = "sha3" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77fd7028345d415a4034cf8777cd4f8ab1851274233b45f84e3d955502d93874" +dependencies = [ + "digest", + "keccak", +] + +[[package]] +name = "siphasher" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649" + +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "string_cache" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf776ba3fa74f83bf4b63c3dcbbf82173db2632ed8452cb2d891d33f459de70f" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared", + "precomputed-hash", +] + +[[package]] +name = "syn" +version = "2.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "term" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c27177b12a6399ffc08b98f76f7c9a1f4fe9fc967c784c5a071fa8d93cf7e1" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "usb-device" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98816b1accafbb09085168b90f27e93d790b4bfa19d883466b5e53315b5f06a6" +dependencies = [ + "heapless", + "portable-atomic", +] + +[[package]] +name = "vcell" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "volatile-register" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de437e2a6208b014ab52972a27e59b33fa2920d3e00fe05026167a1c509d19cc" +dependencies = [ + "vcell", +] + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e31c4c3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "led-button" +version = "0.1.0" +edition = "2024" + +[profile.dev] +opt-level = 's' # default is opt-level = '0', but that makes very verbose machine code +codegen-units = 1 # trade compile speed for slightly better optimisations + +# cargo build/run --release +[profile.release] +opt-level = 's' # default is opt-level = '3', but that makes quite verbose machine code +codegen-units = 1 # trade compile speed for slightly better optimisations +lto = 'fat' # Use Link Time Optimisations to further inline things across crates +debug = 2 # Leave the debug symbols in (default is no debug info) +strip = true # strip stupid stuff. + +[dependencies] +cortex-m = { version = "0.7.7" } +cortex-m-rt = "0.7.5" +critical-section = { version = "1.2.0", features = ["restore-state-u8"] } +embedded-hal = "1.0.0" +rp235x-hal = { version = "0.4.0", features = ["rt", "critical-section-impl"] } +semihosting = "0.1.25" + +[[bin]] +name = "led-button-example" +path = "src/example/led_button.rs" +test = false +bench = false diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..68369be --- /dev/null +++ b/build.rs @@ -0,0 +1,27 @@ +//! Set up linker scripts for the rp235x-hal examples + +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put the linker script somewhere the linker can find it + let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + println!("cargo:rustc-link-search={}", out.display()); + + // The file `memory.x` is loaded by cortex-m-rt's `link.x` script, which + // is what we specify in `.cargo/config.toml` for Arm builds + let memory_x = include_bytes!("memory.x"); + let mut f = File::create(out.join("memory.x")).unwrap(); + f.write_all(memory_x).unwrap(); + println!("cargo:rerun-if-changed=memory.x"); + + // The file `rp235x_riscv.x` is what we specify in `.cargo/config.toml` for + // RISC-V builds + let rp235x_riscv_x = include_bytes!("rp235x_riscv.x"); + let mut f = File::create(out.join("rp235x_riscv.x")).unwrap(); + f.write_all(rp235x_riscv_x).unwrap(); + println!("cargo:rerun-if-changed=rp235x_riscv.x"); + + println!("cargo:rerun-if-changed=build.rs"); +} \ No newline at end of file diff --git a/src/button_config.rs b/src/button_config.rs new file mode 100644 index 0000000..5e448dc --- /dev/null +++ b/src/button_config.rs @@ -0,0 +1,25 @@ +//! Button config + +use super::PullConfig; + +/// The configuration of a simple button. +pub struct ButtonConfig { + /// fn_refresh * pressing_time = how many loops to trigger ButtonState::Pressed + pub pressing_time : f32, + /// fn_refresh * longpressing_time = how many loops to trigger ButtonState::LongPress + pub longpressing_time : f32, + /// if the button is in pull-up/down configuration. + pub pull_configuration : PullConfig +} + +impl Default for ButtonConfig { + + /// some random default values. + fn default() -> Self { + Self { + pressing_time : 0.25, + longpressing_time : 1.77, + pull_configuration : PullConfig::PullUp, + } + } +} \ No newline at end of file diff --git a/src/button_state.rs b/src/button_state.rs new file mode 100644 index 0000000..e3e00c2 --- /dev/null +++ b/src/button_state.rs @@ -0,0 +1,13 @@ + //! contains the enum ButtonState. + + #[derive(Copy, Clone, PartialEq, Eq)] + pub enum ButtonState { + NotPressed, + Released, + LongReleased, + Flank, + LongFlank, + Pressing, + Pressed, + LongPress, + } \ No newline at end of file diff --git a/src/button_stateful.rs b/src/button_stateful.rs new file mode 100644 index 0000000..e8f7931 --- /dev/null +++ b/src/button_stateful.rs @@ -0,0 +1,141 @@ +//! # A stateful button +//! ------------------- +//! has counter and poll that can be used to avoid contact bounce. +//! very very nice, much wow. + +use embedded_hal::digital::InputPin; +use super::button_state::ButtonState; +use super::button_config::ButtonConfig; +use super::PullConfig; + +/// A stateful input pin represented as a button. +pub struct StatefulButton { + /// The pin from hal denoted as IP for Input Pin. + pub pin : IP, + + /// ButtonConfig struct with a few options like pull-configuration. + pub config : ButtonConfig, + + /// Current state of the button + state : ButtonState, + + /// The previous state of the button + last_state : ButtonState, + + /// counter for how many loops the button have been pressed. + pub counter : u32, + + /// Threshold for triggering long pressed. + long_press_th : u32, + + /// Threshold for triggering pressed. + pressing_th : u32 +} + +impl StatefulButton { + + /// Creates a stateful button. + /// + /// ## Parameters: + /// - button_pin : IP + /// - An input pin from embedded-hal crate. + /// - config : ButtonConfig + /// - a config from this crate (see led_button::button_config::ButtonConfig) + /// - fn_refresh : f32 + /// - function refresh, lets say your program runs the function 10.000.000 times a seconds then thats a good value to use here. + /// + /// # Examples + /// ``` + /// use led_button::button_stateful::StatefulButton; + /// use led_button::button_config::ButtonConfig; + /// + /// // remember to always do a weird dance.. + /// + /// let button = StatefulButton::create( + /// pins.gpio21.into_pull_up_input(), + /// ButtonConfig::default(), + /// 274000.1 + /// ); + /// ``` + pub fn create(button_pin: IP, config: ButtonConfig, fn_refresh: f32) -> Self { + let lpt = config.longpressing_time; + let pt = config.pressing_time; + Self { + pin : button_pin, + config : config, + long_press_th : (fn_refresh * lpt) as u32, + pressing_th : (fn_refresh * pt) as u32, + state : ButtonState::NotPressed, + last_state : ButtonState::NotPressed, + counter : 0, + } + } + + /// checks if button is in active state. + pub fn is_pressed(&mut self) -> u8 { + match (&self.config.pull_configuration, self.pin.is_low()) { + (PullConfig::PullUp, Ok(true)) => 1, + (PullConfig::PullDown, Ok(false)) => 1, + _ => 0 + } + } + + /// trigger a state, in case you want to. + pub fn trigger_state(&mut self, state: ButtonState) { + self.last_state = self.state; + self.state = state; + } + + /// polling logic will increase self.counter by one each time you call this function and the button is pressed. + /// if the button is released and poll is called the counter is reset. + /// will also set internal state of the button for a press, long press, not pressed and pressing. + pub fn poll(&mut self) { + self.last_state = self.state; + if self.is_pressed() > 0 { + self.counter = self.counter.wrapping_add(1); + if self.counter >= self.long_press_th { + self.state = ButtonState::LongPress; + } + else if self.counter >= self.pressing_th { + self.state = ButtonState::Pressed; + } + else { + self.state = ButtonState::Pressing; + } + } + else { + self.reset(); + } + } + + /// read the current state and previous state and return one of the following states. + /// - NotPressed + /// - Released + /// - LongReleased + /// - Pressing + /// - Flank + /// - Pressed + /// - LongFlank + /// - LongPress + pub fn read(&mut self) -> ButtonState { + match (self.state, self.last_state) { + (ButtonState::NotPressed, ButtonState::NotPressed) => ButtonState::NotPressed, + (ButtonState::NotPressed, ButtonState::Pressed) => ButtonState::Released, + (ButtonState::NotPressed, ButtonState::Pressing) => ButtonState::Released, + (ButtonState::NotPressed, ButtonState::LongPress) => ButtonState::LongReleased, + (ButtonState::Pressing, _) => ButtonState::Pressing, + (ButtonState::Pressed, ButtonState::Pressing) => ButtonState::Flank, + (ButtonState::Pressed, ButtonState::Pressed) => ButtonState::Pressed, + (ButtonState::LongPress, ButtonState::Pressed) => ButtonState::LongFlank, + (ButtonState::LongPress, ButtonState::LongPress) => ButtonState::LongPress, + _ => ButtonState::NotPressed + } + } + + /// resets the counter and set state and previous state to NotPressed. + pub fn reset(&mut self) { + self.state = ButtonState::NotPressed; + self.last_state = ButtonState::NotPressed; + self.counter = 0; + } +} \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..56940ec --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,73 @@ +use core::{ + fmt, + //convert::{Infallible}, +}; + +//#[cfg(feature = "defmt-03")] +//use crate::defmt; +/* +pub trait Error: fmt::Debug { + /// Convert error to a generic error kind + /// + /// By using this method, errors freely defined by HAL implementations + /// can be converted to a set of generic errors upon which generic + /// code can act. + fn kind(&self) -> ErrorKind; +} + +impl Error for Infallible { + fn kind(&self) -> ErrorKind { + match *self {} + } +} +*/ +/// Error kind. +/// +/// This represents a common set of operation errors. HAL implementations are +/// free to define more specific or additional error types. However, by providing +/// a mapping to these common errors, generic code can still react to them. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +//#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] +#[non_exhaustive] +pub enum ErrorKind { + LedCouldNotTurnPinLow, + LedCouldNotTurnPinHigh, + ButtonSomething +} + +impl fmt::Display for ErrorKind { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::LedCouldNotTurnPinLow => write!( + f, + "[puckoprutt error] <- could not turn on led." + ), + Self::LedCouldNotTurnPinHigh => write!( + f, + "[puckoprutt error] <- could not turn off led." + ), + Self::ButtonSomething => write!( + f, + "[placeholder]" + ) + } + } +} + +/* +/// Error type trait. +/// +/// This just defines the error type, to be used by the other traits. +pub trait ErrorType { + type Error: Error; +} + +impl ErrorType for &T { + type Error = T::Error; +} + +impl ErrorType for &mut T { + type Error = T::Error; +} +*/ \ No newline at end of file diff --git a/src/example/led_button.rs b/src/example/led_button.rs new file mode 100644 index 0000000..9aa47ff --- /dev/null +++ b/src/example/led_button.rs @@ -0,0 +1,126 @@ +//! + +#![no_std] +#![no_main] + +use core::cell::RefCell; +use core::panic::PanicInfo; +use critical_section::Mutex; +use cortex_m::asm; +use cortex_m::peripheral::NVIC; +use cortex_m_rt::entry; +use led_button::{LedButton, PullConfig}; +use led_button::led_state::LedState; +use led_button::button_config::ButtonConfig; +use rp235x_hal::{ + self as hal, + block::ImageDef, + pac::{Peripherals, interrupt}, + gpio::{ + Interrupt, + Pin, FunctionSio, + PullDown, PullUp, + SioInput, SioOutput + } +}; + +type LedPin1 = Pin, PullDown>; +type ButtonPin1 = Pin, PullUp>; +type LedPin2 = Pin, PullDown>; +type ButtonPin2 = Pin, PullUp>; + +static GLOBAL_LED_BTN_1 : Mutex>>> = Mutex::new(RefCell::new(None)); +static GLOBAL_LED_BTN_2 : Mutex>>> = Mutex::new(RefCell::new(None)); + +pub const XOSC_CRYSTAL_FREQ : u32 = 12000000; + +#[interrupt] +#[allow(non_snake_case)] +fn IO_IRQ_BANK0() { + critical_section::with(|cs| { + let mut button1_ref = GLOBAL_LED_BTN_1.borrow(cs).borrow_mut(); + let mut button2_ref = GLOBAL_LED_BTN_2.borrow(cs).borrow_mut(); + + if let (Some(button1), Some(button2)) = (button1_ref.as_mut(), button2_ref.as_mut()) { + if button1.button.pin.interrupt_status(Interrupt::EdgeLow) { + let _ = button2.toggle(); + button1.button.pin.clear_interrupt(Interrupt::EdgeLow); + } + if button2.button.pin.interrupt_status(Interrupt::EdgeLow) { + let _ = button1.toggle(); + button2.button.pin.clear_interrupt(Interrupt::EdgeLow); + } + } + }); +} + +#[entry] +fn main() -> ! { + let mut pac = Peripherals::take().unwrap(); + let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); + + // clock and timers + let _clocks = hal::clocks::init_clocks_and_plls( + XOSC_CRYSTAL_FREQ, + pac.XOSC, + pac.CLOCKS, + pac.PLL_SYS, + pac.PLL_USB, + &mut pac.RESETS, + &mut watchdog + ).ok().unwrap(); + + // init gpio + let sio = hal::Sio::new(pac.SIO); + let pins = hal::gpio::Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS + ); + + let led1_pin = pins.gpio15.into_push_pull_output(); + let button1_pin = pins.gpio16.into_pull_up_input(); + let led2_pin = pins.gpio13.into_push_pull_output(); + let button2_pin = pins.gpio21.into_pull_up_input(); + button1_pin.set_interrupt_enabled(Interrupt::EdgeLow, true); + button2_pin.set_interrupt_enabled(Interrupt::EdgeLow, true); + + let button1 = LedButton::create( + led1_pin, LedState::Off, PullConfig::PullDown, button1_pin, ButtonConfig::default(), 274000.0 + ); + let button2 = LedButton::create( + led2_pin, LedState::Off, PullConfig::PullDown, button2_pin, ButtonConfig::default(), 274000.0 + ); + + critical_section::with(|cs| { + GLOBAL_LED_BTN_1.borrow(cs).replace(Some(button1)); + GLOBAL_LED_BTN_2.borrow(cs).replace(Some(button2)); + }); + + unsafe { + NVIC::unmask(hal::pac::Interrupt::IO_IRQ_BANK0); + } + + loop { + asm::wfi(); + } +} + +#[unsafe(link_section = ".start_block")] +#[used] +pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe(); +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop { asm::udf() } +} + +pub fn exit() -> ! { + semihosting::process::exit(0); +} + +#[allow(non_snake_case)] +#[cortex_m_rt::exception] +unsafe fn HardFault(_frame: &cortex_m_rt::ExceptionFrame) -> ! { + semihosting::process::exit(1); +} \ No newline at end of file diff --git a/src/led_state.rs b/src/led_state.rs new file mode 100644 index 0000000..13509f6 --- /dev/null +++ b/src/led_state.rs @@ -0,0 +1,55 @@ +//! LedState is pretty much a bool. + +use core::ops::Not; + +/// Digital output led state. +/// +/// Conversion from `bool` and logical negation are also implemented +/// for this type. +/// ```rust +/// # use led_button::let_state::LedState; +/// let state = LedState::from(false); +/// assert_eq!(state, LedState::Low); +/// assert_eq!(!state, LedState::High); +/// +/// let state = LedState::from(LedState::On); +/// assert_eq!(state, false); +/// assert_eq!(!state, true); +/// ``` +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum LedState { + On, + Off +} + +impl From for LedState { + #[inline] + fn from(on: bool) -> Self { + match on { + true => LedState::On, + false => LedState::Off + } + } +} + +impl From for bool { + #[inline] + fn from(value: LedState) -> bool { + match value { + LedState::Off => false, + LedState::On => true + } + } +} + +impl Not for LedState { + type Output = LedState; + + #[inline] + fn not(self) -> Self::Output { + match self { + LedState::Off => LedState::On, + LedState::On => LedState::Off + } + } +} \ No newline at end of file diff --git a/src/led_stateful.rs b/src/led_stateful.rs new file mode 100644 index 0000000..71eebd1 --- /dev/null +++ b/src/led_stateful.rs @@ -0,0 +1,111 @@ +//! # Stateful LED +//! -------------- +//! cool stuff. +//! very much wow factor. + +use embedded_hal::digital::OutputPin; +use super::PullConfig; +use super::led_state::LedState; +use super::errors::ErrorKind; + +/// A stateful output pin representing a LED. +pub struct StatefulLed { + /// The pin from hal denoted as OP for Output Pin. + pub pin : OP, + + /// The current state of the led, can be On or Off. + pub current_state : LedState, + + /// Resistor pull-up/down configuration. is the led "on" by a low or high signal. + pull_configuration : PullConfig, +} + +impl StatefulLed { + /// Creates a StatefulLed + /// + /// # Examples + /// ``` + /// use led_button::led_stateful::StatefulLed; + /// use led_button::led_state::LedState; + /// use led_button::button_config::Buttonconfig; + /// use led_button::PullConfig; + /// + /// // ... init your hal and do a weird a dance.. + /// + /// let led1_pin = pins.gpio15.into_push_pull_output(); + /// let button1_pin = pins.gpio16.into_pull_up_input(); + /// + /// let button1 = LedButton::create( + /// led1_pin, + /// LedState::Off, + /// PullConfig::PullDown, + /// button1_pin, + /// ButtonConfig::default(), + /// 274000.0 + /// ); + /// + /// // ... continue doing a weird a dance. + /// ``` + /// + pub fn create(led_pin: OP, pull_config: PullConfig, start_state: LedState) -> Self { + Self { + pin : led_pin, + pull_configuration : pull_config, + current_state : start_state, + } + } + + fn set_low(&mut self) -> Result<(), ErrorKind>{ + match self.pin.set_low() { + Ok(()) => Ok(()), + Err(_e) => Err(ErrorKind::LedCouldNotTurnPinLow) + } + } + + fn set_high(&mut self) -> Result<(), ErrorKind>{ + match self.pin.set_high() { + Ok(()) => Ok(()), + Err(_e) => Err(ErrorKind::LedCouldNotTurnPinHigh) + } + } + + /// will trigger 'on' state depending on your pull configuration. + pub fn on(&mut self) -> Result<(), ErrorKind> { + match self.set_state(LedState::On) { + Ok(()) => { + self.current_state = LedState::On; + Ok(()) + }, + Err(e) => Err(e) + } + } + + /// will trigger 'off' state depending on your pull configuration. + pub fn off(&mut self) -> Result<(), ErrorKind>{ + match self.set_state(LedState::Off) { + Ok(()) => { + self.current_state = LedState::Off; + Ok(()) + }, + Err(e) => Err(e) + } + } + + /// will trigger the 'on/off' state thats not in use. + pub fn toggle(&mut self) -> Result<(), ErrorKind>{ + match self.current_state { + LedState::On => self.off(), + LedState::Off => self.on(), + } + } + + /// set the state that was given in the 'state' argument. + pub fn set_state(&mut self, state: LedState) -> Result<(), ErrorKind> { + match (state, self.pull_configuration) { + (LedState::Off, PullConfig::PullDown) => { self.set_low() } + (LedState::On, PullConfig::PullDown) => { self.set_high() } + (LedState::Off, PullConfig::PullUp) => { self.set_high() } + (LedState::On, PullConfig::PullUp) => { self.set_low() } + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5472fff --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,126 @@ +//! Hej och välkommen till denna skit. + +#![no_std] + +mod errors; +pub mod button_state; +pub mod button_config; +pub mod button_stateful; +pub mod led_state; +pub mod led_stateful; + +use embedded_hal::digital::{InputPin, OutputPin}; +use led_state::LedState; +use led_stateful::StatefulLed; +use button_stateful::StatefulButton; +use button_config::ButtonConfig; +use crate::button_state::ButtonState; + +/// Pull configuration, used by both StatefulButton and StatefulLed. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum PullConfig { + PullUp, + PullDown +} + +/// Represents a button with built in led. +pub struct LedButton { + /// the pin from mcu to LED + pub led : StatefulLed, + + /// the pin from mcu to button + pub button : StatefulButton, + + /// the state of the button, always initialised to NotPressed. + pub state : ButtonState +} + +impl LedButton { + /// Creates a StatefulLed + /// + /// # Examples + /// ``` + /// use led_button::led_stateful::StatefulLed; + /// use led_button::led_state::LedState; + /// use led_button::button_config::Buttonconfig; + /// use led_button::PullConfig; + /// + /// // ... init your hal and do a weird a dance.. + /// + /// let led1_pin = pins.gpio15.into_push_pull_output(); + /// let button1_pin = pins.gpio16.into_pull_up_input(); + /// + /// let button1 = LedButton::create( + /// led1_pin, + /// LedState::Off, + /// PullConfig::PullDown, + /// button1_pin, + /// ButtonConfig::default(), + /// 274000.0 + /// ); + /// + /// // ... continue doing a weird a dance. + /// ``` + /// + pub fn create(led_pin: OP, led_state: LedState, led_pullconf: PullConfig, button_pin: IP, button_config: ButtonConfig, fn_refresh: f32 ) -> Self { + Self { + led : StatefulLed::create(led_pin, led_pullconf, led_state), + button : StatefulButton::create(button_pin, button_config, fn_refresh), + state : ButtonState::NotPressed + } + } + + /// get the current state of the LED. + pub fn led_state(self) -> LedState { + self.led.current_state + } + + /// set the LED to "On"-state. + pub fn on(&mut self) { + let _ = self.led.on(); + } + + /// set the LED to "Off"-state. + pub fn off(&mut self) { + let _ = self.led.off(); + } + + /// toggle the LED. + pub fn toggle(&mut self) { + let _ = self.led.toggle(); + } + + /// the current counter value of the button. + pub fn counter(self) -> u32 { + self.button.counter + } + + /// polls the button and read the current state + pub fn poll_and_read(&mut self, led_while_pressed: bool) -> ButtonState { + self.button.poll(); + if led_while_pressed { let _ = self.led.on(); } + match self.button.read() { + ButtonState::Released => { + if led_while_pressed { let _ = self.led.off(); } + self.state = ButtonState::NotPressed; + ButtonState::Released + } + ButtonState::LongReleased => { + if led_while_pressed { let _ = self.led.off(); } + self.state = ButtonState::NotPressed; + ButtonState::LongReleased + } + ButtonState::Flank => { + self.state = ButtonState::Pressed; + ButtonState::Flank + } + ButtonState::LongFlank => { + self.state = ButtonState::LongPress; + ButtonState::LongFlank + } + n => { + n + } + } + } +} \ No newline at end of file