Skip to content

Commit 5f718bd

Browse files
Fix comments re: Arduino Mega, PROGMEM string in scrolltext example
1 parent 177c079 commit 5f718bd

File tree

10 files changed

+55
-47
lines changed

10 files changed

+55
-47
lines changed

RGBmatrixPanel.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,26 @@ BSD license, all text above must be included in any redistribution.
4343
// be specified as any pin within a specific PORT register stated below.
4444

4545
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
46-
// Arduino Mega hasn't actually been tested -- use at your own peril!
46+
// Arduino Mega is now tested and confirmed, with the following caveats:
4747
// Because digital pins 2-7 don't map to a contiguous port register,
48-
// the Mega will require connecting the matrix data lines to different
49-
// pins. Ports A, C, and L all offer the requisite contiguous 6 bits.
50-
// I wanted to use PORTL in order to keep the external memory interface
51-
// free, but accessing the upper PORT registers in assembly seems to
52-
// require some additional flaming hoops and doesn't work with the
53-
// inline code here. PORTA is used instead (Mega pins 22-29, though
54-
// only 24-29 are actually connected to the LED matrix). Clock may be
55-
// any pin on PORTB -- on the Mega, this CAN'T be pins 8 or 9 (these
56-
// are on PORTH), thus the wiring will need to be slightly different
57-
// than the tutorial's explanation on the Uno, etc. Pins 10-13 are all
58-
// fair game for the clock, as are pins 50-53.
48+
// the Mega requires connecting the matrix data lines to different pins.
49+
// Digital pins 24-29 are used for the data interface, and 22 & 23 are
50+
// unavailable for other outputs because the software needs to write to
51+
// the full PORTA register for speed. Clock may be any pin on PORTB --
52+
// on the Mega, this CAN'T be pins 8 or 9 (these are on PORTH), thus the
53+
// wiring will need to be slightly different than the tutorial's
54+
// explanation on the Uno, etc. Pins 10-13 are all fair game for the
55+
// clock, as are pins 50-53.
5956
#define DATAPORT PORTA
6057
#define DATADIR DDRA
6158
#define SCLKPORT PORTB
6259
#elif defined(__AVR_ATmega32U4__)
63-
// Arduino Leonardo is still a work in progress -- DO NOT USE!!!
64-
// Unlike the Uno, digital pins 2-7 do NOT map to a contiguous port
65-
// register, dashing our hopes for compatible wiring. This is probably
66-
// going to require changes both to bit-shifting code in the library,
67-
// and how this board is wired to the LED matrix. Bummer.
60+
// Arduino Leonardo: this is vestigial code an unlikely to ever be
61+
// finished -- DO NOT USE!!! Unlike the Uno, digital pins 2-7 do NOT
62+
// map to a contiguous port register, dashing our hopes for compatible
63+
// wiring. Making this work would require significant changes both to
64+
// the bit-shifting code in the library, and how this board is wired to
65+
// the LED matrix. Bummer.
6866
#define DATAPORT PORTD
6967
#define DATADIR DDRD
7068
#define SCLKPORT PORTB
@@ -75,6 +73,8 @@ BSD license, all text above must be included in any redistribution.
7573
#define SCLKPORT PORTB
7674
#endif
7775

76+
#define nPlanes 4
77+
7878
// The fact that the display driver interrupt stuff is tied to the
7979
// singular Timer1 doesn't really take well to object orientation with
8080
// multiple RGBmatrixPanel instances. The solution at present is to
@@ -120,7 +120,6 @@ void RGBmatrixPanel::init(uint8_t rows, uint8_t a, uint8_t b, uint8_t c,
120120
addrbpin = digitalPinToBitMask(b);
121121
addrcport = portOutputRegister(digitalPinToPort(c));
122122
addrcpin = digitalPinToBitMask(c);
123-
nPlanes = 4; // Other code is fixed at 4 planes; don't change this
124123
plane = nPlanes - 1;
125124
row = nRows - 1;
126125
swapflag = false;
@@ -527,22 +526,24 @@ void RGBmatrixPanel::updateDisplay(void) {
527526
// A tiny bit of inline assembly is used; compiler doesn't pick
528527
// up on opportunity for post-increment addressing mode.
529528
// 5 instruction ticks per 'pew' = 160 ticks total
530-
#define pew \
531-
asm volatile("ld __tmp_reg__,%a0+" :: "e"(ptr)); \
532-
asm volatile("out %0,__tmp_reg__" :: "I"(_SFR_IO_ADDR(DATAPORT))); \
533-
asm volatile("out %0,%1" :: "I"(_SFR_IO_ADDR(SCLKPORT)),"r"(tick)); \
534-
asm volatile("out %0,%1" :: "I"(_SFR_IO_ADDR(SCLKPORT)),"r"(tock));
529+
#define pew asm volatile( \
530+
"ld __tmp_reg__, %a[ptr]+" "\n\t" \
531+
"out %[data] , __tmp_reg__" "\n\t" \
532+
"out %[clk] , %[tick]" "\n\t" \
533+
"out %[clk] , %[tock]" "\n" \
534+
:: [ptr] "e" (ptr), \
535+
[data] "I" (_SFR_IO_ADDR(DATAPORT)), \
536+
[clk] "I" (_SFR_IO_ADDR(SCLKPORT)), \
537+
[tick] "r" (tick), \
538+
[tock] "r" (tock));
535539

536540
// Loop is unrolled for speed:
537541
pew pew pew pew pew pew pew pew
538542
pew pew pew pew pew pew pew pew
539543
pew pew pew pew pew pew pew pew
540544
pew pew pew pew pew pew pew pew
541545

542-
// From the "Unsolved Mysteries" department: "buffptr += 32" doesn't
543-
// work here, scrambles the display. "buffptr = ptr" does, even though
544-
// both should produce the same results. Couldn't tell you why.
545-
buffptr = ptr;
546+
buffptr += 32;
546547

547548
} else { // 920 ticks from TCNT1=0 (above) to end of function
548549

RGBmatrixPanel.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ class RGBmatrixPanel : public Adafruit_GFX {
3434
Color888(uint8_t r, uint8_t g, uint8_t b, boolean gflag),
3535
ColorHSV(long hue, uint8_t sat, uint8_t val, boolean gflag);
3636

37-
// Printing
3837
private:
3938

40-
uint8_t *matrixbuff[2];
41-
uint8_t nRows, nPlanes, backindex;
42-
boolean swapflag;
39+
uint8_t *matrixbuff[2];
40+
uint8_t nRows;
41+
volatile uint8_t backindex;
42+
volatile boolean swapflag;
4343

4444
// Init/alloc code common to both constructors:
4545
void init(uint8_t rows, uint8_t a, uint8_t b, uint8_t c,

examples/colorwheel_32x32/colorwheel_32x32.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
// If your 32x32 matrix has the SINGLE HEADER input,
1313
// use this pinout:
14-
#define CLK 8 // MUST be on PORTB!
14+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1515
#define OE 9
1616
#define LAT 10
1717
#define A A0
1818
#define B A1
1919
#define C A2
2020
#define D A3
2121
// If your matrix has the DOUBLE HEADER input, use:
22-
//#define CLK 8 // MUST be on PORTB!
22+
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
2323
//#define LAT 9
2424
//#define OE 10
2525
//#define A A3

examples/colorwheel_progmem_32x32/colorwheel_progmem_32x32.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
// If your 32x32 matrix has the SINGLE HEADER input,
1717
// use this pinout:
18-
#define CLK 8 // MUST be on PORTB!
18+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1919
#define OE 9
2020
#define LAT 10
2121
#define A A0
2222
#define B A1
2323
#define C A2
2424
#define D A3
2525
// If your matrix has the DOUBLE HEADER input, use:
26-
//#define CLK 8 // MUST be on PORTB!
26+
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
2727
//#define LAT 9
2828
//#define OE 10
2929
//#define A A3

examples/plasma_16x32/plasma_16x32.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <Adafruit_GFX.h> // Core graphics library
1111
#include <RGBmatrixPanel.h> // Hardware-specific library
1212

13-
#define CLK 8 // MUST be on PORTB!
13+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1414
#define LAT A3
1515
#define OE 9
1616
#define A A0

examples/plasma_32x32/plasma_32x32.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
// If your 32x32 matrix has the SINGLE HEADER input,
1414
// use this pinout:
15-
#define CLK 8 // MUST be on PORTB!
15+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1616
#define OE 9
1717
#define LAT 10
1818
#define A A0
1919
#define B A1
2020
#define C A2
2121
#define D A3
2222
// If your matrix has the DOUBLE HEADER input, use:
23-
//#define CLK 8 // MUST be on PORTB!
23+
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
2424
//#define LAT 9
2525
//#define OE 10
2626
//#define A A3

examples/scrolltext_16x32/scrolltext_16x32.pde

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
#include <Adafruit_GFX.h> // Core graphics library
1010
#include <RGBmatrixPanel.h> // Hardware-specific library
1111

12-
#define CLK 8 // MUST be on PORTB!
12+
// Similar to F(), but for PROGMEM string pointers rather than literals
13+
#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr
14+
15+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1316
#define LAT A3
1417
#define OE 9
1518
#define A A0
@@ -19,11 +22,14 @@
1922
// buttery smooth animation. Note that NOTHING WILL SHOW ON THE DISPLAY
2023
// until the first call to swapBuffers(). This is normal.
2124
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
25+
// Double-buffered mode consumes nearly all the RAM available on the
26+
// Arduino Uno -- only a handful of free bytes remain. Even the
27+
// following string needs to go in PROGMEM:
2228

23-
char str[] = "Adafruit 16x32 RGB LED Matrix";
29+
const char str[] PROGMEM = "Adafruit 16x32 RGB LED Matrix";
2430
int textX = matrix.width(),
25-
textMin = sizeof(str) * -12;
26-
long hue = 0;
31+
textMin = sizeof(str) * -12,
32+
hue = 0;
2733
int8_t ball[3][4] = {
2834
{ 3, 0, 1, 1 }, // Initial X,Y pos & velocity for 3 bouncy balls
2935
{ 17, 15, 1, -1 },
@@ -64,11 +70,12 @@ void loop() {
6470
// Draw big scrolly text on top
6571
matrix.setTextColor(matrix.ColorHSV(hue, 255, 255, true));
6672
matrix.setCursor(textX, 1);
67-
matrix.print(str);
73+
matrix.print(F2(str));
6874

6975
// Move text left (w/wrap), increase hue
7076
if((--textX) < textMin) textX = matrix.width();
7177
hue += 7;
78+
if(hue >= 1536) hue -= 1536;
7279

7380
// Update display
7481
matrix.swapBuffers(false);

examples/testcolors_16x32/testcolors_16x32.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <Adafruit_GFX.h> // Core graphics library
1111
#include <RGBmatrixPanel.h> // Hardware-specific library
1212

13-
#define CLK 8 // MUST be on PORTB!
13+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1414
#define LAT A3
1515
#define OE 9
1616
#define A A0

examples/testshapes_16x32/testshapes_16x32.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <Adafruit_GFX.h> // Core graphics library
1111
#include <RGBmatrixPanel.h> // Hardware-specific library
1212

13-
#define CLK 8 // MUST be on PORTB!
13+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1414
#define LAT A3
1515
#define OE 9
1616
#define A A0

examples/testshapes_32x32/testshapes_32x32.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
// If your 32x32 matrix has the SINGLE HEADER input,
1414
// use this pinout:
15-
#define CLK 8 // MUST be on PORTB!
15+
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
1616
#define OE 9
1717
#define LAT 10
1818
#define A A0
1919
#define B A1
2020
#define C A2
2121
#define D A3
2222
// If your matrix has the DOUBLE HEADER input, use:
23-
//#define CLK 8 // MUST be on PORTB!
23+
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
2424
//#define LAT 9
2525
//#define OE 10
2626
//#define A A3

0 commit comments

Comments
 (0)