| 1 | == 日本語フォントROM GT20L16J1Y の使い方 == |
| 2 | |
| 3 | [https://www.switch-science.com/catalog/1610/ 日本語フォントROM GT20L16J1Y Breakout]でも販売している日本語フォントROM「GT20L16J1Y」の詳細説明と、サンプルプログラムです。[[BR]] |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | == サンプルプログラム == |
| 10 | Arduino IDE のシリアルモニタから送った日本語を、 |
| 11 | ROMから読みだしてシリアルモニタに返すプログラムです。 |
| 12 | 半角カナには対応していません。 |
| 13 | {{{ |
| 14 | /* GT20L16J1Y用サンプルプログラム |
| 15 | * Arduinoのシリアルモニタから送信されたSJISの文字を |
| 16 | * ROMから読みだしてシリアルモニタに返す |
| 17 | * 半角カナまわりはデータにはあるがROM内の順とJISX規格での順が |
| 18 | * 一致しないため未実装 |
| 19 | * |
| 20 | * update:9/30 0:00 |
| 21 | */ |
| 22 | |
| 23 | #include <SPI.h> |
| 24 | unsigned char matrixdata32[32]; //16×16用表示データ |
| 25 | unsigned char matrixdata16[16]; //16×8用表示データ |
| 26 | |
| 27 | void setup (void) |
| 28 | { |
| 29 | pinMode (SS, OUTPUT); |
| 30 | Serial.begin(115200); |
| 31 | /*SPI通信の設定*/ |
| 32 | SPI.begin (); |
| 33 | SPI.setBitOrder(MSBFIRST); |
| 34 | SPI.setDataMode(3); |
| 35 | SPI.setClockDivider(SPI_CLOCK_DIV16); |
| 36 | |
| 37 | Serial.println("input word which you want to show"); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | void loop (void) |
| 42 | { |
| 43 | |
| 44 | if (Serial.available() > 0) |
| 45 | { |
| 46 | /*Arduinoのシリアルモニタから送られてくる日本語がSJISであることに注意*/ |
| 47 | delayMicroseconds(200);//最低でも2文字受信したい |
| 48 | uint16_t msbdata = Serial.read();//1バイト目 |
| 49 | /*SJISの1バイトコードか否か*/ |
| 50 | if ( (msbdata < 0x80) || ((0xA0 < msbdata) && (msbdata <= 0xdF)) ) |
| 51 | { |
| 52 | showSJIS1byte(msbdata); |
| 53 | } else |
| 54 | { |
| 55 | uint16_t lsbdata = Serial.read();//2バイト目 |
| 56 | uint16_t data = ((msbdata << 8) + lsbdata); //2 |
| 57 | showSJIS2byte(data); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /*1byteのSJISを表示する*/ |
| 63 | void showSJIS1byte(uint8_t code) |
| 64 | { |
| 65 | readFontASCII(code); |
| 66 | sendDotsToSerial16(); |
| 67 | } |
| 68 | |
| 69 | /*2byteのSJISを表示する*/ |
| 70 | //showSJIS2byte(SJIS文字コード) |
| 71 | void showSJIS2byte(unsigned short code) |
| 72 | { |
| 73 | /*Arduinoのシリアルで日本語はSJIS送信なのでSJIS->JISx0208変換をする*/ |
| 74 | Serial.print("SJIS, 0x"); Serial.print(code, HEX); Serial.print("\t"); |
| 75 | uint8_t c1 = ((code & 0xff00) >> 8); |
| 76 | uint8_t c2 = (code & 0xFF); |
| 77 | if (c1 >= 0xe0) { |
| 78 | c1 = c1 - 0x40; |
| 79 | } |
| 80 | if (c2 >= 0x80) { |
| 81 | c2 = c2 - 1; |
| 82 | } |
| 83 | if (c2 >= 0x9e) { |
| 84 | c1 = (c1 - 0x70) * 2; |
| 85 | c2 = c2 - 0x7d; |
| 86 | } else { |
| 87 | c1 = ((c1 - 0x70) * 2) - 1; |
| 88 | c2 = c2 - 0x1f; |
| 89 | } |
| 90 | /*読み出し*/ |
| 91 | readFontJIS(c1, c2); |
| 92 | /*表示*/ |
| 93 | sendDotsToSerial32(); |
| 94 | } |
| 95 | |
| 96 | /*漢字ROMとやりとり*/ |
| 97 | //readFontJIS(JIS上位8bit,JIS下位8bit); |
| 98 | void readFontJIS(uint8_t c1, uint8_t c2) |
| 99 | { |
| 100 | /*jisx変換後の表示*/ |
| 101 | Serial.print("jisx up8 = 0x"); Serial.print(c1, HEX); Serial.print("\t"); |
| 102 | Serial.print("jisx down8 = 0x"); Serial.print(c2, HEX); Serial.print("\t"); |
| 103 | /*jisxの区点を求める*/ |
| 104 | uint32_t MSB = c1 - 0x20;//区 |
| 105 | uint32_t LSB = c2 - 0x20;//点 |
| 106 | /*JISの句点番号で分類*/ |
| 107 | uint32_t Address = 0; |
| 108 | Serial.print("MSB = d"); Serial.print(MSB, DEC); Serial.print("\t"); |
| 109 | Serial.print("LSB = d"); Serial.print(LSB, DEC); Serial.print("\t"); |
| 110 | /*各種記号・英数字・かな(一部機種依存につき注意,㍍などWindowsと互換性なし)*/ |
| 111 | if (MSB >= 1 && MSB <= 15 && LSB >= 1 && LSB <= 94) |
| 112 | { |
| 113 | Address = ( (MSB - 1) * 94 + (LSB - 1)) * 32; |
| 114 | } |
| 115 | /*第一水準*/ |
| 116 | if (MSB >= 16 && MSB <= 47 && LSB >= 1 && LSB <= 94) |
| 117 | { |
| 118 | Address = ( (MSB - 16) * 94 + (LSB - 1)) * 32 + 43584; |
| 119 | } |
| 120 | /*第二水準*/ |
| 121 | if (MSB >= 48 && MSB <= 84 && LSB >= 1 && LSB <= 94) |
| 122 | { |
| 123 | Address = ((MSB - 48) * 94 + (LSB - 1)) * 32 + 138464; |
| 124 | } |
| 125 | // |
| 126 | /*GT20L16J1Y内部では1区と同等の内容が収録されている*/ |
| 127 | if (MSB == 85 && LSB >= 0x01 && LSB <= 94) |
| 128 | { |
| 129 | Address = ((MSB - 85) * 94 + (LSB - 1)) * 32 + 246944; |
| 130 | } |
| 131 | /*GT20L16J1Y内部では2区、3区と同等の内容が収録されている*/ |
| 132 | if (MSB >= 88 && MSB <= 89 && LSB >= 1 && LSB <= 94) |
| 133 | { |
| 134 | Address = ((MSB - 88) * 94 + (LSB - 1)) * 32 + 249952; |
| 135 | } |
| 136 | /*漢字ROMにデータを送信*/ |
| 137 | digitalWrite(SS, HIGH); |
| 138 | Serial.print("Address = "); Serial.println(Address, HEX); |
| 139 | digitalWrite(SS, LOW); //通信開始 |
| 140 | SPI.transfer(0x03); |
| 141 | SPI.transfer(Address >> 16 & 0xff); |
| 142 | SPI.transfer(Address >> 8 & 0xff); |
| 143 | SPI.transfer(Address & 0xff); |
| 144 | /*漢字ROMからデータを受信*/ |
| 145 | for (int i = 0; i < 32; i++) |
| 146 | { |
| 147 | matrixdata32[i] = SPI.transfer(0x00); |
| 148 | } |
| 149 | digitalWrite(SS, HIGH); //通信終了 |
| 150 | }//spireadfont |
| 151 | |
| 152 | /*漢字ROMとやりとり*/ |
| 153 | //readFontASCII(ASCIIコード); |
| 154 | void readFontASCII(uint8_t ASCIICODE) |
| 155 | { |
| 156 | Serial.print(ASCIICODE, HEX); |
| 157 | uint32_t Address = 0; |
| 158 | /*ASCII文字*/ |
| 159 | if (ASCIICODE >= 0x20 && ASCIICODE <= 0x7F) |
| 160 | { |
| 161 | Address = ( ASCIICODE - 0x20) * 16 + 255968; |
| 162 | } |
| 163 | /*漢字ROMにデータを送信*/ |
| 164 | digitalWrite(SS, HIGH); |
| 165 | Serial.print("Address = "); Serial.println(Address, HEX); |
| 166 | digitalWrite(SS, LOW); //通信開始 |
| 167 | SPI.transfer(0x03); |
| 168 | SPI.transfer(Address >> 16 & 0xff); |
| 169 | SPI.transfer(Address >> 8 & 0xff); |
| 170 | SPI.transfer(Address & 0xff); |
| 171 | /*漢字ROMからデータを受信*/ |
| 172 | for (int i = 0; i < 16; i++) |
| 173 | { |
| 174 | matrixdata16[i] = SPI.transfer(0x00); |
| 175 | } |
| 176 | digitalWrite(SS, HIGH); //通信終了 |
| 177 | |
| 178 | } |
| 179 | |
| 180 | /*シリアルモニタへ16*16のデータを表示する*/ |
| 181 | void sendDotsToSerial32() |
| 182 | { |
| 183 | /*上半分*/ |
| 184 | for (int i = 0; i < 8; i++) |
| 185 | { |
| 186 | for (int b = 0; b < 16; b++) |
| 187 | { |
| 188 | char byteDigit = (1 << i); |
| 189 | if (matrixdata32[b] & byteDigit) { |
| 190 | Serial.write("XX"); |
| 191 | } else { |
| 192 | Serial.write("--"); |
| 193 | } |
| 194 | } |
| 195 | Serial.println(); |
| 196 | } |
| 197 | /*下半分*/ |
| 198 | for (int i = 0; i < 8; i++) |
| 199 | { |
| 200 | for (int b = 16; b < 32 ; b++) |
| 201 | { |
| 202 | char byteDigit = (1 << i); |
| 203 | if (matrixdata32[b] & byteDigit) { |
| 204 | Serial.write("XX"); |
| 205 | } else { |
| 206 | Serial.write("--"); |
| 207 | } |
| 208 | } |
| 209 | Serial.println(); |
| 210 | } |
| 211 | Serial.println(); |
| 212 | }//sendDataToSerial32 |
| 213 | |
| 214 | |
| 215 | /*シリアルモニタへ16*8のデータを表示する*/ |
| 216 | void sendDotsToSerial16() |
| 217 | { |
| 218 | /*上半分*/ |
| 219 | for (int i = 0; i < 8; i++) |
| 220 | { |
| 221 | for (int b = 0; b < 8; b++) |
| 222 | { |
| 223 | char byteDigit = (1 << i); |
| 224 | if (matrixdata16[b] & byteDigit) { |
| 225 | Serial.write("XX"); |
| 226 | } else { |
| 227 | Serial.write("--"); |
| 228 | } |
| 229 | } |
| 230 | Serial.println(); |
| 231 | } |
| 232 | /*下半分*/ |
| 233 | for (int i = 0; i < 8; i++) |
| 234 | { |
| 235 | for (int b = 8; b < 16; b++) |
| 236 | { |
| 237 | char byteDigit = (1 << i); |
| 238 | if (matrixdata16[b] & byteDigit) { |
| 239 | Serial.write("XX"); |
| 240 | } else { |
| 241 | Serial.write("--"); |
| 242 | } |
| 243 | } |
| 244 | Serial.println(); |
| 245 | } |
| 246 | Serial.println(); |
| 247 | }//sendDataToSerial32 |
| 248 | }}} |