Forked LEDMatrix and added horizontal scrolling

Fork of LEDMatrix by Yihui Xiong

Revision:
3:1e06e89bc0c9
Parent:
2:cd2da920cf98
Child:
4:40d4afefcd74
--- a/LEDMatrix.cpp	Thu Jan 14 12:57:59 2016 +0000
+++ b/LEDMatrix.cpp	Thu Jan 14 16:52:04 2016 +0000
@@ -53,12 +53,18 @@
     for (int i = 0; i < LED_MATRIX_MAX_LINES; i++)
     {
         _lineScrollInc[i] = 0;
+        _lineAlert[i] = false;
+        _lineInvert[i] = false;
     }
     _isBusy = false;
     _charSeparation = 1;
+    _flashCounter = 0;
+    _flashRate = 8;
+    _flashState = false;
 }
 
-void LEDMatrix::begin(uint8_t *_pDisplayBuf, uint16_t width, uint16_t height, uint16_t dispBufWidth, uint16_t numLines, int charSeparation)
+void LEDMatrix::begin(uint8_t *_pDisplayBuf, uint16_t width, uint16_t height, uint16_t dispBufWidth, 
+                uint16_t numLines, int charSeparation, int flashRate)
 {
     ASSERT(0 == (width % LED_MATRIX_LEDS_HORIZONTALLY));
     ASSERT(0 == (scroll_width % LED_MATRIX_LEDS_HORIZONTALLY));
@@ -70,6 +76,7 @@
     this->dispBufWidth = dispBufWidth;
     this->numLines = numLines;
     this->_charSeparation = charSeparation;
+    this->_flashRate = flashRate;
     if (numLines > LED_MATRIX_MAX_LINES)
         this->numLines = LED_MATRIX_MAX_LINES;
     for (int i = 0; i < LED_MATRIX_LEDS_VERTICALLY; i++)
@@ -167,6 +174,17 @@
         // Calculate buffer position within this unit
         uint8_t *pBuf = pBufBase + rowIdx * (dispBufWidth / 8) * LED_MATRIX_LEDS_VERTICALLY;
 
+        // Work out which display line we're in
+        uint8_t dispMask = mask;
+        bool invertRow = false;
+        int rowsInLine = (height/numLines);
+        int dispLine = scanRowIdx / rowsInLine;
+        if (dispLine < numLines)
+        {
+            if (_lineInvert[dispLine])
+                dispMask = ~dispMask;
+        }
+                
         // Work along the display row sending out data as we go
         // Noting that the first data we send out will be shunted to the end of the row
         int hScrollPos = _hScrollPos[scanRowIdx];
@@ -182,7 +200,7 @@
             uint8_t pixels = ((*pByte1) << bitOffset) + (((*pByte2) >> (8 - bitOffset)) % 256);
 
             // Reverse the bits so they come out in the right order                        
-            pixels = reverseBits(pixels) ^ mask;   // reverse: mask = 0xff, normal: mask =0x00 
+            pixels = reverseBits(pixels) ^ dispMask;   // reverse: mask = 0xff, normal: mask =0x00 
             for (uint8_t bit = 0; bit < 8; bit++) 
             {
                 clk = 0;
@@ -319,27 +337,54 @@
     {
         _hScrollPos[i] = 0;
         _hScrollWidth[i] = scrollWidth;
-        _lineScrollInc[i] = scrollInc;
     }
-//    int rowsInALine = height / numLines;
-//    for (int i = 0; i < rowsInALine; i++)
-//        _hScrollWidth[lineIdx*rowsInALine + i] = scrollWidth;
+    // Store the scroll increment
+    int startLine = 0;
+    int linesToProcess = numLines;
+    if (lineIdx != -1)
+    {
+        if ((lineIdx < 0) || (lineIdx >= numLines))
+            return;
+        startLine = lineIdx;
+        linesToProcess = 1;
+    }
+    for (int i = 0; i < linesToProcess; i++)
+        _lineScrollInc[i+startLine] = scrollInc;
 }
 
-//void LEDMatrix::setLineScrollInc(int lineIdx, int scrollInc)
-//{
-//    if ((lineIdx < 0) || (lineIdx >= numLines))
-//        return;
-//    int rowsInALine = height / numLines;
-//    for (int i = 0; i < rowsInALine; i++)
-//        _lineScrollInc[lineIdx*rowsInALine + i] = scrollInc;
-//}
-
+void LEDMatrix::setAlert(int lineIdx, bool alertOn)
+{
+    // Store the alert status
+    int startLine = 0;
+    int linesToProcess = numLines;
+    if (lineIdx != -1)
+    {
+        if ((lineIdx < 0) || (lineIdx >= numLines))
+            return;
+        startLine = lineIdx;
+        linesToProcess = 1;
+    }
+    for (int i = 0; i < linesToProcess; i++)
+        _lineAlert[i+startLine] = alertOn;
+}
+    
 void LEDMatrix::serviceEffects()
 {
-    for (int i = 0; i < LED_MATRIX_MAX_LINES; i++)
+    // Scroll / flash logic
+    int rowsInLine = (height / numLines);
+    for (int i = 0; i < numLines; i++)
     {
-        _hScrollPos[i] += _lineScrollInc[i];
+        for (int j = 0; j < rowsInLine; j++)
+            _hScrollPos[i*rowsInLine+j] += _lineScrollInc[i];
+        _lineInvert[i] = _lineAlert[i] ? _flashState : false;
+    }
+    
+    // Update flash state
+    _flashCounter++;
+    if (_flashCounter >= _flashRate)
+    {
+        _flashCounter = 0;
+        _flashState = !_flashState;
     }
 }