Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions LedMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ LedMatrix::LedMatrix(byte numberOfDevices, byte slaveSelectPin) {
myNumberOfDevices = numberOfDevices;
mySlaveSelectPin = slaveSelectPin;
cols = new byte[numberOfDevices * 8];
rotatedCols = new byte[numberOfDevices * 8];
}

LedMatrix::LedMatrix(byte numberOfDevices, int8_t sck, int8_t miso, int8_t mosi, byte slaveSelectPin) {
myNumberOfDevices = numberOfDevices;
mySlaveSelectPin = slaveSelectPin;
cols = new byte[numberOfDevices * 8];
rotatedCols = new byte[numberOfDevices * 8];
customSpiPins = true;
_sck = sck;
_miso = miso;
_mosi = mosi;
}

/**
Expand All @@ -17,8 +29,12 @@ LedMatrix::LedMatrix(byte numberOfDevices, byte slaveSelectPin) {
*/
void LedMatrix::init() {
pinMode(mySlaveSelectPin, OUTPUT);

SPI.begin ();

if(customSpiPins){
SPI.begin ( _sck, _miso, _mosi, mySlaveSelectPin);
} else {
SPI.begin ();
}
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV128);
for(byte device = 0; device < myNumberOfDevices; device++) {
Expand All @@ -33,7 +49,7 @@ void LedMatrix::init() {
void LedMatrix::sendByte (const byte device, const byte reg, const byte data) {
int offset=device;
int maxbytes=myNumberOfDevices;

for(int i=0;i<maxbytes;i++) {
spidata[i] = (byte)0;
spiregister[i] = (byte)0;
Expand All @@ -49,7 +65,7 @@ void LedMatrix::sendByte (const byte device, const byte reg, const byte data) {
SPI.transfer (spidata[i]);
}
digitalWrite (mySlaveSelectPin, HIGH);

}

void LedMatrix::sendByte (const byte reg, const byte data) {
Expand Down Expand Up @@ -86,17 +102,19 @@ void LedMatrix::calculateTextAlignmentOffset() {
myTextAlignmentOffset = - myText.length() * myCharWidth;
break;
}

}

void LedMatrix::clear() {
for (byte col = 0; col < myNumberOfDevices * 8; col++) {
cols[col] = 0;
}

}

void LedMatrix::commit() {
if ( rotationIsEnabled ) {
rotateLeft();
}
for (byte col = 0; col < myNumberOfDevices * 8; col++) {
sendByte(col / 8, col % 8 + 1, cols[col]);
}
Expand Down Expand Up @@ -163,4 +181,19 @@ void LedMatrix::setColumn(int column, byte value) {

void LedMatrix::setPixel(byte x, byte y) {
bitWrite(cols[x], y, true);
}
}

void LedMatrix::setRotation(bool enabled) {
rotationIsEnabled = enabled;
}

void LedMatrix::rotateLeft() {
for (byte deviceNum = 0; deviceNum < myNumberOfDevices; deviceNum++) {
for(byte posY = 0; posY < 8; posY++) {
for(byte posX = 0; posX < 8; posX++) {
bitWrite(rotatedCols[8 * (deviceNum) + posY], posX, bitRead(cols[8 * (deviceNum) + 7-posX], posY));
}
}
}
memcpy(cols, rotatedCols, myNumberOfDevices * 8);
}
80 changes: 47 additions & 33 deletions LedMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,114 +22,128 @@
#define TEXT_ALIGN_RIGHT_END 3 // End of text is just outside the left side of the display

class LedMatrix {

public:

/**
* Constructor.
* numberOfDisplays: number of connected devices
* slaveSelectPin: CS (or SS) pin connected to your ESP8266
*/
LedMatrix(byte numberOfDisplays, byte slaveSelectPin);


LedMatrix(byte numberOfDevices, int8_t sck, int8_t miso, int8_t mosi, byte slaveSelectPin);

/**
* Initializes the SPI interface
*/
void init();

/**
* Sets the intensity on all devices.
* intensity: 0-15
*/
void setIntensity(byte intensity);

/**
* Sets the width in pixels for one character.
* Default is 7.
*/
void setCharWidth(byte charWidth);

/**
* Sets the text alignment.
* Default is TEXT_ALIGN_LEFT_END.
*
*/
void setTextAlignment(byte textAlignment);

/**
* Send a byte to a specific device.
*/
void sendByte (const byte device, const byte reg, const byte data);

/**
* Send a byte to all devices (convenience method).
*/
void sendByte (const byte reg, const byte data);

/**
* Turn on pixel at position (x,y).
*/
void setPixel(byte x, byte y);

/**
* Clear the frame buffer.
*/
void clear();

/**
* Draw the currently set text at the current offset.
*/
void drawText();

/**
* Set the current text.
*/
void setText(String text);

/**
* Set the text that will replace the current text after a complete scroll
* cycle.
*/
void setNextText(String nextText);

/**
* Set a specific column with a byte value to the framebuffer.
*/
void setColumn(int column, byte value);

/**
* Writes the framebuffer to the displays.
*/
void commit();

/**
* Scroll the text to the right.
*/
void scrollTextRight();

/**
* Scroll the text to the left.
*/
void scrollTextLeft();

/**
* Oscilate the text between the two limits.
*/
void oscillateText();


/**
* Enables 90° rotation for each 8x8 matrix.
*/
void setRotation(bool enabled);

private:
byte* cols;
byte spiregister[8];
byte spidata[8];
String myText;
String myNextText;
int myTextOffset = 1;
int myTextAlignmentOffset = 0;
int increment = -1;
byte myNumberOfDevices = 0;
byte mySlaveSelectPin = 0;
byte myCharWidth = 7;
byte myTextAlignment = 1;

byte* cols;
byte* rotatedCols;
byte spiregister[8];
byte spidata[8];
String myText;
String myNextText;
int myTextOffset = 1;
int myTextAlignmentOffset = 0;
int increment = -1;
byte myNumberOfDevices = 0;
byte mySlaveSelectPin = 0;
byte myCharWidth = 7;
byte myTextAlignment = 1;
bool rotationIsEnabled = false;
bool customSpiPins = false;
int8_t _sck;
int8_t _miso;
int8_t _mosi;

void calculateTextAlignmentOffset();
};
void rotateLeft();
};
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# MAX7219LedMatrix
Library for the ESP8266 on Arduino IDE displaying text on one or multiple MAX7219 8x8 led matrices.
Library for the ESP8266 & ESP32 on Arduino IDE displaying text on one or multiple MAX7219 8x8 led matrices.

This library displays text and sets specific pixels on one or multiple 8x8 led matrices with a MAX7219 driver chip controlled through the SPI interface.
These modules are relatively cheep and can be daisy chained which makes it easy to get a led text bar up and running
You can find modules e.g. with [Banggood](http://www.banggood.com/2Pcs-MAX7219-Dot-Matrix-MCU-LED-Display-Control-Module-Kit-For-Arduino-p-945280.html?p=0P21061109440201501M) (<-affiliate link).

For details about the MAX7219 theory, wiring, schematic, etc. there's a great post by Nick Gammon: http://www.gammon.com.au/forum/?id=11516
For details about the MAX7219 theory, wiring, schematic, etc. there's a great post by Nick Gammon: http://www.gammon.com.au/forum/?id=11516

Currently this library supports the following operations:

- set pixels
- write text with a simple font
- scroll text left or right
- oscillate text between the two ends

- Set pixels
- Write text with a simple font
- Scroll text left or right
- Oscillate text between the two ends
- Rotate pixels (works with the 4 displays in one module)

You're welcome to [read in my blog](http://blog.squix.ch/2015/04/esp8266arduino-max7219-8x8-led-matrix.html) how this library came about.

## Example
Expand All @@ -25,6 +26,8 @@ You're welcome to [read in my blog](http://blog.squix.ch/2015/04/esp8266arduino-
#define NUMBER_OF_DEVICES 1
#define CS_PIN 2
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
// Can also use software SPI
// LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CLK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);

void setup() {
Serial.begin(115200); // For debugging output
Expand Down
35 changes: 35 additions & 0 deletions examples/RotateTextAndSoftwareSpi/RotateTextAndSoftwareSpi.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <SPI.h>
#include "LedMatrix.h"


#define NUMBER_OF_DEVICES 4

// Wiring that works with ESP32
#define CS_PIN 15
#define CLK_PIN 14
#define MISO_PIN 2 //we do not use this pin just fill to match constructor
#define MOSI_PIN 12

LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CLK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
int x = 0;

void setup() {
ledMatrix.init();
ledMatrix.setRotation(true);

ledMatrix.setText("MAX7219 Animation Demo");
ledMatrix.setNextText("Second text");
}

void loop() {

ledMatrix.clear();
ledMatrix.scrollTextLeft();
ledMatrix.drawText();
ledMatrix.commit();
delay(50);
x=x+1;
if (x == 400) {
ledMatrix.setNextText("Third text");
}
}