Implemented subpixel rendering for the line drawing methods.

This commit is contained in:
Fabian Schlenz 2019-10-18 17:02:23 +02:00
parent 54925dfc0e
commit 783cfdae3f
3 changed files with 21 additions and 19 deletions

View File

@ -29,7 +29,7 @@ public:
void setSubPixel(accum88 x, accum88 y, CRGB* color, SubpixelRenderingMode m = SUBPIXEL_RENDERING_ADD);
void setPixelByIndex(uint16_t index, CRGB* color);
void raisePixel(uint8_t x, uint8_t y, CRGB* color);
void line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color);
void line(accum88 x1, accum88 y1, accum88 x2, accum88 y2, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length, CRGB* color);
void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color);
void circle(uint8_t x, uint8_t y, uint8_t r, CRGB* color);

View File

@ -150,26 +150,27 @@ void Window::fadeToBlackBy(fract8 speed) {
}
}
void Window::line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color) {
void Window::line(accum88 x1, accum88 y1, accum88 x2, accum88 y2, CRGB* color) {
// Bresenham algorithm
int dx = x2-x1;
int dy = y2-y1;
const uint8_t stepsize = 32;
saccum78 dx = x2-x1;
saccum78 dy = y2-y1;
int x = x1;
int y = y1;
accum88 x = x1;
accum88 y = y1;
int p = 2*dy - dx;
saccum78 p = 2*dy - dx;
while (x < x2) {
if (p >= 0) {
setPixel(x, y, color);
y++;
setSubPixel(x, y, color, SUBPIXEL_RENDERING_RAISE);
y+=stepsize;
p = p + 2*dy - 2*dx;
} else {
setPixel(x, y, color);
setSubPixel(x, y, color, SUBPIXEL_RENDERING_RAISE);
p = p + 2*dy;
}
x++;
x+=stepsize;
}
}
@ -206,16 +207,16 @@ void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length,
}
void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color) {
int16_t x1 = x;
int16_t y1 = y;
accum88 x1 = x<<8;
accum88 y1 = y<<8;
if (startdist > 0) {
x1 = x + scale8(startdist, cos8(angle));
y1 = y + scale8(startdist, sin8(angle));
x1 = (x<<8) + (startdist<<8) * cos8(angle) / 256;
y1 = (y<<8) + (startdist<<8) * sin8(angle) / 256;
}
int16_t x2 = x + scale8(startdist + length, cos8(angle));
int16_t y2 = y + scale8(startdist + length, sin8(angle));
accum88 x2 = (x<<8) + (startdist + length) * cos16(angle);
accum88 y2 = (y<<8) + (startdist + length) * sin16(angle);
line(x1, y1, x2, y2, color);
}

View File

@ -9,8 +9,9 @@ void AnalogClockEffect::loop(uint16_t ms) {
window->circle(8, 8, 7, &white);
uint8_t seconds = ntpClient.getSeconds();
uint8_t angle = 6 * seconds;
window->lineWithAngle(8, 8, angle, 0, 10, &red);
uint8_t angle = seconds * 256 / 60;
window->lineWithAngle(8, 8, angle, 10, &red);
window->line(1<<8, 1<<8, 12<<8, 4<<8, &white);
/*for (uint8_t i=0; i<=12; i++) {
window->lineWithAngle(8, 8, 255/12*i, 5, 2, &white);
}