Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski committed Sep 2, 2023
1 parent ac95f52 commit db8b13f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 43 deletions.
1 change: 1 addition & 0 deletions esp32/matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ esp-idf-sys.workspace = true
esp-idf-hal.workspace = true
anyhow.workspace = true
fastrand = "2.0.0"
shared = { path = "../../shared" }

[build-dependencies]
embuild.workspace = true
11 changes: 6 additions & 5 deletions esp32/matrix/src/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use esp_idf_hal::gpio::{AnyIOPin, InputPin, Output, OutputPin, PinDriver};
use esp_idf_hal::gpio::{AnyIOPin, AnyOutputPin, InputPin, Output, OutputPin, PinDriver};
use esp_idf_hal::peripheral::Peripheral;
use esp_idf_hal::prelude::*;
use esp_idf_hal::spi::{SpiAnyPins, SpiConfig, SpiDeviceDriver, SpiDriver};
use esp_idf_hal::spi::config::DriverConfig;
use esp_idf_hal::units::FromValueType;
use esp_idf_sys::EspError;

#[derive(Copy, Clone)]
Expand All @@ -29,7 +29,8 @@ impl Into<u8> for RegisterAddressMap {
}
}

enum Intensity {
#[derive(Copy, Clone)]
pub enum Intensity {
OneThirtyTwo = 0x00,
ThreeThirtyTwo = 0x01,
FiveThirtyTwo = 0x02,
Expand Down Expand Up @@ -111,9 +112,9 @@ impl<'d, CS: OutputPin, const BUFFER_SIZE: usize, const DISPLAY_COUNT: usize> Ma
) -> anyhow::Result<Self> {
let driver_config = DriverConfig::default();

let driver = SpiDriver::new(spi, sck, mosi, Option::<AnyIOPin>::None, &driver_config)?;
let driver = SpiDriver::new(spi, sck, mosi, None::<AnyIOPin>, &driver_config)?;
let config = SpiConfig::default().baudrate(5.MHz().into());
let mut spi = SpiDeviceDriver::new(driver, Option::<AnyIOPin>::None, &config)?;
let mut spi = SpiDeviceDriver::new(driver, None::<AnyOutputPin>, &config)?;

let mut cs = PinDriver::output(cs)?;

Expand Down
2 changes: 0 additions & 2 deletions esp32/micro-sdcard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ edition.workspace = true
esp-idf-sys.workspace = true
esp-idf-hal.workspace = true
anyhow.workspace = true
button-driver = { version = "0.1.1", features = ["std", "embedded_hal"] }
embedded-sdmmc = { version = "0.5.0" }
ssd1306 = "0.8.0"
embedded-graphics = "0.8.1"
profont = "0.7.0"
numfmt = "1.1.1"
rotary-encoder-embedded = "0.2.0"
shared = { path = "../../shared" }

[build-dependencies]
Expand Down
6 changes: 2 additions & 4 deletions esp32/micro-sdcard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use std::sync::Mutex;
use anyhow::anyhow;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::prelude::*;
use rotary_encoder_embedded::Direction;
use shared::tiny_display::TinyDisplay;

use crate::file_list::FileList;
use crate::micro_sdcard::MicroSdCard;
use crate::rotary_encoder::RotaryEncoder;
use shared::rotary_encoder::{Direction, RotaryEncoder};

mod file_list;
mod micro_sdcard;
mod rotary_encoder;

fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
Expand All @@ -36,7 +34,7 @@ fn main() -> anyhow::Result<()> {

let display = TinyDisplay::new(peripherals.i2c0, sda, scl)?;
let mut sdcard = MicroSdCard::new(peripherals.spi2, sck, mosi, miso, cs)?;
let mut encoder = RotaryEncoder::new(s1_pin, s2_pin, key_pin)?;
let mut encoder = RotaryEncoder::new(s1_pin, s2_pin, Some(key_pin))?;

let files = sdcard.list_files()?;

Expand Down
6 changes: 4 additions & 2 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ rust-version.workspace = true
esp-idf-sys.workspace = true
esp-idf-hal.workspace = true
anyhow.workspace = true
embedded-graphics.workspace = true
profont.workspace = true
ssd1306 = "0.8.0"
embedded-graphics = "0.8.1"
profont = "0.7.0"
numfmt = "1.1.1"
rotary-encoder-embedded = "0.2.0"
button-driver = { version = "0.1.1", features = ["std", "embedded_hal"] }
16 changes: 1 addition & 15 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
pub mod tiny_display;

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub mod rotary_encoder;
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
use button_driver::{Button, ButtonConfig};
use esp_idf_hal::gpio::{Input, InputPin, PinDriver};
use rotary_encoder_embedded::Direction;
pub use rotary_encoder_embedded::Direction;
use rotary_encoder_embedded::standard::StandardMode;

type Callback = Box<dyn Fn(Direction, bool) -> anyhow::Result<()>>;

pub struct RotaryEncoder<'d, CLK: InputPin, DT: InputPin, KEY: InputPin> {
callbacks: Vec<Callback>,
button: Button<PinDriver<'d, KEY, Input>>,
button: Option<Button<PinDriver<'d, KEY, Input>>>,
encoder: rotary_encoder_embedded::RotaryEncoder<StandardMode, PinDriver<'d, DT, Input>, PinDriver<'d, CLK, Input>>,
}

impl<'d, CLK, DT, KEY> RotaryEncoder<'d, CLK, DT, KEY>
where
CLK: InputPin,
DT: InputPin,
KEY: InputPin,
{
impl<'d, CLK: InputPin, DT: InputPin, KEY: InputPin> RotaryEncoder<'d, CLK, DT, KEY> {
pub fn new(
s1_pin: CLK,
s2_pin: DT,
key_pin: KEY,
key_pin: Option<KEY>,
) -> anyhow::Result<RotaryEncoder<'d, CLK, DT, KEY>> {
let clk = PinDriver::input(s1_pin)?;
let dt = PinDriver::input(s2_pin)?;
let key = PinDriver::input(key_pin)?;

let mut button = Button::new(key, ButtonConfig::default());
let mut button = None;

if let Some(key_pin) = key_pin {
button = Some(Button::new(PinDriver::input(key_pin)?, ButtonConfig::default()));
}

Ok(
Self {
Expand All @@ -42,11 +40,14 @@ impl<'d, CLK, DT, KEY> RotaryEncoder<'d, CLK, DT, KEY>
}

pub fn update(&mut self) -> anyhow::Result<()> {
self.button.tick();
if let Some(button) = &mut self.button {
button.tick();
}

self.encoder.update();

for callback in &self.callbacks {
callback(self.direction(), self.button.is_clicked())?;
callback(self.direction(), self.is_clicked())?;
}

self.reset();
Expand All @@ -59,10 +60,15 @@ impl<'d, CLK, DT, KEY> RotaryEncoder<'d, CLK, DT, KEY>
}

pub fn is_clicked(&self) -> bool {
self.button.is_clicked()
match &self.button {
None => false,
Some(button) => button.is_clicked()
}
}

pub fn reset(&mut self) {
self.button.reset()
if let Some(button) = &mut self.button {
button.reset()
}
}
}

0 comments on commit db8b13f

Please sign in to comment.