Arduino library for chinese 16 segment/10 characters LCD modules based on HT1622 driver.
- Displaying any kind of characters
- Automatic alignment of strings
- Text scrolling capability
- Custom character sets support
You can install the library via Arduino IDE library manager or download latest release and unzip it to your project folder.
-
Connect the display
CS,WRandDATAwires to any three Arduino's digital pins (backlight should be connected to analog pin or to +5V if you don't wan't to control it programmatically). -
Include the library header:
#include <DM8BA10.h>- Include preferable charset instance:
#include <charset/latin_basic.h>- Create charset:
auto charset = new LatinBasicCharset();- Create
DM8BA10instance:
auto lcd = new DM8BA10(charset);- Display something:
lcd->println("PROFIT");DM8BA10(Charset* charset, byte csPin, byte wrPin, byte dataPin, int16_t backlightPin = -1)Creates new DM8BA10 object
charset– a pointer to Charset objectcsPin– pin, connected to CS pin of the displaywrPin– pin, connected to WR pin of the displaydataPin– pin, connected to DATA pin of the displaybacklightPin– pin, connected to LCD's backlight pin
#define CS_PIN 9 // SS
#define WR_PIN 8 // SCK
#define DATA_PIN 7 // MOSI
#define BACKLIGHT_PIN A3 // set to 0 if backlight isn't available
DM8BA10* lcd;
void setup()
{
lcd = new DM8BA10(new CyrillicSlavicCharset(), CS_PIN, WR_PIN, DATA_PIN, BACKLIGHT_PIN);
}void println(String& str, Padding podType = Right)Fills all places on display and resets current position. This function is slower than print() and consumes extra memory.
str– input string (non-ASCII characters should be prepared viaCharset::remapfunction of the chose charset)padding– The alignment of strings shorter than display capacity:RightLeft,Both(center).
auto text = "Пример"; // 'example'
auto preparedText = lcd->getCharset()->remap(text);
lcd->println(preparedText, DM8BA10::Padding::Both);byte print(String& str, int8_t pos = -1)Prints the text starting from specified or current position (by default) to the end of the string or the indicator.
N.B. It doesn't overwrite places outside of specified string and can cause artifacts. Current position will be reset if the display is full.
str– input string (non-ASCII characters should be prepared viaCharset::remapfunction of the chose charset)pos– starting position at the display or-1to use current position
void scroll(String& text, word start = 0)Prints string starting at start and adds a space to the end of string.
Can be used to scroll text on the display.
text– input string (non-ASCII characters should be prepared viaCharset::remapfunction of the chose charset)start– staring position of the text to print. Should be incremented before call.
void loop()
{
auto text = "Lorem ipsum dolor sit amet";
static word strPos = 0;
static uint32_t lastUpd = 0;
auto nowMs = millis();
if (nowMs - lastUpd > 300)
{
lcd->scroll(text, strPos++);
if (strPos >= text.length())
strPos = 0;
lastUpd = nowMs;
}
}void drawChar(word ch, byte pos)Draws anything by filling segments, specified by bits in ch parameter at pos
ch– 16-bit encoded symbolpos- position of the symbol at the display
// let's draw 'T' letter in the middle of the screen
lcd->drawChar(0x84C0, 4);void setChar(const byte value, int8_t pos = -1)Displays specified character at specified position (or at current position if
pos == -1). Doesn't affect the current position.
value– 1-byte character. For non-ASCII characters use Charset::remap() function to get the correct value for used charset.pos– position of the symbol at the display
void point(byte index, bool on = true)Switch on or off decimal point at specified position.
N.B. 9 decimal points aren't part of common symbols, so they should be controlled separately.
index– index of the point from 0 to 8 (left to right)on– statetrue– on,false- off
void setPoint(byte index)Turns on decimal point at specified position, switching off all other.
index– index of the point from 0 to 8 (left to right)
void resetPoints()Turns off all the decimal points.
void allSegments(bool on = true)Turns on or off all the segments of every symbol.
void clearDisplay()Turns all the segments off and reset current position.
byte setPos(int8_t)Sets current position
0..9– symbol from left to right-1– most right place
String padString(String& text, Padding padType)Returns string padded with spaces to fill all the places on the display.
text– input String. Should be prepared withCharset::remap()function to avoid erroneous length detection.- padType – padding type:
RightLeft,Both
Charset* getCharset()Returns current Charset object to access its properties or methods.
void LCD(bool on = true)Turns or and off LCD.
void backlight(bool on = true)Turns on or off backlight.
void systemOscillator(bool on = true)Turns or and off HT-1622 built-in oscillator.
Charset-derived classes used to provide font for 16-segment indicators and auxiallry functions. You can use one of provided Charsets or create your own.
Just create new Charset children class instance and pass it to DM8BA10 driver.
For non-serial mapped and/or non-ASCII characters you should call remap() function
to clean-up UTF-8 extra bytes and map your symbols to match the font:
auto charset = new CyrillicMinCharset();
auto lcd = new DM8BA10(charset);
auto text = "Строка UTF-8"; // 'UTF-8 string'
auto preparedText = charset->remap(text);
lcd->println(preparedText);To create your own charset you need to derive from Charset class and implement
at two functions:
– word Char(word index) – should return 16-bit encoded symbol by its index or some special
character if the index is out of bounds
– word size() – should return your character table size
Firstly, you have to create your own font. To control every segment you have to use corresponding bits of big-endian 2-byte word:
The segments numbering starts from major (rightmost bit).
For instance, if we want to draw a scissor-like character:
We have to encode particular segments by setting corresponding bits:
0001011101011011b – this equals 0x175B in hexadecimal.
Thus, you can create any font for any character set you like.
Also, we can specify dummy character for symbols, that are absent from your font:
word dummy() – return bits for dummy character
First symbol in font usually is the space character with standard code 0x20,
but you can change starting font offset to any other you need by reimplementing
startingOffset() function.
ASCII characters are 1-byte wide, but if you need to use wider (e.g. UTF-8) symbols,
you'll have to implement remap function. Let's have a look to its implementation for
cyrillic charset:
String& remap(String& string) override
{
word curLen = 0;
byte prefix = 0;
// we iterate over the string
for (word i = 0; i < string.length(); i++)
{
byte ch = string[i];
// for cyrillic we can have one of these UTF-8 preambles
if (ch == 0xD0 || ch == 0xD1 || ch == 0xC3)
{
prefix = ch;
continue;
}
// when we find the character per se, let's check
// whether it needs to be remapped to other position
// fix for macOS uppercase 'double umlaut E' – Ë
if (prefix == 0xC3 && ch == 0x8B)
{
ch = 0x81;
}
else if (prefix == 0xD1)
{
// there's a weird gap between п (0xD0BF) and р (0xD180)
ch += 0x40;
}
prefix = 0;
string[curLen++] = ch;
}
string = string.substring(0, curLen);
return string;
}That's all, folks! ©
Feel free to fork, make pull requests and contact me)
Ilya 'road-t' Annikov © 2022



