feat(neopixel): first commit

This commit is contained in:
2025-02-20 19:16:06 +04:00
commit 0f1eb8da31
12 changed files with 576 additions and 0 deletions

46
neopixel_macros/Cargo.lock generated Normal file
View File

@@ -0,0 +1,46 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "neopixel_macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"

View File

@@ -0,0 +1,13 @@
[package]
name = "neopixel_macros"
version = "0.1.0"
edition = "2021"
[lib]
proc_macro = true
[dependencies]
syn = "2.0.58"
[build-dependencies.proc-macro2]
version = "=1.0.79"

View File

@@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2024-03-22"
components = [ "rust-src" ]
profile = "complete"

View File

@@ -0,0 +1,54 @@
#![feature(proc_macro_quote)]
#![no_std]
use core::str::FromStr;
extern crate alloc;
use alloc::string::ToString;
use proc_macro::{quote, TokenStream};
use syn::parse;
#[proc_macro]
pub fn impl_static_pin(pin: TokenStream) -> TokenStream {
let parsing_result = parse::<syn::Ident>(pin.clone());
if let Ok(ident) = parsing_result {
let pin = TokenStream::from_str(ident.to_string().as_str()).unwrap();
let mut identifier = ident.to_string();
identifier.insert_str(1, "ORT");
let pin_num = identifier.pop().unwrap();
if pin_num.is_ascii_digit() {
let pin_num = TokenStream::from_str(&pin_num.to_string()).unwrap();
let port_field = TokenStream::from_str(&identifier.to_ascii_lowercase()).unwrap();
let port_ident = TokenStream::from_str(identifier.as_str()).unwrap();
let trait_ident = TokenStream::from_str("StaticPin").unwrap();
quote! {
impl $trait_ident for $pin {
type Port = $port_ident;
const PIN_NUM: u8 = $pin_num;
fn write(data: u8) {
unsafe {
(*Self::Port::ptr())
.$port_field
.write(|w| w.bits(data));
}
}
fn read() -> u8 {
unsafe { (*Self::Port::ptr()).$port_field.read().bits() }
}
}
}
} else {
unreachable!()
}
} else {
unreachable!()
}
}