2 #ifndef LedControlMAX7221_h
3 #define LedControlMAX7221_h
23 virtual byte addDevice(
byte count = 1) = 0;
25 virtual int getDeviceCount() = 0;
27 virtual bool isPowered(
byte device) = 0;
29 virtual void setPower(
byte device,
bool onState,
byte numDevices = 1) = 0;
31 virtual void setScanLimit(
byte device,
byte limit,
byte numDevices = 1) = 0;
33 virtual void setIntensity(
byte device,
byte intensity,
byte numDevices = 1) = 0;
35 virtual void clearDisplay(
byte device,
byte numDevices = 1) = 0;
46 virtual void setLed(
byte device,
int row,
int column,
boolean state) = 0;
56 virtual void setRow(
byte device,
int row,
byte value) = 0;
66 virtual void setRowNoCache(
byte device,
int row,
byte value) = 0;
76 virtual void setColumn(
byte device,
int column,
byte value) = 0;
78 virtual byte getRow(
byte device,
byte row) = 0;
80 void setAllPower(
bool onState)
82 setPower(0, onState, getDeviceCount());
85 void clearAllDisplays()
87 clearDisplay(0, getDeviceCount());
96 static byte randomRow(
byte randomMode)
101 return (random(256)&random(256)&random(256)&random(256));
103 return (random(256)&random(256)&random(256));
105 return (random(256)&random(256));
109 return (random(256)|random(256));
111 return (random(256)|random(256)|random(256));
113 return (random(256)|random(256)|random(256)|random(256));
128 template <
byte numDevices>
142 pinMode(fSPI_MOSI, OUTPUT);
143 pinMode(fSPI_CLK, OUTPUT);
144 pinMode(fSPI_CS, OUTPUT);
145 digitalWrite(fSPI_CS, HIGH);
147 for (
byte i = 0; i <
sizeof(fBits); i++)
149 for (
byte i = 0; i < numDevices; i++)
151 spiTransfer(i, kOP_DISPLAYTEST, 0);
155 spiTransfer(i, kOP_DECODEMODE, 0);
186 return (device < numDevices) ? ((fPowerMask & (1<<device)) != 0) :
false;
196 virtual void setPower(
byte device,
bool onState,
byte count = 1)
override
198 while (count-- != 0 && device < numDevices)
200 bool shutdown = !onState;
201 fPowerMask = (onState) ? (fPowerMask | (1<<device)) : (fPowerMask & ~(1<<device));
202 spiTransfer(device, kOP_SHUTDOWN, !shutdown);
215 virtual void setScanLimit(
byte device,
byte limit,
byte count = 1)
override
217 while (count-- != 0 && device < numDevices && limit < 8)
219 spiTransfer(device, kOP_SCANLIMIT, limit);
230 virtual void setIntensity(
byte device,
byte intensity,
byte count = 1)
override
232 while (count-- != 0 && device < numDevices && intensity < 16)
234 spiTransfer(device, kOP_INTENSITY, intensity);
246 while (count-- != 0 && device < numDevices)
248 byte* status = &fBits[device * 8];
249 for (
int i = 1; i < 9; i++, status++)
254 spiTransfer(device, i, 0);
270 virtual void setLed(
byte device,
int row,
int column,
boolean state)
override
272 if (device < numDevices &&
273 row >= 0 && row < 8 && column >= 0 && column < 8)
275 byte* status = &fBits[device * 8 + row];
276 byte val = B10000000 >> column;
289 spiTransfer(device, row + 1, val);
302 virtual void setRow(
byte device,
int row,
byte value)
override
304 if (device < numDevices && row >= 0 && row < 8)
306 byte* status = &fBits[device * 8 + row];
307 if (*status != value)
310 spiTransfer(device, row + 1, value);
325 if (device < numDevices && row >= 0 && row < 8)
327 byte* status = &fBits[device * 8 + row];
329 spiTransfer(device, row + 1, value);
341 virtual void setColumn(
byte device,
int column,
byte value)
override
344 if (device < numDevices && column >= 0 && column < 8)
346 for (
int row = 0; row < 8; row++)
348 val = value >> (7 - row);
350 setLed(device, row, column, val);
358 virtual byte getRow(
byte device,
byte row)
override
360 return (device < numDevices && row < 8) ? fBits[device * 8 + row] : 0;
365 void spiTransfer(
byte device,
volatile byte opcode,
volatile byte data)
368 int offset = device * 2;
369 for (
byte i = 0; i <
sizeof(fSPIData); i++)
370 fSPIData[i] = (
byte)0;
372 fSPIData[offset + 1] = opcode;
373 fSPIData[offset] = data;
375 digitalWrite(fSPI_CS, LOW);
377 for (
byte i =
sizeof(fSPIData); i > 0; i--)
378 shiftOut(fSPI_MOSI, fSPI_CLK, MSBFIRST, fSPIData[i - 1]);
380 digitalWrite(fSPI_CS, HIGH);
385 byte fBits[8 * numDevices];
386 byte fSPIData[2 * numDevices];
391 const int kBrightness = 15;