diff --git a/orangepunk-hardware/src/main.rs b/orangepunk-hardware/src/main.rs index 8c2a532..5f02b1f 100644 --- a/orangepunk-hardware/src/main.rs +++ b/orangepunk-hardware/src/main.rs @@ -1,13 +1,27 @@ #![no_std] #![no_main] -use arduino_hal::prelude::*; +use core::str; + +use arduino_hal::{delay_ms, hal::Atmega, pac::{fuse::high, USART0}, prelude::*, Usart}; use panic_halt as _; #[arduino_hal::entry] fn main() -> ! { + let low_expressions = [ + "Choomba, grow up", + "Too low too slow", + "Whadya got, a case of small?" + ]; + let high_expression = [ + "Big thoughts for someone so base", + "Choomba, slow your cool", + "The tall fall the loudest", + ]; + let number: usize = 855; let dp = arduino_hal::Peripherals::take().unwrap(); let pins = arduino_hal::pins!(dp); + let mut buff = [0u8; 1024]; let mut led = pins.d13.into_output(); //let mut serial = arduino_hal::default_serial!(dp, pins, 57600); @@ -20,20 +34,103 @@ fn main() -> ! { arduino_hal::hal::usart::BaudrateArduinoExt::into_baudrate(57600), ); - // Create another serial port using software - // https://en.wikipedia.org/wiki/Bit_banging - let tx = pins.d8.into_output(); - let rx = pins.d9.into_floating_input(); - let tmr = dp.TC1; - ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").unwrap_infallible(); + let mut i = 0; loop { - led.toggle(); - // Read a byte from the serial connection - let b = nb::block!(serial.read()).unwrap_infallible(); + serial.write_buff(b"> "); + let len = read_line(&mut serial, &mut buff).unwrap(); + let s = match str::from_utf8_mut(&mut buff[..len]) { + Ok(s) => s, + Err(_) => { + write_str(&mut serial, "failed to parse line"); + continue; + }, + }; + let g = match s.trim().parse::() { + Ok(g) => g, + Err(_) => { + write_str(&mut serial, "that wasn't a number choomba"); + continue; + } + }; + if g == number { + write_str(&mut serial, "Entrance granted"); + led.toggle(); + loop { + delay_ms(1000); + } + } else if g < number { + write_str(&mut serial, low_expressions[i%low_expressions.len()]); + } else { + write_str(&mut serial, high_expression[i%high_expression.len()]); + } + i += 1; - // Answer - ufmt::uwriteln!(&mut serial, "Got {}!\r", b).unwrap_infallible(); + if i > 10 { + write_str(&mut serial, "Gonk, you done tripped it. Get fried!"); + loop { + delay_ms(500); + led.toggle(); + } + } + } +} + +fn read_line>(reader: &mut R, buff: &mut [u8]) -> Result { + let mut count = 0; + loop { + let c = reader.read_byte(); + if c == b'\n' || c == b'\r' { + reader.write_byte(b'\r'); + reader.write_byte(b'\n'); + break; + } + reader.write_byte(c); + buff[count] = c; + count += 1; + if count == buff.len() { + return Err("not enough space"); + } + } + Ok(count) +} + +fn write_line>(writer: &mut W, buff: &[u8]) { + writer.write_buff(buff); + writer.write_byte(b'\r'); + writer.write_byte(b'\n'); +} + +fn write_str>(writer: &mut W, buff: &str) { + writer.write_buff(buff.as_bytes()); + writer.write_byte(b'\r'); + writer.write_byte(b'\n'); +} + +trait Read { + fn read_byte(&mut self) -> u8; +} + +impl Read for Usart +where USART: arduino_hal::usart::UsartOps { + fn read_byte(&mut self) -> u8 { + Usart::read_byte(self) + } +} + +trait Writer { + fn write_byte(&mut self, _: C); + fn write_buff(&mut self, buff: &[C]) { + for c in buff { + self.write_byte(*c); + } + } +} + +impl Writer for Usart +where USART: arduino_hal::usart::UsartOps { + fn write_byte(&mut self, b: u8) { + Usart::write_byte(self, b); } } \ No newline at end of file