Forked LEDMatrix and added horizontal scrolling

Fork of LEDMatrix by Yihui Xiong

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Thu Jan 14 16:52:04 2016 +0000
Parent:
2:cd2da920cf98
Child:
4:40d4afefcd74
Commit message:
Added flashing effect

Changed in this revision

LEDMatrix.cpp Show annotated file Show diff for this revision Revisions of this file
LEDMatrix.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
     }
 }
     
--- a/LEDMatrix.h	Thu Jan 14 12:57:59 2016 +0000
+++ b/LEDMatrix.h	Thu Jan 14 16:52:04 2016 +0000
@@ -26,7 +26,7 @@
 
 const int LED_MATRIX_LEDS_HORIZONTALLY = 32;
 const int LED_MATRIX_LEDS_VERTICALLY = 16;
-const int LED_MATRIX_MAX_LINES = 16;
+const int LED_MATRIX_MAX_LINES = 2;
 
 class LEDMatrix 
 {
@@ -39,7 +39,8 @@
      * @param pDisplayBuf    display buffer
      * @param number        panels' number
      */
-    void begin(uint8_t *pDisplayBuf, uint16_t width, uint16_t height, uint16_t scrollWidth, uint16_t numLines, int charSeparation = 1);
+    void begin(uint8_t *pDisplayBuf, uint16_t width, uint16_t height, uint16_t scrollWidth, 
+        uint16_t numLines, int charSeparation = 1, int flashRate = 8);
 
     /**
      * draw a point - origin is like a graph with 0,0 at the lower-left corner
@@ -90,6 +91,8 @@
     int displayChar(int xPos, int yPos, char ch);
     int displayLargeDigit(int curX, int curY, char ch);
     int displayLine(int lineIdx, const char* line);
+    
+    void setAlert(int lineIdx, bool alertOn);
 
     // Called frequently and regularly to handle effects like scrolling
     void serviceEffects();
@@ -109,6 +112,11 @@
     bool _isBusy;
     int _charSeparation;
     int _lineScrollInc[LED_MATRIX_MAX_LINES];
+    bool _lineAlert[LED_MATRIX_MAX_LINES];
+    bool _lineInvert[LED_MATRIX_MAX_LINES];
+    int _flashCounter;
+    int _flashRate;
+    bool _flashState;
 };
 
 #endif