Optimized LiquidCrystal class

removed dependecies to arduino Print and String classes
spared 522 bytes of flash and 84bytes of ram
This commit is contained in:
Robert Pelnar 2018-06-08 14:35:47 +02:00
parent 37f82118c8
commit e69fd05fce
5 changed files with 204 additions and 12 deletions

View File

@ -543,4 +543,171 @@ void LiquidCrystal_Prusa::write8bits(uint8_t value) {
} }
pulseEnable(); pulseEnable();
} }
void LiquidCrystal_Prusa::print(const char* s)
{
while (*s) write(*(s++));
}
void LiquidCrystal_Prusa::print(char c, int base)
{
print((long) c, base);
}
void LiquidCrystal_Prusa::print(unsigned char b, int base)
{
print((unsigned long) b, base);
}
void LiquidCrystal_Prusa::print(int n, int base)
{
print((long) n, base);
}
void LiquidCrystal_Prusa::print(unsigned int n, int base)
{
print((unsigned long) n, base);
}
void LiquidCrystal_Prusa::print(long n, int base)
{
if (base == 0) {
write(n);
} else if (base == 10) {
if (n < 0) {
print('-');
n = -n;
}
printNumber(n, 10);
} else {
printNumber(n, base);
}
}
void LiquidCrystal_Prusa::print(unsigned long n, int base)
{
if (base == 0) write(n);
else printNumber(n, base);
}
void LiquidCrystal_Prusa::print(double n, int digits)
{
printFloat(n, digits);
}
void LiquidCrystal_Prusa::println(void)
{
print('\r');
print('\n');
}
/*void LiquidCrystal_Prusa::println(const String &s)
{
print(s);
println();
}*/
void LiquidCrystal_Prusa::println(const char c[])
{
print(c);
println();
}
void LiquidCrystal_Prusa::println(char c, int base)
{
print(c, base);
println();
}
void LiquidCrystal_Prusa::println(unsigned char b, int base)
{
print(b, base);
println();
}
void LiquidCrystal_Prusa::println(int n, int base)
{
print(n, base);
println();
}
void LiquidCrystal_Prusa::println(unsigned int n, int base)
{
print(n, base);
println();
}
void LiquidCrystal_Prusa::println(long n, int base)
{
print(n, base);
println();
}
void LiquidCrystal_Prusa::println(unsigned long n, int base)
{
print(n, base);
println();
}
void LiquidCrystal_Prusa::println(double n, int digits)
{
print(n, digits);
println();
}
void LiquidCrystal_Prusa::printNumber(unsigned long n, uint8_t base)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
print('0');
return;
}
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--)
print((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
}
void LiquidCrystal_Prusa::printFloat(double number, uint8_t digits)
{
// Handle negative numbers
if (number < 0.0)
{
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;
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;
print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}

View File

@ -2,7 +2,8 @@
#define LiquidCrystal_Prusa_h #define LiquidCrystal_Prusa_h
#include <inttypes.h> #include <inttypes.h>
#include "Print.h" #include <stddef.h>
//#include "Print.h"
// commands // commands
#define LCD_CLEARDISPLAY 0x01 #define LCD_CLEARDISPLAY 0x01
@ -42,7 +43,7 @@
#define LCD_5x10DOTS 0x04 #define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00 #define LCD_5x8DOTS 0x00
class LiquidCrystal_Prusa : public Print { class LiquidCrystal_Prusa/* : public Print */{
public: public:
LiquidCrystal_Prusa(uint8_t rs, uint8_t enable, LiquidCrystal_Prusa(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
@ -81,10 +82,34 @@ public:
void createChar(uint8_t, uint8_t[]); void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t); void setCursor(uint8_t, uint8_t);
virtual size_t write(uint8_t); // virtual size_t write(uint8_t);
size_t write(uint8_t);
void command(uint8_t); void command(uint8_t);
using Print::write; void print(const char*);
void print(char, int = 0);
void print(unsigned char, int = 0);
void print(int, int = 10);
void print(unsigned int, int = 10);
void print(long, int = 10);
void print(unsigned long, int = 10);
void print(double, int = 2);
// void println(const String &s);
void println(const char[]);
void println(char, int = 0);
void println(unsigned char, int = 0);
void println(int, int = 10);
void println(unsigned int, int = 10);
void println(long, int = 10);
void println(unsigned long, int = 10);
void println(double, int = 2);
void println(void);
void printNumber(unsigned long n, uint8_t base);
void printFloat(double number, uint8_t digits);
// using Print::write;
private: private:
void send(uint8_t, uint8_t); void send(uint8_t, uint8_t);
void write4bits(uint8_t); void write4bits(uint8_t);

View File

@ -259,11 +259,11 @@ void MarlinSerial::println(void)
print('\n'); print('\n');
} }
void MarlinSerial::println(const String &s) /*void MarlinSerial::println(const String &s)
{ {
print(s); print(s);
println(); println();
} }*/
void MarlinSerial::println(const char c[]) void MarlinSerial::println(const char c[])
{ {

View File

@ -197,12 +197,12 @@ class MarlinSerial //: public Stream
write(*buffer++); write(*buffer++);
} }
static FORCE_INLINE void print(const String &s) /* static FORCE_INLINE void print(const String &s)
{ {
for (int i = 0; i < (int)s.length(); i++) { for (int i = 0; i < (int)s.length(); i++) {
write(s[i]); write(s[i]);
} }
} }*/
static FORCE_INLINE void print(const char *str) static FORCE_INLINE void print(const char *str)
{ {

View File

@ -25,7 +25,7 @@
#ifdef SDSUPPORT #ifdef SDSUPPORT
#include "SdBaseFile.h" #include "SdBaseFile.h"
#include <Print.h> //#include <Print.h>
#ifndef SdFile_h #ifndef SdFile_h
#define SdFile_h #define SdFile_h
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -33,7 +33,7 @@
* \class SdFile * \class SdFile
* \brief SdBaseFile with Print. * \brief SdBaseFile with Print.
*/ */
class SdFile : public SdBaseFile, public Print { class SdFile : public SdBaseFile/*, public Print*/ {
public: public:
SdFile() {} SdFile() {}
SdFile(const char* name, uint8_t oflag); SdFile(const char* name, uint8_t oflag);