add: guessing game

This commit is contained in:
Charles
2024-12-06 22:45:47 -08:00
parent f6d3cc497f
commit c1d6c7364c
+108 -11
View File
@@ -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 {
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::<usize>() {
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();
// Read a byte from the serial connection
let b = nb::block!(serial.read()).unwrap_infallible();
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<R: Read + Writer<u8>>(reader: &mut R, buff: &mut [u8]) -> Result<usize, &'static str> {
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<W: Writer<u8>>(writer: &mut W, buff: &[u8]) {
writer.write_buff(buff);
writer.write_byte(b'\r');
writer.write_byte(b'\n');
}
fn write_str<W: Writer<u8>>(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<USART, RX, TX> Read for Usart<USART, RX, TX>
where USART: arduino_hal::usart::UsartOps<Atmega, RX, TX> {
fn read_byte(&mut self) -> u8 {
Usart::read_byte(self)
}
}
trait Writer<C: Copy> {
fn write_byte(&mut self, _: C);
fn write_buff(&mut self, buff: &[C]) {
for c in buff {
self.write_byte(*c);
}
}
}
impl<USART, RX, TX> Writer<u8> for Usart<USART, RX, TX>
where USART: arduino_hal::usart::UsartOps<Atmega, RX, TX> {
fn write_byte(&mut self, b: u8) {
Usart::write_byte(self, b);
}
}