Skip to content

Instantly share code, notes, and snippets.

@hpwit
Last active May 16, 2020 16:33
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/7176a1ee80cfc0b79fdafb1ccdafec26 to your computer and use it in GitHub Desktop.
Save hpwit/7176a1ee80cfc0b79fdafb1ccdafec26 to your computer and use it in GitHub Desktop.
display sd
#include "SD.h"
#include <SPI.h>
#include <SoftwareSerial.h>
#include <SerialCommand.h>
SerialCommand SCmd;
#include <SDConfigFile.h>
#define DATASIZE 100
#include "LoRa.h"
int channel;
long frequency;
char buf[16];
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6
SPIClass SPI2(HSPI);
uint8_t sdbuffer[512];
#define ESP32_VIRTUAL_DRIVER true //To enable virtual pin driver for esp32
#define ESP_VIRTUAL_DRIVER_8 1 //to use the 8:1 ratio si
//#define ESP_VIRTUAL_DRIVER_82_2 //si tu dois 'renverser' l'ordre des PINS
//#define NBIS2SERIALPINS 7 //number of esp32 pins you will use. the total number of strips available will be NBIS2SERIALPINS * 8 here 56 strips
#define NBIS2SERIALPINS 1
//#define NUM_LEDS_PER_STRIP 256 //the length of your strip if you have different strips size put here the longuest strip
#define NUM_LEDS_PER_STRIP 1058
#include "FastLED.h"
#define LATCH_PIN 21
#define CLOCK_PIN 33
int Pins[NBIS2SERIALPINS] = {4};
#define NUM_LEDS NBIS2SERIALPINS * NUM_LEDS_PER_STRIP *8
#define COLOR_ORDER GRB
String incoming = "";
const char CONFIG_FILE[] = "/config.txt";
boolean didReadConfig;
boolean readConfiguration();
uint8_t readbuffer[NUM_LEDS * 3];
uint8_t indexbuffer[NUM_LEDS * 3];
int fps = 23;
int brightness = 50;
int c_mode = 1;
int debug;
bool start;
bool start_2;
bool error_blink_bool = false;
bool port1_bool, port2_bool, port3_bool, port4_bool, port5_bool, port6_bool, p_end_bool;
int port1, port2, port3, port4, port5, port6, p_end;
bool dataseek = false;
bool plus_pressed, minus_pressed;
unsigned long faststartMillis;
unsigned long fastcurrentMillis;
int x = 0;
// SD card
File datafile;
char fileNameFull[10];
int fileNameSuffix = 1;
int totalFilesCount = 0;
File root;
String files[999];
//SD card names
String substring_stri;
int substring_stri_int;
String string_dot;
int index_under;
int index_dot;
long currentMillis = 0;
long lastMillis = 0;
int loops = 0;
int poss;
int filesuff;
int startPressedPlus, startPressedMinus = 0; // the time button was pressed
int endPressedPlus, endPressedMinus = 0;
int timeHoldPlus, timeHoldMinus = 0; // the time button is hold
int timeReleasedPlus, timeReleasedMinus = 0;
int rf_offset = 1;
CRGB *strip[6];
int lengths[6];
CRGB leds[NUM_LEDS];
volatile bool prevFile = false;
volatile bool nextFile = false;
volatile bool playing = false;
volatile bool hadBeenDisplayed =false;
static TaskHandle_t FastLEDshowTaskHandle2 = 0;
static TaskHandle_t userTaskHandle = 0;
//dixit Yves : les deux fonctions suivantes font travailler les deux core de l'ESP32 en alternance : buffer de données et affichage
void FastLEDshowESP322()
{
if (userTaskHandle == 0) {
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
// -- Store the handle of the current task, so that the show task can
// notify it when it's done
// noInterrupts();
userTaskHandle = xTaskGetCurrentTaskHandle();
hadBeenDisplayed=false;
// -- Trigger the show task
xTaskNotifyGive(FastLEDshowTaskHandle2);
//to thge contrary to the other one we do not wait for the display task to come back
}
}
void FastLEDshowTask2(void *pvParameters)
{
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 500 );
// -- Run forever...
for(;;) {
// -- Wait for the trigger
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
FastLED.show();
Serial.println("Fast led hadBeenDisplayed:%d\n",hadBeenDisplayed)
hadBeenDisplayed=true;
// controller.showPixels();
userTaskHandle=0; //so we can't have two display tasks at the same time
}
}
int pos = 0;
int one_second;
float delayt;
void setup()
{
didReadConfig = false;
Serial.begin(115200);
xTaskCreatePinnedToCore(FastLEDshowTask2, "FastLEDshowTask2", 1000, NULL,3, &FastLEDshowTaskHandle2, 0);
SCmd.addCommand("P", process_command);
SCmd.addCommand("B", process_command_player);
//SCmd.addCommand("OFF", blackout);
SCmd.addDefaultHandler(unrecognized);
SPI.begin(5, 19, 27);
SPI2.begin(12, 22, 15, 13); //SCLK, MISO, MOSI, SS
while (!SD.begin(13, SPI2)) {
SD.end();
SD.begin(13, SPI2);
delay(100);
Serial.println("initialization failed NEW SPI!");
}
didReadConfig = readConfiguration();
switch (channel) {
case 1:
frequency = 433075000;
break;
case 2:
frequency = 433100000;
break;
case 3:
frequency = 433200000;
break;
case 4:
frequency = 433300000;
break;
case 5:
frequency = 433350000;
break;
case 6:
frequency = 433475000;
break;
case 7:
frequency = 433625000;
break;
case 8:
frequency = 433800000;
break;
default:
frequency = 433075000;
}
LoRa.setPins(SS, RST, DI0);
//Serial.println("LoRa Sender");
if (!LoRa.begin(frequency)) {
while (1);
}
LoRa.enableCrc();
// LoRa.setSignalBandwidth(250E3);
// LoRa.setSpreadingFactor(9);
pinMode(23, OUTPUT);
root = SD.open("/");
totalFilesCount = getFileCount("/");
datafile = SD.open(files[fileNameSuffix], FILE_READ);
delay(100);
FastLED.addLeds<VIRTUAL_DRIVER,Pins,CLOCK_PIN, LATCH_PIN>(leds,NUM_LEDS_PER_STRIP);
if (port1_bool == true) {
// FastLED.addLeds<WS2812B, PIN_LED, GRB>(leds, 0, port1);
strip[0]=leds;
lengths[0]=port1;
//Serial.println("port1");
//Serial.println(port1);
Serial.printf("addr:%ld length:%d\n", strip[0]-leds,lengths[0]);
//p_end = port1;
}
if (port2_bool == true)
{
//FastLED.addLeds<WS2812B, PIN_LED2, GRB>(leds, port1, port2 - (port1));
strip[1]=&leds[NUM_LEDS_PER_STRIP];
lengths[1]=port2 - port1;
Serial.printf("addr:%ld length:%d\n", strip[1]-leds,lengths[1]);
//Serial.println("port2");
//Serial.println(port2);
//p_end = port2;
}
if (port3_bool == true)
{
//FastLED.addLeds<WS2812B, PIN_LED3, GRB>(leds, port2, port3 - port2);
strip[2]=&leds[2*NUM_LEDS_PER_STRIP];
lengths[2]=port3 - port2;
Serial.printf("addr:%ld length:%d\n", strip[2]-leds,lengths[2]);
//Serial.println("port3");
//Serial.println(port3);
//p_end = port3;
}
if (port4_bool == true)
{
//FastLED.addLeds<WS2812B, PIN_LED4, GRB>(leds, port3, port4 - port3);
strip[3]=&leds[3*NUM_LEDS_PER_STRIP];
lengths[3]=port4 - port3;
Serial.printf("addr:%ld length:%d\n", strip[3]-leds,lengths[3]);
//Serial.println("port4");
//Serial.println(port4);
//p_end = port4;
}
if (port5_bool == true)
{
//FastLED.addLeds<WS2812B, PIN_LED5, GRB>(leds, port4, port5 - port4);
strip[4]=&leds[4*NUM_LEDS_PER_STRIP];
lengths[4]=port5 - port4;
Serial.printf("addr:%ld length:%d\n", strip[4]-leds,lengths[4]);
//Serial.println("port5");
//Serial.println(port5);
//p_end = port5;
}
if (port6_bool == true)
{
//FastLED.addLeds<WS2812B, PIN_LED6, GRB>(leds, port5, port6 - port5);
strip[5]=&leds[5*NUM_LEDS_PER_STRIP];
lengths[5]=port6 - port5;
Serial.printf("addr:%ld length:%d\n", strip[5]-leds,lengths[5]);
//Serial.println("port6");
//Serial.println(port6);
//p_end = port6;
}
FastLED.setDither(0);
FastLED.setBrightness(brightness);
FastLED.setMaxRefreshRate(fps, true);
FastLED.clear();
FastLED.show();
//Serial.println (error_blink_bool);
one_second = 6 * p_end;
hadBeenDisplayed=false;
int fps=40;
delayt= (float)(1000/fps);
}
long lastHandle;
long lasthandle2;
long startt;
void loop (){
startt=0;
while(1)
{
lasthandle2=__clock_cycles();
Serial.printf("FPS fastled: %f \n", (float) 240000000L/(lasthandle2 - lastHandle));
FastLEDshowESP322();
if (datafile.available())
{
if ((datafile.position() % (one_second)) == 0) {
pos = datafile.position();
// Serial.println(pos);
sprintf(buf, "%d:%d", fileNameSuffix, (pos / (p_end * 3) + rf_offset));
if (debug ==1){
Serial.println(buf);
}
LoRa.beginPacket();
LoRa.print(buf);
LoRa.endPacket();
}
lastHandle = __clock_cycles();
datafile.read(readbuffer, p_end *3);
while(!hadBeenDisplayed or (millis()-startt)<delayt){
Serial.printf("hadBeenDisplayed:%d delay:%f time:%d\n",hadBeenDisplayed,delayt,millis()-startt);
}
startt=millis();
hadBeenDisplayed=false;
int offset=0;
for(int i=0;i<5;i++)
{
//Serial.println("move from %ld to %ld size:%d\n",strip[i]-leds,&readbuffer[3*offset],lengths[i]*3);
memcpy(strip[i],&readbuffer[3*offset],lengths[i]*3);
offset+=lengths[i];
}
//
}
else
{
datafile.seek(0);
}
}
}
boolean readConfiguration() {
/*
Length of the longest line expected in the config file.
The larger this number, the more memory is used
to read the file.
You probably won't need to change this number.
*/
const uint8_t CONFIG_LINE_LENGTH = 127;
// The open configuration file.
SDConfigFile cfg;
// Open the configuration file.
if (!cfg.begin(CONFIG_FILE, CONFIG_LINE_LENGTH)) {
// Serial.print("Failed to open configuration file: ");
// Serial.println(CONFIG_FILE);
return false;
}
// Read each setting from the file.
while (cfg.readNextSetting()) {
// Put a nameIs() block here for each setting you have.
// doDelay
if (cfg.nameIs("port1")) {
port1 = cfg.getIntValue();
port1_bool = true;
}
else
if (cfg.nameIs("port2")) {
port2 = cfg.getIntValue();
port2_bool = true;
}
else if (cfg.nameIs("port3")) {
port3 = cfg.getIntValue();
port3_bool = true;
}
else
if (cfg.nameIs("port4")) {
port4 = cfg.getIntValue();
port4_bool = true;
}
else
if (cfg.nameIs("port5")) {
port5 = cfg.getIntValue();
port5_bool = true;
}
else
if (cfg.nameIs("port6")) {
port6 = cfg.getIntValue();
port6_bool = true;
}
else
if (cfg.nameIs("p_end")) {
p_end = cfg.getIntValue();
p_end_bool = true;
}
else
if (cfg.nameIs("fps")) {
fps = cfg.getIntValue();
//Serial.print("Read fps: ");
// Serial.println(fps);
// waitMs integer
} else
if (cfg.nameIs("mode")) {
c_mode = cfg.getIntValue();
// Serial.print("Mode: ");
// Serial.println(c_mode);
// waitMs integer
} else
if (cfg.nameIs("brightness")) {
brightness = cfg.getIntValue();
// Serial.print("Read brightness: ");
// Serial.println(brightness);
}
else if (cfg.nameIs("offset")) {
rf_offset = cfg.getIntValue();
// Serial.print("Read offset: ");
// Serial.println(rf_offset);
}
else if (cfg.nameIs("channel")) {
channel = cfg.getIntValue();
//Serial.print("Read channel: ");
// Serial.println(channel);
}
else
if (cfg.nameIs("debug")) {
debug = cfg.getIntValue();
//Serial.print("Read channel: ");
// Serial.println(channel);
}
else
{
// report unrecognized names.
// Serial.print("Unknown name in config: ");
// Serial.println(cfg.getName());
error_blink_bool = true;
}
}
// clean up
cfg.end();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment