From 0e50ec19ce94728b99b00b2b7be4da186a23c215 Mon Sep 17 00:00:00 2001
From: Daniel Drizhuk <complynx@yandex.ru>
Date: Wed, 5 May 2021 10:47:24 +0300
Subject: [PATCH] I2S ws2812 color profiles

---
 extras/ws2812_i2s/ws2812_i2s.c | 34 +++++++++++++++--------
 extras/ws2812_i2s/ws2812_i2s.h | 51 ++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/extras/ws2812_i2s/ws2812_i2s.c b/extras/ws2812_i2s/ws2812_i2s.c
index 88caf27..9ad06ff 100644
--- a/extras/ws2812_i2s/ws2812_i2s.c
+++ b/extras/ws2812_i2s/ws2812_i2s.c
@@ -177,23 +177,33 @@ void ws2812_i2s_update(ws2812_pixel_t *pixels, pixeltype_t type)
     uint16_t *p_dma_buf = dma_buffer;
 
     for (uint32_t i = 0; i < (dma_buffer_size / type); i++) {
-        // green
-        *p_dma_buf++ =  bitpatterns[pixels[i].green & 0x0F];
-        *p_dma_buf++ =  bitpatterns[pixels[i].green >> 4];
-
-        // red
-        *p_dma_buf++ =  bitpatterns[pixels[i].red & 0x0F];
-        *p_dma_buf++ =  bitpatterns[pixels[i].red >> 4];
-
-        // blue
-        *p_dma_buf++ =  bitpatterns[pixels[i].blue & 0x0F];
-        *p_dma_buf++ =  bitpatterns[pixels[i].blue >> 4];
-        
+#ifdef I2S_COLOR_WHITE_FIRST
         if(type == PIXEL_RGBW) {
           // white
           *p_dma_buf++ =  bitpatterns[pixels[i].white & 0x0F];
           *p_dma_buf++ =  bitpatterns[pixels[i].white >> 4];
         }
+#endif
+
+        // I2S_COLOR_FIRST, normally green
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_FIRST & 0x0F];
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_FIRST >> 4];
+
+        // I2S_COLOR_SECOND, normally red
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_SECOND & 0x0F];
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_SECOND >> 4];
+
+        // I2S_COLOR_THIRD, normally blue
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_THIRD & 0x0F];
+        *p_dma_buf++ =  bitpatterns[pixels[i].I2S_COLOR_THIRD >> 4];
+        
+#ifndef I2S_COLOR_WHITE_FIRST
+        if(type == PIXEL_RGBW) {
+          // white
+          *p_dma_buf++ =  bitpatterns[pixels[i].white & 0x0F];
+          *p_dma_buf++ =  bitpatterns[pixels[i].white >> 4];
+        }
+#endif
     }
 
     i2s_dma_processing = true;
diff --git a/extras/ws2812_i2s/ws2812_i2s.h b/extras/ws2812_i2s/ws2812_i2s.h
index c6c8df5..7ebf473 100644
--- a/extras/ws2812_i2s/ws2812_i2s.h
+++ b/extras/ws2812_i2s/ws2812_i2s.h
@@ -46,6 +46,57 @@ typedef enum {
   PIXEL_RGBW = 16
 } pixeltype_t;
 
+#define I2S_COLOR_PROFILE_N_RGB 1
+#define I2S_COLOR_PROFILE_N_RBG 2
+#define I2S_COLOR_PROFILE_N_GBR 3
+#define I2S_COLOR_PROFILE_N_GRB 4
+#define I2S_COLOR_PROFILE_N_BGR 5
+#define I2S_COLOR_PROFILE_N_BRG 6
+
+#ifndef I2S_COLOR_PROFILE
+
+#if defined(I2S_COLOR_PROFILE_BRG)
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_BRG
+#elif defined(I2S_COLOR_PROFILE_RBG)
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_RBG
+#elif defined(I2S_COLOR_PROFILE_GBR)
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_GBR
+#elif defined(I2S_COLOR_PROFILE_RGB)
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_RGB
+#elif defined(I2S_COLOR_PROFILE_BGR)
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_BGR
+#else
+#define I2S_COLOR_PROFILE I2S_COLOR_PROFILE_N_GRB
+#endif
+
+#endif
+
+#if I2S_COLOR_PROFILE == I2S_COLOR_PROFILE_N_RGB
+#define I2S_COLOR_FIRST red
+#define I2S_COLOR_SECOND green
+#define I2S_COLOR_THIRD blue
+#elif I2S_COLOR_PROFILE == I2S_COLOR_PROFILE_N_RBG
+#define I2S_COLOR_FIRST red
+#define I2S_COLOR_SECOND blue
+#define I2S_COLOR_THIRD green
+#elif I2S_COLOR_PROFILE == I2S_COLOR_PROFILE_N_GBR
+#define I2S_COLOR_FIRST green
+#define I2S_COLOR_SECOND blue
+#define I2S_COLOR_THIRD red
+#elif I2S_COLOR_PROFILE == I2S_COLOR_PROFILE_N_GRB
+#define I2S_COLOR_FIRST green
+#define I2S_COLOR_SECOND red
+#define I2S_COLOR_THIRD blue
+#elif I2S_COLOR_PROFILE == I2S_COLOR_PROFILE_N_BGR
+#define I2S_COLOR_FIRST blue
+#define I2S_COLOR_SECOND green
+#define I2S_COLOR_THIRD red
+#else
+#define I2S_COLOR_FIRST blue
+#define I2S_COLOR_SECOND red
+#define I2S_COLOR_THIRD green
+#endif
+
 /**
  * Initialize i2s and dma subsystems to work with ws2812 led strip.
  *