Skip to content

Commit

Permalink
Added test shape example and font table
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Aug 17, 2011
1 parent 56b3830 commit b9f8834
Show file tree
Hide file tree
Showing 7 changed files with 589 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
This is an Arduino library for our 16x32 RGB LED matrix panels

Pick one up at http://www.adafruit.com/products/xxx
Pick one up at http://www.adafruit.com/products/420 !

To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder RGBLEDMatrix. Check that the BMP085 folder contains RGBLEDMatrix.cpp and RGBLEDMatrix.h
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder RGBLEDMatrix. Check that the RGBmatrixPanel folder contains RGBmatrixPanel.cpp and RGBmatrixPanel.h

Place the RGBLEDMatrix library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
Place the RGBmatrixPanel library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
189 changes: 188 additions & 1 deletion RGBmatrixPanel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "RGBmatrixPanel.h"
#include "glcdfont.c"

uint8_t RGBmatrixPanel::width() {return WIDTH; }

Expand Down Expand Up @@ -33,6 +34,10 @@ RGBmatrixPanel::RGBmatrixPanel(uint8_t a, uint8_t b, uint8_t c,
addrbpin = digitalPinToBitMask(b);
addrcportreg = portOutputRegister(digitalPinToPort(c));
addrcpin = digitalPinToBitMask(c);

cursor_x = cursor_y = 0;
textsize = 1;
textcolor = Color333(7,7,7); // white
}


Expand Down Expand Up @@ -90,11 +95,13 @@ uint16_t RGBmatrixPanel::Color888(uint8_t r, uint8_t g, uint8_t b) {
}


void RGBmatrixPanel::setPixel(uint8_t x, uint8_t y, uint16_t c) {
void RGBmatrixPanel::drawPixel(uint8_t x, uint8_t y, uint16_t c) {
uint16_t index;
uint8_t old;
uint8_t red, green, blue;

if ((x >= WIDTH) || (y >= HEIGHT)) return;

// extract the 12 bits of color
red = (c >> 8) & 0xF;
green = (c >> 4) & 0xF;
Expand Down Expand Up @@ -144,6 +151,186 @@ void RGBmatrixPanel::setPixel(uint8_t x, uint8_t y, uint16_t c) {
}



// bresenham's algorithm - thx wikpedia
void RGBmatrixPanel::drawLine(int8_t x0, int8_t y0, int8_t x1, int8_t y1,
uint16_t color) {
uint16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}

if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}

uint16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);

int16_t err = dx / 2;
int16_t ystep;

if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;}

for (; x0<=x1; x0++) {
if (steep) {
drawPixel(y0, x0, color);
} else {
drawPixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}

// draw a rectangle
void RGBmatrixPanel::drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint16_t color) {
drawLine(x, y, x+w-1, y, color);
drawLine(x, y+h-1, x+w-1, y+h-1, color);

drawLine(x, y, x, y+h-1, color);
drawLine(x+w-1, y, x+w-1, y+h-1, color);
}

// fill a rectangle
void RGBmatrixPanel::fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint16_t color) {
for (uint8_t i=x; i<x+w; i++) {
for (uint8_t j=y; j<y+h; j++) {
drawPixel(i, j, color);
}
}
}



// draw a circle outline
void RGBmatrixPanel::drawCircle(uint8_t x0, uint8_t y0, uint8_t r,
uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;

drawPixel(x0, y0+r, color);
drawPixel(x0, y0-r, color);
drawPixel(x0+r, y0, color);
drawPixel(x0-r, y0, color);

while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;

drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);

drawPixel(x0 + y, y0 + x, color);
drawPixel(x0 - y, y0 + x, color);
drawPixel(x0 + y, y0 - x, color);
drawPixel(x0 - y, y0 - x, color);

}
}


// fill a circle
void RGBmatrixPanel::fillCircle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;

drawLine(x0, y0-r, x0, y0+r+1, color);

while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;

drawLine(x0+x, y0-y, x0+x, y0+y+1, color);
drawLine(x0-x, y0-y, x0-x, y0+y+1, color);
drawLine(x0+y, y0-x, x0+y, y0+x+1, color);
drawLine(x0-y, y0-x, x0-y, y0+x+1, color);
}
}

void RGBmatrixPanel::fill(uint16_t c) {
for (uint8_t i=0; i<WIDTH; i++) {
for (uint8_t j=0; j<HEIGHT; j++) {
drawPixel(i, j, c);
}
}
}

void RGBmatrixPanel::setCursor(uint8_t x, uint8_t y) {
cursor_x = x;
cursor_y = y;
}

void RGBmatrixPanel::setTextSize(uint8_t s) {
textsize = s;
}

void RGBmatrixPanel::setTextColor(uint16_t c) {
textcolor = c;
}

void RGBmatrixPanel::write(uint8_t c) {
if (c == '\n') {
cursor_y += textsize*8;
cursor_x = 0;
} else if (c == '\r') {
// skip em
} else {
drawChar(cursor_x, cursor_y, c, textcolor, textsize);
cursor_x += textsize*6;
}
}


// draw a character
void RGBmatrixPanel::drawChar(uint8_t x, uint8_t y, char c,
uint16_t color, uint8_t size) {
for (uint8_t i =0; i<5; i++ ) {
uint8_t line = pgm_read_byte(font+(c*5)+i);
for (uint8_t j = 0; j<8; j++) {
if (line & 0x1) {
if (size == 1) // default size
drawPixel(x+i, y+j, color);
else { // big size
fillRect(x+i*size, y+j*size, size, size, color);
}
}
line >>= 1;
}
}
}

void RGBmatrixPanel::updateDisplay(void) {
writeSection(scansection, matrixbuff + (3*32*scansection));
scansection++;
Expand Down
23 changes: 21 additions & 2 deletions RGBmatrixPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@
#endif


class RGBmatrixPanel {
#define swap(a, b) { uint16_t t = a; a = b; b = t; }

class RGBmatrixPanel : public Print {
public:
RGBmatrixPanel(uint8_t a, uint8_t b, uint8_t c, uint8_t latch, uint8_t oe);
void begin();
uint16_t Color333(uint8_t r, uint8_t g, uint8_t b);
uint16_t Color444(uint8_t r, uint8_t g, uint8_t b);
uint16_t Color888(uint8_t r, uint8_t g, uint8_t b);
void setPixel(uint8_t x, uint8_t y, uint16_t c);
void drawPixel(uint8_t x, uint8_t y, uint16_t c);
void drawLine(int8_t x0, int8_t y0, int8_t x1, int8_t y1, uint16_t c);
void drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);
void fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);
void drawCircle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color);
void fillCircle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color);
void fill(uint16_t c);

// Printing
void setCursor(uint8_t x, uint8_t y);
void setTextSize(uint8_t s);
void setTextColor(uint16_t c);
void write(uint8_t c);
void drawChar(uint8_t x, uint8_t y, char c, uint16_t color, uint8_t size);

void updateDisplay();
void dumpMatrix(void);

Expand All @@ -40,6 +56,9 @@ class RGBmatrixPanel {
private:
uint8_t matrixbuff[NUMBYTES]; // 768 bytes for 16x32

uint8_t cursor_x, cursor_y, textsize;
uint16_t textcolor;

volatile uint8_t pwmcounter;
volatile uint8_t scansection;

Expand Down
8 changes: 4 additions & 4 deletions examples/plasma/plasma.pde
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// plasma code based on Windell Oskay's plasma for Peggy
// we sped it up a bunch with fixed point HSV->RGB conversion and
// sine table lookup but havent done fixed point sqrt. its a little sluggish :(
// LGPL

#include <WProgram.h>
#include <TimerOne.h>
Expand Down Expand Up @@ -102,7 +103,7 @@ float dist(float a, float b, float c, float d)
//float s = sqrt((cfp-afp)*(cfp-afp)+(dfp-bfp)*(dfp-bfp));

return s;

/*
Serial.print("\t\tfloat dist = "); Serial.println(s);
// 24.8 fixed point for a and b
Expand All @@ -117,7 +118,7 @@ Serial.print("\t\tfloat dist = "); Serial.println(s);
Serial.print("\t\tfp dist = "); Serial.println(fps);
return s;

*/
}

static int16_t sinetab[512] PROGMEM = {
Expand Down Expand Up @@ -212,7 +213,7 @@ void plasma_morph()
Serial.print(" G = "); Serial.print(colorRGB.g, DEC);
Serial.print(" B = "); Serial.println(colorRGB.b, DEC);
*/
matrix.setPixel(x, y, matrix.Color888(colorRGB.r, colorRGB.g, colorRGB.b));
matrix.drawPixel(x, y, matrix.Color888(colorRGB.r, colorRGB.g, colorRGB.b));
}
}
paletteShift++;
Expand Down Expand Up @@ -273,4 +274,3 @@ void loop()
plasma_morph();
}


6 changes: 4 additions & 2 deletions examples/testallcolors/testallcolors.pde
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// example to draw all 9-bit colors! public domain

#include "RGBmatrixPanel.h"
#include <TimerOne.h>

Expand Down Expand Up @@ -39,7 +41,7 @@ void setup() {
uint8_t r=0, g=0, b=0;
for (uint8_t x=0; x < 32; x++) {
for (uint8_t y=0; y < 8; y++) {
matrix.setPixel(x, y, matrix.Color333(r, g, b));
matrix.drawPixel(x, y, matrix.Color333(r, g, b));
r++;
if (r == 8) {
r = 0; g++;
Expand All @@ -52,7 +54,7 @@ void setup() {

for (uint8_t x=0; x < 32; x++) {
for (uint8_t y=8; y < 16; y++) {
matrix.setPixel(x, y, matrix.Color333(r, g, b));
matrix.drawPixel(x, y, matrix.Color333(r, g, b));
r++;
if (r == 8) {
r = 0; g++;
Expand Down
Loading

0 comments on commit b9f8834

Please sign in to comment.