Forked LEDMatrix and added horizontal scrolling

Fork of LEDMatrix by Yihui Xiong

Revision:
1:79cf2e115449
Parent:
0:13728deac7a7
Child:
2:cd2da920cf98
diff -r 13728deac7a7 -r 79cf2e115449 LEDMatrix.cpp
--- a/LEDMatrix.cpp	Fri Nov 08 06:45:52 2013 +0000
+++ b/LEDMatrix.cpp	Mon Jan 11 21:05:53 2016 +0000
@@ -41,6 +41,7 @@
 
     mask = 0xff;
     state = 0;
+    _horizontalScrollPos = 0;
 }
 
 void LEDMatrix::begin(uint8_t *displaybuf, uint16_t width, uint16_t height)
@@ -60,8 +61,8 @@
     ASSERT(width > x);
     ASSERT(height > y);
 
-    uint8_t *byte = displaybuf + x * 8 + y / 8;
-    uint8_t  bit = y % 8;
+    uint8_t *byte = displaybuf + (height - 1 - y) * width / 8 + (width - 1 - x) / 8;
+    uint8_t  bit = (width - 1 - x) % 8;
 
     if (pixel) {
         *byte |= 0x80 >> bit;
@@ -117,22 +118,32 @@
 {
     static uint8_t row = 0;
 
-    if (!state) {
+    if (!state) 
+    {
         return;
     }
 
     uint8_t *head = displaybuf + row * (width / 8);
-    for (uint8_t line = 0; line < (height / 16); line++) {
+    for (uint8_t line = 0; line < (height / 16); line++) 
+    {
         uint8_t *ptr = head;
         head += line * width * 2;
 
-        for (uint8_t byte = 0; byte < (width / 8); byte++) {
-            uint8_t pixels = *ptr;
-            ptr++;
+        for (uint8_t byteIdx = 0; byteIdx < (width / 8); byteIdx++)
+        {
+            uint8_t* pByte1 = ptr + (((byteIdx*8 + width - _horizontalScrollPos + 7) % width) / 8);
+            uint8_t* pByte2 = ptr + (((byteIdx*8 + width - _horizontalScrollPos - 1) % width) / 8);
+            int bitOffset = _horizontalScrollPos % 8;
+            uint8_t pixels = ((*pByte1) >> bitOffset) + (((*pByte2) << (8 - bitOffset)) % 256);
+            
+//            uint8_t pixels = *ptr;
+//            ptr++;
             pixels = pixels ^ mask;   // reverse: mask = 0xff, normal: mask =0x00 
-            for (uint8_t bit = 0; bit < 8; bit++) {
+            for (uint8_t bit = 0; bit < 8; bit++) 
+            {
                 clk = 0;
                 r1 = pixels & (0x80 >> bit);
+//                wait_us(1);
                 clk = 1;
             }
         }
@@ -148,6 +159,7 @@
 
     // latch data
     stb = 0;
+//    wait_us(1);
     stb = 1;
 
     oe = 0;              // enable display
@@ -166,3 +178,21 @@
     oe = 1;
 }
 
+void LEDMatrix::scrollReset()
+{
+    _horizontalScrollPos = 0;
+}
+
+void LEDMatrix::scrollLeft()
+{
+    _horizontalScrollPos++;
+    if (_horizontalScrollPos >= width)
+        _horizontalScrollPos = 0;
+}
+
+void LEDMatrix::scrollToPos(int pos)
+{
+    if ((pos <= 0) || (pos >= width))
+        return;
+    _horizontalScrollPos = pos;
+}