diff --git a/src/main.cpp b/src/main.cpp index a264d6a..c3058d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,45 +18,73 @@ /* TODO: * - Implement sending strings * - Add OLED Support - * - Add Potentiometer support */ -uint8_t input_dit = 3, input_dah = 6; +boolean debug = true; +uint8_t input_dit = 3, input_dah = 6, input_pot = A6; uint8_t output = 7; uint16_t dit = 200, dah = 500; +uint8_t debounce = 50; +uint16_t p_tol = 10, p_min = 50, p_max = 2000, p_buf = 0, p_cache = 0; String serial_buffer = ""; char buf; +void adjust_length(uint16_t); void set_length(uint16_t, uint16_t); void send(bool); void process_input(String); +void log(String); void setup() { Serial.begin(115200); + log("Set PinModes\n"); pinMode(input_dit, INPUT_PULLUP); pinMode(input_dah, INPUT_PULLUP); + pinMode(input_pot, INPUT); pinMode(output, OUTPUT); digitalWrite(output, HIGH); - - // dumbass - pinMode(9, OUTPUT); - digitalWrite(9, LOW); - + log("Read EEPROM\n"); dit = EEPROM.read(20); dah = EEPROM.read(21); + // debug = EEPROM.read(22); + p_buf = analogRead(input_pot); + adjust_length(p_buf); + log("Setup complete\n"); + log("Dit length: "); + log(dit); + log("\n"); + log("Dah length: "); + log(dah); + log("\n"); + log("Potentiometer: "); + log(p_buf); + log("\n"); } void loop() { - + log("."); if (digitalRead(input_dah)){ send(true); }else if (digitalRead(input_dit)){ send(false); } + p_cache = analogRead(input_pot); + if (abs(p_cache - p_buf) > p_tol){ + p_buf = p_cache; + if (p_cache < p_min){ + p_cache = p_min; + } else if (p_cache > p_max){ + p_cache = p_max; + } + adjust_length(p_cache); + } if (Serial.available() > 0) { buf = Serial.read(); + log("SR "); + log(buf); + log("\n"); if ((uint16_t) buf == 13){ process_input(serial_buffer); serial_buffer = ""; @@ -64,6 +92,13 @@ void loop() { serial_buffer += buf; } } + + delay(debounce); +} + +void adjust_length(uint8_t adjusted_length){ + uint16_t new_dah = map(adjusted_length, 0, 1023, p_min, p_max); + set_length(new_dah/2, new_dah); } void process_input(String input){ @@ -71,6 +106,10 @@ void process_input(String input){ // S send string as CW, unavail chars become " " // I set did length (ms) // A set dah length (ms) + // D<0/1> set debug mode + log("Processing input: "); + log(input); + log("\n"); char command = input[0]; input.remove(0); uint16_t length = 0; @@ -86,6 +125,10 @@ void process_input(String input){ length = input.toInt(); set_length(dit, length); break; + case 'D': + debug = input.toInt()==1; + EEPROM.write(22, debug); + break; default: Serial.println('?'); break; @@ -93,15 +136,28 @@ void process_input(String input){ } void send(bool b) { + log("Sending "); + log(b?"dah":"dit"); + log("\n"); digitalWrite(output, LOW); delay(b?dit:dah); digitalWrite(output, HIGH); } void set_length(uint16_t length_dit, uint16_t length_dah) { + log("Setting dit length to "); + log(length_dit); + log(" and dah length to "); + log(length_dah); dit = length_dit; dah = length_dah; EEPROM.write(20, length_dit); EEPROM.write(21, length_dah); return; -} \ No newline at end of file +} + +void log(String content){ + if (debug){ + Serial.print(content); + } +}