Simple library for UC1701 based GLCD's

Dependents:   Opensmart_LCD_UC1701

With lots of fonts! Will include more in the future. A couple bitmaps have also been added.

Committer:
Anaesthetix
Date:
Sun Dec 18 21:10:26 2016 +0000
Revision:
0:a8cfaf48d064
Child:
1:f396898c2963
Optimized for execution speed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:a8cfaf48d064 1 /**
Anaesthetix 0:a8cfaf48d064 2 * This is a simple library for UC1701 controlled graphic LCD's.
Anaesthetix 0:a8cfaf48d064 3 * Written for a cheap OPEN-SMART 1.8" 128x64 display
Anaesthetix 0:a8cfaf48d064 4 * See http://www.dx.com/p/open-smart-1-8-128-64-lcd-display-breakout-module-w-blue-backlit-444694
Anaesthetix 0:a8cfaf48d064 5
Anaesthetix 0:a8cfaf48d064 6 * Written by: Erik van de Coevering
Anaesthetix 0:a8cfaf48d064 7 * With special thanks to Tim Barr from whom I've reused some code
Anaesthetix 0:a8cfaf48d064 8 * Use this code in whatever way you like, as long as it stays free of charge!
Anaesthetix 0:a8cfaf48d064 9 *
Anaesthetix 0:a8cfaf48d064 10 * Copyright (c) 2016
Anaesthetix 0:a8cfaf48d064 11 *
Anaesthetix 0:a8cfaf48d064 12 * Licensed under the Apache License, Version 2.0 (the "License");
Anaesthetix 0:a8cfaf48d064 13 * you may not use this file except in compliance with the License.
Anaesthetix 0:a8cfaf48d064 14 * You may obtain a copy of the License at
Anaesthetix 0:a8cfaf48d064 15 *
Anaesthetix 0:a8cfaf48d064 16 * http://www.apache.org/licenses/LICENSE-2.0
Anaesthetix 0:a8cfaf48d064 17 *
Anaesthetix 0:a8cfaf48d064 18 * Unless required by applicable law or agreed to in writing, software
Anaesthetix 0:a8cfaf48d064 19 * distributed under the License is distributed on an "AS IS" BASIS,
Anaesthetix 0:a8cfaf48d064 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Anaesthetix 0:a8cfaf48d064 21 * See the License for the specific language governing permissions and
Anaesthetix 0:a8cfaf48d064 22 * limitations under the License.
Anaesthetix 0:a8cfaf48d064 23 *
Anaesthetix 0:a8cfaf48d064 24 *
Anaesthetix 0:a8cfaf48d064 25 */
Anaesthetix 0:a8cfaf48d064 26
Anaesthetix 0:a8cfaf48d064 27 #include "UC1701.h"
Anaesthetix 0:a8cfaf48d064 28
Anaesthetix 0:a8cfaf48d064 29 UC1701::UC1701(SPI &lcd, DigitalOut &lcd_cs, DigitalOut &lcd_cd)
Anaesthetix 0:a8cfaf48d064 30 {
Anaesthetix 0:a8cfaf48d064 31 _lcd = &lcd;
Anaesthetix 0:a8cfaf48d064 32 _lcd_cs = &lcd_cs;
Anaesthetix 0:a8cfaf48d064 33 _lcd_cd = &lcd_cd;
Anaesthetix 0:a8cfaf48d064 34 }
Anaesthetix 0:a8cfaf48d064 35
Anaesthetix 0:a8cfaf48d064 36 void UC1701::init()
Anaesthetix 0:a8cfaf48d064 37 {
Anaesthetix 0:a8cfaf48d064 38 _lcd_cs->write(1);
Anaesthetix 0:a8cfaf48d064 39 _lcd->frequency(24000000); // Abusing SPI to save some time..
Anaesthetix 0:a8cfaf48d064 40 _lcd->format(8,3);
Anaesthetix 0:a8cfaf48d064 41 _lcd_cs->write(0); // enable SPI
Anaesthetix 0:a8cfaf48d064 42 _lcd_cd->write(0); // COMMAND mode
Anaesthetix 0:a8cfaf48d064 43
Anaesthetix 0:a8cfaf48d064 44 wait_ms(50);
Anaesthetix 0:a8cfaf48d064 45
Anaesthetix 0:a8cfaf48d064 46 _lcd->write(0xE2); // Reset
Anaesthetix 0:a8cfaf48d064 47 _lcd->write(0x40); // Set display start line to 0
Anaesthetix 0:a8cfaf48d064 48 _lcd->write(0xA1); // Set SEG direction
Anaesthetix 0:a8cfaf48d064 49 _lcd->write(0xC0); // Set COM direction
Anaesthetix 0:a8cfaf48d064 50 _lcd->write(0xA2); // Set bias = 1/9
Anaesthetix 0:a8cfaf48d064 51 _lcd->write(0x2C); // Boost ON
Anaesthetix 0:a8cfaf48d064 52 _lcd->write(0x2E); // Voltage regulator on
Anaesthetix 0:a8cfaf48d064 53 _lcd->write(0x2F); // Voltage follower on
Anaesthetix 0:a8cfaf48d064 54 _lcd->write(0xF8); // Set booster ratio
Anaesthetix 0:a8cfaf48d064 55 _lcd->write(0x00); // to 4
Anaesthetix 0:a8cfaf48d064 56 _lcd->write(0x23); // Set resistor ratio
Anaesthetix 0:a8cfaf48d064 57 _lcd->write(0x81); // to 3
Anaesthetix 0:a8cfaf48d064 58 _lcd->write(0x28); // Set Electronic volume to 40
Anaesthetix 0:a8cfaf48d064 59 _lcd->write(0xEE); // Set cursor update ON -> after write, column cursor will be updated (rows will not!)
Anaesthetix 0:a8cfaf48d064 60 _lcd->write(0xAC); // Disable static indicator
Anaesthetix 0:a8cfaf48d064 61 _lcd->write(0x00);
Anaesthetix 0:a8cfaf48d064 62 _lcd->write(0xA6); // Disable inverse
Anaesthetix 0:a8cfaf48d064 63 _lcd->write(0xAF); // Display enable
Anaesthetix 0:a8cfaf48d064 64 //_lcd->write(0xA5); // display all points
Anaesthetix 0:a8cfaf48d064 65 _lcd->write(0xA4); // clear
Anaesthetix 0:a8cfaf48d064 66 _lcd_cs->write(1); // disable SPI
Anaesthetix 0:a8cfaf48d064 67 _lcd_cd->write(1); // DATA mode
Anaesthetix 0:a8cfaf48d064 68
Anaesthetix 0:a8cfaf48d064 69 }
Anaesthetix 0:a8cfaf48d064 70
Anaesthetix 0:a8cfaf48d064 71 void UC1701::setCursor(char column, char line)
Anaesthetix 0:a8cfaf48d064 72 {
Anaesthetix 0:a8cfaf48d064 73 int i, j;
Anaesthetix 0:a8cfaf48d064 74 column = column+4;
Anaesthetix 0:a8cfaf48d064 75
Anaesthetix 0:a8cfaf48d064 76 i=(column&0xF0)>>4;
Anaesthetix 0:a8cfaf48d064 77 j=column&0x0F;
Anaesthetix 0:a8cfaf48d064 78 _lcd_cd->write(0);
Anaesthetix 0:a8cfaf48d064 79 _lcd->write(0xb0+line);
Anaesthetix 0:a8cfaf48d064 80 _lcd->write(0x10+i);
Anaesthetix 0:a8cfaf48d064 81 _lcd->write(j);
Anaesthetix 0:a8cfaf48d064 82 _lcd_cd->write(1);
Anaesthetix 0:a8cfaf48d064 83 }
Anaesthetix 0:a8cfaf48d064 84
Anaesthetix 0:a8cfaf48d064 85 void UC1701::clear()
Anaesthetix 0:a8cfaf48d064 86 {
Anaesthetix 0:a8cfaf48d064 87 _lcd_cs->write(0);
Anaesthetix 0:a8cfaf48d064 88 _lcd_cd->write(1);
Anaesthetix 0:a8cfaf48d064 89
Anaesthetix 0:a8cfaf48d064 90 for(unsigned short j = 0; j < 8; j++) {
Anaesthetix 0:a8cfaf48d064 91 UC1701::setCursor(0, j);
Anaesthetix 0:a8cfaf48d064 92 for(unsigned short i = 0; i < 128 ; i++) {
Anaesthetix 0:a8cfaf48d064 93 _lcd->write(0x00);
Anaesthetix 0:a8cfaf48d064 94 }
Anaesthetix 0:a8cfaf48d064 95 }
Anaesthetix 0:a8cfaf48d064 96
Anaesthetix 0:a8cfaf48d064 97 UC1701::setCursor(0, 0);
Anaesthetix 0:a8cfaf48d064 98
Anaesthetix 0:a8cfaf48d064 99 _lcd_cs->write(1);
Anaesthetix 0:a8cfaf48d064 100 }
Anaesthetix 0:a8cfaf48d064 101
Anaesthetix 0:a8cfaf48d064 102 // fill() only used to optimize the library. Using command 0xA5 is faster, if needed.
Anaesthetix 0:a8cfaf48d064 103 void UC1701::fill()
Anaesthetix 0:a8cfaf48d064 104 {
Anaesthetix 0:a8cfaf48d064 105 _lcd_cs->write(0);
Anaesthetix 0:a8cfaf48d064 106 _lcd_cd->write(1);
Anaesthetix 0:a8cfaf48d064 107
Anaesthetix 0:a8cfaf48d064 108 for(unsigned short j = 0; j < 8; j++) {
Anaesthetix 0:a8cfaf48d064 109 UC1701::setCursor(0, j);
Anaesthetix 0:a8cfaf48d064 110 for(unsigned short i = 0; i < 128 ; i++) {
Anaesthetix 0:a8cfaf48d064 111 _lcd->write(0xFF);
Anaesthetix 0:a8cfaf48d064 112 }
Anaesthetix 0:a8cfaf48d064 113 }
Anaesthetix 0:a8cfaf48d064 114
Anaesthetix 0:a8cfaf48d064 115 UC1701::setCursor(0, 0);
Anaesthetix 0:a8cfaf48d064 116
Anaesthetix 0:a8cfaf48d064 117 _lcd_cs->write(1);
Anaesthetix 0:a8cfaf48d064 118 }
Anaesthetix 0:a8cfaf48d064 119
Anaesthetix 0:a8cfaf48d064 120 void UC1701::writeText2d(char column, char page, const char font_address[96][8], const char *text, int size)
Anaesthetix 0:a8cfaf48d064 121 {
Anaesthetix 0:a8cfaf48d064 122 _lcd_cs->write(0);
Anaesthetix 0:a8cfaf48d064 123 UC1701::setCursor(column, page);
Anaesthetix 0:a8cfaf48d064 124 for(int i=0; i<=size; i++) {
Anaesthetix 0:a8cfaf48d064 125 for(int a=0; a<8; a++) {
Anaesthetix 0:a8cfaf48d064 126 _lcd->write((font_address[(text[i]-32)][a]));
Anaesthetix 0:a8cfaf48d064 127 }
Anaesthetix 0:a8cfaf48d064 128 }
Anaesthetix 0:a8cfaf48d064 129 _lcd_cs->write(1);
Anaesthetix 0:a8cfaf48d064 130 }
Anaesthetix 0:a8cfaf48d064 131
Anaesthetix 0:a8cfaf48d064 132 void UC1701::writeText(char column, char page, const char *font_address, const char *text, const uint8_t size)
Anaesthetix 0:a8cfaf48d064 133 {
Anaesthetix 0:a8cfaf48d064 134 // Position of character data in memory array
Anaesthetix 0:a8cfaf48d064 135 uint16_t pos_array;
Anaesthetix 0:a8cfaf48d064 136 // temporary column, page address, and column_cnt are used
Anaesthetix 0:a8cfaf48d064 137 // to stay inside display area
Anaesthetix 0:a8cfaf48d064 138 uint8_t i,y, column_cnt = 0;
Anaesthetix 0:a8cfaf48d064 139 uint8_t count = 0;
Anaesthetix 0:a8cfaf48d064 140
Anaesthetix 0:a8cfaf48d064 141 // font information, needed for calculation
Anaesthetix 0:a8cfaf48d064 142 uint8_t start_code, last_code, width, page_height, bytes_p_char;
Anaesthetix 0:a8cfaf48d064 143
Anaesthetix 0:a8cfaf48d064 144 uint8_t *txtbuffer;
Anaesthetix 0:a8cfaf48d064 145
Anaesthetix 0:a8cfaf48d064 146 start_code = font_address[2]; // get first defined character
Anaesthetix 0:a8cfaf48d064 147 last_code = font_address[3]; // get last defined character
Anaesthetix 0:a8cfaf48d064 148 width = font_address[4]; // width in pixel of one char
Anaesthetix 0:a8cfaf48d064 149 page_height = font_address[6]; // page count per char
Anaesthetix 0:a8cfaf48d064 150 bytes_p_char = font_address[7]; // bytes per char
Anaesthetix 0:a8cfaf48d064 151
Anaesthetix 0:a8cfaf48d064 152 _lcd_cs->write(0); // Enable SPI
Anaesthetix 0:a8cfaf48d064 153 _lcd_cd->write(1); // Data mode
Anaesthetix 0:a8cfaf48d064 154
Anaesthetix 0:a8cfaf48d064 155 if(page_height + page > LCDPAGES) //stay inside display area
Anaesthetix 0:a8cfaf48d064 156 page_height = LCDPAGES - page;
Anaesthetix 0:a8cfaf48d064 157
Anaesthetix 0:a8cfaf48d064 158 // The string is displayed character after character. If the font has more then one page,
Anaesthetix 0:a8cfaf48d064 159 // the top page is printed first, then the next page and so on
Anaesthetix 0:a8cfaf48d064 160 for(y = 0; y < page_height; y++) {
Anaesthetix 0:a8cfaf48d064 161 txtbuffer = &_lcdbuffer[page*LCDWIDTH + column];
Anaesthetix 0:a8cfaf48d064 162 column_cnt = 0; // clear column_cnt start point
Anaesthetix 0:a8cfaf48d064 163 i = 0;
Anaesthetix 0:a8cfaf48d064 164 while(( i < size) && ((column_cnt + column) < LCDWIDTH)) {
Anaesthetix 0:a8cfaf48d064 165 if(text[i] < start_code || (uint8_t)text[i] > last_code) //make sure data is valid
Anaesthetix 0:a8cfaf48d064 166 i++;
Anaesthetix 0:a8cfaf48d064 167 else {
Anaesthetix 0:a8cfaf48d064 168 // calculate position of ASCII character in font array
Anaesthetix 0:a8cfaf48d064 169 // bytes for header + (ASCII - startcode) * bytes per char)
Anaesthetix 0:a8cfaf48d064 170 pos_array = 8 + (uint8_t)(text[i++] - start_code) * bytes_p_char;
Anaesthetix 0:a8cfaf48d064 171
Anaesthetix 0:a8cfaf48d064 172 // get the dot pattern for the part of the char to print
Anaesthetix 0:a8cfaf48d064 173 pos_array += y*width;
Anaesthetix 0:a8cfaf48d064 174
Anaesthetix 0:a8cfaf48d064 175 // stay inside display area
Anaesthetix 0:a8cfaf48d064 176 if((column_cnt + width + column) > LCDWIDTH)
Anaesthetix 0:a8cfaf48d064 177 column_cnt = LCDWIDTH-width;
Anaesthetix 0:a8cfaf48d064 178
Anaesthetix 0:a8cfaf48d064 179 // copy character data to buffer
Anaesthetix 0:a8cfaf48d064 180 memcpy (txtbuffer+column_cnt,font_address+pos_array,width);
Anaesthetix 0:a8cfaf48d064 181 }
Anaesthetix 0:a8cfaf48d064 182
Anaesthetix 0:a8cfaf48d064 183 column_cnt += width;
Anaesthetix 0:a8cfaf48d064 184 }
Anaesthetix 0:a8cfaf48d064 185 UC1701::setCursor(column,page); // set start position x and y
Anaesthetix 0:a8cfaf48d064 186
Anaesthetix 0:a8cfaf48d064 187 do {
Anaesthetix 0:a8cfaf48d064 188 _lcd->write(txtbuffer[count]);
Anaesthetix 0:a8cfaf48d064 189 count++;
Anaesthetix 0:a8cfaf48d064 190 } while ((count <= column_cnt));
Anaesthetix 0:a8cfaf48d064 191 }
Anaesthetix 0:a8cfaf48d064 192
Anaesthetix 0:a8cfaf48d064 193 _lcd_cs->write(1); // Disable SPI
Anaesthetix 0:a8cfaf48d064 194
Anaesthetix 0:a8cfaf48d064 195 }
Anaesthetix 0:a8cfaf48d064 196
Anaesthetix 0:a8cfaf48d064 197 void UC1701::drawBitmap(const char *data)
Anaesthetix 0:a8cfaf48d064 198 {
Anaesthetix 0:a8cfaf48d064 199 int cnt = 0;
Anaesthetix 0:a8cfaf48d064 200 _lcd_cs->write(0);
Anaesthetix 0:a8cfaf48d064 201 _lcd_cd->write(1);
Anaesthetix 0:a8cfaf48d064 202 UC1701::setCursor(0,0);
Anaesthetix 0:a8cfaf48d064 203 for(int row=0; row<8; row++) {
Anaesthetix 0:a8cfaf48d064 204 UC1701::setCursor(0, row);
Anaesthetix 0:a8cfaf48d064 205 for(int column=0; column<128; column++) {
Anaesthetix 0:a8cfaf48d064 206 _lcd->write(data[cnt]);
Anaesthetix 0:a8cfaf48d064 207 cnt++;
Anaesthetix 0:a8cfaf48d064 208 }
Anaesthetix 0:a8cfaf48d064 209 }
Anaesthetix 0:a8cfaf48d064 210 }