From c11aec2152c0bb850b646de371184eee5913f3b6 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Wed, 27 Apr 2022 03:36:33 +0200
Subject: [PATCH] =?UTF-8?q?Working=20packet=20Handler!=20Committing=20with?=
 =?UTF-8?q?=20debug=20output=20and=20stuff=20=F0=9F=98=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CLC-qthing/SiliconTorch/FxCyanRGB8.cpp | 49 ++++++++++++++++++----
 devtools/FxCyanRGB8.py                 | 58 ++++++++++++++++++++++----
 2 files changed, 92 insertions(+), 15 deletions(-)

diff --git a/CLC-qthing/SiliconTorch/FxCyanRGB8.cpp b/CLC-qthing/SiliconTorch/FxCyanRGB8.cpp
index d31f5f7..fa4a88a 100644
--- a/CLC-qthing/SiliconTorch/FxCyanRGB8.cpp
+++ b/CLC-qthing/SiliconTorch/FxCyanRGB8.cpp
@@ -69,14 +69,12 @@ namespace SiliconTorch {
 
       } else if(packetStartIdx > startIdx) {
 
-        ESP_LOGW("Mapping[ P > W ]", "startIdx[ %d ]   packetIdx[ %d ]   mapping: packetPx[ 0 ] to localPx[ %d ]", startIdx, packetStartIdx, packetStartIdx-startIdx);
-
         /*
 
           P = 10;
           W = 5;
 
-          delta = P - W  // 5;
+          delta = P - W;  // 5
 
           for I = delta; I < min(LEDs, packetLen + delta); i++:
             leds[I] = packet[I - delta]
@@ -85,20 +83,56 @@ namespace SiliconTorch {
 
         uint16_t delta = packetStartIdx - startIdx;
 
+        uint8_t X = 0;
+
         for (uint16_t px = delta; px < std::min(leds, (uint16_t)(packetPxs + delta)); px++) {
           buffer1[px].r = packetPx[px - delta].r / 255.0f;
           buffer1[px].g = packetPx[px - delta].g / 255.0f;
           buffer1[px].b = packetPx[px - delta].b / 255.0f;
+
+          X++;
         }
 
+        ESP_LOGW("Mapping[ P > W ]", "startIdx[ %d ]   packetIdx[ %d ]   mapping: packetPx[ 0 ] to localPx[ %d ]    Wrote pixels#[ %d ]", startIdx, packetStartIdx, packetStartIdx-startIdx, X);
+
       } else if (packetStartIdx < startIdx) {
 
-        // ESP_LOGW("Mapping[ P < W ]", "startIdx[ %d ]   packetIdx[ %d ]   mapping: localPx[ 0 ] to packetPx[ 0 ]", startIdx, packetStartIdx, );
-        ESP_LOGE("Mapping[ P < W ]", "NotImplementedYet");
 
-      }
+        /*
+
+          P = 5;
+          W = 10;
+
+          delta = W - P;  // 5
+
+          for I = 0; I < __; i++:
+            leds[I] = packet[I + delta];
+
+
+        */
 
-      requestBufferSwap();
+        uint16_t delta = startIdx - packetStartIdx;
+
+        // Packet does not contain any pixels for us
+        if (packetPxs < delta) return;  // TODO: '<'  or '<='  ??
+
+        uint8_t X = 0;
+
+        for (uint16_t px = 0; px < std::min(leds, (uint16_t)(packetPxs - delta)); px++) {
+          buffer1[px].r = packetPx[px + delta].r / 255.0f;
+          buffer1[px].g = packetPx[px + delta].g / 255.0f;
+          buffer1[px].b = packetPx[px + delta].b / 255.0f;
+
+          X++;
+
+          ESP_LOGW("Mapping[ P < W ]", "__MAP__[  W[ %d ] <-- P[ %d ]  ]", px, px+delta);
+        }
+
+
+        ESP_LOGW("Mapping[ P < W ]", "startIdx[ %d ]   packetIdx[ %d ]   mapping: packetPx[ 0 ] to localPx[ %d ]    Wrote pixels#[ %d ]", startIdx, packetStartIdx, startIdx-packetStartIdx, X);
+
+
+      }
     }
 
     void FxCyanRGB8::registerAtCyanBus(CyanBus::CyanBus& cyanBus) {
@@ -123,6 +157,7 @@ namespace SiliconTorch {
 
         qthing::addUDPPacketCallback(HEADER, [&, headerLength](qthing::udpPacket packet) {
           this->handlePacket((uint8_t*)(packet.payload + headerLength), packet.length - headerLength);
+          requestBufferSwap();
         });
       }
     }
diff --git a/devtools/FxCyanRGB8.py b/devtools/FxCyanRGB8.py
index e89652e..d6282e7 100755
--- a/devtools/FxCyanRGB8.py
+++ b/devtools/FxCyanRGB8.py
@@ -18,6 +18,11 @@ host = sys.argv[1]
 print(f'Using host[ {host} ]')
 
 
+startIdx = 0
+if len(sys.argv) > 2: startIdx = int(sys.argv[2])
+print(f'Using startIdx[ {startIdx} ]')
+
+
 def sendRaw(data: bytes, _socket = socket(AF_INET, SOCK_DGRAM)):
   try:
     _socket.sendto(data, (host, port))
@@ -27,16 +32,39 @@ def sendRaw(data: bytes, _socket = socket(AF_INET, SOCK_DGRAM)):
 
 class RGB:
   def __init__(self, r: int = 0, g: int = 0, b: int = 0):
-    self.r = r
-    self.g = g
-    self.b = b
+    self.r = int(r)
+    self.g = int(g)
+    self.b = int(b)
+
+  def __mul__(self, other: float):
+    other = float(other)
+
+    return RGB (
+      self.r * other,
+      self.g * other,
+      self.b * other
+    )
+
+  def __rmul__(self, other: float):
+    return self.__mul__(other)
+
+  def __imul__(self, other: float):
+    other = float(other)
+
+    self.r = int(self.r * other)
+    self.g = int(self.g * other)
+    self.b = int(self.b * other)
+
+    return self
 
   def toBytes(self):
     return struct.pack('BBB', self.r, self.g, self.b)
 
+  def __repr__(self):
+    return f'RGB({self.r}, {self.g}, {self.b})'
 
 
-def sendPacket(startIdx: int = 0, pixel: list = [], header: bytes = b'fxCyanRGB8'):
+def sendPacket(startIdx: int = startIdx, pixel: list = [], header: bytes = b'fxCyanRGB8'):
   data = header + struct.pack('>H', startIdx)
 
   for px in pixel:
@@ -53,21 +81,35 @@ if __name__ == '__main__':
   green = RGB(  0, 255,   0)
   blue  = RGB(  0,   0, 255)
 
+  yellow = RGB(255, 255,   0)
+  cyan   = RGB(0,   255, 255)
+  violet = RGB(255,   0, 255)
+
   pixels = [
-    white,
-    red, green, blue,
-    white
+    red,
+    RGB(255, 80, 0),
+    yellow,
+    RGB(80, 255, 0),
+    green,
+    cyan,
+    blue,
+    violet
   ]
 
+  pixels = [ px * 0.2 for px in pixels ]
+
   FullHour = [
     red,
     green,
     blue
   ]
 
+  sendPacket(pixel=[black]*8)
+
   fh = 0
   while True:
-    sendPacket(pixel=pixels + [FullHour[fh]])
+    sendPacket(pixel=pixels[:2])
+    # sendPacket(pixel=pixels + [FullHour[fh]] + [black, black])
 
     fh += 1
     if fh >= len(FullHour): fh = 0
-- 
GitLab