Driver for the Seeedstudio RGB OLED module for the xadow M0

Files at this revision

API Documentation at this revision

Comitter:
messi1
Date:
Wed Nov 18 09:28:05 2015 +0000
Parent:
10:ef7440718431
Child:
12:8e27450eb391
Commit message:
Rewrite SGL class to template based class

Changed in this revision

include/SGL.h Show annotated file Show diff for this revision Revisions of this file
include/SSD1331.h Show annotated file Show diff for this revision Revisions of this file
src/SGL.cpp Show annotated file Show diff for this revision Revisions of this file
src/SSD1331.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/include/SGL.h	Tue Nov 17 21:20:37 2015 +0000
+++ b/include/SGL.h	Wed Nov 18 09:28:05 2015 +0000
@@ -33,45 +33,42 @@
 
 #include <stdint.h>
 
-#define FONT_SPACE 6
-#define FONT_X 8
-#define FONT_Y 8
-
 #define MIN(a,b) (((a)<(b))?(a):(b))
 #define MAX(a,b) (((a)>(b))?(a):(b))
 
 typedef const unsigned char** FontType;
 
+template <class T>
 class SGL {
  
 public:
-    SGL(uint8_t width, uint8_t height);
-    virtual void drawPixel(uint8_t x, uint8_t y, uint16_t color) = 0; // implemented by subclass
-    virtual void drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color);
-    virtual void drawVLine(uint8_t x, uint8_t y, uint8_t length,uint16_t color);
-    virtual void drawHLine(uint8_t x, uint8_t y, uint8_t length, uint16_t color);
-    virtual void drawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color);
-    virtual void fillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color);
-    virtual void drawCircle(uint8_t x, uint8_t y, uint8_t r, uint16_t color);
-    virtual void fillCircle(uint8_t x, uint8_t y, uint8_t r, uint16_t color);
-    virtual void drawTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color);
-    virtual void fillTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color);    
+    SGL(T width, T height);
+    virtual void drawPixel(T x, T y, uint16_t color) = 0; // implemented by subclass
+    virtual void drawLine(T x0, T y0, T x1, T y1, uint16_t color);
+    virtual void drawVLine(T x, T y, T length,uint16_t color);
+    virtual void drawHLine(T x, T y, T length, uint16_t color);
+    virtual void drawRect(T x, T y, T width, T height, uint16_t color);
+    virtual void fillRect(T x, T y, T width, T height, uint16_t color);
+    virtual void drawCircle(T x, T y, T r, uint16_t color);
+    virtual void fillCircle(T x, T y, T r, uint16_t color);
+    virtual void drawTraingle(T x0, T y0, T x1, T y1, T x2, T y2, uint16_t color);
+    virtual void fillTraingle(T x0, T y0, T x1, T y1, T x2, T y2, uint16_t color);    
     
     // The zoom factor works at the moment only with integer values. Float values will create bad fonts 
-    virtual void drawChar(uint8_t ascii, uint8_t x, uint8_t y, float zoom, uint16_t color);
-    virtual void drawString(const char *string, uint8_t x, uint8_t y, float zoom, uint16_t color, uint8_t fontSpace=6);
+    virtual void drawChar(uint8_t ascii, T x, T y, uint16_t color, float zoom=1);
+    virtual void drawString(const char *string, T x, T y, uint16_t color, float zoom=1, uint8_t fontSpace=6);
     
-    virtual void drawBitMap(uint8_t x, uint8_t y, const uint8_t *bitmap, uint8_t width, uint8_t height, uint16_t color);
+    virtual void drawBitMap(T x, T y, const uint8_t *bitmap, T width, T height, uint16_t color);
     virtual void fillScreen(uint16_t color);
-    virtual void setFont(FontType font, uint8_t width, uint8_t height, uint8_t start, uint8_t stop);
+    virtual void setFont(FontType font, uint8_t width, uint8_t height, uint8_t asciiStart, uint8_t asciiStop);
     
 private:
-    void swap(uint8_t* a, uint8_t* b){ 
-        uint8_t t = *a; *a = *b; *b = t; 
+    void swap(T* a, T* b){ 
+        T t = *a; *a = *b; *b = t; 
     };
     
-    uint8_t _width;
-    uint8_t _height;
+    T _width;
+    T _height;
     FontType _currentFont;
     uint8_t _fontWidth;
     uint8_t _fontHeight;
--- a/include/SSD1331.h	Tue Nov 17 21:20:37 2015 +0000
+++ b/include/SSD1331.h	Wed Nov 18 09:28:05 2015 +0000
@@ -63,7 +63,7 @@
 
 #include "DigitalOut.h"
 #include "PinNames.h"
-#include "SGL.h"
+#include "SGL.cpp" // Because of the template class we have to include this cpp
 #include "Stream.h"
 
 
@@ -188,7 +188,7 @@
  * }
  * @endcode
  */
-class SSD1331 : public mbed::Stream, public virtual SGL 
+class SSD1331 : public mbed::Stream, public virtual SGL<uint8_t>
 {
     public:
         /** Create a SPI master connected to the specified pins
--- a/src/SGL.cpp	Tue Nov 17 21:20:37 2015 +0000
+++ b/src/SGL.cpp	Wed Nov 18 09:28:05 2015 +0000
@@ -31,8 +31,12 @@
 #include "SGL.h"
 #include "SimpleFont.h"
 
+//template class SGL<uint8_t>;
+//template class SGL<uint16_t>;
+
 //---------------------------------------------------------------------------------------
-SGL::SGL(uint8_t width, uint8_t height):
+template <class T>
+SGL<T>::SGL(T width, T height):
     _width(width), _height(height),
     _currentFont(0),     
     _fontWidth(0),
@@ -42,13 +46,14 @@
 {}
 
 //---------------------------------------------------------------------------------------
-void SGL::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color)
+template <class T>
+void SGL<T>::drawLine(T x0, T y0, T x1, T y1, uint16_t color)
 {
-    uint8_t x   = x1-x0;
-    uint8_t y   = y1-y0;
-    uint8_t dx  = abs(x),  sx = x0<x1 ? 1 : -1;
-    uint8_t dy  = -abs(y), sy = y0<y1 ? 1 : -1;
-    uint8_t err = dx+dy, e2;
+    T x   = x1-x0;
+    T y   = y1-y0;
+    T dx  = abs(x),  sx = x0<x1 ? 1 : -1;
+    T dy  = -abs(y), sy = y0<y1 ? 1 : -1;
+    T err = dx+dy, e2;
     for (;;)
     {
         drawPixel(x0, y0,color);
@@ -65,27 +70,30 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawVLine(uint8_t x, uint8_t y, uint8_t length,uint16_t color)
+template <class T>
+void SGL<T>::drawVLine(T x, T y, T length,uint16_t color)
 {
-    uint8_t y1 = MIN(y+length,_height-1);
-    for(uint8_t i = y; i < y1; ++i)
+    T y1 = MIN(y+length,_height-1);
+    for(T i = y; i < y1; ++i)
     {
         drawPixel(x, i, color);
     }
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawHLine(uint8_t x, uint8_t y, uint8_t length, uint16_t color)
+template <class T>
+void SGL<T>::drawHLine(T x, T y, T length, uint16_t color)
 {
-    uint8_t x1 = MIN(x+length,_width-1);
-    for(uint8_t i = x; i < x1; ++i)
+    T x1 = MIN(x+length,_width-1);
+    for(T i = x; i < x1; ++i)
     {
         drawPixel(i, y, color);
     }
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color)
+template <class T>
+void SGL<T>::drawRect(T x, T y, T width, T height, uint16_t color)
 {
     drawHLine(x, y, width, color);
     drawHLine(x, y+height, width, color);
@@ -94,7 +102,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::fillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color)
+template <class T>
+void SGL<T>::fillRect(T x, T y, T width, T height, uint16_t color)
 {
     for(uint8_t i = 0; i < height; ++i)
     {
@@ -106,7 +115,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawCircle(uint8_t poX, uint8_t poY, uint8_t r, uint16_t color)
+template <class T>
+void SGL<T>::drawCircle(T poX, T poY, T r, uint16_t color)
 {
     int x = -r, y = 0, err = 2-2*r, e2;
     do
@@ -126,7 +136,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::fillCircle(uint8_t poX, uint8_t poY, uint8_t r, uint16_t color)
+template <class T>
+void SGL<T>::fillCircle(T poX, T poY, T r, uint16_t color)
 {
     int x = -r, y = 0, err = 2-2*r, e2;
     do
@@ -144,7 +155,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
+template <class T>
+void SGL<T>::drawTraingle(T x0, T y0, T x1, T y1, T x2, T y2, uint16_t color)
 {
     drawLine(x0, y0, x1, y1,color);
     drawLine(x1, y1, x2, y2,color);
@@ -152,9 +164,10 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::fillTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
+template <class T>
+void SGL<T>::fillTraingle(T x0, T y0, T x1, T y1, T x2, T y2, uint16_t color)
 {
-    uint8_t a, b, y, last;
+    T a, b, y, last;
 
     if(y0 > y1){ swap(&y0, &y1); swap(&x0, &x1); }
     if(y1 > y2){ swap(&y2, &y1); swap(&x2, &x1); }
@@ -201,7 +214,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawChar(uint8_t ascii, uint8_t x, uint8_t y, float zoom, uint16_t color)
+template <class T>
+void SGL<T>::drawChar(uint8_t ascii, T x, T y, uint16_t color, float zoom)
 {
     if(!_currentFont && !*_currentFont)
         return;
@@ -255,7 +269,8 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawString(const char *string, uint8_t x, uint8_t y, float zoom, uint16_t color, uint8_t fontSpace)
+template <class T>
+void SGL<T>::drawString(const char *string, T x, T y, uint16_t color, float zoom, uint8_t fontSpace)
 {
     if(!_currentFont && !*_currentFont)
         return;
@@ -274,13 +289,14 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::drawBitMap(uint8_t x, uint8_t y, const uint8_t *bitmap, uint8_t width, uint8_t height, uint16_t color)
+template <class T>
+void SGL<T>::drawBitMap(T x, T y, const uint8_t *bitmap, T width, T height, uint16_t color)
 {
-    uint8_t byteWidth = (width + 7) / 8;
+    T byteWidth = (width + 7) / 8;
     
-    for(uint8_t j = 0; j < height; ++j)
+    for(T j = 0; j < height; ++j)
     {
-        for(uint8_t i = 0; i < width; ++i ) 
+        for(T i = 0; i < width; ++i ) 
         {
             if( *(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7)) ) 
             {
@@ -291,13 +307,15 @@
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::fillScreen(uint16_t color)
+template <class T>
+void SGL<T>::fillScreen(uint16_t color)
 {
     fillRect(0, 0, _width, _height, color);
 }
 
 //---------------------------------------------------------------------------------------
-void SGL::setFont(FontType font, uint8_t width, uint8_t height, uint8_t start, uint8_t stop)
+template <class T>
+void SGL<T>::setFont(FontType font, uint8_t width, uint8_t height, uint8_t asciiStart, uint8_t asciiStop)
 {
     if(!font && !*font)
         return;
@@ -305,6 +323,6 @@
     _currentFont= font;
     _fontWidth  = width;
     _fontHeight = height;
-    _fontStart  = start;
-    _fontStop   = stop;    
+    _fontStart  = asciiStart;
+    _fontStop   = asciiStop;    
 }
\ No newline at end of file
--- a/src/SSD1331.cpp	Tue Nov 17 21:20:37 2015 +0000
+++ b/src/SSD1331.cpp	Wed Nov 18 09:28:05 2015 +0000
@@ -62,7 +62,7 @@
 #include "wait_api.h"
 
 SSD1331::SSD1331(PinName cs, PinName rst, PinName dc, PinName mosi, PinName miso, PinName sclk)
-    :SGL(RGB_OLED_WIDTH, RGB_OLED_HEIGHT), 
+    :SGL<uint8_t>(RGB_OLED_WIDTH, RGB_OLED_HEIGHT), 
     _cs(cs), 
     _dc(dc),
     _spiPort(mosi, miso, sclk)