fix commands and add encoding
This commit is contained in:
53
src/main.cpp
53
src/main.cpp
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
#include "cw.h"
|
||||
|
||||
/* TODO:
|
||||
* - Implement sending strings
|
||||
@@ -29,6 +30,7 @@ uint16_t p_tol = 10, p_min = 50, p_max = 2000, p_buf = 0, p_cache = 0;
|
||||
String serial_buffer = "";
|
||||
char buf;
|
||||
|
||||
void key_string(String);
|
||||
void adjust_length(uint16_t);
|
||||
void set_length(uint16_t, uint16_t);
|
||||
void send(bool);
|
||||
@@ -42,11 +44,11 @@ void setup() {
|
||||
pinMode(input_dah, INPUT_PULLUP);
|
||||
pinMode(input_pot, INPUT);
|
||||
pinMode(output, OUTPUT);
|
||||
digitalWrite(output, HIGH);
|
||||
digitalWrite(output, LOW);
|
||||
log("Read EEPROM\n");
|
||||
dit = EEPROM.read(20);
|
||||
dah = EEPROM.read(21);
|
||||
// debug = EEPROM.read(22);
|
||||
debug = EEPROM.read(22);
|
||||
p_buf = analogRead(input_pot);
|
||||
adjust_length(p_buf);
|
||||
log("Setup complete\n");
|
||||
@@ -62,7 +64,6 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
log(".");
|
||||
if (!digitalRead(input_dah)){
|
||||
send(true);
|
||||
}else if (!digitalRead(input_dit)){
|
||||
@@ -82,13 +83,10 @@ void loop() {
|
||||
|
||||
if (Serial.available() > 0) {
|
||||
buf = Serial.read();
|
||||
log("SR ");
|
||||
log(String(buf));
|
||||
log("\n");
|
||||
if ((uint16_t) buf == 13){
|
||||
process_input(serial_buffer);
|
||||
serial_buffer = "";
|
||||
} else {
|
||||
} else if ((uint8_t) buf > 27 ){
|
||||
serial_buffer += buf;
|
||||
}
|
||||
}
|
||||
@@ -111,7 +109,7 @@ void process_input(String input){
|
||||
log(input);
|
||||
log("\n");
|
||||
char command = input[0];
|
||||
input.remove(0);
|
||||
input.remove(0,1);
|
||||
log("Command: ");
|
||||
log(String(command));
|
||||
log(" - ");
|
||||
@@ -120,7 +118,7 @@ void process_input(String input){
|
||||
uint16_t length = 0;
|
||||
switch(command){
|
||||
case 'S':
|
||||
// TODO
|
||||
key_string(input);
|
||||
break;
|
||||
case 'I':
|
||||
length = input.toInt();
|
||||
@@ -132,6 +130,7 @@ void process_input(String input){
|
||||
break;
|
||||
case 'D':
|
||||
debug = boolean(input.toInt()==1);
|
||||
log("Debug mode enabled");
|
||||
EEPROM.write(22, debug);
|
||||
break;
|
||||
default:
|
||||
@@ -140,13 +139,39 @@ void process_input(String input){
|
||||
}
|
||||
}
|
||||
|
||||
void key_string(String input){
|
||||
// log("Sending string: ");
|
||||
// log(input);
|
||||
// log("\n");
|
||||
uint8_t b = 0;
|
||||
for (uint8_t i = 0; i < input.length(); i++){
|
||||
char c = input[i];
|
||||
if ((uint8_t)c != 32){
|
||||
b = convertASCII(c);
|
||||
for(uint8_t y = 0; y < strlen(morseCode[b]); y++){
|
||||
send(morseCode[b][y] == '.');
|
||||
delay(dit);
|
||||
}
|
||||
if(debug){
|
||||
Serial.print(' ');
|
||||
}
|
||||
delay(dit);
|
||||
} else {
|
||||
if (debug){
|
||||
Serial.print(" ");
|
||||
}
|
||||
delay(dah);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void send(bool b) {
|
||||
log("Sending ");
|
||||
log(b?"dah":"dit");
|
||||
log("\n");
|
||||
digitalWrite(output, LOW);
|
||||
delay(b?dit:dah);
|
||||
if (debug){
|
||||
Serial.print(b?"+":"=");
|
||||
}
|
||||
digitalWrite(output, HIGH);
|
||||
delay(b?dit:dah);
|
||||
digitalWrite(output, LOW);
|
||||
}
|
||||
|
||||
void set_length(uint16_t length_dit, uint16_t length_dah) {
|
||||
|
||||
Reference in New Issue
Block a user