정작 모스 부호가 어떻게 생겼는지 몰랐다가
최근에 K5 자동차 광고에 나오는 음이 K5의 모스 부호라는 걸 알고
다시 궁금증을 가지게 되었다.
그리고 SOS 정도 알아 두면 도움이 되지 않을까 하는 생각도... ㅎㅎ
아두이노 관련 샘플을 찾다 보니
모스 부호가 라이브러리로 만들어져 있는게 몇개 있었는데..
그 중에서도 코드가 직관적인게 있어서 사용해 봤는데.. 좋아~
출처 : http://www.scribd.com/doc/88533779/Arduino-Generateur-Code-Morse-A-Quick-Start-Guide
위에 링크에서 찾아서 라이브러리 파일로 만드는데 이상하게 엔터가 제대로 안되어 있고 코드가 다 붙어 있어서 정리하는데 좀 귀찮았다.
//telegraph.h
#ifndef __TELEGRAPH_H__
#define __TELEGRAPH_H__
class Telegraph {
public:
Telegraph(const int output_pin, const int dit_length);
void send_message(const char* message);
private:
void dit();
void dah();
void output_code(const char* code);
void output_symbol(const int length);
int _output_pin;
int _dit_length;
int _dah_length;
};
#endif
//telegraph.cpp
#include <ctype.h>
//arduino 1.0에서는 #include "WProgram.h"을 이렇게 바꿔줘야 컴파일 에러가 안남
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif
#include <telegraph.h>
char* LETTERS[] = {
".-","-...","-.-.","-..",".",// A-E
"..-.","--.","....","..",".---",// F-J
"-.-",".-..","--","-.","---",// K-O
".--.","--.-",".-.","...","-",// P-T
"..-","...-",".--","-..-","-.--",// U-Y
"--.."// Z
};
char* DIGITS[] = {
"-----",".----","..---","...--",// 0-3
"....-",".....","-....","--...",// 4-7
"---..","----."// 8-9
};
Telegraph::Telegraph(const int output_pin, const int dit_length)
{
_output_pin = output_pin;
_dit_length = dit_length;
_dah_length = dit_length*3;
pinMode(_output_pin, OUTPUT);
}
void Telegraph::output_code(const char* code)
{
for (int i = 0; i < strlen(code); i++)
{
if (code[i] == '.')
dit();
else
dah();
}
}
void Telegraph::dit()
{
Serial.print(".");
output_symbol(_dit_length);
}
void Telegraph::dah()
{
Serial.print("-");
output_symbol(_dah_length);
}
void Telegraph::output_symbol(const int length)
{
digitalWrite(_output_pin, HIGH);
delay(length);
digitalWrite(_output_pin, LOW);
}
void Telegraph::send_message(const char* message)
{
for (int i = 0; i < strlen(message); i++)
{
const char current_char = toupper(message[i]);
if(isalpha(current_char))
{
output_code(LETTERS[current_char - 'A']);
delay(_dah_length);
}
else if(isdigit(current_char))
{
output_code(DIGITS[current_char - '0']);
delay(_dah_length);
}
else if(current_char == ' ')
{
Serial.print(" ");
delay(_dit_length * 7);
}
}
Serial.println();
}
이걸 arduino-1.0-windows\arduino-1.0\libraries 폴더에 Telegraph 라는 폴더를 만들어서 넣어 놓는다.
#include "telegraph.h"
const unsigned int OUTPUT_PIN = 13;
const unsigned int DIT_LENGTH = 200;
Telegraph telegraph(OUTPUT_PIN, DIT_LENGTH);
void setup()
{}
void loop()
{
telegraph.send_message("SOS");
delay(5000);
}
13번 핀에 LED가 있는 보드는 SOS 신호에 맞게 반짝이는걸 볼 수 있을 것이다.
스피커를 연결하면 소리를 들을 수 있으니 더 확실할 듯..
또 다른 예제로 모스부호 발생기이다.
#include "telegraph.h"
const unsigned int OUTPUT_PIN = 13;
const unsigned int DIT_LENGTH = 200;
const unsigned int MAX_MESSAGE_LEN = 128;
const unsigned int BAUD_RATE = 9600;
const int LINE_FEED = 13;
char message_text[MAX_MESSAGE_LEN];
int index = 0;
Telegraph telegraph(OUTPUT_PIN, DIT_LENGTH);
void setup()
{
Serial.begin(BAUD_RATE);
}
void loop()
{
if (Serial.available() > 0)
{
int current_char = Serial.read();
if (current_char == LINE_FEED || index == MAX_MESSAGE_LEN - 1)
{
message_text[index] = 0;
index = 0;
telegraph.send_message(message_text);
}
else
{
message_text[index++] = current_char;
}
}
}
Serial Monitor에서 문자를 보내면 그걸 아두이노 보드에서 모스부호로 표시해 준다.

댓글 없음:
댓글 쓰기