Drawing test for Japanese UTF-8 string literal using GT20L16J1Y font ROM.

Dependencies:   C12832 GT20L16J1Y_font mbed

Fork of hello_GT20L16J1Y_FONT by Toyomasa Watarai

/media/uploads/MACRUM/10441061_10202364303981428_7731757494769382505_n.jpg

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Mon Aug 04 02:15:06 2014 +0000
Parent:
2:58409820b1c5
Commit message:
Initial release

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
C12832_lcd.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 58409820b1c5 -r 722665b5efc7 C12832.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Mon Aug 04 02:15:06 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/chris/code/C12832/#7de323fa46fe
diff -r 58409820b1c5 -r 722665b5efc7 C12832_lcd.lib
--- a/C12832_lcd.lib	Wed Jan 15 07:05:48 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/users/dreschpe/code/C12832_lcd/#8f86576007d6
diff -r 58409820b1c5 -r 722665b5efc7 main.cpp
--- a/main.cpp	Wed Jan 15 07:05:48 2014 +0000
+++ b/main.cpp	Mon Aug 04 02:15:06 2014 +0000
@@ -1,13 +1,21 @@
 #include "mbed.h"
-#include "C12832_lcd.h"
+#include "C12832.h"
 #include "GT20L16J1Y_font.h"
 
+#include <locale.h>
+#include <cwchar>
+ 
+#pragma import __use_all_ctype
+ 
+char  buf_s[80];
+wchar_t wstr[80];
+
 /* 
     GT20L16J1Y library test program
     works with mbed application board
 */
 
-C12832_LCD lcd;
+C12832 lcd(p5, p7, p6, p8, p11);
 GT20L16J1Y_FONT font(p11, p12, p13, p10);
 
 void draw_kanji(int offset_x, int offset_y)
@@ -27,24 +35,63 @@
     lcd.copy_to_lcd();
 }
 
+static int utf8tosjis(const char* utfBuffer, int utfBufLen, char* sjisBuffer, int sjisBufLen)
+{
+    int         i, wi;
+    wchar_t     wc;
+    mbstate_t   state = {0};
+    size_t      ret;
+    char *      current_locale;
+    i = wi = 0;
+ 
+    current_locale = setlocale(LC_CTYPE, "UTF-8");
+    if (current_locale == NULL)
+        return 0;
+ 
+    while (1) {
+        ret = mbrtowc(&wc, utfBuffer+i, 3, &state);
+        if (ret == (size_t)-2 || ret == (size_t)-1) {
+            //printf("\nThere was a problem decoding the multibyte string.\n");
+            return ret;
+        } else if (ret == 0) {
+            break;          /* we hit \0, end of string */
+        } else {
+            i += ret;
+            wstr[wi++] = wc;
+        }
+    }
+    wstr[wi] = L'\0';
+    
+    current_locale = setlocale(LC_CTYPE, "SJIS");
+    if (current_locale == NULL)
+        return 0;
+ 
+    ret = wcstombs(sjisBuffer, wstr, sjisBufLen);
+ 
+    return ret;
+}
+
+void draw_utf8(int offset_x, int offset_y, char *buf_u)
+{
+    size_t sz;
+    
+    // assuming UTF-8 is NULL terminated
+    sz = strlen(buf_u);
+    int len = utf8tosjis(buf_u, sz, buf_s, sizeof(buf_s));
+
+    for(int i = 0; i < len; i += 2) {
+        font.read((buf_s[i] << 8) | buf_s[i+1]); 
+        draw_kanji(i*8, 0);
+    }
+}
+
+
 int main()
 {
-    unsigned short kbuf[16] = {
-        0x9069,
-        0x92BB,
-        0x82C7,
-        0x82A4,
-        0x82C5,
-        0x82B7,
-        0x82A9,
-        0x8148
-    };
-        
     lcd.cls();
 
-    for(int i=0; i<8; i++) {
-        font.read(kbuf[i]); 
-        draw_kanji(16*i, 0);
-    }
-    
+    draw_utf8(0, 0, "進捗どうですか?");
+    printf("進捗ありません。\n");
+
+    while(1);
 }