Skip to content

Instantly share code, notes, and snippets.

@nullkal
Created July 24, 2015 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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