2019-12-18 01:32:44 -05:00
|
|
|
#include <Arduino.h>
|
2020-04-22 00:14:24 -04:00
|
|
|
#include "pin_definitions.h"
|
2020-01-04 02:11:55 -05:00
|
|
|
#include "settings.h"
|
2020-04-22 00:14:24 -04:00
|
|
|
#include "tuner.h"
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
CW Keyer
|
|
|
|
CW Key logic change with ron's code (ubitx_keyer.cpp)
|
|
|
|
Ron's logic has been modified to work with the original uBITX by KD8CEC
|
|
|
|
|
|
|
|
Original Comment ----------------------------------------------------------------------------
|
|
|
|
* The CW keyer handles either a straight key or an iambic / paddle key.
|
|
|
|
* They all use just one analog input line. This is how it works.
|
|
|
|
* The analog line has the internal pull-up resistor enabled.
|
|
|
|
* When a straight key is connected, it shorts the pull-up resistor, analog input is 0 volts
|
|
|
|
* When a paddle is connected, the dot and the dash are connected to the analog pin through
|
|
|
|
* a 10K and a 2.2K resistors. These produce a 4v and a 2v input to the analog pins.
|
|
|
|
* So, the readings are as follows :
|
|
|
|
* 0v - straight key
|
|
|
|
* 1-2.5 v - paddle dot
|
|
|
|
* 2.5 to 4.5 v - paddle dash
|
|
|
|
* 2.0 to 0.5 v - dot and dash pressed
|
|
|
|
*
|
|
|
|
* The keyer is written to transparently handle all these cases
|
|
|
|
*
|
|
|
|
* Generating CW
|
|
|
|
* The CW is cleanly generated by unbalancing the front-end mixer
|
|
|
|
* and putting the local oscillator directly at the CW transmit frequency.
|
|
|
|
* The sidetone, generated by the Arduino is injected into the volume control
|
|
|
|
*/
|
|
|
|
|
|
|
|
//CW ADC Range
|
2020-01-26 13:40:03 -05:00
|
|
|
//static const unsigned int cwAdcSTFrom = 0;
|
2020-01-04 02:11:55 -05:00
|
|
|
static const unsigned int cwAdcSTTo = 50;
|
|
|
|
static const unsigned int cwAdcBothFrom = cwAdcSTTo + 1;
|
|
|
|
static const unsigned int cwAdcBothTo = 300;
|
|
|
|
static const unsigned int cwAdcDotFrom = cwAdcBothTo + 1;
|
|
|
|
static const unsigned int cwAdcDotTo = 600;
|
|
|
|
static const unsigned int cwAdcDashFrom = cwAdcDotTo + 1;
|
|
|
|
static const unsigned int cwAdcDashTo = 800;
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts transmitting the carrier with the sidetone
|
|
|
|
* It assumes that we have called cwTxStart and not called cwTxStop
|
|
|
|
* each time it is called, the cwTimeOut is pushed further into the future
|
|
|
|
*/
|
|
|
|
void cwKeydown(){
|
2020-04-22 00:14:24 -04:00
|
|
|
tone(PIN_CW_TONE, globalSettings.cwSideToneFreq);
|
|
|
|
digitalWrite(PIN_CW_KEY, 1);
|
2019-12-18 01:32:44 -05:00
|
|
|
|
2020-01-04 02:11:55 -05:00
|
|
|
globalSettings.cwExpirationTimeMs = millis() + globalSettings.cwActiveTimeoutMs;
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the cw carrier transmission along with the sidetone
|
|
|
|
* Pushes the cwTimeout further into the future
|
|
|
|
*/
|
|
|
|
void cwKeyUp(){
|
2020-04-22 00:14:24 -04:00
|
|
|
noTone(PIN_CW_TONE);
|
|
|
|
digitalWrite(PIN_CW_KEY, 0);
|
2019-12-18 01:32:44 -05:00
|
|
|
|
2020-01-04 02:11:55 -05:00
|
|
|
globalSettings.cwExpirationTimeMs = millis() + globalSettings.cwActiveTimeoutMs;
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Variables for Ron's new logic
|
|
|
|
#define DIT_L 0x01 // DIT latch
|
|
|
|
#define DAH_L 0x02 // DAH latch
|
|
|
|
#define DIT_PROC 0x04 // DIT is being processed
|
|
|
|
#define PDLSWAP 0x08 // 0 for normal, 1 for swap
|
|
|
|
#define IAMBICB 0x10 // 0 for Iambic A, 1 for Iambic B
|
|
|
|
enum KSTYPE {IDLE, CHK_DIT, CHK_DAH, KEYED_PREP, KEYED, INTER_ELEMENT };
|
|
|
|
static unsigned long ktimer;
|
|
|
|
unsigned char keyerState = IDLE;
|
2020-01-04 02:11:55 -05:00
|
|
|
uint8_t keyerControl = 0;
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
//Below is a test to reduce the keying error. do not delete lines
|
|
|
|
//create by KD8CEC for compatible with new CW Logic
|
2020-01-04 02:11:55 -05:00
|
|
|
char update_PaddleLatch(bool isUpdateKeyState) {
|
2019-12-18 01:32:44 -05:00
|
|
|
unsigned char tmpKeyerControl = 0;
|
|
|
|
|
2020-04-22 00:14:24 -04:00
|
|
|
unsigned int paddle = analogRead(PIN_ANALOG_KEYER);
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
//use the PTT as the key for tune up, quick QSOs
|
2020-04-22 00:14:24 -04:00
|
|
|
if (digitalRead(PIN_PTT) == 0)
|
2019-12-18 01:32:44 -05:00
|
|
|
tmpKeyerControl |= DIT_L;
|
|
|
|
else if (paddle >= cwAdcDashFrom && paddle <= cwAdcDashTo)
|
|
|
|
tmpKeyerControl |= DAH_L;
|
|
|
|
else if (paddle >= cwAdcDotFrom && paddle <= cwAdcDotTo)
|
|
|
|
tmpKeyerControl |= DIT_L;
|
|
|
|
else if (paddle >= cwAdcBothFrom && paddle <= cwAdcBothTo)
|
2020-01-04 02:11:55 -05:00
|
|
|
tmpKeyerControl |= (DAH_L | DIT_L) ;
|
|
|
|
else{
|
|
|
|
if (KeyerMode_e::KEYER_STRAIGHT != globalSettings.keyerMode)
|
2019-12-18 01:32:44 -05:00
|
|
|
tmpKeyerControl = 0 ;
|
2020-01-26 16:03:50 -05:00
|
|
|
else if (paddle <= cwAdcDashTo)
|
2019-12-18 01:32:44 -05:00
|
|
|
tmpKeyerControl = DIT_L ;
|
|
|
|
else
|
2020-01-04 02:11:55 -05:00
|
|
|
tmpKeyerControl = 0 ;
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-04 02:11:55 -05:00
|
|
|
if (isUpdateKeyState)
|
2019-12-18 01:32:44 -05:00
|
|
|
keyerControl |= tmpKeyerControl;
|
|
|
|
|
|
|
|
return tmpKeyerControl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
// New logic, by RON
|
|
|
|
// modified by KD8CEC
|
|
|
|
******************************************************************************/
|
|
|
|
void cwKeyer(void){
|
|
|
|
bool continue_loop = true;
|
|
|
|
unsigned tmpKeyControl = 0;
|
|
|
|
|
2020-01-04 02:11:55 -05:00
|
|
|
if(KeyerMode_e::KEYER_STRAIGHT != globalSettings.keyerMode){
|
|
|
|
while(continue_loop){
|
|
|
|
switch(keyerState){
|
2019-12-18 01:32:44 -05:00
|
|
|
case IDLE:
|
|
|
|
tmpKeyControl = update_PaddleLatch(0);
|
2020-01-04 02:11:55 -05:00
|
|
|
if((tmpKeyControl == DAH_L)//Currently dah
|
|
|
|
||(tmpKeyControl == DIT_L)//Currently dit
|
|
|
|
||(tmpKeyControl == (DAH_L | DIT_L))//Currently both
|
|
|
|
||( keyerControl & (DAH_L | DIT_L))){//Resolving either
|
|
|
|
update_PaddleLatch(true);
|
2019-12-18 01:32:44 -05:00
|
|
|
keyerState = CHK_DIT;
|
2020-01-04 02:11:55 -05:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (0 < globalSettings.cwExpirationTimeMs && globalSettings.cwExpirationTimeMs < millis()){
|
|
|
|
globalSettings.cwExpirationTimeMs = 0;
|
2019-12-18 01:32:44 -05:00
|
|
|
stopTx();
|
|
|
|
}
|
|
|
|
continue_loop = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHK_DIT:
|
|
|
|
if (keyerControl & DIT_L) {
|
|
|
|
keyerControl |= DIT_PROC;
|
2020-01-04 02:11:55 -05:00
|
|
|
ktimer = globalSettings.cwDitDurationMs;
|
2019-12-18 01:32:44 -05:00
|
|
|
keyerState = KEYED_PREP;
|
|
|
|
}else{
|
|
|
|
keyerState = CHK_DAH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHK_DAH:
|
|
|
|
if (keyerControl & DAH_L) {
|
2020-01-04 02:11:55 -05:00
|
|
|
ktimer = 3*globalSettings.cwDitDurationMs;
|
2019-12-18 01:32:44 -05:00
|
|
|
keyerState = KEYED_PREP;
|
|
|
|
}else{
|
|
|
|
keyerState = IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYED_PREP:
|
|
|
|
//modified KD8CEC
|
2020-01-04 02:11:55 -05:00
|
|
|
if (!globalSettings.txActive){
|
|
|
|
globalSettings.cwExpirationTimeMs = millis() + globalSettings.cwActiveTimeoutMs;
|
|
|
|
startTx(TuningMode_e::TUNE_CW);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
ktimer += millis(); // set ktimer to interval end time
|
|
|
|
keyerControl &= ~(DIT_L + DAH_L); // clear both paddle latch bits
|
|
|
|
keyerState = KEYED; // next state
|
|
|
|
|
|
|
|
cwKeydown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYED:
|
|
|
|
if (millis() > ktimer) { // are we at end of key down ?
|
2020-01-04 02:11:55 -05:00
|
|
|
cwKeyUp();
|
2020-01-30 23:23:17 -05:00
|
|
|
ktimer = millis() + globalSettings.cwDitDurationMs; // inter-element time
|
2019-12-18 01:32:44 -05:00
|
|
|
keyerState = INTER_ELEMENT; // next state
|
2020-01-04 02:11:55 -05:00
|
|
|
}
|
|
|
|
else if(KeyerMode_e::KEYER_IAMBIC_B == globalSettings.keyerMode){
|
2019-12-18 01:32:44 -05:00
|
|
|
update_PaddleLatch(1); // early paddle latch in Iambic B mode
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INTER_ELEMENT:
|
|
|
|
// Insert time between dits/dahs
|
|
|
|
update_PaddleLatch(1); // latch paddle state
|
|
|
|
if (millis() > ktimer) { // are we at end of inter-space ?
|
|
|
|
if (keyerControl & DIT_PROC) { // was it a dit or dah ?
|
|
|
|
keyerControl &= ~(DIT_L + DIT_PROC); // clear two bits
|
|
|
|
keyerState = CHK_DAH; // dit done, check for dah
|
|
|
|
}else{
|
|
|
|
keyerControl &= ~(DAH_L); // clear dah latch
|
|
|
|
keyerState = IDLE; // go idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkCAT();
|
|
|
|
} //end of while
|
|
|
|
}
|
2020-01-04 02:11:55 -05:00
|
|
|
else{//KEYER_STRAIGHT
|
2019-12-18 01:32:44 -05:00
|
|
|
while(1){
|
|
|
|
char state = update_PaddleLatch(0);
|
2020-01-04 02:11:55 -05:00
|
|
|
// Serial.println((int)state);
|
2019-12-18 01:32:44 -05:00
|
|
|
if (state == DIT_L) {
|
|
|
|
// if we are here, it is only because the key is pressed
|
2020-01-04 02:11:55 -05:00
|
|
|
if (!globalSettings.txActive){
|
|
|
|
startTx(TuningMode_e::TUNE_CW);
|
2020-01-26 16:03:50 -05:00
|
|
|
globalSettings.cwExpirationTimeMs = millis() + globalSettings.cwActiveTimeoutMs;
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
cwKeydown();
|
|
|
|
|
2020-02-15 20:10:27 -05:00
|
|
|
while ( update_PaddleLatch(0) == DIT_L );
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
cwKeyUp();
|
|
|
|
}
|
|
|
|
else{
|
2020-01-04 02:11:55 -05:00
|
|
|
if (0 < globalSettings.cwExpirationTimeMs && globalSettings.cwExpirationTimeMs < millis()){
|
|
|
|
globalSettings.cwExpirationTimeMs = 0;
|
2019-12-18 01:32:44 -05:00
|
|
|
stopTx();
|
|
|
|
}
|
2020-01-04 02:11:55 -05:00
|
|
|
return;//Tx stop control by Main Loop
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
checkCAT();
|
|
|
|
} //end of while
|
2020-01-04 02:11:55 -05:00
|
|
|
}//end of else KEYER_STRAIGHT
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|