Skip to content

Commit

Permalink
24-bit color
Browse files Browse the repository at this point in the history
  • Loading branch information
toroidal-code committed May 4, 2014
1 parent 4928ba3 commit 5b14085
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 105 deletions.
8 changes: 4 additions & 4 deletions examples/binary_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#define DIGIT_WIDTH 2
#define DIGIT_HEIGHT 2

const uint16_t color_hour = PixelBone_Matrix::Color(0x00, 0xff, 0xff);
const uint16_t color_min = PixelBone_Matrix::Color( 0x00, 0xf0, 0x00);
const uint16_t color_sec = PixelBone_Matrix::Color( 0xff, 0xff, 0x00);
const uint16_t color_empty = PixelBone_Matrix::Color( 0x00, 0x00, 0x00);
const uint16_t color_hour = PixelBone_Pixel::Color(0x00, 0xff, 0xff);
const uint16_t color_min = PixelBone_Pixel::Color( 0x00, 0xf0, 0x00);
const uint16_t color_sec = PixelBone_Pixel::Color( 0xff, 0xff, 0x00);
const uint16_t color_empty = PixelBone_Pixel::Color( 0x00, 0x00, 0x00);


void draw_clock(PixelBone_Matrix *matrix, tm *t) {
Expand Down
2 changes: 1 addition & 1 deletion examples/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main() {
MATRIX_TOP + MATRIX_LEFT + MATRIX_ROWS + MATRIX_ZIGZAG);

matrix.setTextWrap(false);
matrix.setTextColor(matrix.Color(0,50,0));
matrix.setTextColor(PixelBone_Pixel::Color(0,50,0));

int x = 0;
while (1) {
Expand Down
49 changes: 29 additions & 20 deletions examples/game_of_life.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <iostream>
#include "../matrix.hpp"
#define HEIGHT 4
#define WIDTH 4
#define HEIGHT 8
#define WIDTH 64

const uint16_t BLANK = PixelBone_Matrix::Color(0,0,0);
const uint16_t WHITE = PixelBone_Matrix::Color(255,255,255);
const uint16_t BLANK = PixelBone_Pixel::Color(0,0,0);
const uint16_t WHITE = PixelBone_Pixel::Color(255,255,255);

struct Shape {
uint8_t xCoord;
Expand All @@ -28,37 +28,42 @@ struct Blinker : public Shape {
};

class GameOfLife {
PixelBone_Matrix world;
Shape shape;
public:
PixelBone_Matrix* world;
Shape shape;
GameOfLife( Shape sh );
~GameOfLife();
void update();
uint8_t getState(uint16_t state , uint8_t xCoord , uint8_t yCoord);
void iterate(unsigned int iterations);
};

GameOfLife::GameOfLife( Shape sh ) :
world(PixelBone_Matrix(WIDTH, HEIGHT)),
shape(sh)
{
world.clear();
for ( uint8_t i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {
for ( uint8_t j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {
world = new PixelBone_Matrix(16, 8, 4, 1, MATRIX_TOP + MATRIX_LEFT + MATRIX_ROWS + MATRIX_ZIGZAG + TILE_TOP + TILE_LEFT + TILE_ROWS);
world->clear();

for ( int i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {
for ( int j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {
if ( i < HEIGHT && j < WIDTH ) {
world.drawPixel(i, j, shape.figure[ i - shape.yCoord ][j - shape.xCoord ]);
world->drawPixel(j, i, shape.figure[ i - shape.yCoord ][j - shape.xCoord ]);
}
}
}
world->show();
}

GameOfLife::~GameOfLife(){
delete world;
}

void GameOfLife::update() {
for (uint16_t i = 0; i < WIDTH; i++) {
for (uint16_t j = 0; j < HEIGHT; j++) {
world.drawPixel(i, j, GameOfLife::getState(world.getPixelColor(i, j), j, i));
world->drawPixel(i, j, GameOfLife::getState(world->getPixelColor(i, j), j, i));
}
}
world.moveToNextBuffer();

}

uint8_t GameOfLife::getState(uint16_t state, uint8_t yCoord, uint8_t xCoord) {
Expand All @@ -69,7 +74,7 @@ uint8_t GameOfLife::getState(uint16_t state, uint8_t yCoord, uint8_t xCoord) {
continue;
}
if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {
if ( world.getPixelColor(j, i) == WHITE) {
if ( world->getPixelColor(j, i) == WHITE) {
neighbors++;
}
}
Expand All @@ -85,8 +90,12 @@ uint8_t GameOfLife::getState(uint16_t state, uint8_t yCoord, uint8_t xCoord) {

void GameOfLife::iterate(uint iterations) {
for (uint i = 0; i < iterations; i++ ) {
world.show();
update();
if (getchar() == ' '){
world->show();
world->wait();
}
//world->moveToNextBuffer();
}
}

Expand Down Expand Up @@ -144,9 +153,9 @@ Blinker::~Blinker() {
int main() {
Glider glider(0,0);
GameOfLife gol(glider);
gol.iterate(5);
Blinker blinker(1,0);
GameOfLife gol2(blinker);
gol2.iterate(4);
gol.iterate(20);
// Blinker blinker(1,0);
// GameOfLife gol2(blinker);
// gol2.iterate(4);
}

4 changes: 2 additions & 2 deletions examples/matrix-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main() {
MATRIX_ROWS + MATRIX_ZIGZAG);

matrix.setTextWrap(false);
matrix.setTextColor(matrix.Color(128, 128, 128));
matrix.setTextColor(PixelBone_Pixel::Color(128, 128, 128));

int x = 0;
while (1) {
Expand All @@ -35,4 +35,4 @@ int main() {
usleep(100000);
}
return 0;
}
}
2 changes: 1 addition & 1 deletion examples/rgb-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void HSBtoRGB(int hue, int sat, int val, uint8_t out[]) {
}

int main(void) {
const int num_pixels = 128;
const int num_pixels = 512;
PixelBone_Pixel *const strip = new PixelBone_Pixel(num_pixels);
time_t last_time = time(NULL);
unsigned last_i = 0;
Expand Down
4 changes: 2 additions & 2 deletions examples/tile-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
MATRIX_TOP + MATRIX_LEFT + MATRIX_ROWS + MATRIX_ZIGZAG);

matrix.setTextWrap(false);
matrix.setTextColor(matrix.Color(128, 128, 128));
matrix.setTextColor(PixelBone_Pixel::Color(128, 128, 128));

int x = 0;
while (1) {
Expand All @@ -48,4 +48,4 @@ int main() {
usleep(100000);
}
return 0;
}
}
40 changes: 20 additions & 20 deletions gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ PixelBone_GFX::PixelBone_GFX(int16_t w, int16_t h) : WIDTH(w), HEIGHT(h) {
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
textcolor = textbgcolor = 0xFFFFFFFF;
wrap = true;
}

// Draw a circle outline
void PixelBone_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
uint32_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
Expand Down Expand Up @@ -85,7 +85,7 @@ void PixelBone_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
}

void PixelBone_GFX::drawCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t cornername, uint16_t color) {
uint8_t cornername, uint32_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
Expand Down Expand Up @@ -121,15 +121,15 @@ void PixelBone_GFX::drawCircleHelper(int16_t x0, int16_t y0, int16_t r,
}

void PixelBone_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
uint32_t color) {
drawFastVLine(x0, y0 - r, 2 * r + 1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
}

// Used to do circles and roundrects
void PixelBone_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t cornername, int16_t delta,
uint16_t color) {
uint32_t color) {

int16_t f = 1 - r;
int16_t ddF_x = 1;
Expand Down Expand Up @@ -160,7 +160,7 @@ void PixelBone_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,

// Bresenham's algorithm - thx wikpedia
void PixelBone_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
uint32_t color) {
int16_t steep = std::abs(y1 - y0) > std::abs(x1 - x0);
if (steep) {
swap(x0, y0);
Expand Down Expand Up @@ -201,40 +201,40 @@ void PixelBone_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,

// Draw a rectangle
void PixelBone_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
uint32_t color) {
drawFastHLine(x, y, w, color);
drawFastHLine(x, y + h - 1, w, color);
drawFastVLine(x, y, h, color);
drawFastVLine(x + w - 1, y, h, color);
}

void PixelBone_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
uint32_t color) {
// Update in subclasses if desired!
drawLine(x, y, x, y + h - 1, color);
}

void PixelBone_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
uint32_t color) {
// Update in subclasses if desired!
drawLine(x, y, x + w - 1, y, color);
}

void PixelBone_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
uint32_t color) {
// Update in subclasses if desired!
for (int16_t i = x; i < x + w; i++) {
drawFastVLine(i, y, h, color);
}
}

void PixelBone_GFX::fillScreen(uint16_t color) {
void PixelBone_GFX::fillScreen(uint32_t color) {
fillRect(0, 0, _width, _height, color);
}

// Draw a rounded rectangle
void PixelBone_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h,
int16_t r, uint16_t color) {
int16_t r, uint32_t color) {
// smarter version
drawFastHLine(x + r, y, w - 2 * r, color); // Top
drawFastHLine(x + r, y + h - 1, w - 2 * r, color); // Bottom
Expand All @@ -249,7 +249,7 @@ void PixelBone_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h,

// Fill a rounded rectangle
void PixelBone_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h,
int16_t r, uint16_t color) {
int16_t r, uint32_t color) {
// smarter version
fillRect(x + r, y, w - 2 * r, h, color);

Expand All @@ -260,15 +260,15 @@ void PixelBone_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h,

// Draw a triangle
void PixelBone_GFX::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color) {
int16_t x2, int16_t y2, uint32_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}

// Fill a triangle
void PixelBone_GFX::fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color) {
int16_t x2, int16_t y2, uint32_t color) {

int16_t a, b, y, last;

Expand Down Expand Up @@ -348,7 +348,7 @@ void PixelBone_GFX::fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
}

void PixelBone_GFX::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
int16_t w, int16_t h, uint16_t color) {
int16_t w, int16_t h, uint32_t color) {

int16_t i, j, byteWidth = (w + 7) / 8;

Expand Down Expand Up @@ -394,7 +394,7 @@ void PixelBone_GFX::write(uint8_t c) {

// Draw a character
void PixelBone_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size) {
uint32_t color, uint16_t bg, uint8_t size) {

if ((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
Expand Down Expand Up @@ -434,13 +434,13 @@ void PixelBone_GFX::setCursor(int16_t x, int16_t y) {

void PixelBone_GFX::setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; }

void PixelBone_GFX::setTextColor(uint16_t c) {
void PixelBone_GFX::setTextColor(uint32_t c) {
// For 'transparent' background, we'll set the bg
// to the same as fg instead of using a flag
textcolor = textbgcolor = c;
}

void PixelBone_GFX::setTextColor(uint16_t c, uint16_t b) {
void PixelBone_GFX::setTextColor(uint32_t c, uint32_t b) {
textcolor = c;
textbgcolor = b;
}
Expand Down Expand Up @@ -472,4 +472,4 @@ int16_t PixelBone_GFX::height(void) { return _height; }

void PixelBone_GFX::invertDisplay(bool i) {
// Do nothing, must be subclassed if supported
}
}
Loading

0 comments on commit 5b14085

Please sign in to comment.