dice-game/src/main.rs
2025-09-12 00:18:06 +03:00

109 lines
2.5 KiB
Rust

/*!
* Blink the builtin LED - the "Hello World" of embedded programming.
*/
#![no_std]
#![no_main]
use atmega_hal::{
Adc, Usart,
port::{Pin, mode},
spi::{self, Settings},
usart::Baudrate,
};
use embedded_hal::delay::DelayNs;
use panic_halt as _;
use crate::{
display::{Display, Position, Rgb565},
peripherals::{Button, Knob},
};
mod display;
mod font;
mod image;
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 cs = pins.pb2.into_output();
let (mut spi, mut 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<CoreClock> = atmega_hal::Adc::new(dp.ADC, Default::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 colors = [
Rgb565::red(),
Rgb565::green(),
Rgb565::blue(),
Rgb565::cyan(),
Rgb565::magenta(),
Rgb565::yellow(),
];
let mut idx = 0;
let mut delay = atmega_hal::delay::Delay::<CoreClock>::new();
let mut last_input = 0f32;
loop {
let input = knob.poll();
let button_poll = button.poll();
if button_poll {
idx = (idx + 1) % colors.len();
}
if last_input != input || button_poll {
last_input = input;
display.draw_rect(
Position { x: 0, y: 0 },
Position { x: 200, y: 200 },
(colors[idx] * input).as_color(),
);
}
}
}