Skip to content

Commit a875e9a

Browse files
Adafruit_GFX subclass + fixed 'ghosting' issue
1 parent 9dd9cd8 commit a875e9a

File tree

11 files changed

+145
-575
lines changed

11 files changed

+145
-575
lines changed

RGBmatrixPanel.cpp

Lines changed: 78 additions & 233 deletions
Large diffs are not rendered by default.

RGBmatrixPanel.h

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "WProgram.h"
55
#include "pins_arduino.h"
66
#endif
7+
#include "Adafruit_GFX.h"
78

8-
class RGBmatrixPanel : public Print {
9+
class RGBmatrixPanel : public Adafruit_GFX {
910

1011
public:
1112

@@ -19,19 +20,12 @@ class RGBmatrixPanel : public Print {
1920

2021
void
2122
begin(void),
22-
drawPixel(int x, int y, uint16_t c),
23-
drawLine(int x0, int y0, int x1, int y1, uint16_t c),
24-
drawRect(int x, int y, uint8_t w, uint8_t h, uint16_t c),
25-
fillRect(int x, int y, uint8_t w, uint8_t h, uint16_t c),
26-
drawCircle(int x0, int y0, uint8_t r, uint16_t c),
27-
fillCircle(int x0, int y0, uint8_t r, uint16_t c),
28-
fill(uint16_t c),
23+
drawPixel(int16_t x, int16_t y, uint16_t c),
24+
fillScreen(uint16_t c),
2925
updateDisplay(void),
3026
swapBuffers(boolean),
3127
dumpMatrix(void);
3228
uint8_t
33-
width(void),
34-
height(void),
3529
*backBuffer(void);
3630
uint16_t
3731
Color333(uint8_t r, uint8_t g, uint8_t b),
@@ -41,25 +35,11 @@ class RGBmatrixPanel : public Print {
4135
ColorHSV(long hue, uint8_t sat, uint8_t val, boolean gflag);
4236

4337
// Printing
44-
void
45-
attachInterrupt(void (*func)(void)),
46-
setCursor(int x, int y),
47-
setTextSize(uint8_t s),
48-
setTextColor(uint16_t c),
49-
drawChar(int x, int y, char c, uint16_t color, uint8_t size);
50-
#if ARDUINO >= 100
51-
size_t write(uint8_t c);
52-
#else
53-
void write(uint8_t c);
54-
#endif
55-
5638
private:
5739

58-
uint8_t *matrixbuff[2];
59-
uint8_t nRows, nPlanes, textsize, backindex;
60-
int cursor_x, cursor_y;
61-
uint16_t textcolor;
62-
boolean swapflag;
40+
uint8_t *matrixbuff[2];
41+
uint8_t nRows, nPlanes, backindex;
42+
boolean swapflag;
6343

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

examples/colorwheel_32x32/colorwheel_32x32.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// colorwheel demo for RGBmatrixPanel library.
22
// Renders a nice circle of hues on a 32x32 RGB LED matrix.
33

4-
#include "RGBmatrixPanel.h"
4+
#include <Adafruit_GFX.h> // Core graphics library
5+
#include <RGBmatrixPanel.h> // Hardware-specific library
56

67
#define A A3
78
#define B A2

examples/colorwheel_progmem_32x32/colorwheel_progmem_32x32.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// Uses precomputed image data stored in PROGMEM rather than
44
// calculating each pixel. Nearly instantaneous! Woo!
55

6-
#include "RGBmatrixPanel.h"
6+
#include <Adafruit_GFX.h> // Core graphics library
7+
#include <RGBmatrixPanel.h> // Hardware-specific library
78
#include "image.h"
89

910
#define A A3

examples/plasma_16x32/plasma_16x32.pde

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// plasma demo for RGBmatrixPanel library.
22
// Demonstrates double-buffered animation on 16x32 RGB LED matrix.
33

4-
#include "RGBmatrixPanel.h"
5-
#include <avr/pgmspace.h>
4+
#include <avr/pgmspace.h>
5+
#include <Adafruit_GFX.h> // Core graphics library
6+
#include <RGBmatrixPanel.h> // Hardware-specific library
67

7-
#define A A0
8-
#define B A1
9-
#define C A2
108
#define CLK 8 // MUST be on PORTB!
119
#define LAT A3
1210
#define OE 9
11+
#define A A0
12+
#define B A1
13+
#define C A2
1314
// Last parameter = 'true' enables double-buffering, for flicker-free,
1415
// buttery smooth animation. Note that NOTHING WILL SHOW ON THE DISPLAY
1516
// until the first call to swapBuffers(). This is normal.
@@ -54,10 +55,10 @@ void setup() {
5455
matrix.begin();
5556
}
5657

57-
const float radius1 = 16.3, radius2 = 23.0, radius3 = 40.8, radius4 = 44.2,
58-
centerx1 = 16.1, centerx2 = 11.6, centerx3 = 23.4, centerx4 = 4.1,
59-
centery1 = 8.7, centery2 = 6.5, centery3 = 14.0, centery4 = -2.9;
60-
float angle1 = 0.0, angle2 = 0.0, angle3 = 0.0, angle4 = 0.0;
58+
const float radius1 = 65.2, radius2 = 92.0, radius3 = 163.2, radius4 = 176.8,
59+
centerx1 = 64.4, centerx2 = 46.4, centerx3 = 93.6, centerx4 = 16.4,
60+
centery1 = 34.8, centery2 = 26.0, centery3 = 56.0, centery4 = -11.6;
61+
float angle1 = 0.0, angle2 = 0.0, angle3 = 0.0, angle4 = 0.0;
6162
long hueShift = 0;
6263

6364
void loop() {
@@ -78,10 +79,10 @@ void loop() {
7879
x1 = sx1; x2 = sx2; x3 = sx3; x4 = sx4;
7980
for(x=0; x<matrix.width(); x++) {
8081
value = hueShift
81-
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x1 * x1 + y1 * y1) >> 2))
82-
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x2 * x2 + y2 * y2) >> 2))
83-
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x3 * x3 + y3 * y3) >> 3))
84-
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x4 * x4 + y4 * y4) >> 3));
82+
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x1 * x1 + y1 * y1) >> 4))
83+
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x2 * x2 + y2 * y2) >> 4))
84+
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x3 * x3 + y3 * y3) >> 5))
85+
+ (int8_t)pgm_read_byte(sinetab + (uint8_t)((x4 * x4 + y4 * y4) >> 5));
8586
matrix.drawPixel(x, y, matrix.ColorHSV(value * 3, 255, 255, true));
8687
x1--; x2--; x3--; x4--;
8788
}

examples/plasma_32x32/plasma_32x32.pde

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// plasma demo for RGBmatrixPanel library.
22
// Demonstrates unbuffered animation on 32x32 RGB LED matrix.
33

4-
#include "RGBmatrixPanel.h"
4+
#include <Adafruit_GFX.h> // Core graphics library
5+
#include <RGBmatrixPanel.h> // Hardware-specific library
56
#include <avr/pgmspace.h>
67

78
#define A A3

examples/scrolltext_16x32/scrolltext_16x32.pde

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
11
// scrolltext demo for RGBmatrixPanel library.
22
// Demonstrates double-buffered animation on 16x32 RGB LED matrix.
33

4-
#include "RGBmatrixPanel.h"
4+
#include <Adafruit_GFX.h> // Core graphics library
5+
#include <RGBmatrixPanel.h> // Hardware-specific library
56

6-
#define A A0
7-
#define B A1
8-
#define C A2
97
#define CLK 8 // MUST be on PORTB!
108
#define LAT A3
119
#define OE 9
10+
#define A A0
11+
#define B A1
12+
#define C A2
1213
// Last parameter = 'true' enables double-buffering, for flicker-free,
1314
// buttery smooth animation. Note that NOTHING WILL SHOW ON THE DISPLAY
1415
// until the first call to swapBuffers(). This is normal.
1516
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
1617

17-
char str[] = "Adafruit 16x32 RGB LED Matrix";
18-
int textX = matrix.width(),
19-
textMin = sizeof(str) * -12;
20-
long hue = 0;
21-
int ball[3][4] = {
22-
3, 0, 1, 1, // Initial X,Y pos & velocity for 3 bouncy balls
23-
17, 15, 1, -1,
24-
27, 4, -1, 1
18+
char str[] = "Adafruit 16x32 RGB LED Matrix";
19+
int textX = matrix.width(),
20+
textMin = sizeof(str) * -12;
21+
long hue = 0;
22+
int8_t ball[3][4] = {
23+
{ 3, 0, 1, 1 }, // Initial X,Y pos & velocity for 3 bouncy balls
24+
{ 17, 15, 1, -1 },
25+
{ 27, 4, -1, 1 }
2526
};
26-
uint16_t ballcolor[3] = {
27-
matrix.Color444(0,1,0),
28-
matrix.Color444(0,0,1),
29-
matrix.Color444(1,0,0)
27+
PROGMEM uint16_t ballcolor[3] = {
28+
0x0080, // Green=1
29+
0x0002, // Blue=1
30+
0x1000 // Red=1
3031
};
3132

3233
void setup() {
3334
matrix.begin();
35+
matrix.setTextWrap(false); // Allow text to run off right edge
36+
matrix.setTextSize(2);
3437
}
3538

3639
void loop() {
3740
byte i;
3841

3942
// Clear background
40-
matrix.fill(0);
43+
matrix.fillScreen(0);
4144

4245
// Bounce three balls around
4346
for(i=0; i<3; i++) {
4447
// Draw 'ball'
45-
matrix.fillCircle(ball[i][0], ball[i][1], 5, ballcolor[i]);
48+
matrix.fillCircle(ball[i][0], ball[i][1], 5, pgm_read_word(&ballcolor[i]));
4649
// Update X, Y position
4750
ball[i][0] += ball[i][2];
4851
ball[i][1] += ball[i][3];
@@ -54,10 +57,10 @@ void loop() {
5457
}
5558

5659
// Draw big scrolly text on top
57-
matrix.setTextSize(2);
58-
matrix.setCursor(textX, 1);
5960
matrix.setTextColor(matrix.ColorHSV(hue, 255, 255, true));
61+
matrix.setCursor(textX, 1);
6062
matrix.print(str);
63+
6164
// Move text left (w/wrap), increase hue
6265
if((--textX) < textMin) textX = matrix.width();
6366
hue += 7;

examples/testcolors_16x32/testcolors_16x32.pde

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Renders 512 colors on a 16x32 RGB LED matrix.
33
// Library supports 4096 colors, but there aren't that many pixels!
44

5-
#include "RGBmatrixPanel.h"
5+
#include <Adafruit_GFX.h> // Core graphics library
6+
#include <RGBmatrixPanel.h> // Hardware-specific library
67

7-
#define A A0
8-
#define B A1
9-
#define C A2
108
#define CLK 8 // MUST be on PORTB!
119
#define LAT A3
1210
#define OE 9
11+
#define A A0
12+
#define B A1
13+
#define C A2
1314
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
1415

1516
void setup() {

examples/testshapes_16x32/testshapes_16x32.pde

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
33
// For 16x32 RGB LED matrix.
44

5-
#include "RGBmatrixPanel.h"
5+
#include <Adafruit_GFX.h> // Core graphics library
6+
#include <RGBmatrixPanel.h> // Hardware-specific library
67

7-
#define A A0
8-
#define B A1
9-
#define C A2
108
#define CLK 8 // MUST be on PORTB!
119
#define LAT A3
1210
#define OE 9
11+
#define A A0
12+
#define B A1
13+
#define C A2
1314
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);
1415

1516
void setup() {
@@ -42,7 +43,7 @@ void setup() {
4243
delay(500);
4344

4445
// fill the screen with 'black'
45-
matrix.fill(matrix.Color333(0, 0, 0));
46+
matrix.fillScreen(matrix.Color333(0, 0, 0));
4647

4748
// draw some text!
4849
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing

examples/testshapes_32x32/testshapes_32x32.pde

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
33
// For 32x32 RGB LED matrix.
44

5-
#include "RGBmatrixPanel.h"
5+
#include <Adafruit_GFX.h> // Core graphics library
6+
#include <RGBmatrixPanel.h> // Hardware-specific library
67

78
#define A A3
89
#define B A2
@@ -43,11 +44,12 @@ void setup() {
4344
delay(500);
4445

4546
// fill the screen with 'black'
46-
matrix.fill(matrix.Color333(0, 0, 0));
47+
matrix.fillScreen(matrix.Color333(0, 0, 0));
4748

4849
// draw some text!
49-
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
50-
matrix.setTextSize(1); // size 1 == 8 pixels high
50+
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
51+
matrix.setTextSize(1); // size 1 == 8 pixels high
52+
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
5153

5254
matrix.setTextColor(matrix.Color333(7,7,7));
5355
matrix.println(" Ada");

0 commit comments

Comments
 (0)