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

Dependents:   mbed_ssd1306 USB_meter_SD_file_number_filtro_for EscanerRf escaner_RTOS ... more

Revision:
2:7f1160c1a741
Parent:
1:c5cf4ca5939f
Child:
3:bb6fba3e84ff
--- a/ssd1306.cpp	Wed Oct 25 15:56:50 2017 +0000
+++ b/ssd1306.cpp	Thu Oct 26 16:07:21 2017 +0000
@@ -214,16 +214,14 @@
 {
     idxfb = 0;
     fb = new char[1024];
-    if (!fb)
-    {
+    if (!fb) {
         printf ("SSD1306: Framebuffer allocation failed!\r\n");
         for (;;) { }
     }
-    
+
     bus = new I2C (sda, scl);
     ssd1306_i2c_addr = scan();
-    if (ssd1306_i2c_addr != 0x78 && ssd1306_i2c_addr != 0x7A)
-    {
+    if (ssd1306_i2c_addr != 0x78 && ssd1306_i2c_addr != 0x7A) {
         printf ("SSD1306: slave not found!. ");
         if (ssd1306_i2c_addr != 0)
             printf ("Found I2C slave at 0x%02.2X\r\n", ssd1306_i2c_addr);
@@ -235,17 +233,22 @@
     printf ("SSD1306 debug: fb = 0x%08.8X\r\n", fb);
     printf ("SSD1306 debug: I2C addr = 0x%02.2X\r\n", ssd1306_i2c_addr);
 #endif
-    
-    
+
+
 }
 
 void SSD1306::speed (I2CSpeed spd)
 {
-    switch (spd)
-    {
-    case Low:    bus->frequency(100000); break;
-    case Medium: bus->frequency(400000); break;
-    case Fast:   bus->frequency(1000000); break;
+    switch (spd) {
+        case Low:
+            bus->frequency(100000);
+            break;
+        case Medium:
+            bus->frequency(400000);
+            break;
+        case Fast:
+            bus->frequency(1000000);
+            break;
     }
 }
 
@@ -254,7 +257,7 @@
     int addr;
     int res;
     char i2caddr = 0;
-    
+
     for (addr=0; addr<256; addr++) {
         res = bus->write (addr, NULL, 0);
         if (res == 0)
@@ -266,14 +269,17 @@
 int SSD1306::command_data (char c, char c_or_d, char lastitem)
 {
     int res;
-    
+
     bus->start();
-    res = bus->write(ssd1306_i2c_addr);   if (!res) goto terminate_transaction;
-    res = bus->write(c_or_d | lastitem);  if (!res) goto terminate_transaction;
-    res = bus->write(c);                  if (!res) goto terminate_transaction;
-    
-    terminate_transaction:
-        bus->stop();
+    res = bus->write(ssd1306_i2c_addr);
+    if (!res) goto terminate_transaction;
+    res = bus->write(c_or_d | lastitem);
+    if (!res) goto terminate_transaction;
+    res = bus->write(c);
+    if (!res) goto terminate_transaction;
+
+terminate_transaction:
+    bus->stop();
     return res;
 }
 
@@ -290,50 +296,60 @@
 int SSD1306::init (void)
 {
     static char comando[] = {0x80, SSD1306_DISPLAYOFF, 0x80, SSD1306_CHARGEPUMP, 0x80, 0x14, 0x80, SSD1306_MEMORYMODE, 0x80, 0x00, 0x80, SSD1306_SEGREMAP | 0x1,
-        0x80, SSD1306_COMSCANDEC, 0x80, SSD1306_SETCONTRAST, 0x80, 0x7F, 0x00, SSD1306_DISPLAYON
-    };
-    
+                             0x80, SSD1306_COMSCANDEC, 0x80, SSD1306_SETCONTRAST, 0x80, 0x7F, 0x00, SSD1306_DISPLAYON
+                            };
+
     return bus->write (ssd1306_i2c_addr, comando, sizeof comando);
 }
 
 void SSD1306::scroll (bool refresh)
 {
     int i;
-    
+
     for (i=128; i<1024; i++)
         fb[i-128] = fb[i];
     for (i=896; i<1024; i++)
         fb[i] = 0;
     if (refresh)
-       display();
+        display();
+}
+
+void SSD1306::locate (char row, char column)
+{
+    idxfb = row*128+column*8;
 }
 
 void SSD1306::putchar (char c, bool refresh)
 {
     int idx,i;
-    
+
     idx = c*8;
+    if (idxfb == 1024) {
+        scroll(refresh);
+        idxfb = 896;
+    }
     for (i=0; i<8; i++) {
         fb[idxfb] = charset[idx+i];
         idxfb++;
-        if (idxfb == 1024) {
-            scroll(refresh);
-            idxfb = 896;
-        }
     }
     if (refresh)
-       display();
+        display();
+}
+
+void SSD1306::puts (char *s, bool refresh)
+{
+    while (*s) putchar (*s++, refresh);
 }
 
 void SSD1306::display(void)
 {
     int i;
-    
+
     command (0xb0);
     command (SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
     command (SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
     command (SSD1306_SETSTARTLINE | 0x0); // line #0
-    
+
     bus->start();
     bus->write (ssd1306_i2c_addr);
     bus->write (0x40);
@@ -353,34 +369,36 @@
 {
     int i;
 
-    if (!bkground)
-    {
+    if (!bkground) {
         for (i=0; i<1024; i++)
             fb[i] = 0;
-    }
-    else
-    {
+    } else {
         for (i=0; i<1024; i++)
             fb[i] = bkground[i];
     }
     idxfb = 0;
     if (refresh)
-       display();
+        display();
 }
 
 void SSD1306::plot (char x, char y, PlotStyle modo, bool refresh)
 {
     x = x % 128;
     y = y % 64;
-    
-    switch (modo)
-    {
-    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;
+
+    switch (modo) {
+        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();
+        display();
 }
 
 void SSD1306::line (char x0, char y0, char x1, char y1, PlotStyle modo, bool refresh)
@@ -389,13 +407,19 @@
     int dy = -abs (y1 - y0), sy = y0 < y1 ? 1 : -1;
     int err = dx + dy, e2; /* error value e_xy */
 
-    for (;;)
-    {  /* loop */
+    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 (e2 >= dy) {
+            err += dy;    /* e_xy+e_x > 0 */
+            x0 += sx;
+        }
+        if (e2 <= dx) {
+            err += dx;    /* e_xy+e_y < 0 */
+            y0 += sy;
+        }
     }
     if (refresh)
         display();