Fork of nkhorman's Adafruit mbed port with added I2C support

Fork of Adafruit_GFX by Neal Horman

Committer:
pommzorz
Date:
Thu Mar 13 10:47:03 2014 +0000
Revision:
4:86b9481fa281
Parent:
0:c3dcd4c4983a
Child:
5:b8313591b561
Added I2C support with precompiler defines

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 0:c3dcd4c4983a 1 /*********************************************************************
nkhorman 0:c3dcd4c4983a 2 This is a library for our Monochrome OLEDs based on SSD1306 drivers
nkhorman 0:c3dcd4c4983a 3
nkhorman 0:c3dcd4c4983a 4 Pick one up today in the adafruit shop!
nkhorman 0:c3dcd4c4983a 5 ------> http://www.adafruit.com/category/63_98
nkhorman 0:c3dcd4c4983a 6
nkhorman 0:c3dcd4c4983a 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
nkhorman 0:c3dcd4c4983a 10 Adafruit invests time and resources providing this open source code,
nkhorman 0:c3dcd4c4983a 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
nkhorman 0:c3dcd4c4983a 14 Written by Limor Fried/Ladyada for Adafruit Industries.
nkhorman 0:c3dcd4c4983a 15 BSD license, check license.txt for more information
nkhorman 0:c3dcd4c4983a 16 All text above, and the splash screen below must be included in any redistribution
nkhorman 0:c3dcd4c4983a 17 *********************************************************************/
nkhorman 0:c3dcd4c4983a 18
nkhorman 0:c3dcd4c4983a 19 /*
nkhorman 0:c3dcd4c4983a 20 * Modified by Neal Horman 7/14/2012 for use in LPC1768
nkhorman 0:c3dcd4c4983a 21 */
nkhorman 0:c3dcd4c4983a 22
nkhorman 0:c3dcd4c4983a 23 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 24 #include "Adafruit_SSD1306.h"
pommzorz 4:86b9481fa281 25 #ifdef WITH_SPLASH
pommzorz 4:86b9481fa281 26 uint8_t splashScreen[512] = { //SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8
nkhorman 0:c3dcd4c4983a 27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
nkhorman 0:c3dcd4c4983a 31 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 32 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 36 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
nkhorman 0:c3dcd4c4983a 38 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
nkhorman 0:c3dcd4c4983a 39 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 40 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
nkhorman 0:c3dcd4c4983a 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
nkhorman 0:c3dcd4c4983a 42 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 43 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
nkhorman 0:c3dcd4c4983a 44 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
nkhorman 0:c3dcd4c4983a 46 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
nkhorman 0:c3dcd4c4983a 47 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 48 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
nkhorman 0:c3dcd4c4983a 49 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
nkhorman 0:c3dcd4c4983a 50 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 51 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
nkhorman 0:c3dcd4c4983a 52 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
nkhorman 0:c3dcd4c4983a 53 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
nkhorman 0:c3dcd4c4983a 54 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
nkhorman 0:c3dcd4c4983a 55 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
nkhorman 0:c3dcd4c4983a 56 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
nkhorman 0:c3dcd4c4983a 57 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
pommzorz 4:86b9481fa281 58 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
pommzorz 4:86b9481fa281 59 };
nkhorman 0:c3dcd4c4983a 60 #endif
nkhorman 0:c3dcd4c4983a 61
pommzorz 4:86b9481fa281 62 #ifdef IS_SPI
nkhorman 0:c3dcd4c4983a 63 Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS)
nkhorman 0:c3dcd4c4983a 64 : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
nkhorman 0:c3dcd4c4983a 65 , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi)
nkhorman 0:c3dcd4c4983a 66 {
pommzorz 4:86b9481fa281 67 //memcpy(buffer,splashScreen,sizeof(buffer));
nkhorman 0:c3dcd4c4983a 68 begin();
nkhorman 0:c3dcd4c4983a 69 display();
nkhorman 0:c3dcd4c4983a 70 };
pommzorz 4:86b9481fa281 71 #elif defined IS_I2C
pommzorz 4:86b9481fa281 72 Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST)
pommzorz 4:86b9481fa281 73 : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
pommzorz 4:86b9481fa281 74 , rst(RST,false),mi2c(i2c),pc(USBTX, USBRX)
pommzorz 4:86b9481fa281 75 {
pommzorz 4:86b9481fa281 76
pommzorz 4:86b9481fa281 77 pc.baud(921600);
pommzorz 4:86b9481fa281 78 mi2c.frequency(100000);
pommzorz 4:86b9481fa281 79 pc.printf("\r\n\ninit.");
pommzorz 4:86b9481fa281 80 mi2c.start();
pommzorz 4:86b9481fa281 81 //memcpy(buffer,splashScreen,sizeof(buffer));
pommzorz 4:86b9481fa281 82 pc.printf("cp.");
pommzorz 4:86b9481fa281 83 begin();
pommzorz 4:86b9481fa281 84 pc.printf("bg.");
pommzorz 4:86b9481fa281 85 display();
pommzorz 4:86b9481fa281 86 pc.printf("dp\r\n");
pommzorz 4:86b9481fa281 87 };
pommzorz 4:86b9481fa281 88 #endif
pommzorz 4:86b9481fa281 89
nkhorman 0:c3dcd4c4983a 90
nkhorman 0:c3dcd4c4983a 91 void Adafruit_SSD1306::begin(uint8_t vccstate)
nkhorman 0:c3dcd4c4983a 92 {
nkhorman 0:c3dcd4c4983a 93 rst = 1;
nkhorman 0:c3dcd4c4983a 94 // VDD (3.3V) goes high at start, lets just chill for a ms
nkhorman 0:c3dcd4c4983a 95 wait_ms(1);
nkhorman 0:c3dcd4c4983a 96 // bring reset low
nkhorman 0:c3dcd4c4983a 97 rst = 0;
nkhorman 0:c3dcd4c4983a 98 // wait 10ms
nkhorman 0:c3dcd4c4983a 99 wait_ms(10);
nkhorman 0:c3dcd4c4983a 100 // bring out of reset
nkhorman 0:c3dcd4c4983a 101 rst = 1;
nkhorman 0:c3dcd4c4983a 102 // turn on VCC (9V?)
nkhorman 0:c3dcd4c4983a 103
nkhorman 0:c3dcd4c4983a 104 #if defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 105 // Init sequence for 128x32 OLED module
nkhorman 0:c3dcd4c4983a 106 ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE
nkhorman 0:c3dcd4c4983a 107 ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
nkhorman 0:c3dcd4c4983a 108 ssd1306_command(0x80); // the suggested ratio 0x80
nkhorman 0:c3dcd4c4983a 109 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8
nkhorman 0:c3dcd4c4983a 110 ssd1306_command(0x1F);
nkhorman 0:c3dcd4c4983a 111 ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3
nkhorman 0:c3dcd4c4983a 112 ssd1306_command(0x0); // no offset
nkhorman 0:c3dcd4c4983a 113 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
nkhorman 0:c3dcd4c4983a 114 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D
nkhorman 0:c3dcd4c4983a 115 if (vccstate == SSD1306_EXTERNALVCC)
nkhorman 0:c3dcd4c4983a 116 { ssd1306_command(0x10); }
nkhorman 0:c3dcd4c4983a 117 else
nkhorman 0:c3dcd4c4983a 118 { ssd1306_command(0x14); }
nkhorman 0:c3dcd4c4983a 119 ssd1306_command(SSD1306_MEMORYMODE); // 0x20
nkhorman 0:c3dcd4c4983a 120 ssd1306_command(0x00); // 0x0 act like ks0108
nkhorman 0:c3dcd4c4983a 121 ssd1306_command(SSD1306_SEGREMAP | 0x1);
nkhorman 0:c3dcd4c4983a 122 ssd1306_command(SSD1306_COMSCANDEC);
nkhorman 0:c3dcd4c4983a 123 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
nkhorman 0:c3dcd4c4983a 124 ssd1306_command(0x02);
nkhorman 0:c3dcd4c4983a 125 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
nkhorman 0:c3dcd4c4983a 126 ssd1306_command(0x8F);
nkhorman 0:c3dcd4c4983a 127 ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9
nkhorman 0:c3dcd4c4983a 128 if (vccstate == SSD1306_EXTERNALVCC)
nkhorman 0:c3dcd4c4983a 129 { ssd1306_command(0x22); }
nkhorman 0:c3dcd4c4983a 130 else
nkhorman 0:c3dcd4c4983a 131 { ssd1306_command(0xF1); }
nkhorman 0:c3dcd4c4983a 132 ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB
nkhorman 0:c3dcd4c4983a 133 ssd1306_command(0x40);
nkhorman 0:c3dcd4c4983a 134 ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
nkhorman 0:c3dcd4c4983a 135 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6
nkhorman 0:c3dcd4c4983a 136 #endif
nkhorman 0:c3dcd4c4983a 137
nkhorman 0:c3dcd4c4983a 138 #if defined SSD1306_128_64
nkhorman 0:c3dcd4c4983a 139 // Init sequence for 128x64 OLED module
nkhorman 0:c3dcd4c4983a 140 ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE
nkhorman 0:c3dcd4c4983a 141 ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
nkhorman 0:c3dcd4c4983a 142 ssd1306_command(0x80); // the suggested ratio 0x80
nkhorman 0:c3dcd4c4983a 143 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8
nkhorman 0:c3dcd4c4983a 144 ssd1306_command(0x3F);
nkhorman 0:c3dcd4c4983a 145 ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3
nkhorman 0:c3dcd4c4983a 146 ssd1306_command(0x0); // no offset
nkhorman 0:c3dcd4c4983a 147 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
nkhorman 0:c3dcd4c4983a 148 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D
nkhorman 0:c3dcd4c4983a 149 if (vccstate == SSD1306_EXTERNALVCC)
nkhorman 0:c3dcd4c4983a 150 { ssd1306_command(0x10); }
nkhorman 0:c3dcd4c4983a 151 else
nkhorman 0:c3dcd4c4983a 152 { ssd1306_command(0x14); }
nkhorman 0:c3dcd4c4983a 153 ssd1306_command(SSD1306_MEMORYMODE); // 0x20
nkhorman 0:c3dcd4c4983a 154 ssd1306_command(0x00); // 0x0 act like ks0108
nkhorman 0:c3dcd4c4983a 155 ssd1306_command(SSD1306_SEGREMAP | 0x1);
nkhorman 0:c3dcd4c4983a 156 ssd1306_command(SSD1306_COMSCANDEC);
nkhorman 0:c3dcd4c4983a 157 ssd1306_command(SSD1306_SETCOMPINS); // 0xDA
nkhorman 0:c3dcd4c4983a 158 ssd1306_command(0x12);
nkhorman 0:c3dcd4c4983a 159 ssd1306_command(SSD1306_SETCONTRAST); // 0x81
nkhorman 0:c3dcd4c4983a 160 if (vccstate == SSD1306_EXTERNALVCC)
nkhorman 0:c3dcd4c4983a 161 { ssd1306_command(0x9F); }
nkhorman 0:c3dcd4c4983a 162 else
nkhorman 0:c3dcd4c4983a 163 { ssd1306_command(0xCF); }
nkhorman 0:c3dcd4c4983a 164 ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9
nkhorman 0:c3dcd4c4983a 165 if (vccstate == SSD1306_EXTERNALVCC)
nkhorman 0:c3dcd4c4983a 166 { ssd1306_command(0x22); }
nkhorman 0:c3dcd4c4983a 167 else
nkhorman 0:c3dcd4c4983a 168 { ssd1306_command(0xF1); }
nkhorman 0:c3dcd4c4983a 169 ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB
nkhorman 0:c3dcd4c4983a 170 ssd1306_command(0x40);
nkhorman 0:c3dcd4c4983a 171 ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4
nkhorman 0:c3dcd4c4983a 172 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6
nkhorman 0:c3dcd4c4983a 173 #endif
nkhorman 0:c3dcd4c4983a 174
nkhorman 0:c3dcd4c4983a 175 ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel
nkhorman 0:c3dcd4c4983a 176 }
nkhorman 0:c3dcd4c4983a 177
nkhorman 0:c3dcd4c4983a 178 // the most basic function, set a single pixel
nkhorman 0:c3dcd4c4983a 179 void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color)
nkhorman 0:c3dcd4c4983a 180 {
nkhorman 0:c3dcd4c4983a 181 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
nkhorman 0:c3dcd4c4983a 182 return;
nkhorman 0:c3dcd4c4983a 183
nkhorman 0:c3dcd4c4983a 184 // check rotation, move pixel around if necessary
nkhorman 0:c3dcd4c4983a 185 switch (getRotation())
nkhorman 0:c3dcd4c4983a 186 {
nkhorman 0:c3dcd4c4983a 187 case 1:
nkhorman 0:c3dcd4c4983a 188 swap(x, y);
nkhorman 0:c3dcd4c4983a 189 x = _rawWidth - x - 1;
nkhorman 0:c3dcd4c4983a 190 break;
nkhorman 0:c3dcd4c4983a 191 case 2:
nkhorman 0:c3dcd4c4983a 192 x = _rawWidth - x - 1;
nkhorman 0:c3dcd4c4983a 193 y = _rawHeight - y - 1;
nkhorman 0:c3dcd4c4983a 194 break;
nkhorman 0:c3dcd4c4983a 195 case 3:
nkhorman 0:c3dcd4c4983a 196 swap(x, y);
nkhorman 0:c3dcd4c4983a 197 y = _rawHeight - y - 1;
nkhorman 0:c3dcd4c4983a 198 break;
nkhorman 0:c3dcd4c4983a 199 }
nkhorman 0:c3dcd4c4983a 200
nkhorman 0:c3dcd4c4983a 201 // x is which column
nkhorman 0:c3dcd4c4983a 202 if (color == WHITE)
nkhorman 0:c3dcd4c4983a 203 buffer[x+ (y/8)*SSD1306_LCDWIDTH] |= _BV((y%8));
nkhorman 0:c3dcd4c4983a 204 else
nkhorman 0:c3dcd4c4983a 205 buffer[x+ (y/8)*SSD1306_LCDWIDTH] &= ~_BV((y%8));
nkhorman 0:c3dcd4c4983a 206 }
nkhorman 0:c3dcd4c4983a 207
nkhorman 0:c3dcd4c4983a 208 void Adafruit_SSD1306::invertDisplay(bool i)
nkhorman 0:c3dcd4c4983a 209 {
nkhorman 0:c3dcd4c4983a 210 if (i)
nkhorman 0:c3dcd4c4983a 211 ssd1306_command(SSD1306_INVERTDISPLAY);
nkhorman 0:c3dcd4c4983a 212 else
nkhorman 0:c3dcd4c4983a 213 ssd1306_command(SSD1306_NORMALDISPLAY);
nkhorman 0:c3dcd4c4983a 214 }
nkhorman 0:c3dcd4c4983a 215
nkhorman 0:c3dcd4c4983a 216 void Adafruit_SSD1306::ssd1306_command(uint8_t c)
nkhorman 0:c3dcd4c4983a 217 {
pommzorz 4:86b9481fa281 218 #ifdef IS_SPI
nkhorman 0:c3dcd4c4983a 219 cs = 1;
nkhorman 0:c3dcd4c4983a 220 dc = 0;
nkhorman 0:c3dcd4c4983a 221 cs = 0;
nkhorman 0:c3dcd4c4983a 222 mspi.write(c);
nkhorman 0:c3dcd4c4983a 223 cs = 1;
pommzorz 4:86b9481fa281 224
pommzorz 4:86b9481fa281 225 #elif defined IS_I2C
pommzorz 4:86b9481fa281 226 char buff[2] ;
pommzorz 4:86b9481fa281 227 buff[0] = SSD_Command_Mode ;
pommzorz 4:86b9481fa281 228 buff[1] = c;
pommzorz 4:86b9481fa281 229 if(mi2c.write(I2C_ADDRESS,buff,sizeof(buff)))pc.printf("!");
pommzorz 4:86b9481fa281 230
pommzorz 4:86b9481fa281 231 #endif
pommzorz 4:86b9481fa281 232
nkhorman 0:c3dcd4c4983a 233 }
nkhorman 0:c3dcd4c4983a 234
nkhorman 0:c3dcd4c4983a 235 void Adafruit_SSD1306::ssd1306_data(uint8_t c)
nkhorman 0:c3dcd4c4983a 236 {
pommzorz 4:86b9481fa281 237 #ifdef IS_SPI
nkhorman 0:c3dcd4c4983a 238 cs = 1;
nkhorman 0:c3dcd4c4983a 239 dc = 1;
nkhorman 0:c3dcd4c4983a 240 cs = 0;
nkhorman 0:c3dcd4c4983a 241 mspi.write(c);
nkhorman 0:c3dcd4c4983a 242 cs = 1;
pommzorz 4:86b9481fa281 243 #elif defined IS_I2C
pommzorz 4:86b9481fa281 244 char buff[2] ;
pommzorz 4:86b9481fa281 245 // Setup D/C to switch to data mode
pommzorz 4:86b9481fa281 246 buff[0] = SSD_Data_Mode;
pommzorz 4:86b9481fa281 247 buff[1] = c;
pommzorz 4:86b9481fa281 248 // Write on i2c
pommzorz 4:86b9481fa281 249 if(mi2c.write(I2C_ADDRESS,buff,sizeof(buff)))pc.printf("?");
pommzorz 4:86b9481fa281 250 #endif
pommzorz 4:86b9481fa281 251
nkhorman 0:c3dcd4c4983a 252 }
nkhorman 0:c3dcd4c4983a 253
nkhorman 0:c3dcd4c4983a 254 void Adafruit_SSD1306::display(void)
nkhorman 0:c3dcd4c4983a 255 {
nkhorman 0:c3dcd4c4983a 256 ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
nkhorman 0:c3dcd4c4983a 257 ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
nkhorman 0:c3dcd4c4983a 258 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
pommzorz 4:86b9481fa281 259 #ifdef IS_SPI
nkhorman 0:c3dcd4c4983a 260 cs = 1;
nkhorman 0:c3dcd4c4983a 261 dc = 1;
nkhorman 0:c3dcd4c4983a 262 cs = 0;
nkhorman 0:c3dcd4c4983a 263
nkhorman 0:c3dcd4c4983a 264 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++)
nkhorman 0:c3dcd4c4983a 265 mspi.write(buffer[i]);
nkhorman 0:c3dcd4c4983a 266
nkhorman 0:c3dcd4c4983a 267 // i wonder why we have to do this (check datasheet)
nkhorman 0:c3dcd4c4983a 268 if (SSD1306_LCDHEIGHT == 32)
nkhorman 0:c3dcd4c4983a 269 {
nkhorman 0:c3dcd4c4983a 270 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++)
nkhorman 0:c3dcd4c4983a 271 mspi.write(0);
nkhorman 0:c3dcd4c4983a 272 }
nkhorman 0:c3dcd4c4983a 273
nkhorman 0:c3dcd4c4983a 274 cs = 1;
pommzorz 4:86b9481fa281 275
pommzorz 4:86b9481fa281 276 #elif defined IS_I2C
pommzorz 4:86b9481fa281 277 char buff[17] ;
pommzorz 4:86b9481fa281 278 uint8_t x ;
pommzorz 4:86b9481fa281 279 // Setup D/C to switch to data mode
pommzorz 4:86b9481fa281 280 buff[0] = SSD_Data_Mode;
pommzorz 4:86b9481fa281 281
pommzorz 4:86b9481fa281 282 // loop trough all OLED buffer and
pommzorz 4:86b9481fa281 283 // send a bunch of 16 data byte in one xmission
pommzorz 4:86b9481fa281 284 for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 )
pommzorz 4:86b9481fa281 285 {
pommzorz 4:86b9481fa281 286 for (x=1; x<=16; x++)
pommzorz 4:86b9481fa281 287 buff[x] = buffer[i+x];
pommzorz 4:86b9481fa281 288 if(mi2c.write(I2C_ADDRESS, buff, 17))pc.printf("?");
pommzorz 4:86b9481fa281 289 }
pommzorz 4:86b9481fa281 290 #endif
nkhorman 0:c3dcd4c4983a 291 }
nkhorman 0:c3dcd4c4983a 292
nkhorman 0:c3dcd4c4983a 293 // clear everything
nkhorman 0:c3dcd4c4983a 294 void Adafruit_SSD1306::clearDisplay(void)
nkhorman 0:c3dcd4c4983a 295 {
nkhorman 0:c3dcd4c4983a 296 memset(buffer,0,sizeof(buffer));
nkhorman 0:c3dcd4c4983a 297 }