A class for managing SSD1306 controlled LCD´s (cheap 128x64 models, 0.96'') with more scroll features

Dependents:   2PA2S 2PA2S_v2

Revision:
1:c5cf4ca5939f
Parent:
0:3d84b3bfb794
Child:
2:7f1160c1a741
--- a/ssd1306.cpp	Wed Oct 25 16:28:07 2017 +0200
+++ b/ssd1306.cpp	Wed Oct 25 15:56:50 2017 +0000
@@ -243,9 +243,9 @@
 {
     switch (spd)
     {
-    case I2CSpeed::Low:    bus->frequency(100000); break;
-    case I2CSpeed::Medium: bus->frequency(400000); break;
-    case I2CSpeed::Fast:   bus->frequency(1000000); break;
+    case Low:    bus->frequency(100000); break;
+    case Medium: bus->frequency(400000); break;
+    case Fast:   bus->frequency(1000000); break;
     }
 }
 
@@ -296,7 +296,7 @@
     return bus->write (ssd1306_i2c_addr, comando, sizeof comando);
 }
 
-void SSD1306::scroll (void)
+void SSD1306::scroll (bool refresh)
 {
     int i;
     
@@ -304,9 +304,11 @@
         fb[i-128] = fb[i];
     for (i=896; i<1024; i++)
         fb[i] = 0;
+    if (refresh)
+       display();
 }
 
-void SSD1306::putchar (char c)
+void SSD1306::putchar (char c, bool refresh)
 {
     int idx,i;
     
@@ -315,10 +317,12 @@
         fb[idxfb] = charset[idx+i];
         idxfb++;
         if (idxfb == 1024) {
-            scroll();
+            scroll(refresh);
             idxfb = 896;
         }
     }
+    if (refresh)
+       display();
 }
 
 void SSD1306::display(void)
@@ -345,25 +349,54 @@
     command (v);
 }
 
-void SSD1306::cls (void)
+void SSD1306::cls (char *bkground, bool refresh)
 {
     int i;
-    
-    for (i=0; i<1024; i++) {
-        fb[i] = 0;
+
+    if (!bkground)
+    {
+        for (i=0; i<1024; i++)
+            fb[i] = 0;
+    }
+    else
+    {
+        for (i=0; i<1024; i++)
+            fb[i] = bkground[i];
     }
     idxfb = 0;
+    if (refresh)
+       display();
 }
 
-void SSD1306::plot (char x, char y, PlotStyle modo)
+void SSD1306::plot (char x, char y, PlotStyle modo, bool refresh)
 {
     x = x % 128;
     y = y % 64;
     
     switch (modo)
     {
-    case PlotStyle::Normal:  fb[(y/8)*128+x] |= (1<<(y%8)); break;
-    case PlotStyle::Inverse: fb[(y/8)*128+x] &= ~(1<<(y%8)); break;
-    case PlotStyle::Xor:     fb[(y/8)*128+x] ^= (1<<(y%8)); break;
+    case Normal:  fb[(y/8)*128+x] |= (1<<(y%8)); break;
+    case Inverse: fb[(y/8)*128+x] &= ~(1<<(y%8)); break;
+    case Xor:     fb[(y/8)*128+x] ^= (1<<(y%8)); break;
     }
+    if (refresh)
+       display();
 }
+
+void SSD1306::line (char x0, char y0, char x1, char y1, PlotStyle modo, bool refresh)
+{
+    int dx =  abs (x1 - x0), sx = x0 < x1 ? 1 : -1;
+    int dy = -abs (y1 - y0), sy = y0 < y1 ? 1 : -1;
+    int err = dx + dy, e2; /* error value e_xy */
+
+    for (;;)
+    {  /* loop */
+        plot (x0, y0, modo, false);
+        if (x0 == x1 && y0 == y1) break;
+        e2 = 2 * err;
+        if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
+        if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
+    }
+    if (refresh)
+        display();
+}