ユーザーが Android でメッセージを入力し、LED を切り替えて Arduino でモールス符号で表示できるアプリを作成するとします。モールス符号のメッセージは、一連のダッシュ (LONGS) とドット (ショート) で構成されます。これらのダッシュとドットは、正しい時間単位で LED をオンにすることで表示できます。
#include <Usb.h>
#include <AndroidAccessory.h>
#define LED_PIN 13
#define SHORT 0
#define LONG 1
#define LETTER 2
#define WORD 3
#define STOP 4
#define UNIT 250
AndroidAccessory acc("testing",
"morse_code",
"CoolAccessory",
"1.0",
"http://www.example.com/CoolAccessory",
"0000000012345678");
void setup()
{
// set communiation speed
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
acc.powerOn();
}
void loop()
{
byte msg[125];
if (acc.isConnected()) {
int len = acc.read(msg, sizeof(msg), 1); // read data into msg variable
if (len > 0) { // Only do something if a message has been received.
displayMorseCode(msg, len);
}
}
else
digitalWrite(LED_PIN , LOW); // turn off light
}
//For toggle the LED for certain length of time use the delay()
//call delay(UNIT) to pause execution of UNIT milliseconds
//long unit *3 , short = unit
void displayMorseCode(byte* msg, int len) {
// TODO :Interpret the message toggle LED on and off to display the
morse code
if (msg[0] == 1)
digitalWrite(LED_PIN,HIGH);
else
digitalWrite(LED_PIN,LOW);
}
メッセージは、定数として定義されている次の値で構成されます。
SHORT: モールスのドット
LONG: ダッシュ モールス
LETTER: モールスの文字の終わり WORD: モールスの単語の終わり STOP: モールスの終わり ex: メッセージ「SOS」は (SHORT,SHORT,SHORT としてエンコードされます) ,LETTER,LONG,LONG,LONG,LETTER,SHORT,SHORT,SHORT,LETTER,WORD,STOP)
この機能をdisplayMorseCodeに実装する方法は?