//for Arduino UNO
// to get work, connect NO NC pushbutton to pins 2,3 and gnd
// check output on pin 4
#pragma once
#include "libraries/common.h"
#include "libraries/rtty.h"
#include "libraries/cw.h"
Common co;
CW cw;
Rtty rt;
int typeSelectPin; // switch btw CW and RTTY
int contentSelectPin1; // output
int contentSelectPin2; // inverse output (rtty only)
void setup() {
// put your setup code here, to run once:
co.setup(9600); //serial 9600
co.setPinSet(2); // pushbutton for start, NO-pin2, NC-pin3, common to GND
co.setPinReset(3); //pushbutton NO
cw.setCwOutputPin(4); //cw out
cw.setWps(60); //words per minute 60,70, 80, 90 110 or 120,, select during interrupt
cw.setLedPin(13);
cw.setPermToSend(true); // permission to send cw, this should be changed by interrupt
rt.setRttyOutputPin(4); //rtty out
rt.setRttyOutputPinInverse(5); //rtty inverse out
rt.setBitRate(50); //rtty bit rate 45,50,75, 100 or 150 bps , select during interrupt
rt.setLedPin(13);
rt.setPermToSend(true); // stop sending when false, this should be changed by interrupt
typeSelectPin=6; //HIGH=sending telegraphy, LOW=teletype
contentSelectPin1=7; // combination 7 ,8 will determine the output text
contentSelectPin2=8;
pinMode(typeSelectPin,INPUT_PULLUP);
pinMode(contentSelectPin1,INPUT_PULLUP);
pinMode(contentSelectPin2,INPUT_PULLUP);
}
//******************************************************
void loop() {
//when pushbutton pressed
sendOut();
//wait for next press
}
//=================================================
void sendOut(){
int sp=0; // select what to send
sp=2*digitalRead(contentSelectPin2)+digitalRead(contentSelectPin1); //0,1,2,3
//Serial.println(sp);
if (co.once()){ // press the pushbutton and go through loop only once
if (digitalRead(typeSelectPin)==true){
Serial.println("CW");
Serial.print("sp=");
Serial.println(sp);
if (sp==0) cw.sendAbc();
if (sp==1) cw.sendRy();
if (sp==2) cw.sendFox();
if (sp==3) cw.sendChar("a");
}
else{
Serial.println("RTTY");
Serial.print("sp=");
Serial.println(sp);
if (sp==0) rt.sendAbc();
if (sp==1) rt.sendRY();
if (sp==2) rt.sendFox();
if (sp==3) rt.sendChar("ry");
}
}
}
//**********************************************************************
class Common{
private: int pinSet=2;
private: int pinReset=3;
//public: const int PIN_CW=4;
//public: const int PIN_RTTY=4;
//public: const int PIN_RTTY_INVERSE=5;
private: boolean Q=false;
// pin assigments
public: void setPinSet(int newPin){
pinSet=newPin;
}
public: void setPinReset(int newPin){
pinReset=newPin;
}
public: boolean once(){
return once(pinSet,pinReset);
}
private: boolean once(int pinSet, int pinReset){
int i=0;
setInput_pullup(pinSet);
setInput_pullup(pinReset);
if (Q==false && digitalRead(pinSet)==false){
i=2;
}
if (Q==true && digitalRead(pinReset)==false){
i=3;
}
if (digitalRead(pinSet)==false && digitalRead(pinReset)==true) Q=true;
if (digitalRead(pinReset)==false && digitalRead(pinSet)==true) Q=false;
if (i==2) return true;
return false;
}
public: void setup(int bps) {
//9600
Serial.begin(bps); // Initialize serial communication at 115200 baud
}
private: void setInput_pullup(int pinNumber){
pinMode(pinNumber,INPUT_PULLUP);
}
};
//****************************************************************************
class CW{
private: const int CW_CODE[96]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0xf8,0x78,0x38,0x18,0x8,00,0x80,0xc0,\
0xe0,0xf0,0,0,0,0,0,0,\
0,0x40,0x80,0xa0,0x80,00,0x20,0xc0,\
0,0,0x70,0xa0,0x40,0xc0,0x80,0xe0,\
0x60,0xd0,0x40,0,0x80,0x20,0x10,0x60,\
0x90,0xb0,0xc0,0,0,0,0,0 //x-z
// a=._ =
};
private: const int CW_DUZINA[96]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0x5,0x5,0x5,0x5,0x5,0x5,0x5,0x5,\
0x5,0x5,0,0,0,0,0,0,\
0,0x2,0x4,0x4,0x3,0x1,0x4,0x3,\
0x4,0x2,0x4,0x3,0x4,0x2,0x2,0x3,\
0x4,0x4,0x3,0x3,0x1,0x3,0x4,0x3,\
0x4,0x4,0x4,0,0,0,0,0
};
// ascii (A)= 61; CW_CODE[61]= 0x40 = 0b01000000;
//CW_DUZINA (length) [61]=0x2=2; output= first 2 bites of b01000000
//output = dot, dash
private: const char ABC[36]={
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
};
private: const char FOX[43]={
"The quick brown fox jumps over the lazy dog"
};
private: const char RY[36]={
"ryryryryryryryryryryryryryryryryryry"
};
private: int cwDotLength=100; //ms
private: int cwOutputPin=4;
private: int ledPin=13;
private: boolean permissionToSend=true;
public: void setCwOutputPin(int newOutputPin){
cwOutputPin=newOutputPin;
}
public: void setPermToSend(boolean newPerm){
permissionToSend=newPerm;
}
//public: void setDotLength(int newLength){
// cwDotLength=newLength;
//}
public: void setWps(int newWps){
if (newWps==60) cwDotLength=75;
if (newWps==70) cwDotLength=72;
if (newWps==80) cwDotLength=64;
if (newWps==90) cwDotLength=56;
if (newWps==110) cwDotLength=50;
if (newWps==120) cwDotLength=40;
}
public: void setLedPin(int newPin){
ledPin=newPin;
}
public: void sendAbc(){
sendCW(ABC);
}
public: void sendFox(){
sendCW(FOX);
}
public: void sendRy(){
sendCW(RY);
}
public: void sendChar(char sToSend[]){
sendCW(sToSend);
}
public: void sendCW(char sToSend[]){
int length=strlen(sToSend);
for (int i=0; i<length; i=i+1){
if (permissionToSend) {
sendCW1a(sToSend[i]);
delay(cwDotLength*2);
}
}
}
private: int getAscii(char c){
return (int)c;
}
private: void sendCW1a(char c){
if (c==' '){
delay (cwDotLength*5);
return;
}
char upperC=toupper(c);
int ascii=getAscii(upperC);
int cwKod=CW_CODE[ascii];
int cwDuzina=CW_DUZINA[ascii];
boolean bi[8];
byte b=(byte)cwKod;
boolean bitToSend;
for (int i=7; i>=0;i=i-1){
bi[i]=((b >>i) & 0x1);
}
for (int i=0 ;i<cwDuzina;i=i+1){
sendCW0(bi[7-i]);
}
return;
}
private: void sendCW0( boolean bitToSend){
if (bitToSend==true){
digitalWrite(cwOutputPin, HIGH);
digitalWrite(ledPin,HIGH);
delay(cwDotLength*3);
digitalWrite(cwOutputPin, LOW);
digitalWrite(ledPin,LOW);
delay(cwDotLength);
//Serial.println("dash");
return;
}
else{
digitalWrite(cwOutputPin, HIGH);
digitalWrite(ledPin,HIGH);
delay(cwDotLength);
digitalWrite(cwOutputPin, LOW);
digitalWrite(ledPin,LOW);
delay(cwDotLength);
//Serial.println("dot0");
return;
}
}
};
//********************************************************
class Rtty{
private: const int RTTY[96]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0x4,0,0,0,0x1f,0,0,0,0,0,0,0,0,0,0,0,\
0xd,0x1d,0x19,0x10,0xa,0x1,0x15,0x1c,\
0xc,0x3,0,0,0,0,0,0,\
0,0x18,0x13,0xe,0x12,0x10,0x16,0xb,\
0x5,0xc,0x1a,0x1e,0x9,0x7,0x6,0x3,\
0xd,0x1d,0xa,0x14,0x1,0x1c,0xf,0x19,\
0x17,0x15,0x11,0,0,0,0x1b,0
};
private: const char ABC[36]={
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
};
private: const char FOX[43]={
"The quick brown fox jumps over the lazy dog"
};
private: const char RY[36]={
"ryryryryryryryryryryryryryryryryryry"
};
private: int rttyBitDuration=20; //20 ms trajanje
private: int rttyOutputPin=4;
private: int rttyOutputPinInverse=5;
private: int ledPin=13;
private: boolean permissionToSend=true;
public: setRttyOutputPin(int newOutputPin){
rttyOutputPin=newOutputPin;
}
public: void setPermToSend(boolean newPerm){
permissionToSend=newPerm;
}
//public: setRttyBitLength(int newLength){
// rttyBitDuration=newLength;
//}
public: void setBitRate(int newBitRate){
if (newBitRate==45){}
if (newBitRate==50) rttyBitDuration=20;
if (newBitRate==75){}
if (newBitRate==100){}
if (newBitRate==150){}
return;
}
public: void setRttyOutputPinInverse(int newPin){
rttyOutputPinInverse=newPin;
}
public: void sendRY(){
sendRtty(RY);
}
public: void sendFox(){
sendRtty(FOX);
}
public: void sendAbc(){
sendRtty(ABC);
}
public: void sendChar(char sToSend[]){
sendRtty(sToSend);
}
public: void sendRtty(char sToSend[]){
int length=strlen(sToSend);
for (int i=0; i<length; i=i+1){
if (permissionToSend) sendRtty1(sToSend[i]);
}
}
public: void setLedPin(int newPin){
ledPin=newPin;
}
private: void sendRtty1(char c){
//start,least,...,most,stop,sop
int ascii=getAscii(toupper(c));
int rttyCode=RTTY[ascii];
byte b=rttyCode;
boolean bi[5];
for (int i=4; i>=0;i=i-1){
bi[i]=((b >>i) & 0x1);
}
rtty0(false);
for (int i=0; i<=4; i=i+1){
rtty0(bi[i]);
}
rtty0(true);
rtty0(true);
}
private: void rtty0(boolean bitToSend){
if (bitToSend==true){
digitalWrite(rttyOutputPin, HIGH);
digitalWrite(rttyOutputPinInverse, LOW);
digitalWrite(ledPin,HIGH);
}
else {
digitalWrite(rttyOutputPin, LOW);
digitalWrite(rttyOutputPinInverse, HIGH);
digitalWrite(ledPin,LOW);
}
delay(rttyBitDuration);
return;
}
private: int getAscii(char c){
return (int)c;
}
};