Driver for the Digole Serial universal LCD display adapter
Embed:
(wiki syntax)
Show/hide line numbers
DigoleSerialDisp.cpp
00001 /** Digole Serial Display library 00002 * 00003 * @Author: Digole Digital Solutions : www.digole.com ported to mbed by Michael Shimniok www.bot-thoughts.com 00004 */ 00005 00006 #include "mbed.h" 00007 #include "DigoleSerialDisp.h" 00008 #include <stdio.h> 00009 #include <string.h> 00010 #include <inttypes.h> 00011 00012 char null = 0; 00013 00014 00015 // that resetting the Arduino doesn't reset the LCD, so we 00016 // can't assume that its in that state when a sketch starts (and the 00017 // LiquidCrystal constructor is called). 00018 00019 //UART function 00020 00021 DigoleSerialDisp::DigoleSerialDisp(PinName sda, PinName scl, uint8_t address): 00022 _device(sda, scl) 00023 { 00024 _address = (address<<1); 00025 _device.frequency(100000); 00026 _Comdelay=70; 00027 } 00028 00029 size_t DigoleSerialDisp::write(const char x) 00030 { 00031 _device.write(_address, (char *) &x, 1); 00032 00033 return 1; 00034 } 00035 00036 size_t DigoleSerialDisp::write(const char *str) 00037 { 00038 if (str == NULL) return 0; 00039 return write(str, strlen(str)); 00040 } 00041 00042 size_t DigoleSerialDisp::write(const char *buffer, size_t size) 00043 { 00044 int len = 0; 00045 if (buffer != NULL) { 00046 _device.write(_address, (char *) buffer, size); 00047 len = size; 00048 delay(7); 00049 } 00050 return len; 00051 } 00052 00053 00054 size_t DigoleSerialDisp::print(const char c) 00055 { 00056 buf[0] = 'T'; 00057 buf[1] = 'T'; 00058 buf[2] = c; 00059 buf[3] = 0; 00060 write(buf); 00061 write(null); 00062 return 1; 00063 } 00064 00065 size_t DigoleSerialDisp::print(const char s[]) 00066 { 00067 int len = strlen(s); 00068 00069 if (s == NULL) return 0; 00070 00071 buf[0] = 'T'; 00072 buf[1] = 'T'; 00073 buf[2] = 0; 00074 strncat(buf, s, 125); 00075 write(buf); 00076 write(null); 00077 return len; 00078 } 00079 00080 size_t DigoleSerialDisp::println(const char s[]) 00081 { 00082 return print(s); 00083 } 00084 00085 /* 00086 Print.cpp - Base class that provides print() and println() 00087 Copyright (c) 2008 David A. Mellis. All right reserved. 00088 00089 This library is free software; you can redistribute it and/or 00090 modify it under the terms of the GNU Lesser General Public 00091 License as published by the Free Software Foundation; either 00092 version 2.1 of the License, or (at your option) any later version. 00093 00094 This library is distributed in the hope that it will be useful, 00095 but WITHOUT ANY WARRANTY; without even the implied warranty of 00096 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00097 Lesser General Public License for more details. 00098 00099 You should have received a copy of the GNU Lesser General Public 00100 License along with this library; if not, write to the Free Software 00101 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00102 00103 Modified 23 November 2006 by David A. Mellis 00104 */ 00105 00106 size_t DigoleSerialDisp::print(unsigned char b, int base) 00107 { 00108 return print((unsigned long) b, base); 00109 } 00110 00111 size_t DigoleSerialDisp::print(int n, int base) 00112 { 00113 return print((long) n, base); 00114 } 00115 00116 size_t DigoleSerialDisp::print(unsigned int n, int base) 00117 { 00118 return print((unsigned long) n, base); 00119 } 00120 00121 size_t DigoleSerialDisp::print(long n, int base) 00122 { 00123 if (base == 0) { 00124 return write(n); 00125 } else if (base == 10) { 00126 if (n < 0) { 00127 int t = print('-'); 00128 n = -n; 00129 return printNumber(n, 10) + t; 00130 } 00131 return printNumber(n, 10); 00132 } else { 00133 return printNumber(n, base); 00134 } 00135 } 00136 00137 size_t DigoleSerialDisp::print(unsigned long n, int base) 00138 { 00139 if (base == 0) return write(n); 00140 else return printNumber(n, base); 00141 } 00142 00143 size_t DigoleSerialDisp::print(double n, int digits) 00144 { 00145 return printFloat(n, digits); 00146 } 00147 00148 size_t DigoleSerialDisp::println(unsigned char b, int base) 00149 { 00150 size_t n = print(b, base); 00151 n += println(); 00152 return n; 00153 } 00154 00155 size_t DigoleSerialDisp::println(int num, int base) 00156 { 00157 size_t n = print(num, base); 00158 n += println(); 00159 return n; 00160 } 00161 00162 size_t DigoleSerialDisp::println(unsigned int num, int base) 00163 { 00164 size_t n = print(num, base); 00165 n += println(); 00166 return n; 00167 } 00168 00169 size_t DigoleSerialDisp::println(long num, int base) 00170 { 00171 size_t n = print(num, base); 00172 n += println(); 00173 return n; 00174 } 00175 00176 size_t DigoleSerialDisp::println(unsigned long num, int base) 00177 { 00178 size_t n = print(num, base); 00179 n += println(); 00180 return n; 00181 } 00182 00183 size_t DigoleSerialDisp::println(double num, int digits) 00184 { 00185 size_t n = print(num, digits); 00186 n += println(); 00187 return n; 00188 } 00189 00190 size_t DigoleSerialDisp::println(void) 00191 { 00192 return 1; 00193 } 00194 00195 // Private Methods ///////////////////////////////////////////////////////////// 00196 00197 size_t DigoleSerialDisp::printNumber(unsigned long n, uint8_t base) { 00198 char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. 00199 char *str = &buf[sizeof(buf) - 1]; 00200 00201 *str = '\0'; 00202 00203 // prevent crash if called with base == 1 00204 if (base < 2) base = 10; 00205 00206 do { 00207 unsigned long m = n; 00208 n /= base; 00209 char c = m - base * n; 00210 *--str = c < 10 ? c + '0' : c + 'A' - 10; 00211 } while(n); 00212 00213 return write(str); 00214 } 00215 00216 size_t DigoleSerialDisp::printFloat(double number, uint8_t digits) 00217 { 00218 size_t n = 0; 00219 00220 if (isnan(number)) return print("nan"); 00221 if (isinf(number)) return print("inf"); 00222 if (number > 4294967040.0) return print ("ovf"); // constant determined empirically 00223 if (number <-4294967040.0) return print ("ovf"); // constant determined empirically 00224 00225 // Handle negative numbers 00226 if (number < 0.0) 00227 { 00228 n += print('-'); 00229 number = -number; 00230 } 00231 00232 // Round correctly so that print(1.999, 2) prints as "2.00" 00233 double rounding = 0.5; 00234 for (uint8_t i=0; i<digits; ++i) 00235 rounding /= 10.0; 00236 00237 number += rounding; 00238 00239 // Extract the integer part of the number and print it 00240 unsigned long int_part = (unsigned long)number; 00241 double remainder = number - (double)int_part; 00242 n += print(int_part); 00243 00244 // Print the decimal point, but only if there are digits beyond 00245 if (digits > 0) { 00246 n += print("."); 00247 } 00248 00249 // Extract digits from the remainder one at a time 00250 while (digits-- > 0) 00251 { 00252 remainder *= 10.0; 00253 int toPrint = int(remainder); 00254 n += print(toPrint); 00255 remainder -= toPrint; 00256 } 00257 00258 return n; 00259 } 00260 00261 /*---------functions for Text and Graphic LCD adapters---------*/ 00262 void DigoleSerialDisp::disableCursor(void) 00263 { 00264 write("CS"); 00265 write(null); 00266 } 00267 00268 void DigoleSerialDisp::enableCursor(void) 00269 { 00270 write("CS"); 00271 write(1); 00272 } 00273 00274 void DigoleSerialDisp::drawStr(uint8_t x, uint8_t y, const char *s) 00275 { 00276 write("TP"); 00277 write(x); 00278 write(y); 00279 write("TT"); 00280 write(s); 00281 write(null); 00282 } 00283 00284 void DigoleSerialDisp::setPrintPos(uint8_t x, uint8_t y, uint8_t graph) 00285 { 00286 if (graph == _TEXT_) { 00287 write("TP"); 00288 write(x); 00289 write(y); 00290 } else { 00291 write("GP"); 00292 write(x); 00293 write(y); 00294 } 00295 } 00296 00297 void DigoleSerialDisp::clearScreen(void) 00298 { 00299 //write(null); 00300 write("CL"); 00301 } 00302 00303 void DigoleSerialDisp::setLCDColRow(uint8_t col, uint8_t row) 00304 { 00305 write("STCR"); 00306 write(col); 00307 write(row); 00308 write("\x80\xC0\x94\xD4"); 00309 } 00310 00311 void DigoleSerialDisp::setI2CAddress(uint8_t add) 00312 { 00313 write("SI2CA"); 00314 write(add); 00315 _address = (add<<1); 00316 } 00317 00318 void DigoleSerialDisp::displayConfig(uint8_t v) 00319 { 00320 write("DC"); 00321 write(v); 00322 } 00323 00324 void DigoleSerialDisp::preprint(void) 00325 { 00326 //print("TT"); 00327 } 00328 00329 /*----------Functions for Graphic LCD/OLED adapters only---------*/ 00330 void DigoleSerialDisp::drawBitmap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap) { 00331 uint8_t i = 0; 00332 if ((w & 7) != 0) 00333 i = 1; 00334 write("DIM"); 00335 write(x); //x; 00336 write(y); 00337 write(w); 00338 write(h); 00339 for (int j = 0; j < h * ((w >> 3) + i); j++) { 00340 write( (const char *) (bitmap+j) ); 00341 delay(1); 00342 } 00343 } 00344 00345 void DigoleSerialDisp::setRot90(void) { 00346 write("SD1"); 00347 } 00348 00349 void DigoleSerialDisp::setRot180(void) { 00350 write("SD2"); 00351 } 00352 00353 void DigoleSerialDisp::setRot270(void) { 00354 write("SD3"); 00355 } 00356 00357 void DigoleSerialDisp::undoRotation(void) { 00358 write("SD0"); 00359 } 00360 00361 void DigoleSerialDisp::setRotation(uint8_t d) { 00362 write("SD"); 00363 write(d); 00364 } 00365 00366 void DigoleSerialDisp::setContrast(uint8_t c) { 00367 write("CT"); 00368 write(c); 00369 } 00370 00371 void DigoleSerialDisp::drawBox(uint8_t x, uint8_t y, uint8_t w, uint8_t h) { 00372 write("FR"); 00373 write(x); 00374 write(y); 00375 write(x + w); 00376 write(y + h); 00377 } 00378 00379 void DigoleSerialDisp::drawCircle(uint8_t x, uint8_t y, uint8_t r, uint8_t f) { 00380 write("CC"); 00381 write(x); 00382 write(y); 00383 write(r); 00384 write(f); 00385 } 00386 00387 void DigoleSerialDisp::drawDisc(uint8_t x, uint8_t y, uint8_t r) { 00388 drawCircle(x, y, r, 1); 00389 } 00390 00391 void DigoleSerialDisp::drawFrame(uint8_t x, uint8_t y, uint8_t w, uint8_t h) { 00392 write("DR"); 00393 write(x); 00394 write(y); 00395 write(x + w); 00396 write(y + h); 00397 } 00398 00399 void DigoleSerialDisp::drawPixel(uint8_t x, uint8_t y, uint8_t color) { 00400 write("DP"); 00401 write(x); 00402 write(y); 00403 write(color); 00404 } 00405 00406 void DigoleSerialDisp::drawLine(uint8_t x, uint8_t y, uint8_t x1, uint8_t y1) { 00407 write("LN"); 00408 write(x); 00409 write(y); 00410 write(x1); 00411 write(y1); 00412 } 00413 00414 void DigoleSerialDisp::drawLineTo(uint8_t x, uint8_t y) { 00415 write("LT"); 00416 write(x); 00417 write(y); 00418 } 00419 00420 void DigoleSerialDisp::drawHLine(uint8_t x, uint8_t y, uint8_t w) { 00421 drawLine(x, y, x + w, y); 00422 } 00423 00424 void DigoleSerialDisp::drawVLine(uint8_t x, uint8_t y, uint8_t h) { 00425 drawLine(x, y, x, y + h); 00426 } 00427 00428 void DigoleSerialDisp::nextTextLine(void) { 00429 write(null); 00430 write("TRT"); 00431 } 00432 00433 void DigoleSerialDisp::setFont(uint8_t font) { 00434 write("SF"); 00435 write(font); 00436 } 00437 00438 void DigoleSerialDisp::setColor(uint8_t color) { 00439 write("SC"); 00440 write(color); 00441 } 00442 00443 void DigoleSerialDisp::backLightOn(void) { 00444 write("BL"); 00445 write(1); 00446 } 00447 00448 void DigoleSerialDisp::backLightOff(void) { 00449 write("BL"); 00450 write(null); 00451 } 00452 00453 void DigoleSerialDisp::directCommand(uint8_t d) { 00454 write("MCD"); 00455 write(d); 00456 } 00457 00458 void DigoleSerialDisp::directData(uint8_t d) { 00459 write("MDT"); 00460 write(d); 00461 } 00462 00463 void DigoleSerialDisp::moveArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char xoffset, char yoffset) { 00464 write("MA"); 00465 write(x0); 00466 write(y0); 00467 write(x1); 00468 write(y1); 00469 write(xoffset); 00470 write(yoffset); 00471 } 00472 00473 00474 void DigoleSerialDisp::displayStartScreen(uint8_t m) { 00475 write("DSS"); 00476 write(m); 00477 } //display start screen 00478 00479 00480 void DigoleSerialDisp::setMode(uint8_t m) { 00481 write("DM"); 00482 write(m); 00483 } //set display mode 00484 00485 00486 void DigoleSerialDisp::setTextPosBack(void) { 00487 write("ETB"); 00488 } //set text position back to previous, only one back allowed 00489 00490 00491 void DigoleSerialDisp::setTextPosOffset(char xoffset, char yoffset) { 00492 write("ETO"); 00493 write(xoffset); 00494 write(yoffset); 00495 } 00496 00497 00498 void DigoleSerialDisp::setTextPosAbs(uint8_t x, uint8_t y) { 00499 write("ETP"); 00500 write(x); 00501 write(y); 00502 } 00503 00504 00505 void DigoleSerialDisp::setLinePattern(uint8_t pattern) { 00506 write("SLP"); 00507 write(pattern); 00508 } 00509 00510 00511 void DigoleSerialDisp::setLCDChip(uint8_t chip) { //only for universal LCD adapter 00512 write("SLCD"); 00513 write(chip); 00514 } 00515 00516 00517 void DigoleSerialDisp::uploadStartScreen(int lon, const unsigned char *data) 00518 { 00519 int j; 00520 uint8_t c; 00521 write("SSS"); 00522 write((uint8_t) (lon % 256)); 00523 write((uint8_t) (lon / 256)); 00524 for (j = 0; j < lon;j++) { 00525 if((j%32)==0) 00526 delay(10); 00527 delay(_Comdelay); 00528 c = data[j]; 00529 write(c); 00530 } 00531 } 00532 00533 00534 void DigoleSerialDisp::uploadUserFont(int lon, const unsigned char *data, uint8_t sect) { 00535 uint8_t c; 00536 write("SUF"); 00537 write(sect); 00538 write((uint8_t) (lon % 256)); 00539 write((uint8_t) (lon / 256)); 00540 for (int j = 0; j < lon; j++) { 00541 if((j%32)==0) 00542 delay(10); 00543 delay(_Comdelay); 00544 c = data[j]; 00545 write(c); 00546 } 00547 } 00548 00549 void DigoleSerialDisp::digitalOutput(uint8_t x) 00550 { 00551 write("DOUT"); 00552 write(x); 00553 }
Generated on Tue Jul 12 2022 20:40:53 by 1.7.2