Driver for the Seeedstudio RGB OLED module for the xadow M0

Revision:
5:9de43186f5ea
Parent:
4:1707ca53e7d5
Child:
6:06c211302c93
--- a/src/SSD1331.cpp	Thu Nov 12 23:53:58 2015 +0000
+++ b/src/SSD1331.cpp	Fri Nov 13 23:29:59 2015 +0000
@@ -59,6 +59,7 @@
 #include "DigitalOut.h"
 #include "SPI.h"
 #include "SSD1331.h"
+#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), _cs(cs), _dc(dc), _spiPort(mosi, miso, sclk)
@@ -70,6 +71,201 @@
 };
 
 //------------------------------------------------------------------------------------
+void SSD1331::init(void)
+{
+    // initialize sequence
+    sendCmd(CMD_DISPLAY_OFF);    //OLED display OFF
+    
+    //Row Address
+    sendCmd(CMD_SET_ROW_ADDRESS,    0x00, 0x3f); //Set Row Address with start=0,end=63
+    sendCmd(CMD_SET_COLUMN_ADDRESS, 0x00, 0x5f); //Set Column Address with start=0,end=95
+    
+     //Contrast
+    sendCmd(CMD_SET_REMAP,               0x76); //Set remap & data format 0111 0000 // 65k Color/8bit buswidth/format1
+    sendCmd(CMD_SET_DISPLAY_START_LINE,  0x00); //Set display start row RAM
+    sendCmd(CMD_SET_DISPLAY_OFFSET,      0x00); //Set dispaly offset
+    sendCmd(CMD_NORMAL_DISPLAY);                //Set Display Mode
+    sendCmd(CMD_SET_MULTIPLEX_RATIO,     0x3f); //Set Multiplex Ratio
+    sendCmd(CMD_SET_MASTER_CONFIGURE,    0x8f); //Set Master Configuration (External VCC Supply Selected)
+    sendCmd(CMD_POWER_SAVE_MODE,         0x1a); //Set Power Saving Mode
+    sendCmd(CMD_PHASE_PERIOD_ADJUSTMENT, 0x74); //Set Phase 1 & 2 Period Adjustment
+    sendCmd(CMD_DISPLAY_CLOCK_DIV,       0xd0); //Set Display Clock Divide Ratio / Oscillator Frequency
+    sendCmd(CMD_SET_PRECHARGE_SPEED_A,   0x81); //Set Second Pre-charge Speed of Color A
+    sendCmd(CMD_SET_PRECHARGE_SPEED_B,   0x82); //Set Second Pre-charge Speed of Color B
+    sendCmd(CMD_SET_PRECHARGE_SPEED_C,   0x83); //Set Second Pre-charge Speed of Color C
+    sendCmd(CMD_SET_PRECHARGE_VOLTAGE,   0x3e); //Set Pre-charge Level
+    sendCmd(CMD_SET_V_VOLTAGE,           0x3e); //Set VCOMH
+    sendCmd(CMD_MASTER_CURRENT_CONTROL,  0x0f); //Set Master Current Control
+    sendCmd(CMD_SET_CONTRAST_A,          0x80); //Set Contrast Control for Color gAh
+    sendCmd(CMD_SET_CONTRAST_B,          0x80); //Set Contrast Control for Color gBh
+    sendCmd(CMD_SET_CONTRAST_C,          0x80); //Set Contrast Control for Color gCh 
+    
+    clearArea(0,0, RGB_OLED_XMAX, RGB_OLED_YMAX);
+    sendCmd(CMD_NORMAL_BRIGHTNESS_DISPLAY_ON);    //display ON
+
+//    sendCmd(CMD_SET_CONTRAST_A);       //Set contrast for color A
+//    sendCmd(0x91);                     //145
+//    sendCmd(CMD_SET_CONTRAST_B);       //Set contrast for color B
+//    sendCmd(0x50);                     //80
+//    sendCmd(CMD_SET_CONTRAST_C);       //Set contrast for color C
+//    sendCmd(0x7D);                     //125
+//    sendCmd(CMD_MASTER_CURRENT_CONTROL);//master current control
+//    sendCmd(0x06);                     //6
+//    sendCmd(CMD_SET_PRECHARGE_SPEED_A);//Set Second Pre-change Speed For ColorA
+//    sendCmd(0x64);                     //100
+//    sendCmd(CMD_SET_PRECHARGE_SPEED_B);//Set Second Pre-change Speed For ColorB
+//    sendCmd(0x78);                     //120
+//    sendCmd(CMD_SET_PRECHARGE_SPEED_C);//Set Second Pre-change Speed For ColorC
+//    sendCmd(0x64);                     //100
+//    sendCmd(CMD_SET_REMAP);            //set remap & data format
+//    sendCmd(0x72);                     //0x72              
+//    sendCmd(CMD_SET_MULTIPLEX_RATIO);  //Set multiplex ratio
+//    sendCmd(0x3F);
+//    sendCmd(CMD_SET_MASTER_CONFIGURE); //Set master configuration
+//    sendCmd(0x8E);
+//    sendCmd(CMD_PHASE_PERIOD_ADJUSTMENT);//phase 1 and 2 period adjustment
+//    sendCmd(0x31);                     //0x31
+//    sendCmd(CMD_DISPLAY_CLOCK_DIV);    //display clock divider/oscillator frequency
+//    sendCmd(0xF0);
+//    sendCmd(CMD_SET_PRECHARGE_VOLTAGE);//Set Pre-Change Level
+//    sendCmd(0x3A);
+//    sendCmd(CMD_SET_V_VOLTAGE);        //Set vcomH
+//    sendCmd(0x3E);
+//    sendCmd(CMD_DEACTIVE_SCROLLING);   //disable scrolling
+
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::drawPixel(uint8_t x, uint8_t y, uint16_t color)
+{
+    if (x >= RGB_OLED_WIDTH)  x = RGB_OLED_WIDTH  - 1;
+    if (y >= RGB_OLED_HEIGHT) y = RGB_OLED_HEIGHT - 1;
+    //                         set column point               set row point
+    uint8_t cmdBuffer[6] = { CMD_SET_COLUMN_ADDRESS, x, x,  CMD_SET_ROW_ADDRESS, y, y };
+    sendCmd(cmdBuffer, 6); //Send buffer  
+    sendData(color); //fill 16bit colour
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color)
+{
+    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
+    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
+    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
+    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
+    
+    uint8_t cmd[8] = { CMD_DRAW_LINE, x0, y0, x1, y1, (uint8_t)((color>>11)&0x1F), (uint8_t)((color>>5)&0x3F), (uint8_t)(color&0x1F) };
+    sendCmd(cmd, 8);
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::drawFrame(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t outColor, uint16_t fillColor)
+{
+    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
+    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
+    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
+    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
+    
+    uint8_t cmd[11] = { CMD_DRAW_RECTANGLE, x0, y0, x1, y1, 
+                     (uint8_t)((outColor>>11)&0x1F), (uint8_t)((outColor>>5)&0x3F), (uint8_t)(outColor&0x1F),
+                     (uint8_t)((fillColor>>11)&0x1F), (uint8_t)((fillColor>>5)&0x3F), (uint8_t)(fillColor&0x1F)};
+
+    sendCmd(CMD_FILL_WINDOW, ENABLE_FILL);//fill window
+    sendCmd(cmd, 11);
+    wait(0.05); 
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::copyArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,uint8_t x2, uint8_t y2)
+{
+    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
+    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
+    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
+    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
+    
+    uint8_t cmd[7] = { CMD_COPY_WINDOW, x0, y0, x1, y1, x2, y2};
+    sendCmd(cmd, 7);
+    wait(0.05); 
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::dimArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
+{
+    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
+    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
+    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
+    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
+    
+    uint8_t cmdBuffer[5] = { CMD_DIM_WINDOW, x0, y0, x1, y1 };
+    sendCmd(cmdBuffer, 5); //Send buffer
+    wait(0.05); 
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::clearArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
+{
+    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
+    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
+    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
+    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
+    
+    uint8_t cmdBuffer[5] = { CMD_CLEAR_WINDOW, x0, y0, x1, y1 };
+    sendCmd(cmdBuffer, 5); //Send buffer
+    wait(0.05);  
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::setScolling(ScollingDirection direction, uint8_t rowAddr, uint8_t rowNum, uint8_t timeInterval)
+{
+    uint8_t scolling_horizontal = 0x0;
+    uint8_t scolling_vertical = 0x0;
+    switch(direction){
+        case Horizontal:
+            scolling_horizontal = 0x01;
+            scolling_vertical   = 0x00;
+            break;
+        case Vertical:
+            scolling_horizontal = 0x00;
+            scolling_vertical   = 0x01;
+            break;
+        case Diagonal:
+            scolling_horizontal = 0x01;
+            scolling_vertical   = 0x01;
+            break;
+        default:
+            break;
+    }
+    
+    uint8_t cmdBuffer[6] = { CMD_CONTINUOUS_SCROLLING_SETUP, scolling_horizontal, rowAddr, rowNum, scolling_vertical, timeInterval };
+    sendCmd(cmdBuffer, 6); //Send buffer
+    sendCmd(CMD_ACTIVE_SCROLLING);
+    wait(0.05);  
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::enableScolling(bool enable)
+{
+    if(enable)
+        sendCmd(CMD_ACTIVE_SCROLLING);
+    else
+        sendCmd(CMD_DEACTIVE_SCROLLING);
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::setDisplayMode(DisplayMode mode)
+{
+    sendCmd(mode);
+}
+
+//------------------------------------------------------------------------------------
+void SSD1331::setDisplayPower(DisplayPower power)
+{
+    sendCmd(power);
+}
+
+//------------------------------------------------------------------------------------
+// Private member functions
+//------------------------------------------------------------------------------------
 void SSD1331::sendCmd(uint8_t cmd)
 {
     _dc = 0;
@@ -122,211 +318,6 @@
 }
 
 //------------------------------------------------------------------------------------
-void SSD1331::init(void)
-{
-    // initialize sequence
-    sendCmd(CMD_DISPLAY_OFF);    //OLED display OFF
-    
-    //Row Address
-    sendCmd(CMD_SET_ROW_ADDRESS,    0x00, 0x3f); //Set Row Address with start=0,end=63
-    sendCmd(CMD_SET_COLUMN_ADDRESS, 0x00, 0x5f); //Set Column Address with start=0,end=95
-    
-     //Contrast
-    sendCmd(CMD_SET_REMAP,              0x76); //Set remap & data format 0111 0000 // 65k Color/8bit buswidth/format1
-    sendCmd(CMD_SET_DISPLAY_START_LINE, 0x00); //Set display start row RAM
-    sendCmd(CMD_SET_DISPLAY_OFFSET,     0x00); //Set dispaly offset
-    sendCmd(CMD_NORMAL_DISPLAY);               //Set Display Mode
-    sendCmd(CMD_SET_MULTIPLEX_RATIO,    0x3f); //Set Multiplex Ratio
-    sendCmd(CMD_SET_MASTER_CONFIGURE,   0x8f); //Set Master Configuration (External VCC Supply Selected)
-    sendCmd(CMD_POWER_SAVE_MODE,        0x1a); //Set Power Saving Mode
-    sendCmd(CMD_PHASE_PERIOD_ADJUSTMENT, 0x74);//Set Phase 1 & 2 Period Adjustment
-    sendCmd(CMD_DISPLAY_CLOCK_DIV,      0xd0); //Set Display Clock Divide Ratio / Oscillator Frequency
-    sendCmd(CMD_SET_PRECHARGE_SPEED_A,  0x81); //Set Second Pre-charge Speed of Color A
-    sendCmd(CMD_SET_PRECHARGE_SPEED_B,  0x82); //Set Second Pre-charge Speed of Color B
-    sendCmd(CMD_SET_PRECHARGE_SPEED_C,  0x83); //Set Second Pre-charge Speed of Color C
-    sendCmd(CMD_SET_PRECHARGE_VOLTAGE,  0x3e); //Set Pre-charge Level
-    sendCmd(CMD_SET_V_VOLTAGE,          0x3e); //Set VCOMH
-    sendCmd(CMD_MASTER_CURRENT_CONTROL, 0x0f); //Set Master Current Control
-    sendCmd(CMD_SET_CONTRAST_A,         0x80); //Set Contrast Control for Color gAh
-    sendCmd(CMD_SET_CONTRAST_B,         0x80); //Set Contrast Control for Color gBh
-    sendCmd(CMD_SET_CONTRAST_C,         0x80); //Set Contrast Control for Color gCh 
-    
-    fillRectangle(0,0, RGB_OLED_WIDTH, RGB_OLED_HEIGHT, 0x0);
-    sendCmd(CMD_NORMAL_BRIGHTNESS_DISPLAY_ON);    //display ON
-
-//    sendCmd(CMD_DISPLAY_OFF);          //Display Off
-//    sendCmd(CMD_SET_CONTRAST_A);       //Set contrast for color A
-//    sendCmd(0x91);                     //145
-//    sendCmd(CMD_SET_CONTRAST_B);       //Set contrast for color B
-//    sendCmd(0x50);                     //80
-//    sendCmd(CMD_SET_CONTRAST_C);       //Set contrast for color C
-//    sendCmd(0x7D);                     //125
-//    sendCmd(CMD_MASTER_CURRENT_CONTROL);//master current control
-//    sendCmd(0x06);                     //6
-//    sendCmd(CMD_SET_PRECHARGE_SPEED_A);//Set Second Pre-change Speed For ColorA
-//    sendCmd(0x64);                     //100
-//    sendCmd(CMD_SET_PRECHARGE_SPEED_B);//Set Second Pre-change Speed For ColorB
-//    sendCmd(0x78);                     //120
-//    sendCmd(CMD_SET_PRECHARGE_SPEED_C);//Set Second Pre-change Speed For ColorC
-//    sendCmd(0x64);                     //100
-//    sendCmd(CMD_SET_REMAP);            //set remap & data format
-//    sendCmd(0x72);                     //0x72              
-//    sendCmd(CMD_SET_DISPLAY_START_LINE);//Set display Start Line
-//    sendCmd(0x0);
-//    sendCmd(CMD_SET_DISPLAY_OFFSET);   //Set display offset
-//    sendCmd(0x0);
-//    sendCmd(CMD_NORMAL_DISPLAY);       //Set display mode
-//    sendCmd(CMD_SET_MULTIPLEX_RATIO);  //Set multiplex ratio
-//    sendCmd(0x3F);
-//    sendCmd(CMD_SET_MASTER_CONFIGURE); //Set master configuration
-//    sendCmd(0x8E);
-//    sendCmd(CMD_POWER_SAVE_MODE);      //Set Power Save Mode
-//    sendCmd(0x00);                     //0x00
-//    sendCmd(CMD_PHASE_PERIOD_ADJUSTMENT);//phase 1 and 2 period adjustment
-//    sendCmd(0x31);                     //0x31
-//    sendCmd(CMD_DISPLAY_CLOCK_DIV);    //display clock divider/oscillator frequency
-//    sendCmd(0xF0);
-//    sendCmd(CMD_SET_PRECHARGE_VOLTAGE);//Set Pre-Change Level
-//    sendCmd(0x3A);
-//    sendCmd(CMD_SET_V_VOLTAGE);        //Set vcomH
-//    sendCmd(0x3E);
-//    sendCmd(CMD_DEACTIVE_SCROLLING);   //disable scrolling
-//    sendCmd(CMD_NORMAL_BRIGHTNESS_DISPLAY_ON);//set display on
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::drawPixel(uint8_t x, uint8_t y, uint16_t color)
-{
-    if ((x >= RGB_OLED_WIDTH) || (y >= RGB_OLED_HEIGHT))
-        return;
-        
-    //                      set column point               set row point
-    uint8_t cmd[6] = { CMD_SET_COLUMN_ADDRESS, x, x,  CMD_SET_ROW_ADDRESS, y, y };
-    sendCmd(cmd, 6);
-
-    //fill 16bit colour
-    sendData(color);
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color)
-{
-    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
-    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
-    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
-    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
-    
-    uint8_t cmd[8] = { CMD_DRAW_LINE, x0, y0, x1, y1, (uint8_t)((color>>11)&0x1F), (uint8_t)((color>>5)&0x3F), (uint8_t)(color&0x1F) };
-    sendCmd(cmd, 8);
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::drawFrame(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t outColor, uint16_t fillColor)
-{
-    if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH  - 1;
-    if (y0 >= RGB_OLED_HEIGHT) y0 = RGB_OLED_HEIGHT - 1;
-    if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH  - 1;
-    if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
-
-    sendCmd(CMD_FILL_WINDOW);//fill window
-    sendCmd(ENABLE_FILL);
-    sendCmd(CMD_DRAW_RECTANGLE);//draw rectangle
-    sendCmd(x0);//start column
-    sendCmd(y0);//start row
-    sendCmd(x1);//end column
-    sendCmd(y1);//end row
-    sendCmd((uint8_t)((outColor>>11)&0x1F));//R
-    sendCmd((uint8_t)((outColor>>5)&0x3F));//G
-    sendCmd((uint8_t)(outColor&0x1F));//B
-    sendCmd((uint8_t)((fillColor>>11)&0x1F));//R
-    sendCmd((uint8_t)((fillColor>>5)&0x3F));//G
-    sendCmd((uint8_t)(fillColor&0x1F));//B
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::copyWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,uint16_t x2, uint16_t y2)
-{
-    sendCmd(CMD_COPY_WINDOW);//copy window
-    sendCmd(x0);//start column
-    sendCmd(y0);//start row
-    sendCmd(x1);//end column
-    sendCmd(y1);//end row
-    sendCmd(x2);//new column
-    sendCmd(y2);//new row
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::dimWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
-{
-    sendCmd(CMD_DIM_WINDOW);//copy area
-    sendCmd(x0);//start column
-    sendCmd(y0);//start row
-    sendCmd(x1);//end column
-    sendCmd(y1);//end row
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::clearWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
-{
-    sendCmd(CMD_CLEAR_WINDOW);//clear window
-    sendCmd(x0);//start column
-    sendCmd(y0);//start row
-    sendCmd(x1);//end column
-    sendCmd(y1);//end row
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::setScolling(ScollingDirection direction, uint8_t rowAddr, uint8_t rowNum, uint8_t timeInterval)
-{
-    uint8_t scolling_horizontal = 0x0;
-    uint8_t scolling_vertical = 0x0;
-    switch(direction){
-        case Horizontal:
-            scolling_horizontal = 0x01;
-            scolling_vertical = 0x00;
-            break;
-        case Vertical:
-            scolling_horizontal = 0x00;
-            scolling_vertical = 0x01;
-            break;
-        case Diagonal:
-            scolling_horizontal = 0x01;
-            scolling_vertical = 0x01;
-            break;
-        default:
-            break;
-    }
-    sendCmd(CMD_CONTINUOUS_SCROLLING_SETUP);
-    sendCmd(scolling_horizontal);
-    sendCmd(rowAddr);
-    sendCmd(rowNum);
-    sendCmd(scolling_vertical);
-    sendCmd(timeInterval);
-    sendCmd(CMD_ACTIVE_SCROLLING);
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::enableScolling(bool enable)
-{
-    if(enable)
-        sendCmd(CMD_ACTIVE_SCROLLING);
-    else
-        sendCmd(CMD_DEACTIVE_SCROLLING);
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::setDisplayMode(DisplayMode mode)
-{
-    sendCmd(mode);
-}
-
-//------------------------------------------------------------------------------------
-void SSD1331::setDisplayPower(DisplayPower power)
-{
-    sendCmd(power);
-}
-
-//------------------------------------------------------------------------------------
 int SSD1331::_getc() 
 {
     return -1;