17 #include <Adafruit_GFX.h>
18 #include "Max72xxPanel.h"
31 #define OP_DECODEMODE 9
32 #define OP_INTENSITY 10
33 #define OP_SCANLIMIT 11
34 #define OP_SHUTDOWN 12
35 #define OP_DISPLAYTEST 15
37 Max72xxPanel::Max72xxPanel(
int clkPin,
int csPin,
int sdaPin,
int hDisplays,
int vDisplays)
43 Max72xxPanel::doubleBuffer =
false;
44 Max72xxPanel::bufferSize = hDisplays * vDisplays * 8;
45 Max72xxPanel::buffer = (
byte*)malloc(bufferSize);
56 spiTransfer(OP_DISPLAYTEST, 0);
59 spiTransfer(OP_SCANLIMIT, 7);
62 spiTransfer(OP_DECODEMODE, 0);
70 constructor(hDisplays << 3, vDisplays << 3);
73 void Max72xxPanel::shutdown(
boolean b) {
74 spiTransfer(OP_SHUTDOWN, b ? 0 : 1);
77 void Max72xxPanel::setIntensity(
int intensity) {
78 spiTransfer(OP_INTENSITY, intensity);
81 void Max72xxPanel::drawPixel(int16_t
x, int16_t y, uint16_t color) {
82 drawLine(
x, y,
x, y, color);
85 void Max72xxPanel::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
87 if ( rotation >= 2 ) {
92 if ( rotation == 1 || rotation == 2 ) {
93 y0 = _height - 1 - y0;
94 y1 = _height - 1 - y1;
98 drawLineHelper(y0, x0, y1, x1, color);
101 drawLineHelper(x0, y0, x1, y1, color);
105 void Max72xxPanel::drawLineHelper(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
107 if ( x0 < 0 || x0 > WIDTH ) {
113 y0 = constrain(y0, 0, HEIGHT - 1);
114 int h = constrain(y1 - y0 + 1, 0, HEIGHT - y0);
116 byte *ptr = (
byte *)buffer + x0 + WIDTH * (y0 >> 3);
120 int hPart = min(8 - y0, h);
121 byte val = ((1 << hPart) - 1) << y0;
135 spiTransfer(OP_DIGIT0 + (x0 & 0b111));
138 Adafruit_GFX::drawLine(x0, y0, x1, y1, color);
142 void Max72xxPanel::fillScreen(uint16_t color) {
144 for (
int x = bufferSize - 1;
x >= 0;
x-- ) {
145 buffer[
x] = color ? 0xff : 0;
148 for (
int opcode = OP_DIGIT0; opcode <= OP_DIGIT7; opcode++ ) {
153 void Max72xxPanel::doubleBuffering(
boolean enabled) {
155 boolean old = doubleBuffer;
157 doubleBuffer = enabled;
159 if ( old && ! doubleBuffer ) {
160 for (
int row = OP_DIGIT7; row >= OP_DIGIT0; row-- ) {
166 void Max72xxPanel::spiTransfer(
byte opcode,
byte data) {
171 if ( ! doubleBuffer ) {
173 digitalWrite(SS, LOW);
176 for (
int display = (bufferSize / 8) - 1; display >= 0; display-- ) {
177 _spi.Shiftout(MOSI, SCLK, SPI::Msbfirst, 8, opcode);
178 _spi.Shiftout(MOSI, SCLK, SPI::Msbfirst, 8, opcode <= OP_DIGIT7 ? buffer[display * 8 + (opcode - OP_DIGIT0)] : data);
182 digitalWrite(SS, HIGH);