diff options
feat: ok more minimal
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/bin/btn-test.rs | 2 | ||||
-rw-r--r-- | src/bin/server.rs (renamed from src/bin/main.rs) | 217 |
3 files changed, 124 insertions, 102 deletions
@@ -4,13 +4,16 @@ name = "dumbswitch" version = "0.1.0" [[bin]] -name = "dumbswitch" -path = "./src/bin/main.rs" +name = "dumbswitch-as-server" +path = "./src/bin/server.rs" [[bin]] name = "btn-test" path = "./src/bin/btn-test.rs" +[features] +led-as-busy-led = [] + [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 index b33e402..87663ef 100644 --- a/src/bin/btn-test.rs +++ b/src/bin/btn-test.rs @@ -16,7 +16,7 @@ fn main() -> ! { // 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); + let button = Input::new(peripherals.GPIO3, Pull::Down); // Check the button state and set the LED state accordingly. loop { diff --git a/src/bin/main.rs b/src/bin/server.rs index e9b2386..106295f 100644 --- a/src/bin/main.rs +++ b/src/bin/server.rs @@ -56,19 +56,41 @@ static BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None)); static mut IS_OPEN: bool = false; static mut LED: OurOption<RefCell<Output<'_>>> = OurOption::None; -fn set_led_state(val: bool) { - let v = unsafe { - #[allow(static_mut_refs)] - &LED - }; - let v = match v { - OurOption::None => { - return; - } - OurOption::Some(v) => v, - }; - let mut b = v.borrow_mut(); - b.set_level(if val { Level::High } else { Level::Low }); +fn set_open_led_state(val: bool) { + #[cfg(not(feature="led-as-busy-led"))] + { + let v = unsafe { + #[allow(static_mut_refs)] + &LED + }; + let v = match v { + OurOption::None => { + return; + } + OurOption::Some(v) => v, + }; + let mut b = v.borrow_mut(); + b.set_level(if val { Level::High } else { Level::Low }); + } +} + +// TODO: in future, add another LED for busy +fn set_busy_led_state(val: bool) { + #[cfg(feature="led-as-busy-led")] + { + let v = unsafe { + #[allow(static_mut_refs)] + &LED + }; + let v = match v { + OurOption::None => { + return; + } + OurOption::Some(v) => v, + }; + let mut b = v.borrow_mut(); + b.set_level(if val { Level::High } else { Level::Low }); + } } #[main] @@ -84,8 +106,6 @@ fn main() -> ! { info!("Preparing GPIO"); let mut io = Io::new(peripherals.IO_MUX); - // Set the interrupt handler for GPIO interrupts. - io.set_interrupt_handler(handler); // Set GPIO1 as an output, and set its state low initially. let led = RefCell::new(Output::new(peripherals.GPIO1, Level::Low)); @@ -99,12 +119,7 @@ fn main() -> ! { }; // Set GPIO4 as an input - let mut button = Input::new(peripherals.GPIO3, Pull::Up); - - critical_section::with(|cs| { - button.listen(Event::FallingEdge); - BUTTON.borrow_ref_mut(cs).replace(button) - }); + let mut button = Input::new(peripherals.GPIO3, Pull::Down); info!("Waiting 500ms pre-init"); delay.delay_millis(500); @@ -237,6 +252,7 @@ fn main() -> ! { socket.listen(8080).unwrap(); } + set_busy_led_state(true); if socket.is_connected() { // debug!("Established Connection"); @@ -268,91 +284,110 @@ fn main() -> ! { // break; // } // } - let is_open = unsafe { IS_OPEN }; + let is_open = button.is_high(); + unsafe { + set_open_led_state(is_open); + } let parts = [ - (r##"{ - "api": "0.13", - "api_compatibility": [ - "14", - "15" - ], - "space": "Chaostreff Bern", - "logo": "https://www.chaostreffbern.ch/images/logo_v1.1.png", - "url": "https://www.chaostreffbern.ch","##) - .as_bytes(), + // (r##"{ + // "api": "0.13", + // "api_compatibility": [ + // "14", + // "15" + // ], + // "space": "Chaostreff Bern", + // "logo": "https://www.chaostreffbern.ch/images/logo_v1.1.png", + // "url": "https://www.chaostreffbern.ch","##) + // .as_bytes(), // TODO: Possibly add location.hint that we are in the basement? // TODO: Possibly add location.areas for each area in the space? - (r##" - "location": { - "address": "Zwyssigstrasse 45, 3007 Bern, Switzerland", - "lon": 7.421927, - "lat": 46.944178, - "timezone": "Europe/Zurich", - "country_code": "CH" - }, - "spacefed": { - "spacenet": false, - "spacesaml": false, - "spacephone": false - }, - "state": { - "open": "##) - .as_bytes(), + // (r##" + // "location": { + // "address": "Zwyssigstrasse 45, 3007 Bern, Switzerland", + // "lon": 7.421927, + // "lat": 46.944178, + // "timezone": "Europe/Zurich", + // "country_code": "CH" + // }, + // "spacefed": { + // "spacenet": false, + // "spacesaml": false, + // "spacephone": false + // }, + // "state": { + // "open": "##) + // .as_bytes(), (if is_open { "true" } else { "false" }).as_bytes(), - r##", - "message": "Open every Tuesday from 19h" - }, - "contact": { - "email": "info@chaostreffbern.ch", - "ml": "bern@chaostreff.ch", - "matrix": "#chaostreffbern:chaostreffbern.ch", - "jabber": "xmpp://chaostreffbern@conference.chaostreffbern.ch", - "mastodon": "@chaostreffbern@chaos.social" - }, - "issue_report_channels": [ - "email" - ], - "feeds": { - "blog": { - "type": "rss", - "url": "https://www.chaosbern.ch/feeds/chaosbern_rss.xml" - }, - "calendar": { - "type": "caldav", - "url": "https://nextcloud.jenix.ch/remote.php/dav/public-calendars/xFMZfKSBNfp3mRNR/" - } - }, - "ext_ccc": "chaostreff" -} -"## - .as_bytes(), +// r##", +// "message": "Open every Tuesday from 19h" +// }, +// "contact": { +// "email": "info@chaostreffbern.ch", +// "ml": "bern@chaostreff.ch", +// "matrix": "#chaostreffbern:chaostreffbern.ch", +// "jabber": "xmpp://chaostreffbern@conference.chaostreffbern.ch", +// "mastodon": "@chaostreffbern@chaos.social" +// }, +// "issue_report_channels": [ +// "email" +// ], +// "feeds": { +// "blog": { +// "type": "rss", +// "url": "https://www.chaosbern.ch/feeds/chaosbern_rss.xml" +// }, +// "calendar": { +// "type": "caldav", +// "url": "https://nextcloud.jenix.ch/remote.php/dav/public-calendars/xFMZfKSBNfp3mRNR/" +// } +// }, +// "ext_ccc": "chaostreff" +// } +// "## +// .as_bytes(), ]; // if !time_out { - socket - .write_all( - b"HTTP/1.0 200 OK\r\n\ + let r =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(); + ); + if !r.is_ok() { + error!("{:#?}",r.unwrap_err()); + continue; + }; for part in parts { - socket.write_all(part).unwrap(); + let r = socket.write_all(part); + + if !r.is_ok() { + error!("{:#?}", r.unwrap_err()); + continue; + }; } - socket.flush().unwrap(); + let r = socket.flush(); + if !r.is_ok() { + error!("{:#?}",r.unwrap_err()); + continue; + }; socket.work(); // } socket.close(); } + set_busy_led_state(false); // TODO: what let deadline = time::now() + Duration::millis(100); while time::now() < deadline && !socket.is_connected() { socket.work(); } + + let is_open = button.is_high(); + set_open_led_state(is_open); } } @@ -362,20 +397,4 @@ fn parse_ip(ip: &str) -> [u8; 4] { result[idx] = u8::from_str_radix(octet, 10).unwrap(); } result -} - -#[handler] -fn handler() { - critical_section::with(|cs| { - info!("GPIO interrupt"); - let mut binding = BUTTON.borrow_ref_mut(cs); - let v = binding.as_mut().unwrap(); - if v.is_low() { - unsafe { - IS_OPEN = !IS_OPEN; - set_led_state(IS_OPEN); - } - } - v.clear_interrupt(); - }); -} +}
\ No newline at end of file |