dice-game/src/main.rs
2025-09-13 19:37:06 +03:00

190 lines
4.7 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::{
Adc, Usart,
adc::AdcSettings,
spi::{self, Settings},
usart::Baudrate,
};
use panic_halt as _;
use crate::{
display::{Display, Rgb565, Vec2},
font::{draw_number, draw_text},
graphics::{Image, LARGE_CAT_UNSAFE},
peripherals::{Button, Knob},
};
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 adc = Adc::new(dp.ADC, AdcSettings::default());
let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut knob = Knob {
pin: pins.pc1.into_analog_input(&mut adc),
adc,
};
let mut idx = 0;
let images = [Image::from(addr_of!(LARGE_CAT_UNSAFE))];
let len = images.len();
let mut position = Vec2 { x: 10, y: 10 };
draw_text(
&mut display,
"button:\n",
Rgb565::white(),
Rgb565::black(),
&mut position,
3,
);
let button_pos = position.clone();
let mut counter = 0;
draw_number(
&mut display,
counter,
Rgb565::white(),
Rgb565::black(),
&mut button_pos.clone(),
3,
);
draw_text(
&mut display,
"\nvolume:\n",
Rgb565::white(),
Rgb565::black(),
&mut position,
3,
);
let volume_pos = position.clone();
let mut last_volume = knob.raw();
let mut counter = 0;
draw_number(
&mut display,
last_volume.into(),
Rgb565::white(),
Rgb565::black(),
&mut volume_pos.clone(),
3,
);
loop {
let volume = knob.raw();
if volume != last_volume {
if volume == 0 || last_volume == 0 || last_volume.ilog10() > volume.ilog10() {
// Clear previous
draw_text(
&mut display,
" ",
Rgb565::white(),
Rgb565::black(),
&mut volume_pos.clone(),
3,
);
}
last_volume = volume;
draw_number(
&mut display,
volume.into(),
Rgb565::white(),
Rgb565::black(),
&mut volume_pos.clone(),
3,
);
}
if button.poll() {
counter += 1;
draw_number(
&mut display,
counter,
Rgb565::white(),
Rgb565::black(),
&mut button_pos.clone(),
3,
);
// 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(),
// }
// draw_text(
// &mut display,
// "you just lost the game!",
// Rgb565::white(),
// Rgb565::black(),
// &mut Vec2 { x: 10, y: 10 },
// 3,
// );
// idx = (idx + 1) % len;
}
}
}