Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CarPakingSystem_V10
SSH1106.cpp@3:ccd49dd0621d, 2021-03-08 (annotated)
- Committer:
- alanchip
- Date:
- Mon Mar 08 02:33:45 2021 +0000
- Revision:
- 3:ccd49dd0621d
- Parent:
- 2:b55dd362afb9
; ,;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:ccd49dd0621d | 189 | //write without size |
alanchip | 3:ccd49dd0621d | 190 | void SSH1106::writeText_format(char column, char page, const char *font_address, const char *text) |
alanchip | 3:ccd49dd0621d | 191 | { |
alanchip | 3:ccd49dd0621d | 192 | char str[40]; |
alanchip | 3:ccd49dd0621d | 193 | int n; |
alanchip | 3:ccd49dd0621d | 194 | n = sprintf(str,text); |
alanchip | 3:ccd49dd0621d | 195 | SSH1106::writeText(column, page, font_address, str, n); |
alanchip | 3:ccd49dd0621d | 196 | } |
alanchip | 3:ccd49dd0621d | 197 | |
Anaesthetix | 0:3cd0a11a2f91 | 198 | void SSH1106::writeText(char column, char page, const char *font_address, const char *text, const uint8_t size) |
Anaesthetix | 0:3cd0a11a2f91 | 199 | { |
Anaesthetix | 0:3cd0a11a2f91 | 200 | // Position of character data in memory array |
Anaesthetix | 0:3cd0a11a2f91 | 201 | uint16_t pos_array; |
Anaesthetix | 0:3cd0a11a2f91 | 202 | // temporary column, page address, and column_cnt are used |
Anaesthetix | 0:3cd0a11a2f91 | 203 | // to stay inside display area |
Anaesthetix | 0:3cd0a11a2f91 | 204 | uint8_t i,y, column_cnt = 0; |
Anaesthetix | 0:3cd0a11a2f91 | 205 | uint8_t count = 0; |
Anaesthetix | 0:3cd0a11a2f91 | 206 | |
Anaesthetix | 0:3cd0a11a2f91 | 207 | // font information, needed for calculation |
Anaesthetix | 0:3cd0a11a2f91 | 208 | uint8_t start_code, last_code, width, page_height, bytes_p_char; |
Anaesthetix | 0:3cd0a11a2f91 | 209 | |
Anaesthetix | 0:3cd0a11a2f91 | 210 | uint8_t *txtbuffer; |
Anaesthetix | 0:3cd0a11a2f91 | 211 | |
Anaesthetix | 0:3cd0a11a2f91 | 212 | start_code = font_address[2]; // get first defined character |
Anaesthetix | 0:3cd0a11a2f91 | 213 | last_code = font_address[3]; // get last defined character |
alanchip | 3:ccd49dd0621d | 214 | width = font_address[4]; // width in pixel of one char 6 |
Anaesthetix | 0:3cd0a11a2f91 | 215 | page_height = font_address[6]; // page count per char |
Anaesthetix | 0:3cd0a11a2f91 | 216 | bytes_p_char = font_address[7]; // bytes per char |
Anaesthetix | 0:3cd0a11a2f91 | 217 | |
Anaesthetix | 0:3cd0a11a2f91 | 218 | _lcd_cs->write(0); // Enable SPI |
Anaesthetix | 0:3cd0a11a2f91 | 219 | _lcd_cd->write(1); // Data mode |
Anaesthetix | 0:3cd0a11a2f91 | 220 | |
Anaesthetix | 0:3cd0a11a2f91 | 221 | if(page_height + page > LCDPAGES) //stay inside display area |
Anaesthetix | 0:3cd0a11a2f91 | 222 | page_height = LCDPAGES - page; |
Anaesthetix | 0:3cd0a11a2f91 | 223 | |
Anaesthetix | 0:3cd0a11a2f91 | 224 | // The string is displayed character after character. If the font has more then one page, |
Anaesthetix | 0:3cd0a11a2f91 | 225 | // the top page is printed first, then the next page and so on |
Anaesthetix | 0:3cd0a11a2f91 | 226 | for(y = 0; y < page_height; y++) { |
Anaesthetix | 0:3cd0a11a2f91 | 227 | txtbuffer = &_lcdbuffer[page*LCDWIDTH + column]; |
Anaesthetix | 0:3cd0a11a2f91 | 228 | column_cnt = 0; // clear column_cnt start point |
Anaesthetix | 0:3cd0a11a2f91 | 229 | i = 0; |
Anaesthetix | 0:3cd0a11a2f91 | 230 | while(( i < size) && ((column_cnt + column) < LCDWIDTH)) { |
Anaesthetix | 0:3cd0a11a2f91 | 231 | if(text[i] < start_code || (uint8_t)text[i] > last_code) //make sure data is valid |
Anaesthetix | 0:3cd0a11a2f91 | 232 | i++; |
Anaesthetix | 0:3cd0a11a2f91 | 233 | else { |
Anaesthetix | 0:3cd0a11a2f91 | 234 | // calculate position of ASCII character in font array |
Anaesthetix | 0:3cd0a11a2f91 | 235 | // bytes for header + (ASCII - startcode) * bytes per char) |
Anaesthetix | 0:3cd0a11a2f91 | 236 | pos_array = 8 + (uint8_t)(text[i++] - start_code) * bytes_p_char; |
Anaesthetix | 0:3cd0a11a2f91 | 237 | |
Anaesthetix | 0:3cd0a11a2f91 | 238 | // get the dot pattern for the part of the char to print |
Anaesthetix | 0:3cd0a11a2f91 | 239 | pos_array += y*width; |
Anaesthetix | 0:3cd0a11a2f91 | 240 | |
Anaesthetix | 0:3cd0a11a2f91 | 241 | // stay inside display area |
Anaesthetix | 0:3cd0a11a2f91 | 242 | if((column_cnt + width + column) > LCDWIDTH) |
Anaesthetix | 0:3cd0a11a2f91 | 243 | column_cnt = LCDWIDTH-width; |
Anaesthetix | 0:3cd0a11a2f91 | 244 | |
Anaesthetix | 0:3cd0a11a2f91 | 245 | // copy character data to buffer |
Anaesthetix | 0:3cd0a11a2f91 | 246 | memcpy (txtbuffer+column_cnt,font_address+pos_array,width); |
Anaesthetix | 0:3cd0a11a2f91 | 247 | } |
Anaesthetix | 0:3cd0a11a2f91 | 248 | |
Anaesthetix | 0:3cd0a11a2f91 | 249 | column_cnt += width; |
Anaesthetix | 0:3cd0a11a2f91 | 250 | } |
Anaesthetix | 0:3cd0a11a2f91 | 251 | SSH1106::setCursor(column,page); // set start position x and y |
Anaesthetix | 0:3cd0a11a2f91 | 252 | |
Anaesthetix | 0:3cd0a11a2f91 | 253 | do { |
Anaesthetix | 0:3cd0a11a2f91 | 254 | _lcd->write(txtbuffer[count]); |
Anaesthetix | 0:3cd0a11a2f91 | 255 | count++; |
Anaesthetix | 0:3cd0a11a2f91 | 256 | } while ((count <= column_cnt)); |
Anaesthetix | 0:3cd0a11a2f91 | 257 | } |
Anaesthetix | 0:3cd0a11a2f91 | 258 | |
Anaesthetix | 0:3cd0a11a2f91 | 259 | _lcd_cs->write(1); // Disable SPI |
Anaesthetix | 0:3cd0a11a2f91 | 260 | |
Anaesthetix | 0:3cd0a11a2f91 | 261 | } |
Anaesthetix | 0:3cd0a11a2f91 | 262 | |
Anaesthetix | 0:3cd0a11a2f91 | 263 | void SSH1106::drawBitmap(const char *data) |
Anaesthetix | 0:3cd0a11a2f91 | 264 | { |
Anaesthetix | 0:3cd0a11a2f91 | 265 | int cnt = 0; |
Anaesthetix | 0:3cd0a11a2f91 | 266 | _lcd_cs->write(0); |
Anaesthetix | 0:3cd0a11a2f91 | 267 | _lcd_cd->write(1); |
Anaesthetix | 0:3cd0a11a2f91 | 268 | SSH1106::setCursor(0,0); |
Anaesthetix | 2:b55dd362afb9 | 269 | for(int row=0; row<LCDPAGES; row++) { |
Anaesthetix | 0:3cd0a11a2f91 | 270 | SSH1106::setCursor(0, row); |
Anaesthetix | 2:b55dd362afb9 | 271 | for(int column=0; column<LCDWIDTH; column++) { |
Anaesthetix | 0:3cd0a11a2f91 | 272 | _lcd->write(data[cnt]); |
Anaesthetix | 0:3cd0a11a2f91 | 273 | cnt++; |
Anaesthetix | 0:3cd0a11a2f91 | 274 | } |
Anaesthetix | 0:3cd0a11a2f91 | 275 | } |
Anaesthetix | 1:ac9efaadd666 | 276 | _lcd_cs->write(1); |
Anaesthetix | 1:ac9efaadd666 | 277 | } |
Anaesthetix | 1:ac9efaadd666 | 278 | |
Anaesthetix | 1:ac9efaadd666 | 279 | void SSH1106::drawLineHor(char posx, char posy, char height, char width) |
Anaesthetix | 1:ac9efaadd666 | 280 | { |
Anaesthetix | 1:ac9efaadd666 | 281 | char page, offset, offset2; |
Anaesthetix | 1:ac9efaadd666 | 282 | char buffer[2] = {0xFF, 0xFF}; |
Anaesthetix | 1:ac9efaadd666 | 283 | |
Anaesthetix | 1:ac9efaadd666 | 284 | _lcd_cs->write(0); |
Anaesthetix | 1:ac9efaadd666 | 285 | _lcd_cd->write(1); |
Anaesthetix | 1:ac9efaadd666 | 286 | |
Anaesthetix | 1:ac9efaadd666 | 287 | if(width+posx > LCDWIDTH) width = (LCDWIDTH-posx); // keep inside display area |
Anaesthetix | 1:ac9efaadd666 | 288 | |
Anaesthetix | 1:ac9efaadd666 | 289 | page = posy/8; |
Anaesthetix | 1:ac9efaadd666 | 290 | offset = posy - (page*8); |
Anaesthetix | 1:ac9efaadd666 | 291 | buffer[0] = buffer[0] >> (8-height); |
Anaesthetix | 1:ac9efaadd666 | 292 | buffer[0] = buffer[0] << offset; |
Anaesthetix | 1:ac9efaadd666 | 293 | |
Anaesthetix | 1:ac9efaadd666 | 294 | if((offset + height) > 8) { |
Anaesthetix | 1:ac9efaadd666 | 295 | offset2 = ((offset+height)-8); |
Anaesthetix | 1:ac9efaadd666 | 296 | buffer[1] = buffer[1] - (0xFF << (offset2)); |
Anaesthetix | 1:ac9efaadd666 | 297 | } |
Anaesthetix | 1:ac9efaadd666 | 298 | |
Anaesthetix | 1:ac9efaadd666 | 299 | SSH1106::setCursor(posx, page); |
Anaesthetix | 1:ac9efaadd666 | 300 | |
Anaesthetix | 1:ac9efaadd666 | 301 | for(int i=0; i<width; i++) _lcd->write(buffer[0]); |
Anaesthetix | 1:ac9efaadd666 | 302 | |
Anaesthetix | 1:ac9efaadd666 | 303 | if(buffer[1] != 0xFF && (page+1) < 8) { // only write if line takes up > 1 page & keep inside display area |
Anaesthetix | 1:ac9efaadd666 | 304 | SSH1106::setCursor(posx, (page+1)); |
Anaesthetix | 1:ac9efaadd666 | 305 | for(int i=0; i<width; i++) _lcd->write(buffer[1]); |
Anaesthetix | 1:ac9efaadd666 | 306 | } |
Anaesthetix | 1:ac9efaadd666 | 307 | _lcd_cs->write(1); |
Anaesthetix | 1:ac9efaadd666 | 308 | } |
Anaesthetix | 1:ac9efaadd666 | 309 | |
Anaesthetix | 1:ac9efaadd666 | 310 | void SSH1106::drawLineVert(char posx, char posy, char height, char width) |
Anaesthetix | 1:ac9efaadd666 | 311 | { |
Anaesthetix | 1:ac9efaadd666 | 312 | char page, pagecount, offset, offset2; |
alanchip | 3:ccd49dd0621d | 313 | |
Anaesthetix | 1:ac9efaadd666 | 314 | _lcd_cs->write(0); |
Anaesthetix | 1:ac9efaadd666 | 315 | _lcd_cd->write(1); |
Anaesthetix | 1:ac9efaadd666 | 316 | |
Anaesthetix | 1:ac9efaadd666 | 317 | page = posy/8; |
Anaesthetix | 1:ac9efaadd666 | 318 | pagecount = height/8; |
Anaesthetix | 1:ac9efaadd666 | 319 | offset2 = height - (pagecount*8); |
Anaesthetix | 1:ac9efaadd666 | 320 | |
Anaesthetix | 1:ac9efaadd666 | 321 | SSH1106::setCursor(posx, page); |
Anaesthetix | 1:ac9efaadd666 | 322 | for(int i=0; i<width; i++) _lcd->write((0xFF>>offset)); |
Anaesthetix | 1:ac9efaadd666 | 323 | |
Anaesthetix | 1:ac9efaadd666 | 324 | for(; pagecount > 1; pagecount--) { |
Anaesthetix | 1:ac9efaadd666 | 325 | page++; |
Anaesthetix | 1:ac9efaadd666 | 326 | SSH1106::setCursor(posx, page); |
Anaesthetix | 1:ac9efaadd666 | 327 | for(int i=0; i<width; i++) _lcd->write(0xFF); |
Anaesthetix | 1:ac9efaadd666 | 328 | } |
Anaesthetix | 1:ac9efaadd666 | 329 | |
Anaesthetix | 1:ac9efaadd666 | 330 | SSH1106::setCursor(posx, (page+1)); |
Anaesthetix | 1:ac9efaadd666 | 331 | for(int i=0; i<width; i++) _lcd->write((0xFF<<offset2)); |
Anaesthetix | 1:ac9efaadd666 | 332 | |
Anaesthetix | 1:ac9efaadd666 | 333 | _lcd_cs->write(1); |
Anaesthetix | 1:ac9efaadd666 | 334 | } |
Anaesthetix | 1:ac9efaadd666 | 335 | |
Anaesthetix | 1:ac9efaadd666 | 336 | void SSH1106::clearBuffer(void) |
Anaesthetix | 1:ac9efaadd666 | 337 | { |
Anaesthetix | 1:ac9efaadd666 | 338 | for(int i=0; i<(LCDWIDTH*LCDPAGES); i++) buff[i] = 0; |
Anaesthetix | 1:ac9efaadd666 | 339 | } |
Anaesthetix | 1:ac9efaadd666 | 340 | |
Anaesthetix | 1:ac9efaadd666 | 341 | void SSH1106::update(void) |
Anaesthetix | 1:ac9efaadd666 | 342 | { |
Anaesthetix | 1:ac9efaadd666 | 343 | int cnt = 0; |
Anaesthetix | 1:ac9efaadd666 | 344 | _lcd_cs->write(0); |
Anaesthetix | 1:ac9efaadd666 | 345 | _lcd_cd->write(1); |
Anaesthetix | 1:ac9efaadd666 | 346 | SSH1106::setCursor(0,0); |
Anaesthetix | 2:b55dd362afb9 | 347 | for(int row=0; row<LCDPAGES; row++) { |
Anaesthetix | 1:ac9efaadd666 | 348 | SSH1106::setCursor(0, row); |
Anaesthetix | 2:b55dd362afb9 | 349 | for(int column=0; column<LCDWIDTH; column++) { |
Anaesthetix | 1:ac9efaadd666 | 350 | _lcd->write(buff[cnt]); |
Anaesthetix | 1:ac9efaadd666 | 351 | cnt++; |
Anaesthetix | 1:ac9efaadd666 | 352 | } |
Anaesthetix | 1:ac9efaadd666 | 353 | } |
Anaesthetix | 1:ac9efaadd666 | 354 | _lcd_cs->write(1); |
Anaesthetix | 1:ac9efaadd666 | 355 | } |
Anaesthetix | 1:ac9efaadd666 | 356 | |
Anaesthetix | 1:ac9efaadd666 | 357 | void SSH1106::drawbufferLineHor(char posx, char posy, char height, char width) |
Anaesthetix | 1:ac9efaadd666 | 358 | { |
Anaesthetix | 1:ac9efaadd666 | 359 | char page, offset, offset2; |
Anaesthetix | 1:ac9efaadd666 | 360 | int cursor; |
Anaesthetix | 1:ac9efaadd666 | 361 | char buffer[2] = {0xFF, 0xFF}; |
Anaesthetix | 1:ac9efaadd666 | 362 | |
Anaesthetix | 1:ac9efaadd666 | 363 | if(width+posx > LCDWIDTH) width = (LCDWIDTH-posx); // keep inside display area |
Anaesthetix | 1:ac9efaadd666 | 364 | |
Anaesthetix | 2:b55dd362afb9 | 365 | page = posy/LCDPAGES; |
Anaesthetix | 2:b55dd362afb9 | 366 | offset = posy - (page*LCDPAGES); |
Anaesthetix | 1:ac9efaadd666 | 367 | buffer[0] = buffer[0] >> (8-height); |
Anaesthetix | 1:ac9efaadd666 | 368 | buffer[0] = buffer[0] << offset; |
Anaesthetix | 1:ac9efaadd666 | 369 | |
Anaesthetix | 1:ac9efaadd666 | 370 | if((offset + height) > 8) { |
Anaesthetix | 1:ac9efaadd666 | 371 | offset2 = ((offset+height)-8); |
Anaesthetix | 1:ac9efaadd666 | 372 | buffer[1] = buffer[1] - (0xFF << (offset2)); |
Anaesthetix | 1:ac9efaadd666 | 373 | } |
Anaesthetix | 1:ac9efaadd666 | 374 | |
Anaesthetix | 2:b55dd362afb9 | 375 | cursor = posx + (page*LCDWIDTH); |
Anaesthetix | 1:ac9efaadd666 | 376 | |
Anaesthetix | 1:ac9efaadd666 | 377 | for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= buffer[0]; |
Anaesthetix | 1:ac9efaadd666 | 378 | |
Anaesthetix | 2:b55dd362afb9 | 379 | if(buffer[1] != 0xFF && (page+1) < LCDPAGES) { // only write if line takes up > 1 page & keep inside display area |
Anaesthetix | 2:b55dd362afb9 | 380 | for(int i=0; i<width; i++) SSH1106::buff[cursor+i+LCDWIDTH] |= buffer[1]; |
Anaesthetix | 1:ac9efaadd666 | 381 | } |
Anaesthetix | 1:ac9efaadd666 | 382 | } |
Anaesthetix | 1:ac9efaadd666 | 383 | |
Anaesthetix | 1:ac9efaadd666 | 384 | void SSH1106::drawbufferLineVert(char posx, char posy, char height, char width) |
Anaesthetix | 1:ac9efaadd666 | 385 | { |
Anaesthetix | 1:ac9efaadd666 | 386 | char page, pagecount, offset, offset2; |
Anaesthetix | 1:ac9efaadd666 | 387 | int cursor; |
Anaesthetix | 1:ac9efaadd666 | 388 | |
Anaesthetix | 2:b55dd362afb9 | 389 | page = posy/LCDPAGES; |
Anaesthetix | 2:b55dd362afb9 | 390 | pagecount = height/LCDPAGES; |
Anaesthetix | 2:b55dd362afb9 | 391 | offset2 = height - (pagecount*LCDPAGES); |
Anaesthetix | 2:b55dd362afb9 | 392 | cursor = posx + (page*LCDWIDTH); // LCDWIDTH |
Anaesthetix | 1:ac9efaadd666 | 393 | |
Anaesthetix | 1:ac9efaadd666 | 394 | for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= (0xFF>>offset); |
Anaesthetix | 1:ac9efaadd666 | 395 | |
Anaesthetix | 1:ac9efaadd666 | 396 | for(; pagecount > 1; pagecount--) { |
Anaesthetix | 1:ac9efaadd666 | 397 | page++; |
Anaesthetix | 2:b55dd362afb9 | 398 | cursor += LCDWIDTH; |
Anaesthetix | 1:ac9efaadd666 | 399 | for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= 0xFF; |
Anaesthetix | 1:ac9efaadd666 | 400 | } |
Anaesthetix | 1:ac9efaadd666 | 401 | |
Anaesthetix | 2:b55dd362afb9 | 402 | cursor += LCDWIDTH; |
Anaesthetix | 1:ac9efaadd666 | 403 | for(int i=0; i<width; i++) SSH1106::buff[cursor+i] |= (0xFF >> offset2); |
Anaesthetix | 0:3cd0a11a2f91 | 404 | } |