Skip to content

Commit

Permalink
Initial commit of ticker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
arosspope committed Oct 21, 2023
1 parent 9f605a1 commit 628e4f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
46 changes: 46 additions & 0 deletions src/dot_display.rs → src/display.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use log::*;
use anyhow::{Result, Error};
use esp_idf_hal::gpio::{
PinDriver,
Gpio0, Gpio1, Gpio2,
Output
};
use font8x8::{BASIC_FONTS, UnicodeFonts};
use max7219::{connectors::*, *};

pub type DisplayPins<'a> =
Expand Down Expand Up @@ -83,3 +86,46 @@ impl DotDisplay<'_> {
Ok(())
}
}

pub struct Ticker<'a> {
shift: usize,
index: usize,
len: usize,
message: [u8; 100],
pub display: DotDisplay<'a>,
}

impl Ticker<'_> {
pub fn new<'a>(display: DotDisplay<'a>) -> Ticker<'a> {
Ticker {
shift: 0,
index: 0,
len: 0,
message: [0; 100],
display: display,
}
}

pub fn set_message(&mut self, message: &str) {
debug!("Setting ticker-tape message: {:?}", message);
self.len = message.len();
self.message[..self.len].copy_from_slice(message.as_bytes().try_into().unwrap());
}

pub fn tick(&mut self) {
if self.shift >= 8 {
self.shift = 0;
self.index = (self.index + 1) % self.len;
}

let c = self.message[self.index] as char;

if let Some(mut glyph) = BASIC_FONTS.get(c) {
glyph.iter_mut().for_each(|x| *x = x.reverse_bits() << self.shift);

self.display.write_display(&glyph).expect("Failed to write dot-matrix");
}

self.shift += 1;
}
}
28 changes: 11 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ use esp_idf_hal::{
prelude::Peripherals,
gpio::PinDriver
};
use esp_idf_svc::{
http::server::{Configuration, EspHttpServer},
};
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use embedded_svc::{http::{Method, Headers}, io::{Write, Read}};
use esp_idf_svc::http::server::{Configuration, EspHttpServer};
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;
mod display;
use display::{DotDisplay, Ticker};

mod wifi;
use wifi::Wifi;
Expand Down Expand Up @@ -55,10 +53,10 @@ fn main() -> Result<()> {
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).expect("Failed to initialise dot-matrix");
dp.turn_on_display().expect("Failed to turn on display");

let mut ticker = Ticker::new(
DotDisplay::from(MAX7219::from_pins(1, data, cs, sck).unwrap()).unwrap(),
);
ticker.set_message("Hello world");

// Set the HTTP server
let mut server = EspHttpServer::new(&Configuration::default())?;
Expand Down Expand Up @@ -89,14 +87,10 @@ fn main() -> Result<()> {
Ok(())
})?;

dp.set_brightness(80).expect("Failed to set brightness");
ticker.display.set_brightness(80).expect("Failed to set brightness");
let mut seed = 0;
loop {
if let Some(mut glyph) = BASIC_FONTS.get(char::from_digit((seed % 10) as u32, 10).unwrap()) {
glyph.iter_mut().for_each(|x| *x = x.reverse_bits());

dp.write_display(&glyph).expect("Failed to write dot-matrix");
}
ticker.tick();

seed += 1;

Expand All @@ -106,7 +100,7 @@ fn main() -> Result<()> {
led.set_pixel(RGB8::new(0, 50, 0))?; // Green
}

std::thread::sleep(std::time::Duration::from_secs(1));
std::thread::sleep(std::time::Duration::from_millis(100));
}
}

Expand Down

0 comments on commit 628e4f2

Please sign in to comment.