diff --git a/Lcd.cpp b/Lcd.cpp new file mode 100644 index 0000000..65ad921 --- /dev/null +++ b/Lcd.cpp @@ -0,0 +1,64 @@ +#include "Lcd.h" + +#include + +void Lcd::initDisplay() +{ + static const Command resetCommand{0, 0, 0, 0, 1, 1, 0, 0}; + + execute(resetCommand, false, 5.0); + execute(resetCommand, false, 0.1); + execute(resetCommand, false, 0.0); + execute({0, 1, 0, 0, 0, 0, 0, 0}, false, 0.0); + + // function set + execute({0, 0, 1, 1, 0, 1, 0, 0}, false, 0.039); + + // display on/off + execute({0, 0, 1, 1, 0, 0, 0, 0}, false, 0.039); + + clear(); + + // entry mode + execute({0, 1, 1, 0, 0, 0, 0, 0}, false, 0.039); +} + +void Lcd::clear() +{ + execute({1, 0, 0, 0, 0, 0, 0, 0}, false, 1.53); +} + +void Lcd::setPos(const uint8_t &pos) +{ + Command posCommand; + posCommand.data = pos | (1 << 7); + + execute(posCommand, false, 0.039); +} + +void Lcd::output(const char &character) +{ + execute((const Command &)character, true, 0.043); + +} + +void Lcd::output(const char *string) +{ + uint8_t pos = 0; + while (string != nullptr && *string != 0) + { + if (pos == 8) + { + setPos(40); + } + output(*string++); + pos++; + } +} + +void Lcd::output(const uint16_t &val) +{ + static char buf[5]; + output(itoa(val, buf, 10)); +} + diff --git a/Lcd.h b/Lcd.h new file mode 100644 index 0000000..60fed32 --- /dev/null +++ b/Lcd.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +class Lcd +{ +public: + union Command + { + struct + { + bool DB0: 1; + bool DB1: 1; + bool DB2: 1; + bool DB3: 1; + bool DB4: 1; + bool DB5: 1; + bool DB6: 1; + bool DB7: 1; + }; + uint8_t data; + }; + +public: + void initDisplay(); + void clear(); + + void output(const char &character); + void output(const char *string); + void output(const uint16_t &val); + +private: + void setPos(const uint8_t &pos); + + virtual void execute(const Command &cmd, bool RS, double delay) = 0; +}; +