2021-12-29

I am only able to control the first out of four 8x8 led matrices powered by Max7219

I have 4 8x8 led matrices that are serial connected and I communicate with the using the SPI protocol.

And after rewriting a few libraries (python implementation, rust implementation, c implementation), because it just does not work on my system and my code works at least a bit I am only able to control the first out of the four connected displays.

Here is my code to display a target like image on the first display:

Matrix Object:

#![allow(dead_code)]
use spidev::Spidev;
use std::io::prelude::*;

// Maximum number of displays connected in series supported
const MAX_DISPLAYS: u8 = 8;

// Digits per addr
const MAX_DIGITS: u8 = 8;

// Possible command register values on the addr chip.
#[derive(Clone, Copy)]
pub enum Command {
    Noop = 0x00,
    Digit0 = 0x01,
    Digit1 = 0x02,
    Digit2 = 0x03,
    Digit3 = 0x04,
    Digit4 = 0x05,
    Digit5 = 0x06,
    Digit6 = 0x07,
    Digit7 = 0x08,
    DecodeMode = 0x09,
    Intensity = 0x0A,
    ScanLimit = 0x0B,
    Power = 0x0C,
    DisplayTest = 0x0F,
}

pub struct Matrix {
    pub spi: Spidev,
    pub devices: u8,
}
impl Matrix {
    pub fn init(&mut self) -> std::io::Result<()> {
        for device in 0..self.devices {
            self.send_bytes([device, Command::ScanLimit as u8, 0x07])?; // to scan for 8 digits or in this case columns
            self.send_bytes([device, Command::DecodeMode as u8, 0x00])?; // No Decode Mode for matrix
            self.send_bytes([device, Command::DisplayTest as u8, 0x00])?; // no displaytest desired
            self.send_bytes([device, Command::Power as u8, 0x01])?; // powers on the display
        }

        Ok(())
    }
    pub fn power_off(&mut self) -> std::io::Result<()> {
        for addr in 0..self.devices {
            self.send_bytes([addr, Command::Power as u8, 0x00])?;
        }
        Ok(())
    }
    pub fn send_command(&mut self, addr: u8, command: Command, value: u8) -> std::io::Result<()> {
        self.send_bytes([addr, command as u8, value])?;
        Ok(())
    }
    pub fn draw_raw(&mut self, addr: u8, data: [u8; 8]) -> std::io::Result<()> {
        let mut digit: u8 = 1; // column
        for b in data {
            self.send_bytes([addr, digit, b])?;
            digit += 1;
        }
        Ok(())
    }
    pub fn clear_display(&mut self, addr: u8) -> std::io::Result<()> {
        for col in 1..=MAX_DIGITS {
            self.send_bytes([addr, col, 0x00])?;
        }
        Ok(())
    }

    /// range 0 to 15
    pub fn set_intensity(&mut self, addr: u8, intesity: u8) -> std::io::Result<()> {
        self.send_bytes([addr, Command::Intensity as u8, intesity])?;
        Ok(())
    }

    fn send_bytes(&mut self, bytes: [u8; 3]) -> std::io::Result<()> {
        self.spi.write(&bytes)?;
        Ok(())
    }
}

main.rs

use spidev::{Spidev, SpidevOptions, SpiModeFlags};
use std::thread::sleep;
use std::time::Duration;

mod matrix;
use matrix::*;

fn create_spi() -> std::io::Result<Spidev> {
    let mut spi = Spidev::open("/dev/spidev0.0")?;
    let options = SpidevOptions::new()
         .bits_per_word(8)
         .max_speed_hz(20_000)
         .mode(SpiModeFlags::SPI_MODE_0)
         .build();
    spi.configure(&options)?;
    Ok(spi)
}

fn main() {
    println!("running");

    let spi = create_spi().unwrap();
    let mut matrix = Matrix{
        spi,
        devices: 4,
    };

    matrix.init().unwrap();

    matrix.draw_raw(0x01,
        [
            0b11111111,
            0b10000001,
            0b10000001,
            0b10011001,
            0b10011001,
            0b10000001,
            0b10000001,
            0b11111111
        ])
        .unwrap();

    sleep(Duration::from_secs(3));
    matrix.power_off().unwrap();
}

Just looking at the first display everything works just right, but these are the problems:

  • on the second display every pixel is lit
  • the third display is fully unlit just as expected
  • the "bitmap" is drawn on display 1 and 4

I think the problem is how I send the data via SPI but I am not sure.

What do I have to for it to work?



from Recent Questions - Stack Overflow https://ift.tt/3z5zlYJ
https://ift.tt/eA8V8J

No comments:

Post a Comment