6 #include <Adafruit_GFX.h>
7 #include <Adafruit_SSD1331.h>
26 template<u
int8_t DATA_PIN = 45, u
int32_t RGB_ORDER = GRB, u
int16_t NUM_LEDS = 12>
28 public HoloLights<DATA_PIN, RGB_ORDER, NUM_LEDS>
42 HoloDisplay(PixelType type = kRGBW, const int id = 0, const byte pin = 45, const byte
numPixels = 12) :
57 pinMode(SD_CS, OUTPUT);
58 digitalWrite(SD_CS, HIGH);
81 fDownloadStream = stream;
111 if (cmd[0] !=
'H' || cmd[1] !=
'O')
120 int commandLength = strlen(cmd);
121 const char* ch = (commandLength >= 3) ? &cmd[2] :
"";
124 unsigned char c = (
unsigned char)*ch++;
125 *fp++ = (char)((c >=
'a' && c <=
'z') ? c - (
'a' -
'A') : c);
130 *fp++ =
'.'; *fp++ =
'B'; *fp++ =
'D'; *fp++ =
'2'; *fp =
'\0';
132 if (cmd[1] ==
'D' && fDownloadStream != NULL)
136 fDownloadStream->println(
"ACK");
137 if (fDownloadStream->find(
"LEN"))
139 len = fDownloadStream->readStringUntil(
'\n').toInt();
141 uint32_t bytesReceived = uploadFile(filename, len);
143 if (bytesReceived > 0)
159 else if (cmd[1] ==
'P')
170 void testPattern(
void)
174 for(y = 0; y < 64; y++)
176 fDisplay.setAddrWindow(0, y, 96, 1);
177 uint16_t color = BLACK;
178 if(y > 55) color = WHITE;
179 else if(y > 47) color = BLUE;
180 else if(y > 39) color = GREEN;
181 else if(y > 31) color = CYAN;
182 else if(y > 23) color = RED;
183 else if(y > 15) color = MAGENTA;
184 else if(y > 7) color = YELLOW;
185 fDisplay.writeColor(color, 96);
196 fDisplay.fillScreen(BLACK);
206 fFrameCountInitial = 0;
216 void play(
const char* filename)
223 f = SD.open(filename);
239 fFrameCount = read32();
240 fFrameCountInitial = fFrameCount;
241 fFramesPerSec = read32();
243 fTimestart = millis();
252 if (fFrameCount > 0 && millis() - fTimestamp >= 100)
254 fTimestamp = millis();
263 DEBUG_PRINT((
float)fFrameCountInitial / ((fTimestamp - fTimestart) / 1000.0));
275 long uploadFile(
const char* filename, uint32_t fileLength)
277 uint32_t receivedFileSize = 0;
278 const uint32_t bufferSize = 4098;
279 unsigned char* buffer = (
unsigned char*)malloc(bufferSize);
280 if (buffer == NULL || fDownloadStream == NULL)
282 if (SD.exists(filename))
284 File file = SD.open(filename, FILE_WRITE);
285 while (receivedFileSize < fileLength)
288 if (!fDownloadStream->find(
"ACK"))
296 sum = fDownloadStream->readStringUntil(
'\n').toInt();
297 unsigned char* bufin = buffer;
298 unsigned char* bufout = buffer;
299 int nprbytes = fDownloadStream->readBytesUntil(
'\n', buffer, bufferSize);
301 for (
int i = 0; i < nprbytes; i++)
307 static char sPr2Six[256] PROGMEM =
309 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
310 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
311 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
312 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
313 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
314 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
315 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
316 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
317 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
318 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
319 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
320 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
321 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
322 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
323 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
324 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
327 receivedFileSize += nprbytes;
328 showDownloadProgress(receivedFileSize, fileLength);
331 *bufout++ = (
unsigned char)((
unsigned int) (pgm_read_byte(&sPr2Six[bufin[0]]) << 2) | (
unsigned int)pgm_read_byte(&sPr2Six[bufin[1]]) >> 4);
332 *bufout++ = (
unsigned char)((
unsigned int) (pgm_read_byte(&sPr2Six[bufin[1]]) << 4) | (
unsigned int)pgm_read_byte(&sPr2Six[bufin[2]]) >> 2);
333 *bufout++ = (
unsigned char)((
unsigned int) (pgm_read_byte(&sPr2Six[bufin[2]]) << 6) | (
unsigned int)pgm_read_byte(&sPr2Six[bufin[3]])]);
339 *bufout++ = (
unsigned char) (pgm_read_byte(&sPr2Six[bufin[0]]) << 2 | pgm_read_byte(&sPr2Six[bufin[1]]) >> 4);
343 *bufout++ = (
unsigned char) (pgm_read_byte(&sPr2Six[bufin[1]]) << 4 | pgm_read_byte(&sPr2Six[bufin[2]]) >> 2);
347 *bufout++ = (
unsigned char) (pgm_read_byte(&sPr2Six[bufin[2]]) << 6 | pgm_read_byte(&sPr2Six[bufin[3]]));
349 file.write(buffer, bufout - buffer);
351 fDownloadStream->println(
"ACK");
364 showDownloadProgress(0, 0);
368 if (receivedFileSize == 0 || receivedFileSize != fileLength)
373 return receivedFileSize;
379 uint16_t sdbuffer[96];
380 int frameType = (char)f.read();
384 for (
int y = 0; y < 64; y++)
386 int num = f.read((uint8_t*)sdbuffer,
sizeof(sdbuffer));
387 if (num !=
sizeof(sdbuffer))
397 fDisplay.setAddrWindow(0, y, 96, 1);
398 fDisplay.writePixels(sdbuffer, 96);
402 else if (frameType == 1)
411 int num2 = f.read((uint8_t*)sdbuffer, w*
sizeof(uint16_t));
412 if (num2 !=
int(w*
sizeof(uint16_t)))
422 fDisplay.setAddrWindow(x,y,w,1);
423 fDisplay.writePixels(sdbuffer, w);
431 void showDownloadProgress(uint32_t receivedFileSize, uint32_t fileLength)
435 int val = (fileLength > 0) ? (
int)(255 * numLEDs * (double)receivedFileSize / (
double)fileLength) : 0;
436 for (
unsigned i = 0; i < numLEDs; i++)
460 ((uint8_t *)&result)[0] = f.read();
461 ((uint8_t *)&result)[1] = f.read();
462 ((uint8_t *)&result)[2] = f.read();
463 ((uint8_t *)&result)[3] = f.read();
468 Adafruit_SSD1331 fDisplay;
471 boolean fDownload =
false;
472 uint32_t fFrameCount;
475 uint32_t fFrameCountInitial;
476 uint32_t fFramesPerSec;
477 Stream* fDownloadStream = NULL;
491 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
513 #elif defined(__AVR__)
546 #error Holo OLED - Target MCU not supported