SmallClock eingebaut und Text-Rendering-Funktionen verallgemeinert. #8, closes #9.

This commit is contained in:
Fabian Schlenz 2019-05-21 20:28:11 +02:00
parent 81433310f9
commit a88f7b5ec2
3 changed files with 59 additions and 30 deletions

View File

@ -43,3 +43,27 @@ class Clock : public Effect {
drawNumber(ntpClient.getSeconds(), 8, 8, color_colon);
}
};
class SmallClock : public Effect {
private:
CRGB color_h = CRGB(0xFF0000);
CRGB color_m = CRGB(0x00FF00);
CRGB color_colon = CRGB(0xFFFF00);
public:
SmallClock() {}
void loop() {
clear();
int h = ntpClient.getHours();
drawDigit(numbers3x5, 3, 5, 0, 11, h/10, color_h);
drawDigit(numbers3x5, 3, 5, 4, 11, h%10, color_h);
int m = ntpClient.getMinutes();
drawDigit(numbers3x5, 3, 5, 9, 11, m/10, color_m);
drawDigit(numbers3x5, 3, 5, 13, 11, m%10, color_m);
if (ntpClient.getSeconds() & 1) {
leds[XYsafe(7, 12)] = color_colon;
leds[XYsafe(7, 14)] = color_colon;
}
}
};

View File

@ -36,15 +36,17 @@ void setup() {
Sinematrix3 effect_sinematrix3;
Clock effect_clock;
SmallClock effect_small_clock;
Bell effect_bell;
Static effect_off(CRGB(0x000000));
Effect* current_effect = &effect_clock;
Effect* current_effect = &effect_small_clock;
#define NUM_EFFECTS 4
#define NUM_EFFECTS 5
EffectEntry effects[NUM_EFFECTS] = {
{"bell", &effect_bell},
{"sinematrix3", &effect_sinematrix3},
{"clock", &effect_clock},
{"small_clock", &effect_small_clock},
{"off", &effect_off}
};

59
text.h
View File

@ -1,4 +1,4 @@
static unsigned char Font5x7[] = {
static unsigned char font5x7[] = {
0x00, 0x00, 0x00, 0x00, 0x00,// (space)
0x00, 0x00, 0x5F, 0x00, 0x00,// !
0x00, 0x07, 0x00, 0x07, 0x00,// "
@ -105,40 +105,43 @@ static unsigned char numbers4x7[] = {
0x18, 0x14, 0x7F, 0x10,// 4
0x27, 0x45, 0x45, 0x39,// 5
0x3C, 0x4A, 0x49, 0x30,// 6
0x01, 0x71, 0x09, 0x05, // 7
0x01, 0x71, 0x09, 0x05,// 7
0x36, 0x49, 0x49, 0x36,// 8
0x06, 0x49, 0x29, 0x1E// 9
0x06, 0x49, 0x29, 0x1E // 9
};
unsigned char* Char(unsigned char* font, char c) {
return &font[(c - 32) * 5];
}
static unsigned char numbers3x5[] = {
B01110, B10001, B01110, // 0
B01000, B11111, B00000, // 1
B01001, B10011, B01101, // 2
B10001, B10101, B01110, // 3
B11100, B00100, B11111, // 4
B11101, B10101, B10111, // 5
B01110, B10101, B10110, // 6
B10000, B10011, B11100, // 7
B11111, B10101, B11111, // 8
B11101, B10101, B11111, // 9
};
void DrawSprite(unsigned char* sprite, int length, int xOffset, int yOffset, CRGB color) {
// leds[ XYsafe(x, y) ] = CHSV(0, 0, 255);
for ( byte y = 0; y < 7; y++) {
for ( byte x = 0; x < 5; x++) {
bool on = (sprite[x] >> y & 1) * 255;
if (on) {
leds[ XYsafe(x + xOffset, y + yOffset) ] = color;
}
}
void drawTextSprite(unsigned char* sprite, int w, int h, int xPos, int yPos, CRGB color) {
for (byte y=0; y<h; y++) for (byte x=0; x<w; x++) {
bool on = (sprite[x]>>(h-1-y)&1)*255;
if (on) leds[XYsafe(x+xPos, y+yPos)] = color;
}
}
void DrawText(char *text, int x, int y, CRGB color) {
void drawChar(unsigned char* font, int w, int h, int x, int y, char c, CRGB color) {
unsigned char* sprite = &font[(c-32)*w];
drawTextSprite(sprite, w, h, x, y, color);
}
void drawDigit(unsigned char* font, int w, int h, int x, int y, int digit, CRGB color) {
unsigned char* sprite = &font[digit*w];
drawTextSprite(sprite, w, h, x, y, color);
}
void drawText(char *font, int w, int h, char *text, int x, int y, CRGB color) {
for (int i = 0; i < strlen(text); i++) {
DrawSprite(Char(Font5x7, text[i]), 5, x + i * 6, y, color);
drawChar(font5x7, 5, 7, x+i*(w+1), y, text[i], color);
}
}
void DrawTextOneFrame(char *text, int xOffset, int yOffset, CRGB color) {
DrawText(text, xOffset, yOffset, color);
}
void DrawNumberOneFrame(uint32_t number, int xOffset, int yOffset, CRGB color) {
char buffer[7];
//itoa(number,buffer,10);
sprintf(buffer, "%02d", number);
DrawTextOneFrame(buffer, xOffset, yOffset, color);
}