use atmega_hal::{ Adc, Atmega, adc::{AdcChannel, AdcOps}, pac::ADC, port::{ Pin, PinOps, mode::{self, PullUp}, }, }; use crate::CoreClock; pub struct Button { pin: Pin, T>, was_pressed: bool, pub is_pressed: bool, } impl Button { pub fn from(pin: Pin, T>) -> Button { 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 { pub pin: Pin, pub adc: Adc, } impl Knob where Pin: AdcChannel, { pub fn poll(&mut self) -> f32 { self.raw() as f32 / 1024f32 } pub fn raw(&mut self) -> u16 { self.pin.analog_read(&mut self.adc) } }