Roqyun KO / Mbed 2 deprecated MeringueCitron

Dependencies:   mbed DHT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADA326.cpp Source File

ADA326.cpp

00001 #include "ADA326.h"
00002 #include "LCDGraphics.h"
00003 #include "mbed.h"
00004 
00005 
00006 #define SSD1306_LCDHEIGHT 64
00007 #define SSD1306_LCDWIDTH 128
00008 
00009 #define SSD1306_SETCONTRAST 0x81
00010 #define SSD1306_DISPLAYALLON_RESUME 0xA4
00011 #define SSD1306_DISPLAYALLON 0xA5
00012 #define SSD1306_NORMALDISPLAY 0xA6
00013 #define SSD1306_INVERTDISPLAY 0xA7
00014 #define SSD1306_DISPLAYOFF 0xAE
00015 #define SSD1306_DISPLAYON 0xAF
00016 
00017 #define SSD1306_SETDISPLAYOFFSET 0xD3
00018 #define SSD1306_SETCOMPINS 0xDA
00019 
00020 #define SSD1306_SETVCOMDETECT 0xDB
00021 
00022 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
00023 #define SSD1306_SETPRECHARGE 0xD9
00024 
00025 #define SSD1306_SETMULTIPLEX 0xA8
00026 
00027 #define SSD1306_SETLOWCOLUMN 0x00
00028 #define SSD1306_SETHIGHCOLUMN 0x10
00029 
00030 #define SSD1306_SETSTARTLINE 0x40
00031 
00032 #define SSD1306_MEMORYMODE 0x20
00033 #define SSD1306_COLUMNADDR 0x21
00034 #define SSD1306_PAGEADDR   0x22
00035 
00036 #define SSD1306_COMSCANINC 0xC0
00037 #define SSD1306_COMSCANDEC 0xC8
00038 
00039 #define SSD1306_SEGREMAP 0xA0
00040 
00041 #define SSD1306_CHARGEPUMP 0x8D
00042 
00043 #define SSD1306_EXTERNALVCC 0x1
00044 #define SSD1306_SWITCHCAPVCC 0x2
00045 
00046 // Scrolling #defines
00047 #define SSD1306_ACTIVATE_SCROLL 0x2F
00048 #define SSD1306_DEACTIVATE_SCROLL 0x2E
00049 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
00050 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
00051 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
00052 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
00053 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
00054 
00055 #define BLACK 0
00056 #define WHITE 1
00057 #define INVERSE 2
00058 
00059 
00060     
00061 extern Serial pc;
00062 DigitalOut reset(D3);
00063 
00064 #ifdef _ADA326_VER_I2C_
00065 #define I2C_ADDR (0x3D << 1)
00066 extern I2C i2c;
00067 #endif 
00068 Ada326::Ada326():
00069 LCDGraphics(SSD1306_LCDWIDTH,SSD1306_LCDHEIGHT, MAX_PAGE)
00070 {
00071     pc.baud(9600);
00072 #ifdef _ADA326_VER_I2C_
00073     pc.printf("ADA326 : Initialize I2C\r\n");
00074     pc.printf("ADA326 : %d x %d\r\n", width, height);
00075     i2c.frequency(400000);
00076 #endif    
00077 }
00078 void Ada326::display_on() 
00079 {
00080     initialize();
00081 }
00082 void Ada326::display_off()
00083 {
00084     
00085 #ifdef _ADA326_VER_I2C_
00086     i2c.start();  
00087 #endif    
00088     ssd1306_command(SSD1306_DISPLAYOFF);                    // 0xAE
00089 #ifdef _ADA326_VER_I2C_
00090     i2c.stop(); 
00091 #endif    
00092 }
00093 void Ada326::initialize()
00094 {
00095     
00096     // Setup reset pin direction (used by both SPI and I2C)
00097     reset = 1;
00098     // VDD (3.3V) goes high at start, lets just chill for a ms
00099     wait_ms(1);
00100     // bring reset low
00101     reset = 0;
00102     // wait 10ms
00103     wait_ms(10);
00104     // bring out of reset
00105     reset = 1;
00106     
00107 #ifdef _ADA326_VER_I2C_
00108     i2c.start();  
00109 #endif    
00110     ssd1306_command(SSD1306_DISPLAYOFF);                    // 0xAE
00111     ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV);            // 0xD5
00112     ssd1306_command(0x80);                                  // the suggested ratio 0x80
00113     
00114     ssd1306_command(SSD1306_CHARGEPUMP);                    // 0x8D
00115     ssd1306_command(0x14);
00116     ssd1306_command(SSD1306_MEMORYMODE);                    // 0x20
00117     ssd1306_command(0x00);                                  
00118     ssd1306_command(SSD1306_COMSCANDEC);                    // Write Pixels Downward.
00119     ssd1306_command(SSD1306_SEGREMAP|0x1);                  // Write Pixels Rightward.
00120     
00121     ssd1306_command(SSD1306_NORMALDISPLAY);
00122     ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel 
00123 #ifdef _ADA326_VER_I2C_
00124     i2c.stop(); 
00125 #endif    
00126 }
00127 // Send command through i2c communication
00128 void Ada326::ssd1306_command(uint8_t cmd)
00129 {
00130     char data[] = {0x00, cmd};
00131     i2c.write(I2C_ADDR, data, 2);
00132     wait_us(1);
00133 }
00134 // Wipe out all pixels (display pitch black)
00135 void Ada326::clear(bool show)
00136 {
00137     for (uint16_t page = 0; page < MAX_PAGE; page++)
00138         for (uint16_t seg=0; seg < width; seg++) 
00139             screen[seg][page] = 0x0;
00140     if(show)
00141         display();   
00142 }
00143 // Renew display
00144 void Ada326::display()
00145 {
00146     
00147 #ifdef _ADA326_VER_I2C_
00148     i2c.start();  
00149 #endif    
00150     // Let screen know that the entire display is used. (128x64)
00151     ssd1306_command(SSD1306_COLUMNADDR);
00152     ssd1306_command(0);   // Column start address (0 = reset)
00153     ssd1306_command(SSD1306_LCDWIDTH-1); // Column end address (127 = reset)
00154 
00155     ssd1306_command(SSD1306_PAGEADDR);
00156     ssd1306_command(0); // Page start address (0 = reset)
00157     ssd1306_command(7); // Page end address (8th page)
00158 
00159     // Signal to write pixels on the screen.
00160     i2c.write(I2C_ADDR);
00161     i2c.write(0x40);
00162     wait_us(10);
00163     int error;
00164     // Write in consecutive manner.
00165     for (uint16_t page = 0; page < MAX_PAGE; page++) {
00166         for (uint16_t seg=0; seg < width; seg++) {
00167 #ifdef _ADA326_VER_I2C_
00168             error =i2c.write(screen[seg][page]);
00169 #elif _ADA326_VER_SPI_
00170 #endif    
00171         if (error != 1)
00172             pc.printf("Error occured.\r\n");
00173         }
00174     }
00175 #ifdef _ADA326_VER_I2C_
00176     i2c.stop(); 
00177 #endif    
00178 
00179 }
00180 uint16_t Ada326::getWidth()
00181 {
00182     return SSD1306_LCDWIDTH;
00183 }
00184 uint16_t Ada326::getHeight()
00185 {
00186     return SSD1306_LCDHEIGHT;
00187 }
00188 // Replicate screen on the pc terminal. (Debugging purpose)
00189 void Ada326::serial_display()
00190 {
00191       for (uint16_t y = 0; y < height; y++) {
00192            for (uint16_t x = 0; x < width; x++) {
00193              pc.printf("%d", (screen[x][y / page] >> y % com_per_page) & 0x1);
00194         }
00195         pc.printf("\r\n");
00196     }
00197 }