Skip to content

Instantly share code, notes, and snippets.

@nullkal
Created July 24, 2015 14:53
Show Gist options
  • Save nullkal/44fa3543554130b8d9e6 to your computer and use it in GitHub Desktop.
Save nullkal/44fa3543554130b8d9e6 to your computer and use it in GitHub Desktop.
Arduino+GROVEで和文モールス入力
#include <Wire.h>
#include "rgb_lcd.h"
const char MORSE_TABLE[][16] = {
/* ア */ "\xb1__.__",
/* イ */ "\xb2._",
/* ウ */ "\xb3.._",
/* エ */ "\xb4_.___",
/* オ */ "\xb5._...",
/* カ */ "\xb6._..",
/* キ */ "\xb7_._..",
/* ク */ "\xb8..._",
/* ケ */ "\xb9_.__",
/* コ */ "\xba____",
/* サ */ "\xbb_._._",
/* シ */ "\xbc__._.",
/* ス */ "\xbd___._",
/* セ */ "\xbe.___.",
/* ソ */ "\xbf___.",
/* タ */ "\xc0_.",
/* チ */ "\xc1.._.",
/* ツ */ "\xc2.__.",
/* テ */ "\xc3._.__",
/* ト */ "\xc4.._..",
/* ナ */ "\xc5._.",
/* ニ */ "\xc6_._.",
/* ヌ */ "\xc7....",
/* ネ */ "\xc8__._",
/* ノ */ "\xc9..__",
/* ハ */ "\xca_...",
/* ヒ */ "\xcb__.._",
/* フ */ "\xcc__..",
/* ヘ */ "\xcd.",
/* ホ */ "\xce_..",
/* マ */ "\xcf_.._",
/* ミ */ "\xd0.._._",
/* ム */ "\xd1_",
/* メ */ "\xd2_..._",
/* モ */ "\xd3_.._.",
/* ヤ */ "\xd4.__",
/* ユ */ "\xd5_..__",
/* ヨ */ "\xd6__",
/* ラ */ "\xd7...",
/* リ */ "\xd8__.",
/* ル */ "\xd9_.__.",
/* レ */ "\xda___",
/* ロ */ "\xdb._._",
/* ワ */ "\xdc_._",
/* ヰ */ "\xb2._.._",
/* ヱ */ "\xb4.__..",
/* ヲ */ "\xa6.___",
/* ン */ "\xdd._._.",
/* ー */ "\xb0.__._",
/* ゛ */ "\xde..",
/* ゜ */ "\xdf..__.",
/* 、 */ "\xa4._._._",
/* BS */ "\x08........",
"",
};
const int PIN_BUTTON = 3;
const int PIN_BUZZER = 8;
rgb_lcd lcd;
int g_cursorCount = 0;
bool g_buttonFlag = 0;
int g_buttonCount = 301;
char g_morse[16] = {0};
int g_morseLength = 0;
char g_result[16] = {0};
int g_resultLength = 0;
void draw() {
lcd.setCursor(0, 1);
lcd.print(g_morse);
lcd.setCursor(0, 0);
lcd.print(g_result);
}
void setup()
{
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_BUZZER, OUTPUT);
lcd.begin(16, 2);
lcd.setRGB(255, 255, 255);
}
void loop() {
if (digitalRead(PIN_BUTTON)) {
digitalWrite(PIN_BUZZER, HIGH);
if (!g_buttonFlag) {
g_buttonFlag = 1;
g_buttonCount = 0;
} else {
g_buttonCount ++;
}
} else {
if (g_buttonFlag) {
if (g_morseLength < 16) {
if (g_buttonCount < 100) {
g_morse[g_morseLength++] = '.';
} else {
g_morse[g_morseLength++] = '_';
}
draw();
}
g_buttonFlag = 0;
g_buttonCount = 0;
} else {
digitalWrite(PIN_BUZZER, LOW);
if (g_buttonCount == 200) {
for (int i = 0;; ++i) {
char lcdCharcode = MORSE_TABLE[i][0];
if (!lcdCharcode) break;
bool mismatched = 0;
const char *c = MORSE_TABLE[i] + 1;
const char *d = g_morse;
while (*c != '\0' || *d != '\0') {
if (*(c++) != *(d++)) {
mismatched = 1;
break;
}
}
if (!mismatched) {
if (lcdCharcode == '\x08') {
for (int i = 0; i < 16; i++) {
g_result[i] = '\0';
}
g_resultLength = 0;
} else {
if (g_resultLength < 16) {
g_result[g_resultLength++] = lcdCharcode;
} else {
for (int i = 1; i < 16; i++) {
g_result[i - 1] = g_result[i];
}
g_result[15] = lcdCharcode;
}
}
break;
}
}
for (int i = 0; i < g_morseLength; ++i) {
g_morse[i] = '\0';
}
g_morseLength = 0;
lcd.clear();
draw();
}
g_buttonCount ++;
}
}
if (g_cursorCount == 0) {
lcd.cursor();
} else if (g_cursorCount == 500) {
lcd.noCursor();
}
delayMicroseconds(1000);
g_cursorCount ++;
if (g_cursorCount > 1000) {
g_cursorCount = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment