~pcein/max7219-pico-rust

89d34d662e9d0206af564cf94af2a49641430fb9 — Pramode 1 year, 2 months ago 48dad15 master
Some cleanups
2 files changed, 34 insertions(+), 28 deletions(-)

M src/cp437.rs
M src/main.rs
M src/cp437.rs => src/cp437.rs +1 -1
@@ 3,7 3,7 @@

// Also see: https://github.com/riyas-org/max7219/blob/master/max7219-rpi/src/transitions.py

pub(crate) static cp437_FONT: [[u8; 8]; 256] = [
pub(crate) static CP437_FONT: [[u8; 8]; 256] = [
    [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // 0x00
    [0x7E, 0x81, 0x95, 0xB1, 0xB1, 0x95, 0x81, 0x7E], // 0x01
    [0x7E, 0xFF, 0xEB, 0xCF, 0xCF, 0xEB, 0xFF, 0x7E], // 0x02

M src/main.rs => src/main.rs +33 -27
@@ 1,22 1,16 @@
//! Blinks the LED on a Pico board
//! Display a scrolling message on an 8x8 LED matrix.
//!
//! This will blink an LED attached to GP25, which is the pin the Pico uses for the on-board LED.
//! Uses the Pico SPI interface to communicate with a MAX7219
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use defmt::*;
use defmt_rtt as _;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex;
use embedded_time::fixed_point::FixedPoint;
use embedded_time::rate::Extensions;
use panic_probe as _;

// Provide an alias for our BSP so we can switch targets quickly.
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
use rp_pico as bsp;
// use sparkfun_pro_micro_rp2040 as bsp;

use bsp::hal::{
    clocks::{init_clocks_and_plls, Clock},


@@ 27,7 21,15 @@ use bsp::hal::{
};

mod cp437;
use cp437::cp437_FONT;
use cp437::CP437_FONT;

// Refer Page 7 of:
// https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf
const MAX7219_REG_DECODEMODE:u8  = 0x9;
const MAX7219_REG_INTENSITY:u8   = 0xA;
const MAX7219_REG_SCANLIMIT:u8   = 0xB;
const MAX7219_REG_SHUTDOWN:u8    = 0xC;
const MAX7219_REG_DISPLAYTEST:u8 = 0xF;

struct Max7219<D>
where


@@ 45,34 47,39 @@ where
        Max7219 { spi: s, delay: d }
    }

    fn spi_write(&mut self, reg: u8, val: u8) {
        let _ = self.spi.send(((reg as u16) << 8) + val as u16);
        self.delay.delay_ms(1);
    }

    fn init(&mut self) {
        self.spi.send(0x0b07);
        self.spi.send(0x0900);
        self.spi.send(0x0f00);
        // intensity = 10
        self.spi.send(0x0a0a);
        self.spi_write(MAX7219_REG_SCANLIMIT, 7);
        // Control individual pixels
        self.spi_write(MAX7219_REG_DECODEMODE, 0);
        self.spi_write(MAX7219_REG_DISPLAYTEST, 0);
        // intensity
        self.spi_write(MAX7219_REG_INTENSITY, 0xa);
        // not in shutdown mode
        self.spi.send(0x0c01);
        self.spi_write(MAX7219_REG_SHUTDOWN, 1);
    }

    fn clear(&mut self) {
        for col in 1..9 {
            self.spi.send((col as u16) << 8);
            self.delay.delay_ms(1);
            self.spi_write(col as u8, 0u8);
        }
    }

    fn scroll_left(&mut self, from_char: u8, to_char: u8, font: &[[u8; 8]]) {
        let mut data = 0u16;
        let mut data:u8;
        for i in 0..8 {
            self.delay.delay_ms(90);
            for col in 0..8 {
                if (col + i) < 8 {
                    data = font[from_char as usize][col + i] as u16;
                    data = font[from_char as usize][col + i];
                } else {
                    data = font[to_char as usize][col + i - 8] as u16;
                    data = font[to_char as usize][col + i - 8];
                }
                self.spi.send(((col as u16 + 1) << 8) + data);
                self.spi_write((col + 1) as u8, data);
                self.delay.delay_ms(1);
            }
        }


@@ 84,7 91,7 @@ where
            self.scroll_left(prev as u8, curr as u8, font);
            prev = curr;
        }
        self.scroll_left(prev as u8, ' ' as u8, font);
        self.scroll_left(prev as u8, b' ', font);
    }
}



@@ 116,7 123,7 @@ fn main() -> ! {
        &mut pac.RESETS,
    );

    let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());
    let  delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer());

    let _spi1_sck = pins.gpio14.into_mode::<bsp::hal::gpio::FunctionSpi>();
    let _spi1_tx = pins.gpio15.into_mode::<bsp::hal::gpio::FunctionSpi>();


@@ 124,7 131,7 @@ fn main() -> ! {

    let spi = bsp::hal::Spi::<_, _, 16>::new(pac.SPI1);
    // Exchange the uninitialised SPI driver for an initialised one
    let mut spi = spi.init(
    let  spi = spi.init(
        &mut pac.RESETS,
        clocks.peripheral_clock.freq(),
        1_000_000u32.Hz(),


@@ 135,9 142,8 @@ fn main() -> ! {

    max7219.init();
    max7219.clear();

    loop {
        max7219.show_message("Happy New Year @rustembedded !!", &cp437_FONT);
        max7219.show_message("Happy New Year @rustembedded !!", &CP437_FONT);
    }

    loop {}
}