Skip to content

Commit

Permalink
Merge branch 'master' of github.com:marcmerlin/FastLED_NeoMatrix_Smar…
Browse files Browse the repository at this point in the history
…tMatrix_LEDMatrix_GFX_Demos
  • Loading branch information
marcmerlin committed May 9, 2023
2 parents 3c24408 + 9044f47 commit 0576a79
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 30 deletions.
152 changes: 124 additions & 28 deletions neomatrix_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ but SmartMatrix is sufficiently different to need its own exceptions and handlin
the other libraries define their own FastLED CRGB buffer (RGB888) ).
Backends you should choose from (define 1):
- FRAMEBUFFER (dumb framebuffer that doens't display anywhere)
- SMARTMATRIX (if you are using the old SMARTMATRIX3, also define SMARTMATRIXV3)
- ILI9341
- ST7735_128b160
Expand Down Expand Up @@ -64,7 +65,7 @@ to use, set the define before you include the file.
// chip and do a hardcoded define that works for me, but is unlikely to be what you are also
// using, so really you want to define your driver above, or one will be picked for you and
// it'll probably be the wrong one :)
#if !defined(M24BY24) && !defined(M32BY8X3) && !defined(M16BY16T4) && !defined(M64BY64) && !defined(SMARTMATRIX) && !defined(SSD1331) && !defined(ST7735_128b128) && !defined(ST7735_128b160) && !defined(ILI9341) && !defined(ARDUINOONPC)
#if !defined(M24BY24) && !defined(M32BY8X3) && !defined(M16BY16T4) && !defined(M64BY64) && !defined(SMARTMATRIX) && !defined(SSD1331) && !defined(ST7735_128b128) && !defined(ST7735_128b160) && !defined(ILI9341) && !defined(ARDUINOONPC) && !defined(FRAMEBUFFER)
#ifdef ESP8266
//#define SSD1331
//#define SSD1331_ROTATE 1
Expand Down Expand Up @@ -141,7 +142,13 @@ uint32_t tft_spi_speed;
#define FASTLED_ALLOW_INTERRUPTS 1
// Newer Samguyver ESP32 FastLED has a new I2S implementation that can be
// better (or worse) than then default RMT which only supports 8 channels.
#define FASTLED_ESP32_I2S
// I'm getting brightness issues on LED strips with I2S when outputting to a matrix too
//#define FASTLED_ESP32_I2S

// https://github.com/FastLED/FastLED/blob/master/src/platforms/esp/32/clockless_rmt_esp32.h
// Trying random options to see if they help with my dual output setup on ESP32
#define FASTLED_RMT_MAX_CHANNELS 4
#define FASTLED_ESP32_FLASH_LOCK 1
#pragma message "Please use https://github.com/samguyer/FastLED.git if stock FastLED is unstable with ESP32"
#endif
#include <FastLED.h>
Expand All @@ -161,13 +168,6 @@ uint32_t tft_spi_speed;
// here (NeoMatrix-FastLED-IR actually also uses this to read a config file)
//============================================================================

// control if we decode in 32x32 or 64x64, or something else
#ifdef ESP8266
#define gif_size 32
#else
#define gif_size 64
#endif

// Note, you can use an sdcard on ESP32 or ESP8266 if you really want,
// but if your data fits in built in flash, why not use it?
// Use built in flash via SPIFFS/FATFS
Expand All @@ -190,17 +190,30 @@ uint32_t tft_spi_speed;
}
#elif defined(ESP32)
#define FS_PREFIX ""
//#include <SPIFFS.h>
//#define FSOSPIFFS
//#define FSO SPIFFS
#include "FFat.h"
#define FSO FFat
#define FSOFAT
// Do NOT add a trailing slash, or things will fail
//#define ESP32LITTLEFS
#define ESP32FATFS
#ifdef ESP32FATFS
#include "FFat.h"
#define FSO FFat
#define FSOFAT
#elif defined(ESP32LITTLEFS) // Out of tree LITTLEFS for older ESP32 core
#include "FS.h"
#include <LittleFS.h>
#define FSO LittleFS
#define FSOLITTLEFS
#else // default LittleFS in newer ESP32 core
// LittleFS is more memory efficient than FatFS
#include "FS.h"
#include <LittleFS.h>
#define FSO LittleFS
#define FSOLittleFS
#endif
#if gif_size == 64
#define GIF_DIRECTORY FS_PREFIX "/gifs64"
#else
#elif gif_size == 32
#define GIF_DIRECTORY FS_PREFIX "/gifs"
#else
#define GIF_DIRECTORY FS_PREFIX "/"
#endif
#elif defined(ARDUINOONPC)
#define UNIXFS
Expand Down Expand Up @@ -299,6 +312,8 @@ uint32_t tft_spi_speed;
#include <FastLED_NeoMatrix.h>
#define FASTLED_NEOMATRIX

#define NUM_LEDS_PER_STRIP 256

uint8_t matrix_brightness = 64;
// Used by LEDMatrix
const uint16_t MATRIX_TILE_WIDTH = 8; // width of EACH NEOPIXEL MATRIX (not total display)
Expand Down Expand Up @@ -398,6 +413,31 @@ uint32_t tft_spi_speed;
NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );

//----------------------------------------------------------------------------
#elif defined(FRAMEBUFFER)
#include <Framebuffer_GFX.h>
uint8_t matrix_brightness = 255;

#pragma message "Dumb Framebuffer for ESP32 with 64x96 resolution"
const uint16_t MATRIX_TILE_WIDTH = 64; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 96; // height of each matrix

// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically

#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;

void show_callback() {};
Framebuffer_GFX *matrix = new Framebuffer_GFX(matrixleds, MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, show_callback);


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#elif defined(SMARTMATRIX)
// CHANGEME for ESP32, see MatrixHardware_ESP32_V0.h in SmartMatrix/src
Expand Down Expand Up @@ -517,6 +557,7 @@ uint32_t tft_spi_speed;
#include "Adafruit_ILI9341.h"
#include <FastLED_SPITFT_GFX.h>
#else
#define NO_TFT_SPI_PIN_DEFAULTS
#include <FastLED_ArduinoGFX_TFT.h>
#endif

Expand Down Expand Up @@ -564,8 +605,10 @@ uint32_t tft_spi_speed;
//Adafruit_ILI9341 *tft = new Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
Adafruit_ILI9341 *tft = new Adafruit_ILI9341((int8_t) TFT_CS2, TFT_DC, TFT_RST);
#else
// There used to be support for DMA and ESP32 DMA, but it was removed
// https://github.com/moononournation/Arduino_GFX/commit/3461afcc4288892cea54da1a82ffdfafd68eeac9
Arduino_DataBus *bus2 = new Arduino_HWSPI(TFT_DC, TFT_CS2); // 42fps ILI9341 at 80Mhz
Arduino_ILI9341 *tft = new Arduino_ILI9341(bus2, TFT_RST, 1 /* rotation */);
Arduino_ILI9341 *tft = new Arduino_ILI9341(bus2, TFT_RST, 3 /* rotation */);
#endif
// It would be great if we could do this, but many programs use size related variables to
// define static arrays, which required constants
Expand Down Expand Up @@ -632,6 +675,7 @@ uint32_t tft_spi_speed;
#include <Adafruit_ST7735.h>
#include <FastLED_SPITFT_GFX.h>
#else
#define NO_TFT_SPI_PIN_DEFAULTS
#include <FastLED_ArduinoGFX_TFT.h>
#endif

Expand Down Expand Up @@ -731,6 +775,7 @@ uint32_t tft_spi_speed;
#include <Adafruit_SSD1331.h>
#include <FastLED_SPITFT_GFX.h>
#else
#define NO_TFT_SPI_PIN_DEFAULTS
#include <FastLED_ArduinoGFX_TFT.h>
#endif

Expand Down Expand Up @@ -891,6 +936,10 @@ uint32_t tft_spi_speed;
#pragma message "M128BY192 read from /root/NM/gfxdisplay"
const uint16_t MATRIX_TILE_WIDTH = 128;
const uint16_t MATRIX_TILE_HEIGHT= 192;
#elif GFXDISPLAY_M128BY192_4_3
#pragma message "M128BY192_4_3 read from /root/NM/gfxdisplay"
const uint16_t MATRIX_TILE_WIDTH = 128;
const uint16_t MATRIX_TILE_HEIGHT= 192;
#elif GFXDISPLAY_M64BY96
#pragma message "M64Y96 read from /root/NM/gfxdisplay"
const uint16_t MATRIX_TILE_WIDTH = 64;
Expand Down Expand Up @@ -940,6 +989,10 @@ uint32_t tft_spi_speed;
#pragma message "M128BY192 read from /root/NM/gfxdisplay"
const uint16_t MATRIX_TILE_WIDTH = 128;
const uint16_t MATRIX_TILE_HEIGHT= 192;
#elif GFXDISPLAY_M128BY192_4_3
#pragma message "M128BY192_4_3 read from /root/NM/gfxdisplay"
const uint16_t MATRIX_TILE_WIDTH = 128;
const uint16_t MATRIX_TILE_HEIGHT= 192;
#else
#pragma message "Please write M384BY256 or equivalent to /root/NM/gfxdisplay (see ../../makeNativeArduino.mk)"
const uint16_t MATRIX_TILE_WIDTH = 128;
Expand Down Expand Up @@ -1060,10 +1113,10 @@ void *mallocordie(const char *varname, uint32_t req, bool psram=true) {
if (psram) {
mem = ps_malloc(req);
} else {
mem = malloc(req);
mem = calloc(1, req);
}
#else
mem = malloc(req);
mem = calloc(1, req);
#endif

if (mem) {
Expand All @@ -1087,6 +1140,29 @@ uint32_t millisdiff(uint32_t before) {
return((millis()-before) ? (millis()-before): 1);
}

uint16_t text_width(char *text, Adafruit_GFX *gfx = NULL) {
if (!gfx) gfx = matrix;
int16_t dummy;
uint16_t w, h;

gfx->getTextBounds(text, 0, 0, &dummy, &dummy, &w, &h);
return(w);
}

uint16_t text_xcenter(char *text, Adafruit_GFX *gfx = NULL) {
uint8_t fudge = 0;

if (!gfx) gfx = matrix;
// workaround for bug in adafruit::gfx that returns a width too large?
if (gfx->width() == 24) fudge = 2;
//Serial.print(gfx->width() );
//Serial.print(" ");
//Serial.print(text_width(text, gfx));
//Serial.print(" ");
//Serial.println((gfx->width() - text_width(text, gfx)) / 2);
return(fudge + (gfx->width() - text_width(text, gfx)) / 2);
}

void matrix_setup(bool initserial=true, int reservemem = 40000) {
reservemem = reservemem; // squelch compiler warning if var is unused.

Expand Down Expand Up @@ -1150,7 +1226,9 @@ void matrix_setup(bool initserial=true, int reservemem = 40000) {
#ifdef ESP8266
FastLED.addLeds<WS2811_PORTA,3>(matrixleds, NUMMATRIX/MATRIX_TILE_H).setCorrection(TypicalLEDStrip);
#else
FastLED.addLeds<WS2811_PORTD,3>(matrixleds, NUMMATRIX/MATRIX_TILE_H).setCorrection(TypicalLEDStrip);
FastLED.addLeds<WS2812B,14, GRB>(matrixleds,0*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP).setCorrection(0x333333);
FastLED.addLeds<WS2812B,12, GRB>(matrixleds,1*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP).setCorrection(0x333333);
FastLED.addLeds<WS2812B,15, GRB>(matrixleds,2*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP).setCorrection(0x333333);
#endif
Serial.print("Neomatrix parallel output, total LEDs: ");
Serial.println(NUMMATRIX);
Expand Down Expand Up @@ -1191,32 +1269,35 @@ void matrix_setup(bool initserial=true, int reservemem = 40000) {
#endif // ESP32
matrix_gamma = 2.4; // higher number is darker, needed for Neomatrix more than SmartMatrix

//============================================================================================
#elif defined(FRAMEBUFFER)
Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Framebuffer GFX output, total LEDs: ");
Serial.println(NUMMATRIX);

//============================================================================================
#elif defined(SMARTMATRIX)
matrix_gamma = 1; // SmartMatrix should be good by default.
matrixLayer.addLayer(&backgroundLayer);
Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix GFX output, total LEDs: ");
Serial.println(NUMMATRIX);
// SmartMatrix takes all the RAM it can get its hands on. Get it to leave some
// free RAM so that other libraries can work too.
#ifdef ESP32
if (reservemem) matrixLayer.begin(reservemem); else matrixLayer.begin();
#else
matrixLayer.begin();
#endif
// This sets the neomatrix and LEDMatrix pointers
show_callback();
matrixLayer.setRefreshRate(240);
backgroundLayer.enableColorCorrection(true);
Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix GFX output, total LEDs: ");
Serial.println(NUMMATRIX);
// Quick hello world test
#ifndef DISABLE_MATRIX_TEST
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix Grey Demo");
backgroundLayer.fillScreen( {0x80, 0x80, 0x80} );
// backgroundLayer.swapBuffers();
show_callback();
delay(1000);
#endif
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix Init Done");
// This sets the neomatrix and LEDMatrix pointers
show_callback();

//============================================================================================
#elif defined(LINUX_RENDERER_X11)
Expand Down Expand Up @@ -1279,6 +1360,21 @@ void matrix_setup(bool initserial=true, int reservemem = 40000) {
defaults.pwm_dither_bits = 1;
defaults.led_rgb_sequence = "RBG";
defaults.panel_type = "FM6126A";
#elif GFXDISPLAY_M128BY192_4_3
defaults.rows = 32;
defaults.cols = 64;
defaults.chain_length = 4;
defaults.parallel = 3;
defaults.pwm_lsb_nanoseconds = 100;
defaults.pwm_bits = 9;
// Time dithering of lower bits
// 2 changes speed from 400Hz (from 160Hz)
// or 520Hz with lsb_ns at 50 not 100
// but things are 1/3rd as bright so
// we go back to 0 for 333Hz with 50ns
defaults.pwm_dither_bits = 1;
//defaults.led_rgb_sequence = "RBG";
defaults.pixel_mapper_config = "V-mapper;Rotate:90";
#else
defaults.rows = 64;
defaults.cols = 128;
Expand Down Expand Up @@ -1317,7 +1413,7 @@ void matrix_setup(bool initserial=true, int reservemem = 40000) {
#elif defined(ILI9341)
// On my test bench, my ILI9341 doesn't like more than 24Mhz, although when wired properly
// it should run at 80Mhz
tft_spi_speed = 20 * 1000 * 1000;
tft_spi_speed = 60 * 1000 * 1000;
Serial.println("");
Serial.println("ILI9341 tft begin");
// Need to init the underlying TFT SPI engine
Expand Down
5 changes: 3 additions & 2 deletions neomatrix_config_tftonly.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ CS2: 2 => if you have 2 different screens, need 2 CS pins
//----------------------------------------------------------------------------
// ILI0341
#define HAS_TFT

#define NO_TFT_SPI_PIN_DEFAULTS
#include <FastLED_ArduinoGFX_TFT.h>


// There used to be support for DMA and ESP32 DMA, but it was removed
// https://github.com/moononournation/Arduino_GFX/commit/3461afcc4288892cea54da1a82ffdfafd68eeac9
Arduino_DataBus *bus2 = new Arduino_HWSPI(TFT_DC, TFT_CS2); // 42fps ILI9341 at 80Mhz
Arduino_ILI9341 *tft = new Arduino_ILI9341(bus2, TFT_RST, 1 /* rotation */);
// It would be great if we could do this, but many programs use size related variables to
Expand Down

0 comments on commit 0576a79

Please sign in to comment.