Tweak a little bit

This commit is contained in:
Sofia 2025-09-12 00:18:06 +03:00
parent d60efaa381
commit 0a71347771
3 changed files with 92 additions and 15 deletions

View File

@ -35,7 +35,7 @@ impl Rgb565 {
Rgb565(255, 255, 0) Rgb565(255, 255, 0)
} }
pub fn purple() -> Rgb565 { pub fn magenta() -> Rgb565 {
Rgb565(255, 0, 255) Rgb565(255, 0, 255)
} }

View File

@ -6,17 +6,22 @@
use atmega_hal::{ use atmega_hal::{
Adc, Usart, Adc, Usart,
port::{Pin, mode},
spi::{self, Settings}, spi::{self, Settings},
usart::Baudrate, usart::Baudrate,
}; };
use embedded_hal::delay::DelayNs; use embedded_hal::delay::DelayNs;
use panic_halt as _; use panic_halt as _;
use crate::display::{Display, Position, Rgb565}; use crate::{
display::{Display, Position, Rgb565},
peripherals::{Button, Knob},
};
mod display; mod display;
mod font; mod font;
mod image; mod image;
mod peripherals;
type CoreClock = atmega_hal::clock::MHz8; type CoreClock = atmega_hal::clock::MHz8;
@ -64,25 +69,40 @@ fn main() -> ! {
let mut adc: Adc<CoreClock> = atmega_hal::Adc::new(dp.ADC, Default::default()); let mut adc: Adc<CoreClock> = atmega_hal::Adc::new(dp.ADC, Default::default());
let button = pins.pd5.into_pull_up_input(); let mut button = Button::from(pins.pd5.into_pull_up_input());
let potentiometer = pins.pc1.into_analog_input(&mut adc); let mut knob = Knob {
pin: pins.pc1.into_analog_input(&mut adc),
adc,
};
let colors = [Rgb565::red(), Rgb565::green(), Rgb565::blue()]; let colors = [
Rgb565::red(),
Rgb565::green(),
Rgb565::blue(),
Rgb565::cyan(),
Rgb565::magenta(),
Rgb565::yellow(),
];
let mut idx = 0; let mut idx = 0;
let mut delay = atmega_hal::delay::Delay::<CoreClock>::new(); let mut delay = atmega_hal::delay::Delay::<CoreClock>::new();
loop {
let raw_input = potentiometer.analog_read(&mut adc);
let input = raw_input as f32 / 1024f32;
display.draw_rect( let mut last_input = 0f32;
Position { x: 0, y: 0 }, loop {
Position { x: 200, y: 200 }, let input = knob.poll();
(colors[idx] * input).as_color(), let button_poll = button.poll();
);
if button.is_low() { if button_poll {
idx = (idx + 1) % colors.len(); idx = (idx + 1) % colors.len();
} }
delay.delay_ms(1000);
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(),
);
}
} }
} }

57
src/peripherals.rs Normal file
View File

@ -0,0 +1,57 @@
use atmega_hal::{
Adc, Atmega,
adc::{AdcChannel, AdcOps},
pac::ADC,
port::{
Pin, PinOps,
mode::{self, PullUp},
},
};
use crate::CoreClock;
pub struct Button<T: PinOps> {
pin: Pin<mode::Input<PullUp>, T>,
was_pressed: bool,
pub is_pressed: bool,
}
impl<T: PinOps> Button<T> {
pub fn from(pin: Pin<mode::Input<PullUp>, T>) -> Button<T> {
Button {
pin,
is_pressed: false,
was_pressed: false,
}
}
pub fn poll(&mut self) -> bool {
self.is_pressed = self.pin.is_low();
if self.is_pressed {
if !self.was_pressed {
self.was_pressed = true;
true
} else {
false
}
} else {
self.was_pressed = false;
false
}
}
}
pub struct Knob<T: PinOps> {
pub pin: Pin<mode::Analog, T>,
pub adc: Adc<CoreClock>,
}
impl<T: PinOps> Knob<T>
where
Pin<mode::Analog, T>: AdcChannel<Atmega, ADC>,
{
pub fn poll(&mut self) -> f32 {
let read = self.pin.analog_read(&mut self.adc);
read as f32 / 1024f32
}
}