-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastLED-textScroll.ino
196 lines (162 loc) · 6.25 KB
/
fastLED-textScroll.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <FastLED.h> // version 3.7.8
#include "Font6x8.h"
#define LED_PIN 0
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 20
// Params for width and height
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 8;
// Param for different pixel layouts
const bool kMatrixSerpentineLayout = true;
const bool kMatrixVertical = true;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
const char* message = "Historia est Magistra VITAE";
int scrollPosition = 0;
void setup()
{
//Serial.begin(115200); // OFF for better performance
//delay(3000);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 700); // cap on 700mA
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
displayMessage(CRGB::Blue, message, 8); // Scroll message
for (int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8_video(BRIGHTNESS); } // adjust brightness
//FastLED.setBrightness(BRIGHTNESS);
// Show the LEDDs
FastLED.show();
FastLED.delay(10);
}
String convertToSingleByte(String input) {
String output = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 0xC4 || c == 0xC5) {
char nextChar = input.charAt(i + 1);
switch (nextChar) {
case 0x8D: output += (char)0xE8; i++; break; // č (C4)
case 0x8C: output += (char)0xC8; i++; break; // Č (C4)
case 0x87: output += (char)0xE6; i++; break; // ć (C4)
case 0x86: output += (char)0xC6; i++; break; // Ć (C4)
case 0xBE: output += (char)0x9E; i++; break; // ž (C5)
case 0xBD: output += (char)0x8E; i++; break; // Ž (C5)
case 0xA1: output += (char)0x9A; i++; break; // š (C5)
case 0xA0: output += (char)0x8A; i++; break; // Š (C5)
case 0x91: output += (char)0xF0; i++; break; // đ (C5)
case 0x90: output += (char)0xD0; i++; break; // Đ (C5)
default: output += c; break;
}
} else {
output += c;
}
}
return output;
}
void displayMessage(CRGB color, const char* message, int numSpaces) {
static bool bufferInitialized = false;
static CRGB* displayBuffer = nullptr; // Pointer to store the buffer
static int bufferSize = 0;
static String previousMessage = "";
static int previousNumSpaces = 0;
String convertedMessage = convertToSingleByte(message);
// Check if the message or number of spaces has changed
if (previousMessage != convertedMessage || previousNumSpaces != numSpaces)
{
// Free the old buffer if it exists
if (displayBuffer != nullptr) {
delete[] displayBuffer;
}
// Calculate the new buffer size
bufferSize = (convertedMessage.length() + numSpaces) * 6 * kMatrixHeight;
displayBuffer = new CRGB[bufferSize]; // Allocate memory for the new buffer
// Initialize the buffer with the new message and spaces
for (int i = 0; i < bufferSize; i++)
displayBuffer[i] = CRGB::Black;
// Add spaces in front of the message
for (int i = 0; i < numSpaces; i++) {
int charPosition = i * 6;
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 8; y++) {
int bufferIndex = (charPosition + x) + (y * (convertedMessage.length() + numSpaces) * 6);
if (bufferIndex < bufferSize)
displayBuffer[bufferIndex] = CRGB::Black;
}
}
}
for (int i = 0; i < convertedMessage.length(); i++) {
char charToDisplay = convertedMessage.charAt(i);
int charPosition = (i + numSpaces) * 6;
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 8; y++) {
if (bitRead(pgm_read_byte(&(Font[charToDisplay][x])), y) == 1) {
int bufferIndex = (charPosition + x) + (y * (convertedMessage.length() + numSpaces) * 6);
if (bufferIndex < bufferSize)
displayBuffer[bufferIndex] = color;
}
}
}
}
previousMessage = convertedMessage; // Update the previous message
previousNumSpaces = numSpaces; // Update the previous number of spaces
scrollPosition = 0; // Reset the scroll position
}
fill_solid(leds, NUM_LEDS, CRGB::Black); // Clear the display
// Copy the relevant part of the buffer to the LED matrix
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
int bufferIndex = (scrollPosition + x) % ((convertedMessage.length() + numSpaces) * 6) + (y * (convertedMessage.length() + numSpaces) * 6);
if (bufferIndex < bufferSize)
leds[XYsafe(kMatrixWidth - 1 - x, y)] = displayBuffer[bufferIndex];
}
}
scrollPosition++;
if (scrollPosition >= (convertedMessage.length() + numSpaces) * 6)
scrollPosition = 0;
}
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false)
{
if (kMatrixVertical == false)
i = (y * kMatrixWidth) + x;
else
i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
}
if( kMatrixSerpentineLayout == true)
{
if (kMatrixVertical == false)
{
if( y & 0x01)
{
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
}
else
{
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
else
{ // vertical positioning
if ( x & 0x01)
i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
else
i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
}
}
return i;
}
uint16_t XYsafe( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}