diff --git a/inside/Cargo.lock b/inside/Cargo.lock index be1aaf9..4a24684 100644 --- a/inside/Cargo.lock +++ b/inside/Cargo.lock @@ -526,6 +526,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "ident_case" version = "1.0.1" @@ -547,6 +553,7 @@ name = "inside" version = "0.1.0" dependencies = [ "blocking-network-stack", + "embedded-hal 1.0.0", "embedded-io", "esp-alloc", "esp-backtrace", @@ -554,6 +561,7 @@ dependencies = [ "esp-println", "esp-wifi", "heapless", + "hex", "log", "smoltcp", ] diff --git a/inside/Cargo.toml b/inside/Cargo.toml index f83a85d..8824f09 100644 --- a/inside/Cargo.toml +++ b/inside/Cargo.toml @@ -16,6 +16,7 @@ esp-println = { version = "0.12.0", features = ["esp32", "log"] } log = { version = "0.4.22" } esp-alloc = { version = "0.5.0" } embedded-io = "0.6.1" +embedded-hal = "1" esp-wifi = { version = "0.10.1", features = [ "esp32", "phy-enable-usb", @@ -34,6 +35,7 @@ smoltcp = { version = "0.11.0", default-features = false, features = [ "socket-tcp", "socket-udp", ] } +hex = { version = "0.4.3", default-features=false} [profile.dev] # Rust debug is too slow. diff --git a/inside/rust-toolchain.toml b/inside/rust-toolchain.toml new file mode 100644 index 0000000..a2f5ab5 --- /dev/null +++ b/inside/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "esp" diff --git a/inside/src/main.rs b/inside/src/main.rs index f14be92..47c0ab3 100644 --- a/inside/src/main.rs +++ b/inside/src/main.rs @@ -1,13 +1,16 @@ #![no_std] #![no_main] +#![feature(const_trait_impl)] use embedded_io::{Read, ReadReady, Write}; +use embedded_hal::pwm::SetDutyCycle; use esp_backtrace as _; -use esp_hal::{delay::Delay, prelude::*, rng::Rng, time::{self, Duration}}; +use esp_hal::{gpio::Io, ledc::{channel, timer, LSGlobalClkSource, Ledc, LowSpeed}, prelude::*, rng::Rng, time::{self, Duration}}; use esp_println::{print, println}; use esp_wifi::wifi::{utils::create_network_interface, AccessPointInfo, ClientConfiguration, Configuration, WifiError, WifiStaDevice}; -use smoltcp::iface::{SocketSet, SocketStorage}; +use smoltcp::iface::SocketStorage; use blocking_network_stack::{ipv4, Stack}; +use inside::handle; extern crate alloc; @@ -18,8 +21,34 @@ fn main() -> ! { let mut config = esp_hal::Config::default(); config.cpu_clock = CpuClock::max(); let peripherals = esp_hal::init(config); + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + let pin = io.pins.gpio15; + + let mut ledc = Ledc::new(peripherals.LEDC); + ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); + let mut lstimer0 = ledc.get_timer::(timer::Number::Timer0); + lstimer0 + .configure(timer::config::Config { + duty: timer::config::Duty::Duty14Bit, + clock_source: timer::LSClockSource::APBClk, + frequency: 50.Hz(), + }) + .unwrap(); + + + let mut motor = ledc.get_channel(channel::Number::Channel0, pin); + + motor + .configure(channel::config::Config { + timer: &lstimer0, + duty_pct: 0, + pin_config: channel::config::PinConfig::PushPull, + }) + .unwrap(); + + println!("max duty {}", motor.max_duty_cycle()); + motor.set_duty_cycle(1229).unwrap(); - let delay = Delay::new(); esp_alloc::heap_allocator!(72 * 1024); esp_println::logger::init_logger_from_env(); @@ -36,7 +65,7 @@ fn main() -> ! { let mut wifi = peripherals.WIFI; let mut socket_set_entries: [SocketStorage; 3] = Default::default(); - let (iface, device, mut controller, mut socket_set) = + let (iface, device, mut controller, socket_set) = create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); let now = || time::now().duration_since_epoch().to_millis(); @@ -122,6 +151,7 @@ fn main() -> ! { if to_print.contains("\r\n") { print!("{}", to_print); println!(); + pos += len; break; } @@ -137,6 +167,14 @@ fn main() -> ! { } } + let mut command = buffer[..pos].trim_ascii().split(|c| *c == b' '); + let buf: [u8;2] = hex::FromHex::from_hex(command.next().unwrap()).unwrap(); + let card: u16 = u16::from_be_bytes(buf); + println!("{card}"); + motor.set_duty_cycle(card).unwrap(); + + + handle(&buffer[..pos], &mut motor); if !time_out { let _ = socket.write_all( @@ -145,8 +183,12 @@ fn main() -> ! { socket.flush().unwrap(); } + while time::now() < deadline && socket.is_connected() {} socket.close(); log::info!("ending transaction"); + // open: 0x0533 + // close: 0x04c8 + motor.set_duty_cycle(1229).unwrap(); } //let deadline = time::now() + Duration::secs(5);