Skip to content

Commit

Permalink
Initial commit of dot-display firmware. Still a WIP (especially error…
Browse files Browse the repository at this point in the history
… handling), but can write to the display!
  • Loading branch information
arosspope committed Aug 6, 2023
1 parent 59032a5 commit a47a7cb
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ embedded-svc = { version = "0.25", optional = true, default-features = false }
toml-cfg = "0.1.3"
anyhow = "1.0.72"
rgb = "0.8.36"
max7219 = "0.4.0"

[patch.crates-io]
esp-idf-svc = { git="https://github.com/esp-rs/esp-idf-svc"}
Expand Down
110 changes: 110 additions & 0 deletions src/dot_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use anyhow::{Result};
use esp_idf_hal::gpio::{
PinDriver,
Gpio0, Gpio1, Gpio2,
Output
};
use max7219::{connectors::*, *};

#[derive(Debug)]
pub struct Error;

impl From<core::convert::Infallible> for Error {
fn from(_: core::convert::Infallible) -> Self {
Error {}
}
}

// impl From<nb::Error<()>> for Error {
// fn from(_: nb::Error<()>) -> Self {
// Error {}
// }
// }

impl From<max7219::DataError> for Error {
fn from(_: max7219::DataError) -> Self {
Error {}
}
}

impl From<()> for Error {
fn from(_: ()) -> Self {
Error {}
}
}

pub type DisplayPins<'a> =
PinConnector<PinDriver<'a, Gpio0, Output>, PinDriver<'a, Gpio1, Output>, PinDriver<'a, Gpio2, Output>>;

pub struct DotDisplay<'a> {
display: MAX7219<DisplayPins<'a>>,
display_is_on: bool,
}

impl DotDisplay<'_> {
pub fn from(display: MAX7219<DisplayPins>) -> DotDisplay<'_> {
let mut controller = DotDisplay {
display,
display_is_on: true,
};

// Start the DotDisplay in a known state
controller.reset_display();
controller.turn_off_display().unwrap();
controller
}

pub fn write_display(&mut self, input: &[u8; 8]) -> Result<()> {
if !self.display_is_on {
self.turn_on_display()?;
}

self.display.write_raw(0, &input).unwrap();

Ok(())
}

pub fn turn_off_display(&mut self) -> Result<()> {
if !self.display_is_on {
// return Err(Error{});
}

self.display.power_off().unwrap();
self.display_is_on = false;
Ok(())
}

pub fn turn_on_display(&mut self) -> Result<()> {
if self.display_is_on {
// return Err(Error);
}

self.display.power_on().unwrap();
self.display_is_on = true;
Ok(())
}

pub fn toggle_display(&mut self) -> Result<()> {
if self.display_is_on {
self.turn_off_display().unwrap();
} else {
self.turn_on_display().unwrap();
}

Ok(())
}

pub fn reset_display(&mut self) -> Result<()> {
Ok(self.display.clear_display(0).unwrap())
}

pub fn set_brightness(&mut self, brightness: u8) -> Result<()> {
if brightness > 100 {
// return Err(Error);
}

let brightness = (brightness as f32 * 2.55) as u8;
self.display.set_intensity(0, brightness).unwrap();
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/led.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result; // TODO: why use anyhow?
use core::time::Duration;
use esp_idf_hal::{
gpio::{OutputPin},
gpio::OutputPin,
rmt::{config::TransmitConfig, FixedLengthSignal, PinState, Pulse, TxRmtDriver, RmtChannel}, peripheral::Peripheral,
};

Expand Down
34 changes: 27 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use anyhow::{Result};
use esp_idf_hal::prelude::Peripherals;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use anyhow::Result;
use esp_idf_hal::{
prelude::Peripherals,
gpio::PinDriver
};
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use log::*;
use max7219::*;

mod led;
use led::{RGB8, WS2812RMT};

mod dot_display;
use dot_display::DotDisplay;

#[toml_cfg::toml_config]
pub struct Config {
#[default("")]
Expand All @@ -23,21 +29,35 @@ fn main() -> Result<()> {
esp_idf_svc::log::EspLogger::initialize_default();

let peripherals = Peripherals::take().unwrap();
let sysloop = EspSystemEventLoop::take()?;

// The constant CONFIG is auto-generated by toml_config
let app_config = CONFIG;
let mut led = WS2812RMT::new(peripherals.pins.gpio2, peripherals.rmt.channel0)?;
let mut led = WS2812RMT::new(peripherals.pins.gpio18, peripherals.rmt.channel0)?;
led.set_pixel(RGB8::new(50, 50, 0))?;

info!("Hello, world! {:?}{:?}", app_config.wifi_ssid, app_config.wifi_psk);
info!("Loading with credentials, ssid:{:?} psk:{:?}", app_config.wifi_ssid, app_config.wifi_psk);

// Setup for the MAX7219 display
// let data = peripherals.pins.gpio0.into();
// let cs = peripherals.pins.gpio1.into();
// let sck = peripherals.pins.gpio2.into();
// let display = MAX7219::from_pins(1, data, cs, sck).unwrap();

// let mut dp = DotDisplay::from(display).unwrap();
let data = PinDriver::output(peripherals.pins.gpio0).unwrap();
let cs = PinDriver::output(peripherals.pins.gpio1).unwrap();
let sck = PinDriver::output(peripherals.pins.gpio2).unwrap();
let display = MAX7219::from_pins(1, data, cs, sck).unwrap();
let mut dp = DotDisplay::from(display);

loop {
dp.turn_on_display()?;
dp.write_display(&[1,2,3,4,5,6,7,8])?;

// Blue!
led.set_pixel(RGB8::new(0, 0, 50))?;
// Wait...
std::thread::sleep(std::time::Duration::from_secs(1));
info!("Hello, world!");

// Green!
led.set_pixel(RGB8::new(0, 50, 0))?;
Expand Down

0 comments on commit a47a7cb

Please sign in to comment.