diff --git a/src/display.rs b/src/display.rs index 51f543c..50e2d1a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,3 +1,5 @@ +use core::ops::Mul; + use atmega_hal::{ Spi, port::{self, Pin, PinOps, mode}, @@ -5,6 +7,7 @@ use atmega_hal::{ }; use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiBus}; +#[derive(Clone, Copy)] pub struct Rgb565(pub u8, pub u8, pub u8); impl Rgb565 { @@ -15,6 +18,42 @@ impl Rgb565 { Color::from(r | b | g) } + + pub fn red() -> Rgb565 { + Rgb565(255, 0, 0) + } + + pub fn green() -> Rgb565 { + Rgb565(0, 255, 0) + } + + pub fn blue() -> Rgb565 { + Rgb565(0, 0, 255) + } + + pub fn yellow() -> Rgb565 { + Rgb565(255, 255, 0) + } + + pub fn purple() -> Rgb565 { + Rgb565(255, 0, 255) + } + + pub fn cyan() -> Rgb565 { + Rgb565(0, 255, 255) + } +} + +impl Mul for Rgb565 { + type Output = Rgb565; + + fn mul(self, rhs: f32) -> Self::Output { + Rgb565( + (self.0 as f32 * rhs) as u8, + (self.1 as f32 * rhs) as u8, + (self.2 as f32 * rhs) as u8, + ) + } } #[derive(Default, Clone, Copy)] diff --git a/src/font.rs b/src/font.rs new file mode 100644 index 0000000..13f7837 --- /dev/null +++ b/src/font.rs @@ -0,0 +1,9 @@ +fn letter(character: char) -> [u8; 8] { + match character { + 'a' => [ + 0b01111110, 0b01000010, 0b10000001, 0b11111111, 0b10000001, 0b10000001, 0b10000001, + 0b10000001, + ], + _ => [0; 8], + } +} diff --git a/src/main.rs b/src/main.rs index 437b5cf..0cbc359 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ #![no_main] use atmega_hal::{ - Usart, + Adc, Usart, spi::{self, Settings}, usart::Baudrate, }; @@ -15,6 +15,7 @@ use panic_halt as _; use crate::display::{Display, Position, Rgb565}; mod display; +mod font; mod image; type CoreClock = atmega_hal::clock::MHz8; @@ -61,17 +62,27 @@ fn main() -> ! { display.init(); + let mut adc: Adc = atmega_hal::Adc::new(dp.ADC, Default::default()); + + let button = pins.pd5.into_pull_up_input(); + let potentiometer = pins.pc1.into_analog_input(&mut adc); + + let colors = [Rgb565::red(), Rgb565::green(), Rgb565::blue()]; + let mut idx = 0; + let mut delay = atmega_hal::delay::Delay::::new(); - let mut color = 0; loop { - // color += 40; - delay.delay_ms(1000); + let raw_input = potentiometer.analog_read(&mut adc); + let input = raw_input as f32 / 1024f32; + display.draw_rect( Position { x: 0, y: 0 }, Position { x: 200, y: 200 }, - Rgb565(color % 255, color % 100, color % 50).as_color(), + (colors[idx] * input).as_color(), ); - color = color + 50; - ufmt::uwriteln!(&mut serial, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + if button.is_low() { + idx = (idx + 1) % colors.len(); + } + delay.delay_ms(1000); } }