John Bunda
/
DigoleSerialDisp1
UART object
Fork of DigoleSerialDisp1 by
Revision 6:fc42c0dc7b7f, committed 2015-11-22
- Comitter:
- jdbunda
- Date:
- Sun Nov 22 16:01:18 2015 +0000
- Parent:
- 5:7d3fd21b9ead
- Commit message:
- New DigoleSerialDisp UART object
Changed in this revision
--- a/DigoleSerialDisp.cpp Tue Nov 17 16:09:34 2015 +0000 +++ b/DigoleSerialDisp.cpp Sun Nov 22 16:01:18 2015 +0000 @@ -2,61 +2,48 @@ * * @Author: Digole Digital Solutions : www.digole.com ported to mbed by Michael Shimniok www.bot-thoughts.com */ - + #include "mbed.h" #include "DigoleSerialDisp.h" #include <stdio.h> #include <string.h> #include <inttypes.h> +char buf[128]; +char tmp[128]; + char null = 0; - // that resetting the Arduino doesn't reset the LCD, so we // can't assume that its in that state when a sketch starts (and the // LiquidCrystal constructor is called). -//UART function - -char buf[128]; -char tmp[128]; -/* -DigoleSerialDisp::DigoleSerialDisp(PinName sda, PinName scl, uint8_t address): - _device(sda, scl) +DigoleSerialDisp::DigoleSerialDisp(PinName tx, PinName rx) : + Serial (tx, rx, "digole") { - _address = (address<<1); - _device.frequency(100000); + Serial::baud(9600); _Comdelay=70; } -*/ -/* -DigoleSerialDisp(PinName tx, PinName rx, unsigned long baud) //UART set up - { - _mySerial = s; - _Baud = baud; - _Comdelay=2; - } -*/ - + + size_t DigoleSerialDisp::write(const char x) { - _device.write(_address, (char *) &x, 1); - + Serial::write((char *) &x, 1); return 1; } -size_t DigoleSerialDisp::write(const char *str) +size_t DigoleSerialDisp::write(const char *str) { if (str == NULL) return 0; - return write(str, strlen(str)); + return Serial::write(str, strlen(str)); } - + size_t DigoleSerialDisp::write(const char *buffer, size_t size) { int len = 0; if (buffer != NULL) { - _device.write(_address, (char *) buffer, size); + Serial::write((char *) buffer, size); len = size; delay(7); } @@ -98,193 +85,194 @@ /* Print.cpp - Base class that provides print() and println() Copyright (c) 2008 David A. Mellis. All right reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - + Modified 23 November 2006 by David A. Mellis */ size_t DigoleSerialDisp::print(unsigned char b, int base) { - return print((unsigned long) b, base); + return print((unsigned long) b, base); } size_t DigoleSerialDisp::print(int n, int base) { - return print((long) n, base); + return print((long) n, base); } size_t DigoleSerialDisp::print(unsigned int n, int base) { - return print((unsigned long) n, base); + return print((unsigned long) n, base); } size_t DigoleSerialDisp::print(long n, int base) { - if (base == 0) { - return write(n); - } else if (base == 10) { - if (n < 0) { - int t = print('-'); - n = -n; - return printNumber(n, 10) + t; + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); } - return printNumber(n, 10); - } else { - return printNumber(n, base); - } } size_t DigoleSerialDisp::print(unsigned long n, int base) { - if (base == 0) return write(n); - else return printNumber(n, base); + if (base == 0) + return printNumber(n, 10); + else + return printNumber(n, base); } size_t DigoleSerialDisp::print(double n, int digits) { - return printFloat(n, digits); + return printFloat(n, digits); } size_t DigoleSerialDisp::println(unsigned char b, int base) { - size_t n = print(b, base); - n += println(); - return n; + size_t n = print(b, base); + n += println(); + return n; } size_t DigoleSerialDisp::println(int num, int base) { - size_t n = print(num, base); - n += println(); - return n; + size_t n = print(num, base); + n += println(); + return n; } size_t DigoleSerialDisp::println(unsigned int num, int base) { - size_t n = print(num, base); - n += println(); - return n; + size_t n = print(num, base); + n += println(); + return n; } size_t DigoleSerialDisp::println(long num, int base) { - size_t n = print(num, base); - n += println(); - return n; + size_t n = print(num, base); + n += println(); + return n; } size_t DigoleSerialDisp::println(unsigned long num, int base) { - size_t n = print(num, base); - n += println(); - return n; + size_t n = print(num, base); + n += println(); + return n; } size_t DigoleSerialDisp::println(double num, int digits) { - size_t n = print(num, digits); - n += println(); - return n; + size_t n = print(num, digits); + n += println(); + return n; } -size_t DigoleSerialDisp::println(void) +size_t DigoleSerialDisp::println(void) { return 1; } // Private Methods ///////////////////////////////////////////////////////////// -size_t DigoleSerialDisp::printNumber(unsigned long n, uint8_t base) { - char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. - char *str = &buf[sizeof(buf) - 1]; +size_t DigoleSerialDisp::printNumber(unsigned long n, uint8_t base) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; - *str = '\0'; - - // prevent crash if called with base == 1 - if (base < 2) base = 10; + *str = '\0'; - do { - unsigned long m = n; - n /= base; - char c = m - base * n; - *--str = c < 10 ? c + '0' : c + 'A' - 10; - } while(n); + // prevent crash if called with base == 1 + if (base < 2) base = 10; - return write(str); + do { + unsigned long m = n; + n /= base; + char c = m - base * n; + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); } -size_t DigoleSerialDisp::printFloat(double number, uint8_t digits) -{ - size_t n = 0; - - if (isnan(number)) return print("nan"); - if (isinf(number)) return print("inf"); - if (number > 4294967040.0) return print ("ovf"); // constant determined empirically - if (number <-4294967040.0) return print ("ovf"); // constant determined empirically - - // Handle negative numbers - if (number < 0.0) - { - n += print('-'); - number = -number; - } +size_t DigoleSerialDisp::printFloat(double number, uint8_t digits) +{ + size_t n = 0; + + if (isnan(number)) return print("nan"); + if (isinf(number)) return print("inf"); + if (number > 4294967040.0) return print ("ovf"); // constant determined empirically + if (number <-4294967040.0) return print ("ovf"); // constant determined empirically + + // Handle negative numbers + if (number < 0.0) { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i<digits; ++i) + rounding /= 10.0; - // Round correctly so that print(1.999, 2) prints as "2.00" - double rounding = 0.5; - for (uint8_t i=0; i<digits; ++i) - rounding /= 10.0; - - number += rounding; + number += rounding; - // Extract the integer part of the number and print it - unsigned long int_part = (unsigned long)number; - double remainder = number - (double)int_part; - n += print(int_part); + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long)number; + double remainder = number - (double)int_part; + n += print(int_part); - // Print the decimal point, but only if there are digits beyond - if (digits > 0) { - n += print("."); - } + // Print the decimal point, but only if there are digits beyond + if (digits > 0) { + n += print("."); + } - // Extract digits from the remainder one at a time - while (digits-- > 0) - { - remainder *= 10.0; - int toPrint = int(remainder); - n += print(toPrint); - remainder -= toPrint; - } - - return n; + // Extract digits from the remainder one at a time + while (digits-- > 0) { + remainder *= 10.0; + int toPrint = int(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; } /*---------functions for Text and Graphic LCD adapters---------*/ -void DigoleSerialDisp::disableCursor(void) +void DigoleSerialDisp::disableCursor(void) { write("CS"); write(null); } -void DigoleSerialDisp::enableCursor(void) +void DigoleSerialDisp::enableCursor(void) { write("CS"); write(1); } -void DigoleSerialDisp::drawStr(uint8_t x, uint8_t y, const char *s) +void DigoleSerialDisp::drawStr(uint8_t x, uint8_t y, const char *s) { write("TP"); write(x); @@ -294,7 +282,7 @@ write(null); } -void DigoleSerialDisp::setPrintPos(uint8_t x, uint8_t y, uint8_t graph) +void DigoleSerialDisp::setPrintPos(uint8_t x, uint8_t y, uint8_t graph) { if (graph == _TEXT_) { write("TP"); @@ -307,13 +295,13 @@ } } -void DigoleSerialDisp::clearScreen(void) +void DigoleSerialDisp::clearScreen(void) { //write(null); write("CL"); } -void DigoleSerialDisp::setLCDColRow(uint8_t col, uint8_t row) +void DigoleSerialDisp::setLCDColRow(uint8_t col, uint8_t row) { write("STCR"); write(col); @@ -321,26 +309,27 @@ write("\x80\xC0\x94\xD4"); } -void DigoleSerialDisp::setI2CAddress(uint8_t add) +/*void DigoleSerialDisp::setI2CAddress(uint8_t add) { write("SI2CA"); write(add); _address = (add<<1); } - -void DigoleSerialDisp::displayConfig(uint8_t v) +*/ +void DigoleSerialDisp::displayConfig(uint8_t v) { write("DC"); write(v); } -void DigoleSerialDisp::preprint(void) +void DigoleSerialDisp::preprint(void) { //print("TT"); } /*----------Functions for Graphic LCD/OLED adapters only---------*/ -void DigoleSerialDisp::drawBitmap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap) { +void DigoleSerialDisp::drawBitmap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *bitmap) +{ uint8_t i = 0; if ((w & 7) != 0) i = 1; @@ -355,33 +344,40 @@ } } -void DigoleSerialDisp::setRot90(void) { +void DigoleSerialDisp::setRot90(void) +{ write("SD1"); } -void DigoleSerialDisp::setRot180(void) { +void DigoleSerialDisp::setRot180(void) +{ write("SD2"); } -void DigoleSerialDisp::setRot270(void) { +void DigoleSerialDisp::setRot270(void) +{ write("SD3"); } -void DigoleSerialDisp::undoRotation(void) { +void DigoleSerialDisp::undoRotation(void) +{ write("SD0"); } -void DigoleSerialDisp::setRotation(uint8_t d) { +void DigoleSerialDisp::setRotation(uint8_t d) +{ write("SD"); write(d); } -void DigoleSerialDisp::setContrast(uint8_t c) { +void DigoleSerialDisp::setContrast(uint8_t c) +{ write("CT"); write(c); } -void DigoleSerialDisp::drawBox(uint8_t x, uint8_t y, uint8_t w, uint8_t h) { +void DigoleSerialDisp::drawBox(uint8_t x, uint8_t y, uint8_t w, uint8_t h) +{ write("FR"); write(x); write(y); @@ -389,7 +385,8 @@ write(y + h); } -void DigoleSerialDisp::drawCircle(uint8_t x, uint8_t y, uint8_t r, uint8_t f) { +void DigoleSerialDisp::drawCircle(uint8_t x, uint8_t y, uint8_t r, uint8_t f) +{ write("CC"); write(x); write(y); @@ -397,11 +394,13 @@ write(f); } -void DigoleSerialDisp::drawDisc(uint8_t x, uint8_t y, uint8_t r) { +void DigoleSerialDisp::drawDisc(uint8_t x, uint8_t y, uint8_t r) +{ drawCircle(x, y, r, 1); } -void DigoleSerialDisp::drawFrame(uint8_t x, uint8_t y, uint8_t w, uint8_t h) { +void DigoleSerialDisp::drawFrame(uint8_t x, uint8_t y, uint8_t w, uint8_t h) +{ write("DR"); write(x); write(y); @@ -409,14 +408,16 @@ write(y + h); } -void DigoleSerialDisp::drawPixel(uint8_t x, uint8_t y, uint8_t color) { +void DigoleSerialDisp::drawPixel(uint8_t x, uint8_t y, uint8_t color) +{ write("DP"); write(x); write(y); write(color); } -void DigoleSerialDisp::drawLine(uint8_t x, uint8_t y, uint8_t x1, uint8_t y1) { +void DigoleSerialDisp::drawLine(uint8_t x, uint8_t y, uint8_t x1, uint8_t y1) +{ write("LN"); write(x); write(y); @@ -424,56 +425,67 @@ write(y1); } -void DigoleSerialDisp::drawLineTo(uint8_t x, uint8_t y) { +void DigoleSerialDisp::drawLineTo(uint8_t x, uint8_t y) +{ write("LT"); write(x); write(y); } -void DigoleSerialDisp::drawHLine(uint8_t x, uint8_t y, uint8_t w) { +void DigoleSerialDisp::drawHLine(uint8_t x, uint8_t y, uint8_t w) +{ drawLine(x, y, x + w, y); } -void DigoleSerialDisp::drawVLine(uint8_t x, uint8_t y, uint8_t h) { +void DigoleSerialDisp::drawVLine(uint8_t x, uint8_t y, uint8_t h) +{ drawLine(x, y, x, y + h); } -void DigoleSerialDisp::nextTextLine(void) { +void DigoleSerialDisp::nextTextLine(void) +{ write(null); write("TRT"); } -void DigoleSerialDisp::setFont(uint8_t font) { +void DigoleSerialDisp::setFont(uint8_t font) +{ write("SF"); write(font); } -void DigoleSerialDisp::setColor(uint8_t color) { +void DigoleSerialDisp::setColor(uint8_t color) +{ write("SC"); write(color); } -void DigoleSerialDisp::backLightOn(void) { +void DigoleSerialDisp::backLightOn(void) +{ write("BL"); write(1); } -void DigoleSerialDisp::backLightOff(void) { +void DigoleSerialDisp::backLightOff(void) +{ write("BL"); write(null); } -void DigoleSerialDisp::directCommand(uint8_t d) { +void DigoleSerialDisp::directCommand(uint8_t d) +{ write("MCD"); write(d); } -void DigoleSerialDisp::directData(uint8_t d) { +void DigoleSerialDisp::directData(uint8_t d) +{ write("MDT"); write(d); } -void DigoleSerialDisp::moveArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char xoffset, char yoffset) { +void DigoleSerialDisp::moveArea(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char xoffset, char yoffset) +{ write("MA"); write(x0); write(y0); @@ -484,57 +496,64 @@ } -void DigoleSerialDisp::displayStartScreen(uint8_t m) { +void DigoleSerialDisp::displayStartScreen(uint8_t m) +{ write("DSS"); write(m); } //display start screen -void DigoleSerialDisp::setMode(uint8_t m) { +void DigoleSerialDisp::setMode(uint8_t m) +{ write("DM"); write(m); } //set display mode -void DigoleSerialDisp::setTextPosBack(void) { +void DigoleSerialDisp::setTextPosBack(void) +{ write("ETB"); } //set text position back to previous, only one back allowed -void DigoleSerialDisp::setTextPosOffset(char xoffset, char yoffset) { +void DigoleSerialDisp::setTextPosOffset(char xoffset, char yoffset) +{ write("ETO"); write(xoffset); write(yoffset); } -void DigoleSerialDisp::setTextPosAbs(uint8_t x, uint8_t y) { +void DigoleSerialDisp::setTextPosAbs(uint8_t x, uint8_t y) +{ write("ETP"); write(x); write(y); } -void DigoleSerialDisp::setLinePattern(uint8_t pattern) { +void DigoleSerialDisp::setLinePattern(uint8_t pattern) +{ write("SLP"); write(pattern); } -void DigoleSerialDisp::setLCDChip(uint8_t chip) { //only for universal LCD adapter +void DigoleSerialDisp::setLCDChip(uint8_t chip) //only for universal LCD adapter +{ write("SLCD"); write(chip); } -void DigoleSerialDisp::uploadStartScreen(int lon, const unsigned char *data) +void DigoleSerialDisp::uploadStartScreen(int lon, const unsigned char *data) { int j; uint8_t c; write("SSS"); write((uint8_t) (lon % 256)); write((uint8_t) (lon / 256)); - for (j = 0; j < lon;j++) { + for (j = 0; j < lon; j++) { if((j%32)==0) delay(10); delay(_Comdelay); @@ -544,7 +563,8 @@ } -void DigoleSerialDisp::uploadUserFont(int lon, const unsigned char *data, uint8_t sect) { +void DigoleSerialDisp::uploadUserFont(int lon, const unsigned char *data, uint8_t sect) +{ uint8_t c; write("SUF"); write(sect); @@ -559,7 +579,7 @@ } } -void DigoleSerialDisp::digitalOutput(uint8_t x) +void DigoleSerialDisp::digitalOutput(uint8_t x) { write("DOUT"); write(x);
--- a/DigoleSerialDisp.h Tue Nov 17 16:09:34 2015 +0000 +++ b/DigoleSerialDisp.h Sun Nov 22 16:01:18 2015 +0000 @@ -6,6 +6,7 @@ #define DigoleSerialDisp_h #include "mbed.h" +#include "Serial.h" #include <inttypes.h> #define DEC 10 @@ -25,57 +26,13 @@ #define _TEXT_ 0 #define _GRAPH_ 1 -/** Digole Serial LCD/OLED Library - * www.digole.com/index.php?productID=535 - * - * Includes Arduino Print class member functions - */ -class DigoleSerialDisp { + +class DigoleSerialDisp : Serial +{ public: - -#if defined(_Digole_Serial_UART_) - - - size_t write(uint8_t value) { - _mySerial->write((uint8_t) value); - return 1; // assume sucess - } - - void begin(void) { - _mySerial->begin(9600); - _mySerial->print("SB"); - _mySerial->println(_Baud); - delay(100); - _mySerial->begin(_Baud); - } - size_t read1(void) - { - int t; - do { - t = _mySerial->read(); - } while (t == -1); - return t; - } -#endif - - /** Create a new Digole Serial Display interface - * - * @param sda is the pin for I2C SDA - * @param scl is the pin for I2C SCL - * @param address is the 7-bit address (default is 0x27 for the device) - */ - DigoleSerialDisp(PinName sda, PinName scl, uint8_t address=0x27); - - - /** Carryover from Arduino library, not needed - */ - void begin(void) { } // nothing to do here - - - /** Write out a raw character - * @param x is the character to write - * @returns 1 - */ + DigoleSerialDisp(PinName, PinName); + + // Simple write methods. size_t write(const char x); @@ -366,8 +323,8 @@ void digitalOutput(uint8_t x); private: - I2C _device; - uint8_t _address; +// I2C _device; +// uint8_t _address; uint8_t _Comdelay; size_t printNumber(unsigned long n, uint8_t base);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Nov 22 16:01:18 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9296ab0bfc11 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.cpp Sun Nov 22 16:01:18 2015 +0000 @@ -0,0 +1,70 @@ +#include "mbed.h" +#include "DigoleSerialDisp.h" + +DigoleSerialDisp display(dp11, dp12);//UART DigoleSerialDisp(PinName TX, PinName RX); + +int main (int argc, char**argv) +{ +// display.baud(9600); + wait(1); //wait for DigoleSerialDisp to power up + + + display.write("CL\r"); + + + while(1) + { + display.write("SD3\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg + display.write("TRT");//offset Line 1 in housing + display.write("SF3\rTT Line 1\r\n"); + display.write("TRT");//next line + wait(0.5); + display.write("SF3\r\n"); + display.write("CT5\r\n");//set contrast (0-30) + display.write("ESC0f0\r\n"); + //display.write("SD0\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg + display.write("TT Line 2\r\n"); + display.write("TRT"); + wait(0.5); + display.write("SF3\r\n"); + display.write("ESC6\r\n"); + display.write("CT10\r\n");//set contrast (0-30) + //display.write("SD1\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg + display.write("TT Line 3\r\n"); + display.write("TRT"); + wait(0.5); + display.write("SF3\r\n"); + display.write("ESC16\r\n"); + display.write("CT80\r\n");//set contrast (0-30) + //display.write("SD2\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg + display.write("TT Line 4\r\n"); + display.write("TRT"); + wait(0.5); + display.write("SF3\r\n"); + display.write("ESC1ff\r"); + display.write("CT20\r\n");//set contrast (0-30) + //display.write("SD3\r\n");//set rotation 0=0deg,1=90deg,2=180deg,3=270deg + display.write("TT Line 5\r\n"); + display.write("TRT"); + wait(0.5); + display.write("SF3\r\n"); + display.write("ESC0f\r\n"); + display.write("CT525\r\n");//set contrast (0-30) + display.write("TT Line 6\r\n"); + display.write("TRT"); + wait(0.5); + display.write("BL100\r\n"); + display.write("TRT"); + wait(0.5); + //display.write("FR1111\r\n"); + wait(1); + display.write("CL\r\n"); + + + display.drawLine(10,10,100,100); + display.drawCircle(20,20,20,20); +// display.write("Testing the display."); +//DigoleSerialDisp.write(0xD); + wait(1); + } + } \ No newline at end of file