rongyu lou / SSH1106-alan

Dependents:   CarPakingSystem_V13

Committer:
alanchip
Date:
Mon Apr 19 10:09:52 2021 +0000
Revision:
4:cbc557852061
Parent:
3:ccd49dd0621d
111

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:3cd0a11a2f91 1 /**
Anaesthetix 1:ac9efaadd666 2 * This is a simple library for SSH1106 controlled graphic LCD's.
Anaesthetix 0:3cd0a11a2f91 3 * Written for a cheap 1.3" OLED GLCD
Anaesthetix 0:3cd0a11a2f91 4 * See http://www.dx.com/p/open-smart-1-8-128-64-lcd-display-breakout-module-w-blue-backlit-444694
Anaesthetix 0:3cd0a11a2f91 5 *
Anaesthetix 0:3cd0a11a2f91 6 * Written by: Erik van de Coevering
Anaesthetix 0:3cd0a11a2f91 7 * With thanks to Tim Barr from whom I've reused some code
Anaesthetix 0:3cd0a11a2f91 8 * Use this code in whatever way you like, as long as it stays free of charge!
Anaesthetix 0:3cd0a11a2f91 9 */
Anaesthetix 0:3cd0a11a2f91 10
Anaesthetix 0:3cd0a11a2f91 11 #include "SSH1106.h"
alanchip 3:ccd49dd0621d 12 SPI lcd(D4, NC, D3); // mosi, miso (nc), sclk
alanchip 3:ccd49dd0621d 13 DigitalOut cs(D15); // chip select (active low)
alanchip 3:ccd49dd0621d 14 DigitalOut cd(D14); // command/data (0=command, 1=data)
alanchip 3:ccd49dd0621d 15 DigitalOut rst(D10); // Reset (active low)
Anaesthetix 0:3cd0a11a2f91 16
alanchip 3:ccd49dd0621d 17 SSH1106 ssh1106(lcd, cs, cd, rst);
Anaesthetix 0:3cd0a11a2f91 18 SSH1106::SSH1106(SPI &lcd, DigitalOut &lcd_cs, DigitalOut &lcd_cd, DigitalOut &lcd_rst)
Anaesthetix 0:3cd0a11a2f91 19 {
Anaesthetix 0:3cd0a11a2f91 20 _lcd = &lcd;
Anaesthetix 0:3cd0a11a2f91 21 _lcd_cs = &lcd_cs;
Anaesthetix 0:3cd0a11a2f91 22 _lcd_cd = &lcd_cd;
Anaesthetix 0:3cd0a11a2f91 23 _lcd_rst = &lcd_rst;
Anaesthetix 0:3cd0a11a2f91 24 }
Anaesthetix 0:3cd0a11a2f91 25
Anaesthetix 0:3cd0a11a2f91 26 void SSH1106::init()
Anaesthetix 0:3cd0a11a2f91 27 {
Anaesthetix 0:3cd0a11a2f91 28 _lcd_cs->write(1);
Anaesthetix 0:3cd0a11a2f91 29 _lcd->frequency(24000000); // Abusing SPI to save some time.. Try a lower freq if this doesn't work
Anaesthetix 0:3cd0a11a2f91 30 _lcd->format(8,3);
Anaesthetix 0:3cd0a11a2f91 31 _lcd_cs->write(0); // enable SPI
Anaesthetix 0:3cd0a11a2f91 32 _lcd_cd->write(0); // COMMAND mode
Anaesthetix 1:ac9efaadd666 33
Anaesthetix 0:3cd0a11a2f91 34 _lcd_rst->write(0);
alanchip 3:ccd49dd0621d 35 //wait_ms(100);
Anaesthetix 0:3cd0a11a2f91 36 _lcd_rst->write(1);
Anaesthetix 0:3cd0a11a2f91 37
alanchip 3:ccd49dd0621d 38 //wait_ms(50);
Anaesthetix 0:3cd0a11a2f91 39
Anaesthetix 1:ac9efaadd666 40 _lcd->write(0xAE); // Display off
Anaesthetix 0:3cd0a11a2f91 41 _lcd->write(0x02); // Set lower column address
Anaesthetix 0:3cd0a11a2f91 42 _lcd->write(0x10); // Set higher column address
Anaesthetix 0:3cd0a11a2f91 43 _lcd->write(0x40); // Set display start line
Anaesthetix 0:3cd0a11a2f91 44 _lcd->write(0xB0); // Set page address
Anaesthetix 0:3cd0a11a2f91 45 _lcd->write(0x81); // Set contrast to
Anaesthetix 0:3cd0a11a2f91 46 _lcd->write(0x80); // 128
Anaesthetix 0:3cd0a11a2f91 47 _lcd->write(0xA1); // Segment remap
Anaesthetix 0:3cd0a11a2f91 48 _lcd->write(0xA6); // Inverse: normal (A7 = inverse)
Anaesthetix 0:3cd0a11a2f91 49 _lcd->write(0xA8); // Multiplex ratio
Anaesthetix 0:3cd0a11a2f91 50 _lcd->write(0x3F); // Duty = 1/32
Anaesthetix 0:3cd0a11a2f91 51 _lcd->write(0xAD); // Charge pump enable
Anaesthetix 0:3cd0a11a2f91 52 _lcd->write(0x8B); // External VCC
Anaesthetix 0:3cd0a11a2f91 53 _lcd->write(0x33); // VPP: 9v
Anaesthetix 0:3cd0a11a2f91 54 _lcd->write(0xC8); // Com scan direction
Anaesthetix 0:3cd0a11a2f91 55 _lcd->write(0xD3); // Display offset
Anaesthetix 1:ac9efaadd666 56 _lcd->write(0x00); // 0x20
Anaesthetix 0:3cd0a11a2f91 57 _lcd->write(0xD5); // Osc division
Anaesthetix 1:ac9efaadd666 58 _lcd->write(0x80);
Anaesthetix 0:3cd0a11a2f91 59 _lcd->write(0xD9); // Pre-charge period
Anaesthetix 1:ac9efaadd666 60 _lcd->write(0x1F); // 0x22
Anaesthetix 0:3cd0a11a2f91 61 _lcd->write(0xDA); // Set com pins
Anaesthetix 1:ac9efaadd666 62 _lcd->write(0x12);
Anaesthetix 0:3cd0a11a2f91 63 _lcd->write(0xDB); // Set vcomh
Anaesthetix 1:ac9efaadd666 64 _lcd->write(0x40);
Anaesthetix 0:3cd0a11a2f91 65 _lcd->write(0xAF); // Display ON
Anaesthetix 1:ac9efaadd666 66 _lcd_cs->write(1);
Anaesthetix 1:ac9efaadd666 67 _lcd_cd->write(1);
Anaesthetix 0:3cd0a11a2f91 68 }
Anaesthetix 0:3cd0a11a2f91 69
Anaesthetix 0:3cd0a11a2f91 70 void SSH1106::setContrast(char contrast)
Anaesthetix 0:3cd0a11a2f91 71 {
Anaesthetix 0:3cd0a11a2f91 72 _lcd_cs->write(0); // enable SPI
Anaesthetix 0:3cd0a11a2f91 73 _lcd_cd->write(0); // command mode
Anaesthetix 0:3cd0a11a2f91 74 _lcd->write(0x81); // command to set contrast
Anaesthetix 0:3cd0a11a2f91 75 _lcd->write(contrast); // set contrast
Anaesthetix 0:3cd0a11a2f91 76 _lcd_cs->write(1);
Anaesthetix 0:3cd0a11a2f91 77 _lcd_cd->write(1);
Anaesthetix 1:ac9efaadd666 78 }
Anaesthetix 0:3cd0a11a2f91 79
Anaesthetix 0:3cd0a11a2f91 80 void SSH1106::setCursor(char column, char line)
Anaesthetix 0:3cd0a11a2f91 81 {
Anaesthetix 0:3cd0a11a2f91 82 int i, j;
Anaesthetix 0:3cd0a11a2f91 83 column = column+2; // column+4
Anaesthetix 0:3cd0a11a2f91 84
Anaesthetix 0:3cd0a11a2f91 85 i=(column&0xF0)>>4;
Anaesthetix 0:3cd0a11a2f91 86 j=column&0x0F;
Anaesthetix 0:3cd0a11a2f91 87 _lcd_cd->write(0);
Anaesthetix 0:3cd0a11a2f91 88 _lcd->write(0xb0+line);
Anaesthetix 0:3cd0a11a2f91 89 _lcd->write(0x10+i);
Anaesthetix 0:3cd0a11a2f91 90 _lcd->write(j);
Anaesthetix 0:3cd0a11a2f91 91 _lcd_cd->write(1);
Anaesthetix 0:3cd0a11a2f91 92 }
Anaesthetix 0:3cd0a11a2f91 93
alanchip 3:ccd49dd0621d 94
alanchip 3:ccd49dd0621d 95 void SSH1106::clear_page(int column,int page)
alanchip 3:ccd49dd0621d 96 {
alanchip 3:ccd49dd0621d 97 _lcd_cs->write(0);
alanchip 3:ccd49dd0621d 98 _lcd_cd->write(1);
alanchip 3:ccd49dd0621d 99
alanchip 3:ccd49dd0621d 100 for(unsigned short j = page; j < LCDPAGES; j++) {
alanchip 3:ccd49dd0621d 101 SSH1106::setCursor(column, j);
alanchip 3:ccd49dd0621d 102 for(unsigned short i = column; i < LCDWIDTH ; i++) {
alanchip 3:ccd49dd0621d 103 _lcd->write(0x00);
alanchip 3:ccd49dd0621d 104 }
alanchip 3:ccd49dd0621d 105 }
alanchip 3:ccd49dd0621d 106 SSH1106::setCursor(0, 0);
alanchip 3:ccd49dd0621d 107
alanchip 3:ccd49dd0621d 108 _lcd_cs->write(1);
alanchip 3:ccd49dd0621d 109 }
alanchip 3:ccd49dd0621d 110
alanchip 3:ccd49dd0621d 111 //void SSH1106::clear_line(int column, int line)
alanchip 3:ccd49dd0621d 112 //{
alanchip 3:ccd49dd0621d 113 // _lcd_cs->write(0);
alanchip 3:ccd49dd0621d 114 // _lcd_cd->write(1);
alanchip 3:ccd49dd0621d 115 //
alanchip 3:ccd49dd0621d 116 // for(unsigned short j = line; j < LCDPAGES; j++) {
alanchip 3:ccd49dd0621d 117 // SSH1106::setCursor(column, j);
alanchip 3:ccd49dd0621d 118 // for(unsigned short i = column; i < LCDWIDTH ; i++) {
alanchip 3:ccd49dd0621d 119 // _lcd->write(0x00);
alanchip 3:ccd49dd0621d 120 // }
alanchip 3:ccd49dd0621d 121 // }
alanchip 3:ccd49dd0621d 122 // SSH1106::setCursor(0, 0);
alanchip 3:ccd49dd0621d 123 //
alanchip 3:ccd49dd0621d 124 // _lcd_cs->write(1);
alanchip 3:ccd49dd0621d 125 //
alanchip 3:ccd49dd0621d 126 //}
alanchip 3:ccd49dd0621d 127
alanchip 3:ccd49dd0621d 128 void SSH1106::fillScreen()
alanchip 3:ccd49dd0621d 129 {
alanchip 3:ccd49dd0621d 130 _lcd_cs->write(0);
alanchip 3:ccd49dd0621d 131 _lcd_cd->write(1);
alanchip 3:ccd49dd0621d 132
alanchip 3:ccd49dd0621d 133 for(unsigned short j = 0; j < LCDPAGES; j++) {
alanchip 3:ccd49dd0621d 134 SSH1106::setCursor(0, j);
alanchip 3:ccd49dd0621d 135 for(unsigned short i = 0; i < LCDWIDTH ; i++) {
alanchip 3:ccd49dd0621d 136 _lcd->write(0xff);
alanchip 3:ccd49dd0621d 137 }
alanchip 3:ccd49dd0621d 138 }
alanchip 3:ccd49dd0621d 139 SSH1106::setCursor(0, 0);
alanchip 3:ccd49dd0621d 140
alanchip 3:ccd49dd0621d 141 _lcd_cs->write(1);
alanchip 3:ccd49dd0621d 142 }
alanchip 3:ccd49dd0621d 143
Anaesthetix 0:3cd0a11a2f91 144 void SSH1106::clear()
Anaesthetix 0:3cd0a11a2f91 145 {
Anaesthetix 0:3cd0a11a2f91 146 _lcd_cs->write(0);
Anaesthetix 0:3cd0a11a2f91 147 _lcd_cd->write(1);
Anaesthetix 0:3cd0a11a2f91 148
Anaesthetix 2:b55dd362afb9 149 for(unsigned short j = 0; j < LCDPAGES; j++) {
Anaesthetix 0:3cd0a11a2f91 150 SSH1106::setCursor(0, j);
Anaesthetix 2:b55dd362afb9 151 for(unsigned short i = 0; i < LCDWIDTH ; i++) {
Anaesthetix 0:3cd0a11a2f91 152 _lcd->write(0x00);
Anaesthetix 0:3cd0a11a2f91 153 }
Anaesthetix 0:3cd0a11a2f91 154 }
Anaesthetix 0:3cd0a11a2f91 155 SSH1106::setCursor(0, 0);
Anaesthetix 0:3cd0a11a2f91 156
Anaesthetix 0:3cd0a11a2f91 157 _lcd_cs->write(1);
Anaesthetix 0:3cd0a11a2f91 158 }
Anaesthetix 0:3cd0a11a2f91 159
Anaesthetix 0:3cd0a11a2f91 160 void SSH1106::writeText2d(char column, char page, const char font_address[96][8], const char *text, int size)
Anaesthetix 0:3cd0a11a2f91 161 {
Anaesthetix 0:3cd0a11a2f91 162 _lcd_cs->write(0);
Anaesthetix 0:3cd0a11a2f91 163 SSH1106::setCursor(column, page);
Anaesthetix 0:3cd0a11a2f91 164 for(int i=0; i<size; i++) {
Anaesthetix 0:3cd0a11a2f91 165 for(int a=0; a<8; a++) {
Anaesthetix 0:3cd0a11a2f91 166 _lcd->write((font_address[(text[i]-32)][a]));
Anaesthetix 0:3cd0a11a2f91 167 }
Anaesthetix 0:3cd0a11a2f91 168 }
Anaesthetix 0:3cd0a11a2f91 169 _lcd_cs->write(1);
Anaesthetix 0:3cd0a11a2f91 170 }
Anaesthetix 0:3cd0a11a2f91 171
alanchip 3:ccd49dd0621d 172 //write decimal without size
alanchip 3:ccd49dd0621d 173 void SSH1106::writeDec_format(char column, char page, const char *font_address, const int num)
alanchip 3:ccd49dd0621d 174 {
alanchip 3:ccd49dd0621d 175 char str[40];
alanchip 3:ccd49dd0621d 176 int n;
alanchip 3:ccd49dd0621d 177 if((num+1)%10 != 0) //if there is a two bit decimal
alanchip 3:ccd49dd0621d 178 {
alanchip 3:ccd49dd0621d 179 n = sprintf(str,"%d",num);
alanchip 3:ccd49dd0621d 180 SSH1106::writeText(column, page, font_address, str, n);
alanchip 3:ccd49dd0621d 181 }
alanchip 3:ccd49dd0621d 182 else
alanchip 3:ccd49dd0621d 183 {
alanchip 3:ccd49dd0621d 184 n = sprintf(str," %d",num);
alanchip 3:ccd49dd0621d 185 SSH1106::writeText(column, page, font_address, str, n);
alanchip 3:ccd49dd0621d 186 }
alanchip 3:ccd49dd0621d 187 }
alanchip 3:ccd49dd0621d 188
alanchip 4:cbc557852061 189 void SSH1106::writeTim_format(char column, char page, const char *font_address, const int num)
alanchip 4:cbc557852061 190 {
alanchip 4:cbc557852061 191 char str[40];
alanchip 4:cbc557852061 192 int n;
alanchip 4:cbc557852061 193 if((num+1)%10 != 0) //if there is a two bit decimal
alanchip 4:cbc557852061 194 {
alanchip 4:cbc557852061 195 n = sprintf(str,"%d",num);
alanchip 4:cbc557852061 196 SSH1106::writeText(column, page, font_address, str, n);
alanchip 4:cbc557852061 197 }
alanchip 4:cbc557852061 198 else
alanchip 4:cbc557852061 199 {
alanchip 4:cbc557852061 200 n = sprintf(str," %02d",num);
alanchip 4:cbc557852061 201 SSH1106::writeText(column, page, font_address, str, n);
alanchip 4:cbc557852061 202 }
alanchip 4:cbc557852061 203 }
alanchip 4:cbc557852061 204
alanchip 4:cbc557852061 205
alanchip 3:ccd49dd0621d 206 //write without size
alanchip 3:ccd49dd0621d 207 void SSH1106::writeText_format(char column, char page, const char *font_address, const char *text)
alanchip 3:ccd49dd0621d 208 {
alanchip 3:ccd49dd0621d 209 char str[40];
alanchip 3:ccd49dd0621d 210 int n;
alanchip 3:ccd49dd0621d 211 n = sprintf(str,text);
alanchip 3:ccd49dd0621d 212 SSH1106::writeText(column, page, font_address, str, n);
alanchip 3:ccd49dd0621d 213 }
alanchip 3:ccd49dd0621d 214
Anaesthetix 0:3cd0a11a2f91 215 void SSH1106::writeText(char column, char page, const char *font_address, const char *text, const uint8_t size)
Anaesthetix 0:3cd0a11a2f91 216 {
Anaesthetix 0:3cd0a11a2f91 217 // Position of character data in memory array
Anaesthetix 0:3cd0a11a2f91 218 uint16_t pos_array;
Anaesthetix 0:3cd0a11a2f91 219 // temporary column, page address, and column_cnt are used
Anaesthetix 0:3cd0a11a2f91 220 // to stay inside display area
Anaesthetix 0:3cd0a11a2f91 221 uint8_t i,y, column_cnt = 0;
Anaesthetix 0:3cd0a11a2f91 222 uint8_t count = 0;
Anaesthetix 0:3cd0a11a2f91 223
Anaesthetix 0:3cd0a11a2f91 224 // font information, needed for calculation
Anaesthetix 0:3cd0a11a2f91 225 uint8_t start_code, last_code, width, page_height, bytes_p_char;
Anaesthetix 0:3cd0a11a2f91 226
Anaesthetix 0:3cd0a11a2f91 227 uint8_t *txtbuffer;
Anaesthetix 0:3cd0a11a2f91 228
Anaesthetix 0:3cd0a11a2f91 229 start_code = font_address[2]; // get first defined character
Anaesthetix 0:3cd0a11a2f91 230 last_code = font_address[3]; // get last defined character
alanchip 3:ccd49dd0621d 231 width = font_address[4]; // width in pixel of one char 6
Anaesthetix 0:3cd0a11a2f91 232 page_height = font_address[6]; // page count per char
Anaesthetix 0:3cd0a11a2f91 233 bytes_p_char = font_address[7]; // bytes per char
Anaesthetix 0:3cd0a11a2f91 234
Anaesthetix 0:3cd0a11a2f91 235 _lcd_cs->write(0); // Enable SPI
Anaesthetix 0:3cd0a11a2f91 236 _lcd_cd->write(1); // Data mode
Anaesthetix 0:3cd0a11a2f91 237
Anaesthetix 0:3cd0a11a2f91 238 if(page_height + page > LCDPAGES) //stay inside display area
Anaesthetix 0:3cd0a11a2f91 239 page_height = LCDPAGES - page;
Anaesthetix 0:3cd0a11a2f91 240
Anaesthetix 0:3cd0a11a2f91 241 // The string is displayed character after character. If the font has more then one page,
Anaesthetix 0:3cd0a11a2f91 242 // the top page is printed first, then the next page and so on
Anaesthetix 0:3cd0a11a2f91 243 for(y = 0; y < page_height; y++) {
Anaesthetix 0:3cd0a11a2f91 244 txtbuffer = &_lcdbuffer[page*LCDWIDTH + column];
Anaesthetix 0:3cd0a11a2f91 245 column_cnt = 0; // clear column_cnt start point
Anaesthetix 0:3cd0a11a2f91 246 i = 0;
Anaesthetix 0:3cd0a11a2f91 247 while(( i < size) && ((column_cnt + column) < LCDWIDTH)) {
Anaesthetix 0:3cd0a11a2f91 248 if(text[i] < start_code || (uint8_t)text[i] > last_code) //make sure data is valid
Anaesthetix 0:3cd0a11a2f91 249 i++;
Anaesthetix 0:3cd0a11a2f91 250 else {
Anaesthetix 0:3cd0a11a2f91 251 // calculate position of ASCII character in font array
Anaesthetix 0:3cd0a11a2f91 252 // bytes for header + (ASCII - startcode) * bytes per char)
Anaesthetix 0:3cd0a11a2f91 253 pos_array = 8 + (uint8_t)(text[i++] - start_code) * bytes_p_char;
Anaesthetix 0:3cd0a11a2f91 254
Anaesthetix 0:3cd0a11a2f91 255 // get the dot pattern for the part of the char to print
Anaesthetix 0:3cd0a11a2f91 256 pos_array += y*width;
Anaesthetix 0:3cd0a11a2f91 257
Anaesthetix 0:3cd0a11a2f91 258 // stay inside display area
Anaesthetix 0:3cd0a11a2f91 259 if((column_cnt + width + column) > LCDWIDTH)
Anaesthetix 0:3cd0a11a2f91 260 column_cnt = LCDWIDTH-width;
Anaesthetix 0:3cd0a11a2f91 261
Anaesthetix 0:3cd0a11a2f91 262 // copy character data to buffer
Anaesthetix 0:3cd0a11a2f91 263 memcpy (txtbuffer+column_cnt,font_address+pos_array,width);
Anaesthetix 0:3cd0a11a2f91 264 }
Anaesthetix 0:3cd0a11a2f91 265
Anaesthetix 0:3cd0a11a2f91 266 column_cnt += width;
Anaesthetix 0:3cd0a11a2f91 267 }
Anaesthetix 0:3cd0a11a2f91 268 SSH1106::setCursor(column,page); // set start position x and y
Anaesthetix 0:3cd0a11a2f91 269
Anaesthetix 0:3cd0a11a2f91 270 do {
Anaesthetix 0:3cd0a11a2f91 271 _lcd->write(txtbuffer[count]);
Anaesthetix 0:3cd0a11a2f91 272 count++;
Anaesthetix 0:3cd0a11a2f91 273 } while ((count <= column_cnt));
Anaesthetix 0:3cd0a11a2f91 274 }
Anaesthetix 0:3cd0a11a2f91 275
Anaesthetix 0:3cd0a11a2f91 276 _lcd_cs->write(1); // Disable SPI
Anaesthetix 0:3cd0a11a2f91 277
Anaesthetix 0:3cd0a11a2f91 278 }
Anaesthetix 0:3cd0a11a2f91 279
Anaesthetix 0:3cd0a11a2f91 280 void SSH1106::drawBitmap(const char *data)
Anaesthetix 0:3cd0a11a2f91 281 {
Anaesthetix 0:3cd0a11a2f91 282 int cnt = 0;
Anaesthetix 0:3cd0a11a2f91 283 _lcd_cs->write(0);
Anaesthetix 0:3cd0a11a2f91 284 _lcd_cd->write(1);
Anaesthetix 0:3cd0a11a2f91 285 SSH1106::setCursor(0,0);
Anaesthetix 2:b55dd362afb9 286 for(int row=0; row<LCDPAGES; row++) {
Anaesthetix 0:3cd0a11a2f91 287 SSH1106::setCursor(0, row);
Anaesthetix 2:b55dd362afb9 288 for(int column=0; column<LCDWIDTH; column++) {
Anaesthetix 0:3cd0a11a2f91 289 _lcd->write(data[cnt]);
Anaesthetix 0:3cd0a11a2f91 290 cnt++;
Anaesthetix 0:3cd0a11a2f91 291 }
Anaesthetix 0:3cd0a11a2f91 292 }
Anaesthetix 1:ac9efaadd666 293 _lcd_cs->write(1);
Anaesthetix 1:ac9efaadd666 294 }
Anaesthetix 1:ac9efaadd666 295
Anaesthetix 1:ac9efaadd666 296 void SSH1106::drawLineHor(char posx, char posy, char height, char width)
Anaesthetix 1:ac9efaadd666 297 {
Anaesthetix 1:ac9efaadd666 298 char page, offset, offset2;
Anaesthetix 1:ac9efaadd666 299 char buffer[2] = {0xFF, 0xFF};
Anaesthetix 1:ac9efaadd666 300
Anaesthetix 1:ac9efaadd666 301 _lcd_cs->write(0);
Anaesthetix 1:ac9efaadd666 302 _lcd_cd->write(1);
Anaesthetix 1:ac9efaadd666 303
Anaesthetix 1:ac9efaadd666 304 if(width+posx > LCDWIDTH) width = (LCDWIDTH-posx); // keep inside display area
Anaesthetix 1:ac9efaadd666 305
Anaesthetix 1:ac9efaadd666 306 page = posy/8;
Anaesthetix 1:ac9efaadd666 307 offset = posy - (page*8);
Anaesthetix 1:ac9efaadd666 308 buffer[0] = buffer[0] >> (8-height);
Anaesthetix 1:ac9efaadd666 309 buffer[0] = buffer[0] << offset;
Anaesthetix 1:ac9efaadd666 310
Anaesthetix 1:ac9efaadd666 311 if((offset + height) > 8) {
Anaesthetix 1:ac9efaadd666 312 offset2 = ((offset+height)-8);
Anaesthetix 1:ac9efaadd666 313 buffer[1] = buffer[1] - (0xFF << (offset2));
Anaesthetix 1:ac9efaadd666 314 }
Anaesthetix 1:ac9efaadd666 315
Anaesthetix 1:ac9efaadd666 316 SSH1106::setCursor(posx, page);
Anaesthetix 1:ac9efaadd666 317
Anaesthetix 1:ac9efaadd666 318 for(int i=0; i<width; i++) _lcd->write(buffer[0]);
Anaesthetix 1:ac9efaadd666 319
Anaesthetix 1:ac9efaadd666 320 if(buffer[1] != 0xFF && (page+1) < 8) { // only write if line takes up > 1 page & keep inside display area
Anaesthetix 1:ac9efaadd666 321 SSH1106::setCursor(posx, (page+1));
Anaesthetix 1:ac9efaadd666 322 for(int i=0; i<width; i++) _lcd->write(buffer[1]);
Anaesthetix 1:ac9efaadd666 323 }
Anaesthetix 1:ac9efaadd666 324 _lcd_cs->write(1);
Anaesthetix 1:ac9efaadd666 325 }
Anaesthetix 1:ac9efaadd666 326
Anaesthetix 1:ac9efaadd666 327 void SSH1106::drawLineVert(char posx, char posy, char height, char width)
Anaesthetix 1:ac9efaadd666 328 {
Anaesthetix 1:ac9efaadd666 329 char page, pagecount, offset, offset2;
alanchip 3:ccd49dd0621d 330
Anaesthetix 1:ac9efaadd666 331 _lcd_cs->write(0);
Anaesthetix 1:ac9efaadd666 332 _lcd_cd->write(1);
Anaesthetix 1:ac9efaadd666 333
Anaesthetix 1:ac9efaadd666 334 page = posy/8;
Anaesthetix 1:ac9efaadd666 335 pagecount = height/8;
Anaesthetix 1:ac9efaadd666 336 offset2 = height - (pagecount*8);
Anaesthetix 1:ac9efaadd666 337
Anaesthetix 1:ac9efaadd666 338 SSH1106::setCursor(posx, page);
Anaesthetix 1:ac9efaadd666 339 for(int i=0; i<width; i++) _lcd->write((0xFF>>offset));
Anaesthetix 1:ac9efaadd666 340
Anaesthetix 1:ac9efaadd666 341 for(; pagecount > 1; pagecount--) {
Anaesthetix 1:ac9efaadd666 342 page++;
Anaesthetix 1:ac9efaadd666 343 SSH1106::setCursor(posx, page);
Anaesthetix 1:ac9efaadd666 344 for(int i=0; i<width; i++) _lcd->write(0xFF);
Anaesthetix 1:ac9efaadd666 345 }
Anaesthetix 1:ac9efaadd666 346
Anaesthetix 1:ac9efaadd666 347 SSH1106::setCursor(posx, (page+1));
Anaesthetix 1:ac9efaadd666 348 for(int i=0; i<width; i++) _lcd->write((0xFF<<offset2));
Anaesthetix 1:ac9efaadd666 349
Anaesthetix 1:ac9efaadd666 350 _lcd_cs->write(1);
Anaesthetix 1:ac9efaadd666 351 }
Anaesthetix 1:ac9efaadd666 352
Anaesthetix 1:ac9efaadd666 353 void SSH1106::clearBuffer(void)
Anaesthetix 1:ac9efaadd666 354 {
Anaesthetix 1:ac9efaadd666 355 for(int i=0; i<(LCDWIDTH*LCDPAGES); i++) buff[i] = 0;
Anaesthetix 1:ac9efaadd666 356 }
Anaesthetix 1:ac9efaadd666 357
Anaesthetix 1:ac9efaadd666 358 void SSH1106::update(void)
Anaesthetix 1:ac9efaadd666 359 {
Anaesthetix 1:ac9efaadd666 360 int cnt = 0;
Anaesthetix 1:ac9efaadd666 361 _lcd_cs->write(0);
Anaesthetix 1:ac9efaadd666 362 _lcd_cd->write(1);
Anaesthetix 1:ac9efaadd666 363 SSH1106::setCursor(0,0);
Anaesthetix 2:b55dd362afb9 364 for(int row=0; row<LCDPAGES; row++) {
Anaesthetix 1:ac9efaadd666 365 SSH1106::setCursor(0, row);
Anaesthetix 2:b55dd362afb9 366 for(int column=0; column<LCDWIDTH; column++) {
Anaesthetix 1:ac9efaadd666 367 _lcd->write(buff[cnt]);
Anaesthetix 1:ac9efaadd666 368 cnt++;
Anaesthetix 1:ac9efaadd666 369 }
Anaesthetix 1:ac9efaadd666 370 }
Anaesthetix 1:ac9efaadd666 371 _lcd_cs->write(1);
Anaesthetix 1:ac9efaadd666 372 }
Anaesthetix 1:ac9efaadd666 373
Anaesthetix 1:ac9efaadd666 374 void SSH1106::drawbufferLineHor(char posx, char posy, char height, char width)
Anaesthetix 1:ac9efaadd666 375 {
Anaesthetix 1:ac9efaadd666 376 char page, offset, offset2;
Anaesthetix 1:ac9efaadd666 377 int cursor;
Anaesthetix 1:ac9efaadd666 378 char buffer[2] = {0xFF, 0xFF};
Anaesthetix 1:ac9efaadd666 379
Anaesthetix 1:ac9efaadd666 380 if(width+posx > LCDWIDTH) width = (LCDWIDTH-posx); // keep inside display area
Anaesthetix 1:ac9efaadd666 381
Anaesthetix 2:b55dd362afb9 382 page = posy/LCDPAGES;
Anaesthetix 2:b55dd362afb9 383 offset = posy - (page*LCDPAGES);
Anaesthetix 1:ac9efaadd666 384 buffer[0] = buffer[0] >> (8-height);
Anaesthetix 1:ac9efaadd666 385 buffer[0] = buffer[0] << offset;
Anaesthetix 1:ac9efaadd666 386
Anaesthetix 1:ac9efaadd666 387 if((offset + height) > 8) {
Anaesthetix 1:ac9efaadd666 388 offset2 = ((offset+height)-8);
Anaesthetix 1:ac9efaadd666 389 buffer[1] = buffer[1] - (0xFF << (offset2));
Anaesthetix 1:ac9efaadd666 390 }
Anaesthetix 1:ac9efaadd666 391
Anaesthetix 2:b55dd362afb9 392 cursor = posx + (page*LCDWIDTH);
Anaesthetix 1:ac9efaadd666 393
Anaesthetix 1:ac9efaadd666 394 for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= buffer[0];
Anaesthetix 1:ac9efaadd666 395
Anaesthetix 2:b55dd362afb9 396 if(buffer[1] != 0xFF && (page+1) < LCDPAGES) { // only write if line takes up > 1 page & keep inside display area
Anaesthetix 2:b55dd362afb9 397 for(int i=0; i<width; i++) SSH1106::buff[cursor+i+LCDWIDTH] |= buffer[1];
Anaesthetix 1:ac9efaadd666 398 }
Anaesthetix 1:ac9efaadd666 399 }
Anaesthetix 1:ac9efaadd666 400
Anaesthetix 1:ac9efaadd666 401 void SSH1106::drawbufferLineVert(char posx, char posy, char height, char width)
Anaesthetix 1:ac9efaadd666 402 {
Anaesthetix 1:ac9efaadd666 403 char page, pagecount, offset, offset2;
Anaesthetix 1:ac9efaadd666 404 int cursor;
Anaesthetix 1:ac9efaadd666 405
Anaesthetix 2:b55dd362afb9 406 page = posy/LCDPAGES;
Anaesthetix 2:b55dd362afb9 407 pagecount = height/LCDPAGES;
Anaesthetix 2:b55dd362afb9 408 offset2 = height - (pagecount*LCDPAGES);
Anaesthetix 2:b55dd362afb9 409 cursor = posx + (page*LCDWIDTH); // LCDWIDTH
Anaesthetix 1:ac9efaadd666 410
Anaesthetix 1:ac9efaadd666 411 for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= (0xFF>>offset);
Anaesthetix 1:ac9efaadd666 412
Anaesthetix 1:ac9efaadd666 413 for(; pagecount > 1; pagecount--) {
Anaesthetix 1:ac9efaadd666 414 page++;
Anaesthetix 2:b55dd362afb9 415 cursor += LCDWIDTH;
Anaesthetix 1:ac9efaadd666 416 for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= 0xFF;
Anaesthetix 1:ac9efaadd666 417 }
Anaesthetix 1:ac9efaadd666 418
Anaesthetix 2:b55dd362afb9 419 cursor += LCDWIDTH;
Anaesthetix 1:ac9efaadd666 420 for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= (0xFF >> offset2);
Anaesthetix 0:3cd0a11a2f91 421 }