
В данной статье мы рассмотрим как можно русифицировать библиотеку Adafruit_GFX. Статья основана на работе пользователя arduinec с форума arduino.ru
Хотите узнать, как использовать русские символы при работе с пиксельными LCD дисплеями на Arduino? Добро пожаловать под кат.
Основной и наиболее удобной библиотекой для работы с LCD пиксельными дисплеями является библиотека Adafruit GFX.
Данная библиотека чисто графическая, поэтому для работы с определенной моделью дисплея вместе с ней стоит использовать еще одну специализированную библиотеку.
Например, для Nokia 5110 дисплея это Adafruit-PCD8544, Для OLED 0.96" OLED 128x64 - Adafruit_SSD1306. Библиотеки для других моделей можно найти здесь (или здесь: список).
Итак, вы подключили свой дисплей, в скетче добавили две библиотеки, о которых шла речь выше и пробуете вывести русские буквы. Но вот незадача - вместо них ваш дисплей отображает всякие кракозябры. Это происходит потому, что в библиотеках от Adafruit нет русского шрифта. Добавить его совсем не сложно. Нужно заменить в файле glcdfont.c из библиотеки Adafruit GFX определенные символы на русские в нужной кодировке. Однако, шрифт рассчитан на однобайтную кодировку букв, а Arduino IDE использует для русских букв двухбайтовую UTF-8,
В русской кодировке UTF-8 прослеживается определенная закономерность, которая позволяет нехитрым образом преобразовывать из UTF-8 в однобайтовую русскую кодировку Windows-1251, которая и была выбрана для замены букв.
Функция utf8rus() получает исходную строку, символы с кодами 0x00-0xBF пропускает без изменения в выходную строку, а в оставшихся кодах отбирает русские буквы и перекодирует их.
Функция перекодировки русских букв из UTF-8 в Win-1251
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
int i,k;
String target;
unsigned char n;
char m[2] = { '0', '\0' };
k = source.length(); i = 0;
while (i < k) {
n = source[i]; i++;
if (n >= 0xC0) {
switch (n) {
case 0xD0: {
n = source[i]; i++;
if (n == 0x81) { n = 0xA8; break; }
if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
break;
}
case 0xD1: {
n = source[i]; i++;
if (n == 0x91) { n = 0xB8; break; }
if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
break;
}
}
}
m[0] = n; target = target + String(m);
}
return target;
}
Таким образом, для русификации вашего дисплея необходимо:
- скачать любой из приведённых ниже (или выше) архивов;
- заменить файл glcdfont.c в Adafruit-GFX на вот этот;
- добавить функцию utf8rus() в скетч;
- вставить в начале скетча команду: display.cp437(true);
Применять функцию utf8rus() можно внутри команд печати строк:
display.println(utf8rus("Тест"));
Примеры для разных дисплеев
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
void setup() {
Serial.begin(9600);
display.begin();
display.cp437(true);
// init done
// you can change the contrast around to adapt the display
// for the best viewing!
display.setContrast(60);
display.display(); // show splashscreen
delay(2000);
// russian font
display.clearDisplay();
display.setTextColor(BLACK);
display.setCursor(0,0);
display.setTextSize(1);
display.println(utf8rus("Шрифт1"));
display.setTextSize(2);
display.println(utf8rus("Шрифт2"));
display.setTextSize(3);
display.println(utf8rus("Шр3"));
display.display();
delay(5000);
}
void loop() {
}
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
int i,k;
String target;
unsigned char n;
char m[2] = { '0', '\0' };
k = source.length(); i = 0;
while (i < k) {
n = source[i]; i++;
if (n >= 0xC0) {
switch (n) {
case 0xD0: {
n = source[i]; i++;
if (n == 0x81) { n = 0xA8; break; }
if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
break;
}
case 0xD1: {
n = source[i]; i++;
if (n == 0x91) { n = 0xB8; break; }
if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
break;
}
}
}
m[0] = n; target = target + String(m);
}
return target;
}
2.8" TFT Touch Shield 320x240
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
//Technical support:goodtft@163.com
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
void setup(void) {
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
#else
Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
#endif
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x4535) {
Serial.println(F("Found LGDP4535 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else if(identifier==0x0101) {
identifier=0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
identifier=0x9341;
}
tft.begin(identifier);
tft.cp437(true);
}
void loop(void) {
TEST_display_1();
delay(5000);
TEST_display_2();
delay(10000);
tft.setRotation(tft.getRotation()-1);
TEST_display_3();
delay(5000);
tft.setRotation(tft.getRotation()+1);
}
void TEST_display_1()
{
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(1);
tft.println(utf8rus("Шрифт 1 (самый маленький)"));
tft.println(01234.56789);
tft.println();
tft.println();
tft.setTextColor(YELLOW); tft.setTextSize(2);
tft.println(utf8rus("Шрифт 2"));
tft.println(01234.56789);
tft.println();
tft.setTextColor(CYAN); tft.setTextSize(3);
tft.println(utf8rus("Шрифт 3"));
tft.println(01234.56789);
tft.println();
tft.setTextColor(RED); tft.setTextSize(4);
tft.println(utf8rus("Шрифт 4"));
tft.setTextColor(GREEN); tft.setTextSize(5);
tft.println(utf8rus("Шрифт 5"));
tft.setTextColor(BLUE); tft.setTextSize(6);
tft.println(utf8rus("Шрифт6"));
}
void TEST_display_2()
{
byte i1,i2;
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.setTextColor(GREEN);
tft.setTextSize(2);
for(i1=0; i1<16; i1++) {
for(i2=0; i2<16; i2++)
if((i1 == 0) && (i2 == 0x0A || i2 == 0x0D)) tft.write(0x20);
else tft.write(i1*16+i2);
tft.println("");
}
}
void TEST_display_3()
{
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.println(utf8rus("АБВГДЕЖЗИЙКЛМНОП"));
tft.println(utf8rus("РСТУФХЦЧШЩЪЫЬЭЮЯ"));
tft.println(utf8rus("абвгдежзийклмноп"));
tft.println(utf8rus("рстуфхцчшщъыьэюя"));
tft.println(utf8rus("Ёё123ABCabc!@#\xBC\xBD"));
tft.println(utf8rus("10\x83 10\x8A\x82 10\x81\x80 2\x85"));
}
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
int i,k;
String target;
unsigned char n;
char m[2] = { '0', '\0' };
k = source.length(); i = 0;
while (i < k) {
n = source[i]; i++;
if (n >= 0xC0) {
switch (n) {
case 0xD0: {
n = source[i]; i++;
if (n == 0x81) { n = 0xA8; break; }
if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
break;
}
case 0xD1: {
n = source[i]; i++;
if (n == 0x91) { n = 0xB8; break; }
if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
break;
}
}
}
m[0] = n; target = target + String(m);
}
return target;
}
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// software SPI:
#define OLED_MOSI 12
#define OLED_CLK 13
#define OLED_DC 10
#define OLED_CS 9
#define OLED_RESET 11
unsigned char i1,i2,c3;
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup(){
display.begin(SSD1306_SWITCHCAPVCC);
display.cp437(true);
}
void loop() {
TEST_display_1();
delay(10000);
TEST_display_2();
delay(10000);
TEST_display_3();
delay(8000);
TEST_display_4();
delay(4000);
}
void TEST_display_1()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
for(i1=0; i1<8; i1++) {
for(i2=0; i2<16; i2++) {
c3=i1*16+i2;
if(c3 == 0x0A || c3 == 0x0D) display.print(" ");
else display.write(c3);
}
display.println("");
}
display.display();
}
void TEST_display_2()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
for(i1=8; i1<16; i1++) {
for(i2=0; i2<16; i2++)
display.write(i1*16+i2);
display.println("");
}
display.display();
}
void TEST_display_3()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.println(utf8rus("АБВГДЕЖЗИЙКЛМНОП"));
display.println(utf8rus("РСТУФХЦЧШЩЪЫЬЭЮЯ"));
display.println(utf8rus("абвгдежзийклмноп"));
display.println(utf8rus("рстуфхцчшщъыьэюя"));
display.println(utf8rus("Ёё123ABCabc!@#\xBC\xBD"));
display.println(utf8rus("10\x83 10\x8A\x82 10\x81\x80 2\x85"));
display.display();
}
void TEST_display_4()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setTextSize(1);
display.println(utf8rus("Размер шрифта 1"));
display.setTextSize(2);
display.println(utf8rus("Размер 2"));
display.setTextSize(3);
display.println(utf8rus("Разм 3"));
display.display();
}
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
int i,k;
String target;
unsigned char n;
char m[2] = { '0', '\0' };
k = source.length(); i = 0;
while (i < k) {
n = source[i]; i++;
if (n >= 0xC0) {
switch (n) {
case 0xD0: {
n = source[i]; i++;
if (n == 0x81) { n = 0xA8; break; }
if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
break;
}
case 0xD1: {
n = source[i]; i++;
if (n == 0x91) { n = 0xB8; break; }
if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
break;
}
}
}
m[0] = n; target = target + String(m);
}
return target;
}
В данный момент еще реализованы не все элементы нашего сообщества.
Мы активно работаем над ним и в ближайшее время возможность комментирования статей будет добавлена.