Driver for the Seeedstudio RGB OLED module for the xadow M0

Revision:
2:e033aab5daad
Parent:
0:6e810b5b40a3
Child:
3:af00512c9694
--- a/src/SSD1331.cpp	Tue Nov 10 23:16:37 2015 +0000
+++ b/src/SSD1331.cpp	Thu Nov 12 22:31:58 2015 +0000
@@ -60,91 +60,178 @@
 #include "SPI.h"
 #include "SSD1331.h"
 
-SSD1331::SSD1331(PinName cs, PinName dc, PinName mosi, PinName sck)
-:SGL(RGB_OLED_WIDTH, RGB_OLED_HEIGHT)
+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)
 {
-    _cs = new mbed::DigitalOut(cs);
-    _dc = new mbed::DigitalOut(dc);
-    _spiPort = new mbed::SPI(mosi, NC, sck);
-    _spiPort->format(8,3); //8bit frame and POL=1 /PHA=1(UpEdge Sampled)
-    _spiPort->frequency(25000000); // modify later
+    _spiPort.format(8,3); //8bit frame and POL=1 /PHA=1(UpEdge Sampled)
+    _spiPort.frequency(25000000); // modify later
+    
+    init();
 };
 
-void SSD1331::_sendCmd(uint8_t c)
+void SSD1331::sendCmd(unsigned char cmd)
+{
+    _dc = 0;
+    _cs = 0;
+    _spiPort.write(cmd);
+    _cs = 1;
+}
+
+void SSD1331::sendCmd(unsigned char cmd, unsigned char arg1)
+{
+    _dc = 0;
+    _cs = 0;
+    _spiPort.write(cmd);
+    _spiPort.write(arg1);
+    _cs = 1;
+}
+    
+void SSD1331::sendCmd(unsigned char cmd, unsigned char arg1, unsigned char arg2)
 {
-    _dc->write(LOW);
-    _cs->write(LOW);
-    _spiPort->write(c);
-    _cs->write(HIGH);
+    _dc = 0;
+    _cs = 0;
+    _spiPort.write(cmd);
+    _spiPort.write(arg1);
+    _spiPort.write(arg2);
+    _cs = 1;
+}    
+
+void  SSD1331::sendCmd(unsigned char *cmd, int count)
+{
+    if(!cmd) return;
+    
+    _dc = 0;
+    _cs = 0;
+    for(int i=0; i<count; ++i)
+       _spiPort.write(*cmd++);
+    _cs = 1;
+}
+
+void  SSD1331::sendData(uint16_t data)
+{
+    _dc = HIGH;
+    _cs = LOW;
+    _spiPort.write(data >> 8);
+    _spiPort.write((data & 0x00ff));
+    _cs = HIGH;
+}
+
+int SSD1331::_getc() 
+{
+    return -1;
+}
+
+int SSD1331::_putc( int c )
+{  
+    return -1;
 }
 
 void SSD1331::init(void)
 {
-//    pinMode(_dc, OUTPUT);
-//    pinMode(_cs, OUTPUT);
-
-//    _spiPort->begin();
+    // 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 &#129;gA&#129;h
+    sendCmd(CMD_SET_CONTRAST_B,         0x80); //Set Contrast Control for Color &#129;gB&#129;h
+    sendCmd(CMD_SET_CONTRAST_C,         0x80); //Set Contrast Control for Color &#129;gC&#129;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
+//    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(uint16_t x, uint16_t y, uint16_t color)
 {
     if ((x >= RGB_OLED_WIDTH) || (y >= RGB_OLED_HEIGHT))
         return;
+        
     //set column point
-    _sendCmd(CMD_SET_COLUMN_ADDRESS);
-    _sendCmd(x);
-    _sendCmd(RGB_OLED_WIDTH-1);
+    sendCmd(CMD_SET_COLUMN_ADDRESS, x, RGB_OLED_WIDTH-1);
+
     //set row point
-    _sendCmd(CMD_SET_ROW_ADDRESS);
-    _sendCmd(y);
-    _sendCmd(RGB_OLED_HEIGHT-1);
+    sendCmd(CMD_SET_ROW_ADDRESS, y, RGB_OLED_HEIGHT-1);
+
     //fill 16bit colour
-    _dc->write(HIGH);
-    _cs->write(LOW);
-    _spiPort->write(color >> 8);
-    _spiPort->write(color);
-    _cs->write(HIGH);
+    sendData(color);
 }
 
+//------------------------------------------------------------
+
+
+void SSD1331::Draw_Dot(uint8_t x, uint8_t y,unsigned int Color)
+{
+    x=96-x;
+    
+    unsigned char cmd[7]={0x15,0x00,0x00,0x75,0x00,0x00,0x00};
+
+    cmd[1] = (unsigned char)x; 
+    cmd[2] = (unsigned char)x; 
+    cmd[4] = (unsigned char)y; 
+    cmd[5] = (unsigned char)y; 
+    sendCmd(cmd, 6);
+    sendData(Color);
+}
+
+
+//-------------------------------------------------------------------------
 void SSD1331::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
 {
     if (x0 >= RGB_OLED_WIDTH)  x0 = RGB_OLED_WIDTH - 1;
@@ -152,14 +239,14 @@
     if (x1 >= RGB_OLED_WIDTH)  x1 = RGB_OLED_WIDTH - 1;
     if (y1 >= RGB_OLED_HEIGHT) y1 = RGB_OLED_HEIGHT - 1;
 
-    _sendCmd(CMD_DRAW_LINE);//draw line
-    _sendCmd(x0);//start column
-    _sendCmd(y0);//start row
-    _sendCmd(x1);//end column
-    _sendCmd(y1);//end row
-    _sendCmd((uint8_t)((color>>11)&0x1F));//R
-    _sendCmd((uint8_t)((color>>5)&0x3F));//G
-    _sendCmd((uint8_t)(color&0x1F));//B
+    sendCmd(CMD_DRAW_LINE);//draw line
+    sendCmd(x0);//start column
+    sendCmd(y0);//start row
+    sendCmd(x1);//end column
+    sendCmd(y1);//end row
+    sendCmd((uint8_t)((color>>11)&0x1F));//R
+    sendCmd((uint8_t)((color>>5)&0x3F));//G
+    sendCmd((uint8_t)(color&0x1F));//B
 }
 
 void SSD1331::drawFrame(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t outColor, uint16_t fillColor)
@@ -169,48 +256,48 @@
     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
+    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
+    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
+    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
+    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)
@@ -233,29 +320,29 @@
         default:
             break;
     }
-    _sendCmd(CMD_CONTINUOUS_SCROLLING_SETUP);
-    _sendCmd(scolling_horizontal);
-    _sendCmd(rowAddr);
-    _sendCmd(rowNum);
-    _sendCmd(scolling_vertical);
-    _sendCmd(timeInterval);
-    _sendCmd(CMD_ACTIVE_SCROLLING);
+    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);
+        sendCmd(CMD_ACTIVE_SCROLLING);
     else
-        _sendCmd(CMD_DEACTIVE_SCROLLING);
+        sendCmd(CMD_DEACTIVE_SCROLLING);
 }
 
 void SSD1331::setDisplayMode(DisplayMode mode)
 {
-    _sendCmd(mode);
+    sendCmd(mode);
 }
 
 void SSD1331::setDisplayPower(DisplayPower power)
 {
-    _sendCmd(power);
+    sendCmd(power);
 }