From e6d874c734fb9ebec3de5c9b652425b704480a55 Mon Sep 17 00:00:00 2001 From: Dmitry Mikhirev Date: Mon, 27 Aug 2012 23:39:56 +0400 Subject: rewrite text convertion --- brainuino.ino | 397 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ brainuino.pde | 400 ---------------------------------------------------------- font_en.h | 0 font_ru-en.h | 160 +++++++++++++++++++++++ lcdprint.cpp | 52 ++++++++ lcdprint.h | 27 ++++ lcdrus.cpp | 120 ------------------ lcdrus.h | 28 ---- utf8.cpp | 63 +++++++++ utf8.h | 36 ++++++ 10 files changed, 735 insertions(+), 548 deletions(-) create mode 100644 brainuino.ino delete mode 100644 brainuino.pde create mode 100644 font_en.h create mode 100644 font_ru-en.h create mode 100644 lcdprint.cpp create mode 100644 lcdprint.h delete mode 100644 lcdrus.cpp delete mode 100644 lcdrus.h create mode 100644 utf8.cpp create mode 100644 utf8.h diff --git a/brainuino.ino b/brainuino.ino new file mode 100644 index 0000000..fe59074 --- /dev/null +++ b/brainuino.ino @@ -0,0 +1,397 @@ +/* + Brainuino Aleph + + Copyright (C) 2011 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +// undefine the following if your LCD does not support Russian font +#define RUSSIAN + +#include "pinout.h" +#include "game.h" +#include "lcdprint.h" + +LiquidCrystal lcd(LCD4, LCD6, LCD11, LCD12, LCD13, LCD14); + + +void setup() +{ + lcd.begin(16, 2); + pinInit(); + time = 0; + + // the following settings should be readen from EEPROM + gameType = SI; + withFalseStart = false; + timer1 = 5; + timer2 = 5; + preSignal = 0; +} + + +void loop() +{ + buttonPressed = 0; + startTime = 0; + timer = timer1; + printGameType(); + + // ensure that stop button is released + while (digitalRead(CONTROL2) == LOW) { + delay(20); + } + ask(); +} + + +void ask() +{ +// waiting while question is being asked + +#ifdef RUSSIAN + printState(convert("Задаётся вопрос ")); +#else + printState("Asking question "); +#endif + + while (true) { + + // if start button is pressed + if (digitalRead(CONTROL1) == LOW) { + discuss(); + return; + } + + // if stop button is pressed + if (digitalRead(CONTROL2) == LOW) { + return; + } + + // some other button could be pressed + switch (buttonPressed) { + case 0: + break; + case 1: + case 2: + case 3: + case 4: + if (withFalseStart) { + falseStart(buttonPressed); + } + else { + answer(buttonPressed); + } + } + + delay(100); + } +} + + +void discuss() +{ +// here teams can discuss question or single players think themselves + + // is random delay needed here when playing with false starts? unsure... + + time = 0; + + // reset timer or continue count + if (startTime == 0 || timer2 > 0) + startTime = millis(); + else + startTime = millis() - time; + + // we need sound signal here only if we are playing with false starts + // or am i wrong? + if (withFalseStart) + tone(SPEAKER, 3000, 1000); + + digitalWrite(GREENLAMP, HIGH); + printGameType(); + +#ifdef RUSSIAN + printState(convert("Отсчёт: ")); +#else + printState("Count: "); +#endif + + while (!buttonPressed && time < (timer - preSignal) * 1000) { + refresh(); + delay(25); + if (digitalRead(CONTROL2) == LOW) { + tone(SPEAKER, 1500, 500); + return; + } + } + if (!buttonPressed) + tone(SPEAKER, 2121, 1000); + while (!buttonPressed && time < timer * 1000) { + refresh(); + delay(25); + if (digitalRead(CONTROL2) == LOW) { + tone(SPEAKER, 1784, 500); + return; + } + } + + // the time is out or someone pressed a button... hmm... + switch (buttonPressed) { + case 1: + case 2: + case 3: + case 4: + answer(buttonPressed); + break; + case 0: + tone(SPEAKER, 3000, 1000); + } + digitalWrite(GREENLAMP, LOW); + digitalWrite(REDLAMP, LOW); + delay(1000); + return; +} + + +void answer(uint8_t num) { + +// waiting while answer is given + + digitalWrite(REDLAMP, HIGH); + if (timer2 > 0) + timer = timer2; + printPlayer(num); + +#ifdef RUSSIAN + printState(convert("Ответ ")); +#else + printState("Answer "); +#endif + + printPreciseTime(); + tone(SPEAKER, 2523, 1000); + buttonPressed = 0; + + // a pause is needed to release start button, if it was not yet + delay(2000); + + while (true) { + + // if start button was pressed + if (digitalRead(CONTROL1) == LOW) { + digitalWrite(REDLAMP, LOW); + buttonPressed = 0; + discuss(); + return; + } + + // if stop button was pressed + if (digitalRead(CONTROL2) == LOW) { + digitalWrite(REDLAMP, LOW); + buttonPressed = 0; + return; + } + + delay(100); + } +} + + +void falseStart(uint8_t num) { + +// if there is false start, show info about it during few time + + tone(SPEAKER, 1784, 1000); + digitalWrite(REDLAMP, HIGH); +#ifdef RUSSIAN + printState(convert(" Фальстарт ")); +#else + printState(" False start "); +#endif + printPlayer(num); + delay(3000); + digitalWrite(REDLAMP, LOW); + return; +} + + +void refresh() { + +// displaying current timer state + + time = millis() - startTime; + printTime(); +} + + +void printState(String state) { + +// displaying string in the beginning of LCD second row + + lcd.setCursor(0, 1); + lcd.print(state); +} + + +void printTime() { + +// displaying time passed after starting timer + + String timestr; + + timestr = String(time/1000) + '.' + String((time%1000)/100); + lcd.setCursor(8, 1); + lcd.print(timestr); +} + + +void printPreciseTime() { + +// displaying time passed after starting timer +// or that it was not started yet + + String timestr; + + // if timer was started + if (startTime > 0) { + time = millis()-startTime; + timestr = String(time/1000) + '.' + String(time%1000); + } + // if it was not + else + +#ifdef RUSSIAN + timestr = convert("досрочно"); +#else + timestr = "prematur"; +#endif + + lcd.setCursor(8, 1); + lcd.print(timestr); +} + + +void readButton() { + +// scan buttons + + if (buttonPressed > 0) + return; + if (digitalRead(BUTTON1) == LOW) { + buttonPressed = 1; + return; + } + if (digitalRead(BUTTON2) == LOW) { + buttonPressed = 2; + return; + } + if (digitalRead(BUTTON3) == LOW) { + buttonPressed = 3; + return; + } + if (digitalRead(BUTTON4) == LOW) { + buttonPressed = 4; + return; + } +} + + +void pinInit() { + +// Arduino I/O initialisation, needed on startup only + + digitalWrite(2, HIGH); + digitalWrite(3, HIGH); + digitalWrite(BUTTON1, HIGH); + digitalWrite(BUTTON2, HIGH); + digitalWrite(BUTTON3, HIGH); + digitalWrite(BUTTON4, HIGH); + digitalWrite(CONTROL1, HIGH); + digitalWrite(CONTROL2, HIGH); + digitalWrite(ENCPUSH, HIGH); + pinMode(GREENLAMP, OUTPUT); + pinMode(REDLAMP, OUTPUT); + pinMode(SPEAKER, OUTPUT); + attachInterrupt(0, readButton, FALLING); +} + + +void printGameType() { + +// displaying game title on LCD + + lcd.setCursor(0, 0); + switch (gameType) { + case BRAIN: + +#ifdef RUSSIAN + lcd.print(convert(" Брейн-ринг ")); +#else + lcd.print(" Brain-ring "); +#endif + + break; + case SI: + +#ifdef RUSSIAN + lcd.print(convert(" Своя игра ")); +#else + lcd.print(" Jeopardy "); +#endif + + break; + case CHGK: + +#ifdef RUSSIAN + lcd.print(convert("Что? Где? Когда?")); +#else + lcd.print("What?Where?When?"); +#endif + + break; + } +} + + +void printPlayer(uint8_t num) { + +// displaying number of player or team + + lcd.setCursor(0, 0); + switch (gameType) { + case BRAIN: + +#ifdef RUSSIAN + lcd.print(convert("Команда ") + String(int(num)) + " "); +#else + lcd.print("Team " + String(int(num)) + " "); +#endif + + break; + case SI: + +#ifdef RUSSIAN + lcd.print(convert("Игрок ") + String(int(num)) + " "); +#else + lcd.print("Player " + String(int(num) + " ")); +#endif + + break; + } + buttonPressed = 0; +} diff --git a/brainuino.pde b/brainuino.pde deleted file mode 100644 index e935d3f..0000000 --- a/brainuino.pde +++ /dev/null @@ -1,400 +0,0 @@ -/* - Brainuino Aleph - - Copyright (C) 2011 Dmitry Mikhirev - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -// undefine the following if your LCD does not support Russian font -#define RUSSIAN - -#include "pinout.h" -#include "game.h" - -#ifdef RUSSIAN -#include "lcdrus.h" -#endif - -LiquidCrystal lcd(LCD4, LCD6, LCD11, LCD12, LCD13, LCD14); - - -void setup() -{ - lcd.begin(16, 2); - pinInit(); - time = 0; - - // the following settings should be readen from EEPROM - gameType = SI; - withFalseStart = false; - timer1 = 5; - timer2 = 5; - preSignal = 0; -} - - -void loop() -{ - buttonPressed = 0; - startTime = 0; - timer = timer1; - printGameType(); - - // ensure that stop button is released - while (digitalRead(CONTROL2) == LOW) { - delay(20); - } - ask(); -} - - -void ask() -{ -// waiting while question is being asked - -#ifdef RUSSIAN - printState(convert("Задаётся вопрос ")); -#else - printState("Asking question "); -#endif - - while (true) { - - // if start button is pressed - if (digitalRead(CONTROL1) == LOW) { - discuss(); - return; - } - - // if stop button is pressed - if (digitalRead(CONTROL2) == LOW) { - return; - } - - // some other button could be pressed - switch (buttonPressed) { - case 0: - break; - case 1: - case 2: - case 3: - case 4: - if (withFalseStart) { - falseStart(buttonPressed); - } - else { - answer(buttonPressed); - } - } - - delay(100); - } -} - - -void discuss() -{ -// here teams can discuss question or single players think themselves - - // is random delay needed here when playing with false starts? unsure... - - time = 0; - - // reset timer or continue count - if (startTime == 0 || timer2 > 0) - startTime = millis(); - else - startTime = millis() - time; - - // we need sound signal here only if we are playing with false starts - // or am i wrong? - if (withFalseStart) - tone(SPEAKER, 3000, 1000); - - digitalWrite(GREENLAMP, HIGH); - printGameType(); - -#ifdef RUSSIAN - printState(convert("Отсчёт: ")); -#else - printState("Count: "); -#endif - - while (!buttonPressed && time < (timer - preSignal) * 1000) { - refresh(); - delay(25); - if (digitalRead(CONTROL2) == LOW) { - tone(SPEAKER, 1500, 500); - return; - } - } - if (!buttonPressed) - tone(SPEAKER, 2121, 1000); - while (!buttonPressed && time < timer * 1000) { - refresh(); - delay(25); - if (digitalRead(CONTROL2) == LOW) { - tone(SPEAKER, 1784, 500); - return; - } - } - - // the time is out or someone pressed a button... hmm... - switch (buttonPressed) { - case 1: - case 2: - case 3: - case 4: - answer(buttonPressed); - break; - case 0: - tone(SPEAKER, 3000, 1000); - } - digitalWrite(GREENLAMP, LOW); - digitalWrite(REDLAMP, LOW); - delay(1000); - return; -} - - -void answer(uint8_t num) { - -// waiting while answer is given - - digitalWrite(REDLAMP, HIGH); - if (timer2 > 0) - timer = timer2; - printPlayer(num); - -#ifdef RUSSIAN - printState(convert("Ответ ")); -#else - printState("Answer "); -#endif - - printPreciseTime(); - tone(SPEAKER, 2523, 1000); - buttonPressed = 0; - - // a pause is needed to release start button, if it was not yet - delay(2000); - - while (true) { - - // if start button was pressed - if (digitalRead(CONTROL1) == LOW) { - digitalWrite(REDLAMP, LOW); - buttonPressed = 0; - discuss(); - return; - } - - // if stop button was pressed - if (digitalRead(CONTROL2) == LOW) { - digitalWrite(REDLAMP, LOW); - buttonPressed = 0; - return; - } - - delay(100); - } -} - - -void falseStart(uint8_t num) { - -// if there is false start, show info about it during few time - - tone(SPEAKER, 1784, 1000); - digitalWrite(REDLAMP, HIGH); -#ifdef RUSSIAN - printState(convert(" Фальстарт ")); -#else - printState(" False start "); -#endif - printPlayer(num); - delay(3000); - digitalWrite(REDLAMP, LOW); - return; -} - - -void refresh() { - -// displaying current timer state - - time = millis() - startTime; - printTime(); -} - - -void printState(String state) { - -// displaying string in the beginning of LCD second row - - lcd.setCursor(0, 1); - lcd.print(state); -} - - -void printTime() { - -// displaying time passed after starting timer - - String timestr; - - timestr = String(time/1000) + '.' + String((time%1000)/100); - lcd.setCursor(8, 1); - lcd.print(timestr); -} - - -void printPreciseTime() { - -// displaying time passed after starting timer -// or that it was not started yet - - String timestr; - - // if timer was started - if (startTime > 0) { - time = millis()-startTime; - timestr = String(time/1000) + '.' + String(time%1000); - } - // if it was not - else - -#ifdef RUSSIAN - timestr = convert("досрочно"); -#else - timestr = "prematur"; -#endif - - lcd.setCursor(8, 1); - lcd.print(timestr); -} - - -void readButton() { - -// scan buttons - - if (buttonPressed > 0) - return; - if (digitalRead(BUTTON1) == LOW) { - buttonPressed = 1; - return; - } - if (digitalRead(BUTTON2) == LOW) { - buttonPressed = 2; - return; - } - if (digitalRead(BUTTON3) == LOW) { - buttonPressed = 3; - return; - } - if (digitalRead(BUTTON4) == LOW) { - buttonPressed = 4; - return; - } -} - - -void pinInit() { - -// Arduino I/O initialisation, needed on startup only - - digitalWrite(2, HIGH); - digitalWrite(3, HIGH); - digitalWrite(BUTTON1, HIGH); - digitalWrite(BUTTON2, HIGH); - digitalWrite(BUTTON3, HIGH); - digitalWrite(BUTTON4, HIGH); - digitalWrite(CONTROL1, HIGH); - digitalWrite(CONTROL2, HIGH); - digitalWrite(ENCPUSH, HIGH); - pinMode(GREENLAMP, OUTPUT); - pinMode(REDLAMP, OUTPUT); - pinMode(SPEAKER, OUTPUT); - attachInterrupt(0, readButton, FALLING); -} - - -void printGameType() { - -// displaying game title on LCD - - lcd.setCursor(0, 0); - switch (gameType) { - case BRAIN: - -#ifdef RUSSIAN - lcd.print(convert(" Брейн-ринг ")); -#else - lcd.print(" Brain-ring "); -#endif - - break; - case SI: - -#ifdef RUSSIAN - lcd.print(convert(" Своя игра ")); -#else - lcd.print(" Jeopardy "); -#endif - - break; - case CHGK: - -#ifdef RUSSIAN - lcd.print(convert("Что? Где? Когда?")); -#else - lcd.print("What?Where?When?"); -#endif - - break; - } -} - - -void printPlayer(uint8_t num) { - -// displaying number of player or team - - lcd.setCursor(0, 0); - switch (gameType) { - case BRAIN: - -#ifdef RUSSIAN - lcd.print(convert("Команда ") + String(int(num)) + " "); -#else - lcd.print("Team " + String(int(num)) + " "); -#endif - - break; - case SI: - -#ifdef RUSSIAN - lcd.print(convert("Игрок ") + String(int(num)) + " "); -#else - lcd.print("Player " + String(int(num) + " ")); -#endif - - break; - } - buttonPressed = 0; -} diff --git a/font_en.h b/font_en.h new file mode 100644 index 0000000..e69de29 diff --git a/font_ru-en.h b/font_ru-en.h new file mode 100644 index 0000000..2b6c785 --- /dev/null +++ b/font_ru-en.h @@ -0,0 +1,160 @@ +/* + Brainuino Aleph + + Copyright (C) 2012 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef en_ru_h +#define en_ru_h + +#include + +struct charcode { + const int32_t uni; + const char font; +}; + +const charcode charmap[] PROGMEM = { +// tilde + {0x007e, 0xe9}, +// latin-1 supplement + {0x0083, 0xce}, + {0x00a2, 0x5c}, + {0x00a7, 0xfd}, + {0x00a8, 0xe8}, + {0x00ab, 0xc8}, + {0x00b4, 0xe7}, + {0x00b6, 0xfe}, + {0x00b7, 0xdf}, + {0x00bb, 0xc9}, + {0x00bc, 0xf0}, + {0x00bd, 0xf2}, + {0x00be, 0xf3}, + {0x00bf, 0xcd}, + {0x00d7, 0xd5}, + {0x00e7, 0xeb}, + {0x00e9, 0xea}, + {0x00eb, 0xb5}, +// greek + {0x0391, 0x41}, + {0x0392, 0x42}, + {0x0393, 0xa1}, + {0x0395, 0x45}, + {0x0396, 0x5a}, + {0x0397, 0x48}, + {0x0399, 0x49}, + {0x039a, 0x4b}, + {0x039c, 0x4d}, + {0x039d, 0x4e}, + {0x039f, 0x4f}, + {0x03a0, 0xa8}, + {0x03a1, 0x50}, + {0x03a4, 0x54}, + {0x03a5, 0x59}, + {0x03a6, 0xaa}, + {0x03a7, 0x58}, + {0x03ba, 0xba}, + {0x03bf, 0x6f}, + {0x03c0, 0xbe}, + {0x03c1, 0x70}, +// cyryllic + {0x0401, 0xa2}, + {0x0410, 0x41}, + {0x0411, 0xa0}, + {0x0412, 0x42}, + {0x0413, 0xa1}, + {0x0414, 0xe0}, + {0x0415, 0x45}, + {0x0416, 0xa3}, + {0x0417, 0xa4}, + {0x0418, 0xa5}, + {0x0419, 0xa6}, + {0x041a, 0x4b}, + {0x041b, 0xa7}, + {0x041c, 0x4d}, + {0x041e, 0x4f}, + {0x041f, 0xa8}, + {0x0420, 0x50}, + {0x0421, 0x43}, + {0x0422, 0x54}, + {0x0423, 0xa9}, + {0x0424, 0xaa}, + {0x0425, 0x58}, + {0x0426, 0xe1}, + {0x0427, 0xab}, + {0x0428, 0xac}, + {0x0429, 0xe2}, + {0x042a, 0xad}, + {0x042b, 0xae}, + {0x042c, 0x62}, + {0x042d, 0xaf}, + {0x042e, 0xb0}, + {0x042f, 0xb1}, + {0x0430, 0x61}, + {0x0431, 0xb2}, + {0x0432, 0xb3}, + {0x0433, 0xb4}, + {0x0434, 0xe3}, + {0x0435, 0x65}, + {0x0436, 0xb6}, + {0x0437, 0xb7}, + {0x0438, 0xb8}, + {0x0439, 0xb9}, + {0x043a, 0xba}, + {0x043b, 0xbb}, + {0x043c, 0xbc}, + {0x043d, 0xbd}, + {0x043e, 0x6f}, + {0x043f, 0xbf}, + {0x0440, 0x70}, + {0x0441, 0x63}, + {0x0442, 0xbf}, + {0x0443, 0x79}, + {0x0444, 0xe4}, + {0x0445, 0x78}, + {0x0446, 0xe5}, + {0x0447, 0xc0}, + {0x0448, 0xc1}, + {0x0449, 0xe6}, + {0x044a, 0xc2}, + {0x044b, 0xc3}, + {0x044c, 0xc4}, + {0x044d, 0xc5}, + {0x044e, 0xc6}, + {0x044f, 0xc7}, + {0x0451, 0xb5}, +// general punctuation + {0x2010, 0x2e}, + {0x2011, 0x2e}, + {0x2012, 0x2e}, + {0x2013, 0x2e}, + {0x2014, 0x2e}, + {0x2015, 0x2e}, + {0x201c, 0xca}, + {0x201d, 0xcb}, +// one third + {0x2153, 0xf1}, +// arrows + {0x2191, 0xd9}, + {0x2193, 0xda}, + {0x21b5, 0x7e}, +// mathematical operators + {0x2212, 0x2e}, + {0x2218, 0xef}, + {0x22c5, 0xdf} +}; + +#endif diff --git a/lcdprint.cpp b/lcdprint.cpp new file mode 100644 index 0000000..585b2de --- /dev/null +++ b/lcdprint.cpp @@ -0,0 +1,52 @@ +/* + Brainuino Aleph + + Copyright (C) 2011-2012 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +#include "lcdprint.h" +#include "font_ru-en.h" +//#include "font_en.h" + +char* convert(utf8 str) +{ + int32_t ucode; + int i, j; + int numcodes = sizeof(charmap)/sizeof(charcode); + char result[str.chars]; + + for (i = 0; i < str.chars; i++) { + ucode = str.get(); + if (ucode > 0x0000) { + if (ucode <= 0x007d) { + result[i] = char(ucode); + } else { + result[i] = 0xff; + for (j = 0; j < numcodes && charmap[j].uni < ucode; j++) { + if (charmap[j].uni == ucode) { + result[i] = charmap[j].font; + } + } + } + } else { + result[i] = 0x00; + break; + } + } + + return result; +} diff --git a/lcdprint.h b/lcdprint.h new file mode 100644 index 0000000..b76c425 --- /dev/null +++ b/lcdprint.h @@ -0,0 +1,27 @@ +/* + Brainuino Aleph + + Copyright (C) 2011-2012 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef lcdprint_h +#define lcdprint_h + +#include "utf8.h" + +char* convert (utf8); + +#endif diff --git a/lcdrus.cpp b/lcdrus.cpp deleted file mode 100644 index b12a108..0000000 --- a/lcdrus.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - Brainuino Aleph - - Copyright (C) 2011 Dmitry Mikhirev - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "lcdrus.h" - -String convert(String str) -{ - struct toFrom { - prog_char to; - prog_char from[5]; - }; - toFrom symbols[] PROGMEM = {{0x41, "А"}, - {0x42, "В"}, - {0x43, "С"}, - {0x45, "Е"}, - {0x48, "Н"}, - {0x4B, "К"}, - {0x4D, "М"}, - {0x4F, "О"}, - {0x50, "Р"}, - {0x54, "Т"}, - {0x58, "Х"}, - {0x61, "а"}, - {0x62, "Ь"}, - {0x63, "с"}, - {0x65, "е"}, - {0x6F, "о"}, - {0x70, "р"}, - {0x78, "х"}, - {0x79, "у"}, - {0x7E, "↵"}, - {0xA0, "Б"}, - {0xA1, "Г"}, - {0xA2, "Ё"}, - {0xA3, "Ж"}, - {0xA4, "З"}, - {0xA5, "И"}, - {0xA6, "Й"}, - {0xA7, "Л"}, - {0xA8, "П"}, - {0xA9, "У"}, - {0xAA, "Ф"}, - {0xAB, "Ч"}, - {0xAC, "Ш"}, - {0xAD, "Ъ"}, - {0xAE, "Ы"}, - {0xAF, "Э"}, - {0xB0, "Ю"}, - {0xB1, "Я"}, - {0xB2, "б"}, - {0xB3, "в"}, - {0xB4, "г"}, - {0xB5, "ё"}, - {0xB6, "ж"}, - {0xB7, "з"}, - {0xB8, "и"}, - {0xB9, "й"}, - {0xBA, "к"}, - {0xBB, "л"}, - {0xBC, "м"}, - {0xBD, "н"}, - {0xBE, "п"}, - {0xBF, "т"}, - {0xC0, "ч"}, - {0xC1, "ш"}, - {0xC2, "ъ"}, - {0xC3, "ы"}, - {0xC4, "ь"}, - {0xC5, "э"}, - {0xC6, "ю"}, - {0xC7, "я"}, - {0xC8, "«"}, - {0xC9, "»"}, - {0xCA, "〟"}, - {0xCB, "〞"}, - {0xCD, "¿"}, - {0xCE, "ƒ"}, - {0xCF, "£"}, - {0xD3, "‼"}, - {0xD9, "↑"}, - {0xDA, "↓"}, - {0xE0, "Д"}, - {0xE1, "Ц"}, - {0xE2, "Щ"}, - {0xE3, "д"}, - {0xE4, "ф"}, - {0xE5, "ц"}, - {0xE6, "щ"}, - {0xE9, "~"}, - {0xEA, "é"}, - {0xEB, "ç"}, - {0xF0, "¼"}, - {0xF1, "⅓"}, - {0xF2, "½"}, - {0xF3, "¾"}, - {0xFD, "§"}, - {0xFE, "¶"}}; - - uint8_t i; - - for (i = 0; i < sizeof(symbols)/sizeof(toFrom); i++) - str = str.replace((char*)symbols[i].from, symbols[i].to); - return str; -} diff --git a/lcdrus.h b/lcdrus.h deleted file mode 100644 index d0feb5e..0000000 --- a/lcdrus.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - Brainuino Aleph - - Copyright (C) 2011 Dmitry Mikhirev - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef lcdrus_h -#define lcdrus_h - -#include -#include - -String convert (String); - -#endif diff --git a/utf8.cpp b/utf8.cpp new file mode 100644 index 0000000..026d16e --- /dev/null +++ b/utf8.cpp @@ -0,0 +1,63 @@ +/* + Brainuino Aleph + + Copyright (C) 2012 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "utf8.h" +#include + +utf8::utf8 (char* string) +{ + bytes = strlen(string); + _string = (char *)malloc(bytes + 1); + strcpy(_string, string); + chars = 0; + for (_index = 0; _index < bytes; _index++) { + if (_string[_index] & 0x80 == 0) { + chars++; + } else if (_string[_index] & 0x20 == 0) { + chars += 2; + _index++; + } else if (_string[_index] & 0x10 == 0) { + chars += 3; + _index += 2; + } else if (_string[_index] & 0x8 == 0) { + chars += 4; + _index += 3; + } + } + _index = 0; +} + +int32_t utf8::get() +{ + int32_t code; + if (_string[_index] & 0x80 == 0) { + code = int32_t(_string[_index]); + _index++; + } else if (_string[_index] & 0x20 == 0) { + code = int32_t(_string[_index] & 0x1f) << 6 | int32_t(_string[_index+1] & 0x3f); + _index += 2; + } else if (_string[_index] & 0x10 == 0) { + code = int32_t(_string[_index] & 0xf) << 12 | int32_t(_string[_index+1] & 0x3f) << 6 | int32_t(_string[_index+2] & 0x3f); + _index += 3; + } else if (_string[_index] & 0x8 == 0) { + code = int32_t(_string[_index] & 0x7) << 18 | int32_t(_string[_index+1] & 0x3f) << 12 | int32_t(_string[_index+2] & 0x3f) << 6 | int32_t(_string[_index+3] & 0x3f); + } + if (_index > bytes) _index = 0; + return code; +} diff --git a/utf8.h b/utf8.h new file mode 100644 index 0000000..5dbc221 --- /dev/null +++ b/utf8.h @@ -0,0 +1,36 @@ +/* + Brainuino Aleph + + Copyright (C) 2012 Dmitry Mikhirev + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef utf8_h +#define utf8_h + +#include "Arduino.h" + +class utf8 { + public: + utf8(char* string); + uint16_t chars; + uint16_t bytes; + int32_t get(); + private: + char* _string; + uint16_t _index; +}; + +#endif -- cgit v1.2.1