dice-game/src/main.rs
2025-09-13 15:00:42 +03:00

122 lines
3.0 KiB
Rust

/*!
* Blink the builtin LED - the "Hello World" of embedded programming.
*/
#![no_std]
#![no_main]
#![feature(iter_array_chunks)]
#![feature(const_slice_make_iter)]
#![feature(slice_as_chunks)]
#![feature(asm_experimental_arch)]
use core::ptr::addr_of;
use atmega_hal::{
Usart,
spi::{self, Settings},
usart::Baudrate,
};
use panic_halt as _;
use crate::{
display::{Display, Rgb565, Vec2},
font::{Letter, draw_text},
graphics::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image},
peripherals::Button,
};
mod display;
mod font;
mod graphics;
mod peripherals;
type CoreClock = atmega_hal::clock::MHz8;
#[avr_device::entry]
fn main() -> ! {
let dp = atmega_hal::Peripherals::take().unwrap();
let pins = atmega_hal::pins!(dp);
let rx = pins.pd0;
let tx = pins.pd1;
let mut serial = Usart::new(
dp.USART0,
rx,
tx.into_output(),
Baudrate::<CoreClock>::new(57600),
);
// let eeprom = Eeprom::new(dp.EEPROM);
// ufmt::uwriteln!(serial, "Eeprom capacity: {}", eeprom.capacity()).unwrap();
let cs = pins.pb2.into_output();
let (spi, cs) = spi::Spi::new(
dp.SPI,
pins.pb5.into_output(),
pins.pb3.into_output(),
pins.pb4.into_pull_up_input(),
cs,
Settings {
data_order: spi::DataOrder::MostSignificantFirst,
clock: spi::SerialClockRate::OscfOver2,
mode: embedded_hal::spi::Mode {
polarity: embedded_hal::spi::Polarity::IdleHigh,
phase: embedded_hal::spi::Phase::CaptureOnFirstTransition,
},
},
);
let mut display = Display {
spi,
dc: pins.pb1.into_output(),
cs,
rst: pins.pd7.into_output(),
delay: atmega_hal::delay::Delay::<CoreClock>::new(),
};
display.init();
let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut idx = 0;
let images = [Image::from(addr_of!(LARGE_CAT_UNSAFE))];
let len = images.len();
// match draw_image(
// &mut serial,
// &mut Image::from(addr_of!(PRESS_BTN_UNSAFE)).iter(),
// &mut display,
// Vec2 { x: 0, y: 0 },
// ) {
// Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
// Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),
// }
let letter = Letter::from('h', Rgb565::white(), Rgb565::black());
draw_text(
&mut display,
"hello there!",
Rgb565::white(),
Rgb565::red(),
Vec2 { x: 10, y: 10 },
3,
);
loop {
if button.poll() {
match draw_image(
&mut serial,
&mut images[idx].iter(),
&mut display,
Vec2 { x: 0, y: 0 },
) {
Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),
}
idx = (idx + 1) % len;
}
}
}