summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatarLarge Libravatar memdmp <memdmpestrogenzone>2025-01-25 04:33:41 +0100
committerLibravatarLarge Libravatar memdmp <memdmpestrogenzone>2025-01-25 04:33:41 +0100
commitdae3d42264b7d1ebb7f7b1b3967d866c49135401 (patch)
treece3320f88ed634e8b02f5965fb29be02a92225ea
parent411ef1c654cf9d41f06bdbd9e8c2a7985e117a17 (diff)
downloaddumbswitch-dae3d42264b7d1ebb7f7b1b3967d866c49135401.tar.gz
dumbswitch-dae3d42264b7d1ebb7f7b1b3967d866c49135401.tar.bz2
dumbswitch-dae3d42264b7d1ebb7f7b1b3967d866c49135401.tar.lz
dumbswitch-dae3d42264b7d1ebb7f7b1b3967d866c49135401.zip

feat: things

-rw-r--r--.gitignore2
-rw-r--r--Cargo.toml4
-rw-r--r--src/bin/btn-test.rs31
-rw-r--r--src/bin/main.rs177
-rw-r--r--src/lib.rs1
-rw-r--r--src/network_data.example.rs4
6 files changed, 159 insertions, 60 deletions
diff --git a/.gitignore b/.gitignore
index 6e771f8..70bc422 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,5 @@ target/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
+
+src/network_data.rs
diff --git a/Cargo.toml b/Cargo.toml
index 1928b16..da4a122 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,10 @@ version = "0.1.0"
name = "dumbswitch"
path = "./src/bin/main.rs"
+[[bin]]
+name = "btn-test"
+path = "./src/bin/btn-test.rs"
+
[dependencies]
critical-section = "1.2.0"
embedded-io = "0.6.1"
diff --git a/src/bin/btn-test.rs b/src/bin/btn-test.rs
new file mode 100644
index 0000000..b33e402
--- /dev/null
+++ b/src/bin/btn-test.rs
@@ -0,0 +1,31 @@
+#![no_std]
+#![no_main]
+
+use esp_backtrace as _;
+use esp_hal::{
+ gpio::{Input, Level, Output, Pull},
+ main,
+};
+use esp_println::println;
+
+#[main]
+fn main() -> ! {
+ let peripherals = esp_hal::init(esp_hal::Config::default());
+
+ println!("Hello world!");
+
+ // Set GPIO7 as an output, and set its state high initially.
+ let mut led = Output::new(peripherals.GPIO1, Level::Low);
+ let button = Input::new(peripherals.GPIO3, Pull::Up);
+
+ // Check the button state and set the LED state accordingly.
+ loop {
+ if button.is_high() {
+ println!("high");
+ led.set_high();
+ } else {
+ println!("low");
+ led.set_low();
+ }
+ }
+}
diff --git a/src/bin/main.rs b/src/bin/main.rs
index 3446f90..246aa8f 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -13,14 +13,20 @@
#![no_std]
#![no_main]
+use core::cell::RefCell;
use core::ops::Deref;
use core::str::FromStr;
use blocking_network_stack::Stack;
+use critical_section::Mutex;
+use dumbswitch::network_data;
use embedded_io::*;
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::delay::Delay;
+use esp_hal::gpio::{Event, Input, Io, Level, Output, Pull};
+use esp_hal::handler;
+use esp_hal::interrupt::InterruptConfigurable;
use esp_hal::{
clock::CpuClock,
main,
@@ -28,7 +34,7 @@ use esp_hal::{
time::{self, Duration},
timer::timg::TimerGroup,
};
-use esp_println::{print, println};
+use esp_println::println;
use esp_wifi::{
init,
wifi::{
@@ -36,15 +42,9 @@ use esp_wifi::{
WifiError, WifiStaDevice,
},
};
-use heapless::String;
-use log::{debug, info};
+use log::{debug, error, info, trace, warn};
use smoltcp::iface::{SocketSet, SocketStorage};
-const SSID: &str = "Chaostreff Bern";
-const PASSWORD: &str = "-";
-const STATIC_IP: &str = "10.0.0.77";
-const GATEWAY_IP: &str = "10.0.0.1";
-
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
@@ -53,10 +53,20 @@ fn main() -> ! {
let delay = Delay::new();
- debug!("Waiting 500ms pre-init");
+ esp_alloc::heap_allocator!(72 * 1024);
+
+ info!("Preparing GPIO");
+
+ // Set GPIO1 as an output, and set its state low initially.
+ let mut led = Output::new(peripherals.GPIO1, Level::Low);
+
+ // Set GPIO4 as an input
+ let button = Input::new(peripherals.GPIO3, Pull::Up);
+
+ info!("Waiting 500ms pre-init");
delay.delay_millis(500);
- esp_alloc::heap_allocator!(72 * 1024);
+ led.set_low();
let timg0 = TimerGroup::new(peripherals.TIMG0);
@@ -75,8 +85,8 @@ fn main() -> ! {
let mut stack = Stack::new(iface, device, socket_set, now, rng.random());
let client_config = Configuration::Client(ClientConfiguration {
- ssid: SSID.try_into().unwrap(),
- password: PASSWORD.try_into().unwrap(),
+ ssid: network_data::SSID.try_into().unwrap(),
+ password: network_data::PASSWORD.try_into().unwrap(),
..Default::default()
});
let res = controller.set_configuration(&client_config);
@@ -86,10 +96,10 @@ fn main() -> ! {
debug!("is wifi started: {:?}", controller.is_started());
info!("Start Wifi Scan");
- let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = controller.scan_n();
+ let res: Result<(heapless::Vec<AccessPointInfo, 30>, usize), WifiError> = controller.scan_n();
if let Ok((res, _count)) = res {
for ap in res {
- debug!("{:?}", ap);
+ debug!("AP Info: {:?}", ap);
}
}
@@ -97,28 +107,39 @@ fn main() -> ! {
debug!("wifi_connect {:?}", controller.connect());
// wait to get connected
- debug!("Wait to get connected");
+ info!("Wait to get connected");
loop {
match controller.is_connected() {
Ok(true) => break,
Ok(false) => {}
Err(err) => {
- println!("{:?}", err);
- loop {}
+ error!("Failed to connect to wifi: {:?}", err);
+ let mut high = false;
+ loop {
+ delay.delay_millis(1000);
+ high = !high;
+ if high {
+ led.set_high();
+ } else {
+ led.set_low();
+ }
+ }
}
}
}
debug!("{:?}", controller.is_connected());
- debug!("Setting static IP {}", STATIC_IP);
+ info!("Setting static IP {}", network_data::STATIC_IP);
stack
.set_iface_configuration(&blocking_network_stack::ipv4::Configuration::Client(
blocking_network_stack::ipv4::ClientConfiguration::Fixed(
blocking_network_stack::ipv4::ClientSettings {
- ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(STATIC_IP)),
+ ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(network_data::STATIC_IP)),
subnet: blocking_network_stack::ipv4::Subnet {
- gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(GATEWAY_IP)),
+ gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(
+ network_data::GATEWAY_IP,
+ )),
mask: blocking_network_stack::ipv4::Mask(24),
},
dns: None,
@@ -128,9 +149,18 @@ fn main() -> ! {
))
.unwrap();
+ for i in 0..4 {
+ delay.delay_millis(100);
+ if i % 2 == 0 {
+ led.set_high();
+ } else {
+ led.set_low();
+ }
+ }
+
info!(
"Start busy loop on main. Point your browser to http://{}:8080/",
- STATIC_IP
+ network_data::STATIC_IP
);
let mut rx_buffer = [0u8; 1536];
@@ -141,35 +171,64 @@ fn main() -> ! {
socket.listen(8080).unwrap();
+ fn update_open(is_open: bool, button: &Input<'_>, led: &mut Output<'_>, delay: &Delay) -> bool {
+ let mut is_open = if is_open { true } else { false };
+ if button.is_low() {
+ is_open = !is_open;
+
+ if is_open {
+ led.set_high();
+ } else {
+ led.set_low();
+ }
+ while button.is_low() {
+ trace!("waiting for unpress btn");
+ delay.delay_millis(10);
+ }
+ }
+ is_open
+ }
+
loop {
socket.work();
+ is_open = update_open(is_open, &button, &mut led, &delay);
if !socket.is_open() {
socket.listen(8080).unwrap();
}
if socket.is_connected() {
- info!("Connected");
-
- let mut time_out = false;
- let deadline = time::now() + Duration::millis(500);
- let mut buffer = [0u8; 2048];
- let mut pos = 0;
- while let Ok(len) = socket.read(&mut buffer[pos..]) {
- let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
-
- if to_print.contains("\r\n\r\n") {
- break;
- }
-
- pos += len;
-
- if time::now() > deadline {
- println!("Timeout");
- time_out = true;
- break;
- }
- }
+ is_open = update_open(is_open, &button, &mut led, &delay);
+ // debug!("Established Connection");
+
+ // let mut time_out = false;
+ // let deadline = time::now() + Duration::millis(500);
+ // let mut buffer = [0u8; 8192];
+ // let mut pos = 0;
+ // while let Ok(len) = socket.read(&mut buffer[pos..]) {
+ // if pos + len > buffer.len() {
+ // error!(
+ // "We got {} bytes. Buffer overflowed, treating as timeout.",
+ // pos + len
+ // );
+ // time_out = true;
+ // break;
+ // }
+
+ // let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
+
+ // if to_print.contains("\r\n\r\n") {
+ // break;
+ // }
+
+ // pos += len;
+
+ // if time::now() > deadline {
+ // println!("Timeout");
+ // time_out = true;
+ // break;
+ // }
+ // }
let parts = [
(r##"{
@@ -230,34 +289,32 @@ fn main() -> ! {
.as_bytes(),
];
- if !time_out {
- socket
- .write_all(
- b"HTTP/1.0 200 OK\r\n\
+ // if !time_out {
+ socket
+ .write_all(
+ b"HTTP/1.0 200 OK\r\n\
Content-Type: application/json\r\n\
UwU: if u read this u have been catgirled :3\r\n\
\r\n\
",
- )
- .unwrap();
- for part in parts {
- socket.write_all(part).unwrap();
- }
- debug!("wrote {parts:?}");
-
- socket.flush().unwrap();
+ )
+ .unwrap();
+ for part in parts {
+ socket.write_all(part).unwrap();
}
- socket.close();
+ socket.flush().unwrap();
+ socket.work();
+ // }
- println!("Done\n");
- println!();
+ socket.close();
+ is_open = update_open(is_open, &button, &mut led, &delay);
}
-
// TODO: what
- let deadline = time::now() + Duration::millis(1000);
- while time::now() < deadline {
+ let deadline = time::now() + Duration::millis(100);
+ while time::now() < deadline && !socket.is_connected() {
socket.work();
+ is_open = update_open(is_open, &button, &mut led, &delay);
}
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 0c9ac1a..01d8c46 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1,2 @@
#![no_std]
+pub mod network_data;
diff --git a/src/network_data.example.rs b/src/network_data.example.rs
new file mode 100644
index 0000000..13cd6d5
--- /dev/null
+++ b/src/network_data.example.rs
@@ -0,0 +1,4 @@
+pub const SSID: &str = "Chaostreff Bern";
+pub const PASSWORD: &str = "nyaa :3";
+pub const STATIC_IP: &str = "10.0.0.77";
+pub const GATEWAY_IP: &str = "10.0.0.1";