Skip to content

Instantly share code, notes, and snippets.

@hpwit
Last active May 2, 2023 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpwit/4dbef2dc24dc23c04d8c7715a04d7ff1 to your computer and use it in GitHub Desktop.
Save hpwit/4dbef2dc24dc23c04d8c7715a04d7ff1 to your computer and use it in GitHub Desktop.
#include "FastLED.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "SD.h"
#include "SPI.h"
#define NUM_LEDS 5184
#define LEDS_PER_PIN (NUM_LEDS / 6)
#define QUEUE_SIZE 50
#define DELAY_FRAMES 30 //to set up the delay between frames you'll not be doing more than 36fps anyway
CRGB leds[NUM_LEDS];
CRGB buffer[NUM_LEDS];
File myFile;
//this is to indicate that a new frame is ready
static xQueueHandle _frame_queue;
static TaskHandle_t _displayFrameTaskhandle;
bool sd_card_present=false;
static void displayframe(void *pvParameters)
{
CRGB *frame=NULL;
for(;;)
{
if(xQueueReceive(_frame_queue, &frame, portMAX_DELAY) == pdTRUE)
{
memcpy(leds,frame,NUM_LEDS*sizeof(CRGB));
FastLED.show();
}
}
}
void stripsConfig() {
FastLED.addLeds<WS2812B, 12, EOrder::GRB>(leds, 0 * LEDS_PER_PIN, LEDS_PER_PIN);
FastLED.addLeds<WS2812B, 13, EOrder::GRB>(leds, 1 * LEDS_PER_PIN, LEDS_PER_PIN);
FastLED.addLeds<WS2812B, 16, EOrder::GRB>(leds, 2 * LEDS_PER_PIN, LEDS_PER_PIN);
FastLED.addLeds<WS2812B, 17, EOrder::GRB>(leds, 3 * LEDS_PER_PIN, LEDS_PER_PIN);
FastLED.addLeds<WS2812B, 18, EOrder::GRB>(leds, 4 * LEDS_PER_PIN, LEDS_PER_PIN);
FastLED.addLeds<WS2812B, 19, EOrder::GRB>(leds, 5 * LEDS_PER_PIN, LEDS_PER_PIN);
}
bool createQueue()
{
_frame_queue = xQueueCreate(QUEUE_SIZE, sizeof(CRGB *));
if(!_frame_queue){
Serial.printf("Impossible to create the queue");
return false;
}
return true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//strip init
stripsConfig();
//queue init
if(!createQueue())
return;
//init displatyframe task on second core
xTaskCreateUniversal(displayframe, "displayframe", 4096, NULL, 3, &_displayFrameTaskhandle, 0);
//init SD card
if(!SD.begin()){
Serial.println("Card Mount Failed");
sd_card_present=false;
}
else
{
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
sd_card_present=false;
}
else
{
sd_card_present=true;
}
}
//open the file
myFile=SD.open("/filename"); //to change with your filename
}
bool readNextFrame(File sdfile)
{
if (sdfile.available() > 0)
{
sdfile.read((uint8_t *)buffer, NUM_LEDS*3);
return true;
}
else
{
return false;
}
}
void loop() {
// put your main code here, to run repeatedly:
if(sd_card_present)
{
if (!readNextFrame(myFile))
{
//loop over the file
myFile.seek(0);
}
else
{
xQueueSend(_frame_queue, &buffer, portMAX_DELAY) ;
vTaskDelay(DELAY_FRAMES);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment