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

Dependents:   2PA2S 2PA2S_v2

Revision:
4:35757c8b7625
Parent:
3:bb6fba3e84ff
Child:
5:4f7cdc2ee49a
--- a/ssd1306.h	Wed Nov 01 11:04:20 2017 +0000
+++ b/ssd1306.h	Wed Nov 01 16:19:50 2017 +0000
@@ -1,5 +1,5 @@
 /*
-* ssd1306.h
+* ssd1306.cpp
 *
 *  Created on: 20 oct. 2017
 *  Author: Miguel Angel Rodriguez Jodar
@@ -14,10 +14,10 @@
 
 #include "mbed.h"
 
-#define IS_COMMAND         0x00
-#define IS_DATA            0x40
-#define IS_NOT_LAST        0x80
-#define IS_LAST            0x00
+#define SSD1306_IS_COMMAND         0x00
+#define SSD1306_IS_DATA            0x40
+#define SSD1306_IS_NOT_LAST        0x80
+#define SSD1306_IS_LAST            0x00
 
 #define SSD1306_SETCONTRAST 0x81
 #define SSD1306_DISPLAYALLON_RESUME 0xA4
@@ -63,8 +63,7 @@
 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
 
-class SSD1306 : private NonCopyable<SSD1306> {
-    
+class SSD1306 {
 public:
     enum PlotStyle
     {
@@ -75,7 +74,7 @@
     
     enum I2CSpeed
     {
-        Low,
+        Slow,
         Medium,
         Fast
     };
@@ -83,6 +82,7 @@
     /** Creates an instance of a SSD1306 driver, specifying I2C pins to use
     *
     *  @param sda I2C data line pin
+    *
     *  @param scl I2C clock line pin
     */
     SSD1306 (PinName sda, PinName scl, char ssd1306_addr = 0x78);
@@ -90,6 +90,7 @@
     /** Creates an instance of a SSD1306 driver using an existing I2C object
     *
     *  @param busi2c I2C object
+    *
     *  @param ssd1306_addr I2C addr of SSD1306 controller
     */
     SSD1306 (I2C &busi2c, char ssd1306_addr = 0x78);
@@ -100,31 +101,115 @@
     */
     void speed (I2CSpeed spd);
     
+    /** Initialize the SSD1306 controller
+    */
     int init (void);
     
+    /** Scrolls up one text line
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
     void scroll (bool refresh=false);
     
+    /** Print a character at the current text position. Updates the text position and forces a scroll if at the end of the screen
+    *
+    *  @param c ASCII code of the character to print (8-bit ASCII code, use original IBM code page 437. No control codes.
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
     void putchar (char c, bool refresh=false);
     
+    /** Prints a NUL terminated string
+    *
+    *  @param s C-style string (NUL terminated) to print.
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
     void puts (char *s, bool refresh=false);
         
-    void printf (char *fmt,...);
+    /** printf interface to SSD1306 controller
+    *
+    *  @param fmt Format string.
+    */
+    void printf (const char *fmt,...);
     
+    /** Change the text position
+    *
+    *  @param row Text row (0-7) to print the next character
+    *
+    *  @param column Text column (0-15) to print the next character
+    */
     void locate (char row, char column);
     
+    /** Redraw the physical display, sending the content of the display memory to the SSD1306 controller using the I2C bus
+    */
     void display (void);
     
+    /** Changes the contrast (actually, pixel brightness) of the screen
+    *
+    *  @param v Contrast level (0-255)
+    */
     void set_contrast (char v);
     
+    /** Clear screen and optionally, fills it with a predefined picture (in 128x64 OLED format, see display datasheet). Also resets the text position.
+    *
+    *  @param bkground If not NULL, should point to a 1024 byte buffer with the image to load to the display
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
     void cls (char *bkground=NULL, bool refresh=false);
     
-    void plot (char x, char y, PlotStyle modo, bool refresh=false);
+    /** Plots a pixel.
+    *
+    *  @param x x-coordinate (0-127) of the pixel. X coordinates go left to right.
+    *
+    *  @param y y-coordinate (0-63) of the pixel. Y coordinates go up to down.
+    *
+    *  @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
+    void plot (char x, char y, PlotStyle mode, bool refresh=false);
     
+    /** Returns the state of a pixel coordinate from screen
+    *
+    *  @param x x-coordinate (0-127) of the pixel. X coordinates go left to right.
+    *
+    *  @param y y-coordinate (0-63) of the pixel. Y coordinates go up to down.
+    *
+    *  @return true if the pixel was set, or false otherwise
+    */
     bool point (char x, char y);
     
-    void line (char x0, char y0, char x1, char y1, PlotStyle modo, bool refresh=false);
+    /** Draws a line using the Bresenham algorithm
+    *
+    *  @param x0 x-coordinate (0-127) of the starting point
+    *
+    *  @param y0 y-coordinate (0-63) of the starting point
+    *
+    *  @param x1 x-coordinate (0-127) of the ending point
+    *
+    *  @param y1 y-coordinate (0-63) of the ending point
+    *
+    *  @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
+    void line (char x0, char y0, char x1, char y1, PlotStyle mode, bool refresh=false);
     
-    void circle (char x0, char y0, char r, PlotStyle modo, bool refresh=false);
+    /** Draws a circle
+    *
+    *  @param x0 x-coordinate (0-127) of the center point
+    *
+    *  @param y0 y-coordinate (0-63) of the center point
+    *
+    *  @param r radius of the circle
+    *
+    *  @param mode. Plot style mode: Normal (pixel is drawn), Inverse (pixel is erased), or Xor (pixel is erased if background position is already set, otherwise is drawn)
+    *
+    *  @param refresh (optional) Force an actual screen redraw after scrolling
+    */
+    void circle (char x0, char y0, char r, PlotStyle mode, bool refresh=false);
     
     virtual ~SSD1306 () {
         if (!do_not_delete_bus)
@@ -133,16 +218,15 @@
     }
     
 protected:
-    I2C *bus;
-    char ssd1306_i2c_addr;
-    char *fb;
-    char do_not_delete_bus;
-    int idxfb;
-    int command (char c);
-    int data (char d);
-    int command_data (char c, char c_or_d, char lastitem);
-    char scan ();
-    
+    I2C *bus;               // I2C object
+    char ssd1306_i2c_addr;  // I2C address of SSD1306 controller
+    char *fb;               // pointer to display buffer (1024 bytes)
+    char do_not_delete_bus; // flag to prevent deletion of bus when destroying SSD1306 object
+    int idxfb;              // Current text position (referred to screen address memory)
+    int command (char c);   // Sends a I2C command to SSD1306
+    int data (char d);      // Sends I2C data to SSD1306
+    int command_data (char c, char c_or_d, char lastitem);  // Sends a command or data and signal if it is not the last command/data in a list
+    char scan ();           // Scans the I2C bus searcing for I2C id's 0x78 or 0x7A. Currently not used    
 };
 
 #endif