Skip to content

Commit

Permalink
Mainly bug fix for the setPixel function with some internal improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro707 committed Oct 11, 2020
1 parent f5e4bba commit fb82d0c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v1.4.1
-Fixed a bug where the setPixel function didn't have a function definition
-Added a clearPixel function
-Improved interal programming (mainly for the sendMatrixBuffer function)
-Changed the ScanPixels example to use the clearPixel and setPixel functions
v1.4
-Added support for any size LED matrix
-Added option for left-to-right and right-to-left text scrolling
Expand Down
13 changes: 4 additions & 9 deletions examples/ScanPixels/ScanPixels.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#define DELAY 10 //Sets the delay between each frame in mS
#define NUMBER_OF_DISPLAYS 4 //Sets the number of display (4 for this example)
uint8_t matrix_b[NUMBER_OF_DISPLAYS*8]; //Column-adressed array to store what's on the display

/*
Initialize the library. The 4 means that the CS pin is connected to pin D4.
Expand Down Expand Up @@ -31,32 +30,28 @@ void loop(){
//This sets an LED ON sequentially per row.
for(int i=0;i<8;i++){ //Repreat per row
for(int k=0;k<(NUMBER_OF_DISPLAYS*8);k++){ //Repeat per column
matrix_b[k] |= (1<<i); //Sets a 1 to the i-th bit(row number) in the k(column number) element
disp.sendMatrixBuffer(matrix_b); //Send the matrix buffer
disp.setPixel(i, k);
delay(DELAY); //Delay by DELAY mS
}
}
//This sets an LED OFF sequentially per row.
for(int i=0;i<8;i++){ //Repreat per row
for(int k=0;k<(NUMBER_OF_DISPLAYS*8);k++){ //Repeat per column
matrix_b[k] &= ~(1<<i); //Sets a 0 to the i-th bit(row number) in the k(column number) element
disp.sendMatrixBuffer(matrix_b); //Send the matrix buffer
disp.clearPixel(i, k);
delay(DELAY); //Delay by DELAY mS
}
}
//This sets an LED ON sequentially per column.
for(int i=0;i<(NUMBER_OF_DISPLAYS*8);i++){ //Repreat per column
for(int k=0;k<8;k++){ //Repreat per row
matrix_b[i] |= (1<<k); //Sets a 1 to the k-th bit(row number) in the i(column number) element
disp.sendMatrixBuffer(matrix_b); //Send the matrix buffer
disp.setPixel(k, i);
delay(DELAY); //Delay by DELAY mS
}
}
//This sets an LED OFF sequentially per column.
for(int i=0;i<(NUMBER_OF_DISPLAYS*8);i++){ //Repreat per column
for(int k=0;k<8;k++){ //Repreat per row
matrix_b[i] &= ~(1<<k); //Sets a 0 to the k-th bit(row number) in the i(column number) element
disp.sendMatrixBuffer(matrix_b); //Send the matrix buffer
disp.clearPixel(k, i);
delay(DELAY); //Delay by DELAY mS
}
}
Expand Down
2 changes: 1 addition & 1 deletion extra/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Simple LED Matrix Library"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 1.4
PROJECT_NUMBER = 1.4.1

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ scrollTextPROGMEMRightToLeft KEYWORD2
print KEYWORD2
setIntensity KEYWORD2
setPixel KEYWORD2
clearPixel KEYWORD2
sendColumnBuffer KEYWORD2
scrollBuffer KEYWORD2
sendMatrixBuffer KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Simple LED Matrix
version=1.4
version=1.4.1
author=Jamal Bouajjaj
maintainer=Jamal Bouajjaj <[email protected]>
sentence=A library for the MAX7219 LED display drivers.
Expand Down
45 changes: 32 additions & 13 deletions src/simple_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Originally designed for the University of New Haven's
Author: Jamal Bouajjaj
********************************************************************************/
// Macro to treat the 1D array _matrix as a 2D array
#define _GET_MATRIX_LOC(_Y, _X) _matrix[(8*_Y)+_X]
#define _GET_MATRIX_LOC(_MATRIX_NUMB, _ROW_NUMB) _matrix[(8*_MATRIX_NUMB)+_ROW_NUMB]

/********************************************************************************
Constructor
Expand All @@ -27,6 +27,8 @@ simpleMatrix::simpleMatrix(int pin, bool rotateIndividualDislay, unsigned int nu
_matrix = new uint8_t[(_NUMB_OF_LED_MATRICES+1)*8];
// A copy of a column_addressed matrix. Used as memory for when new stuff is scrolled unto the display.
_matrix_col = new uint8_t[(_NUMB_OF_LED_MATRICES)*8];
memset(_matrix, 0, (_NUMB_OF_LED_MATRICES+1)*8);
memset(_matrix_col, 0, (_NUMB_OF_LED_MATRICES)*8);
pinMode(_DL_PIN,OUTPUT);
digitalWrite(_DL_PIN,HIGH);
}
Expand All @@ -36,11 +38,11 @@ Low Level Function
//Sends a command to a single MS7219
void simpleMatrix::sendCommandtoOne(uint8_t command, uint8_t data, uint8_t display){
uint8_t d[_NUMB_OF_LED_MATRICES*2]; //Array that containts data to be sent
memset(d, 0, _NUMB_OF_LED_MATRICES*2);
d[2*display] = command;
d[(2*display)+1] = data;
digitalWrite(_DL_PIN,LOW);
for(int k=0;k<_NUMB_OF_LED_MATRICES*2;k++){SPI.transfer(d[k]);} // Send data
for(int k=0;k<_NUMB_OF_LED_MATRICES*8;k++){_matrix_col[k] = 0; _GET_MATRIX_LOC(k/_NUMB_OF_LED_MATRICES, k%_NUMB_OF_LED_MATRICES) = 0;}
digitalWrite(_DL_PIN,HIGH);
}

Expand All @@ -63,7 +65,7 @@ void simpleMatrix::senddisplay(){
SPI.transfer(r+1);
SPI.transfer(_GET_MATRIX_LOC(i, r));
}
digitalWrite(_DL_PIN,HIGH);
digitalWrite(_DL_PIN,HIGH);
}
}

Expand All @@ -85,10 +87,11 @@ High-Level functions

//Converts a column-addressed buffer to a display-row-addressed buffer and sends it
void simpleMatrix::sendMatrixBuffer(uint8_t *mat, int start_from){
for(int i=0;i<_NUMB_OF_LED_MATRICES;i++){
for(int k=0;k<8;k++){
_matrix_col[k+(i*8)] = mat[k+(i*8)+start_from];
_GET_MATRIX_LOC(i, k) = mat[k+(i*8)+start_from]; //Copy *mat to *_matrix with it split up
if(mat != NULL){
for(int i=0;i<_NUMB_OF_LED_MATRICES;i++){
for(int k=0;k<8;k++){
_matrix_col[k+(i*8)] = mat[k+(i*8)+start_from];
}
}
}
//Function to transpose display (Thanks Spencer Hopwood!)
Expand All @@ -101,9 +104,9 @@ void simpleMatrix::sendMatrixBuffer(uint8_t *mat, int start_from){
// To combat this, there is a flag which will decide whether or not to rotate
// each matrix's buffer by 180degrees
if(_ROTATE_INDIV_DISPLAY){
temp[7-i] |= ((_GET_MATRIX_LOC(d, k) & bitmask_v) >> (i)) << (7-k);
temp[7-i] |= ((_matrix_col[k+(d*8)] & bitmask_v) >> (i)) << (7-k);
}else{
temp[i] |= ((_GET_MATRIX_LOC(d, k) & bitmask_v) >> (i)) << (k);
temp[i] |= ((_matrix_col[k+(d*8)] & bitmask_v) >> (i)) << (k);
}
}
}
Expand All @@ -114,8 +117,11 @@ void simpleMatrix::sendMatrixBuffer(uint8_t *mat, int start_from){

//Fills the display. Can fill all or some displays
void simpleMatrix::fillDisplay(int from, int to){
if(to==(int)0x8000){to=_NUMB_OF_LED_MATRICES-1;}
for(int i=from;i<(to+1);i++){
if(to==(int)0x8000){to=_NUMB_OF_LED_MATRICES;} // If to is default, set to end of matrix
memset(_matrix_col+(from*8), 0xFF, (to-from)*8); // Set the internal column-addressed column to said value
// Due to the easiness of this operation, manually update the row/display addressed array and send it
// instead of using the sendMatrixBuffer function
for(int i=from;i<to;i++){
for(int k=0;k<8;k++){
_GET_MATRIX_LOC(i, k) = 0xFF;
}
Expand All @@ -125,15 +131,28 @@ void simpleMatrix::fillDisplay(int from, int to){

//Clears the displays. Can clear all or some displays
void simpleMatrix::clearDisplay(int from, int to){
if(to==(int)0x8000){to=_NUMB_OF_LED_MATRICES-1;}
for(int d=from;d<(to+1);d++){
if(to==(int)0x8000){to=_NUMB_OF_LED_MATRICES;} // If to is default, set to end of matrix
memset(_matrix_col+(from*8), 0, (to-from)*8); // Set the internal column-addressed column to said value
// Due to the easiness of this operation, manually update the row/display addressed array and send it
// instead of using the sendMatrixBuffer function
for(int d=from;d<to;d++){
for(int i=0;i<8;i++){
_GET_MATRIX_LOC(d, i) = 0x00;
}
}
senddisplay();
}

void simpleMatrix::setPixel(int x, int y){
_matrix_col[y] |= (1<<x);
sendMatrixBuffer();
}

void simpleMatrix::clearPixel(int x, int y){
_matrix_col[y] &= ~(1<<x);
sendMatrixBuffer();
}

//Sends and scrolls a text from right to left which is stored in Flash memory
void simpleMatrix::scrollTextPROGMEM(const char *text, int del, bool left_to_right, int start_from){
// If the default value is unchanged, set the start from the end of the matrix
Expand Down
16 changes: 10 additions & 6 deletions src/simple_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class simpleMatrix{
*/
void fillDisplay(int from=0, int to=0x8000);


/**
* \brief Scrolls a text left or right
* \param del The delay between each frame while scrolling.
Expand Down Expand Up @@ -148,11 +147,16 @@ class simpleMatrix{
void setIntensity(int intensity);

/**
* \brief Sets a single pixel in the 4 8x8 LED matrices to either on or off.
* \param x,y The coordinates of the pixel to be sent/cleared.
* \param value true or false depending on whether you want the pixel to be turned off or on.
* \brief Sets a single pixel in the LED matrices.
* \param x,y The coordinates of the pixel to be set.
*/
void setPixel(int x, int y);

/**
* \brief Clears a single pixel in the LED matrices.
* \param x,y The coordinates of the pixel to be cleared.
*/
void setPixel(int x, int y, int value);
void clearPixel(int x, int y);

/**
* \brief A wrapper function for the sendColumnBuffer() function.
Expand Down Expand Up @@ -181,7 +185,7 @@ class simpleMatrix{
* \param *mat A column-addressed array that contains the bytes (*mat must be an array of uint8_t type) of size that is equal to the number of columns in your buffer (For a 4 matrix display, there will be 32 columns)
* \param start_from The offset in the *mat where the function will start accessing.
*/
void sendMatrixBuffer(uint8_t *mat, int start_from=0);
void sendMatrixBuffer(uint8_t *mat=NULL, int start_from=0);
private:
uint8_t *_matrix; // The display and row addressed array containing data to be sent to the displays
uint8_t *_matrix_col; // A column-addressed array containing the previous data. Used for memory
Expand Down

0 comments on commit fb82d0c

Please sign in to comment.