11#include < Arduino.h>
2+ #include " settings.h"
23#include " ubitx.h"
34
45/* *
2829 */
2930
3031 // CW ADC Range
31- int cwAdcSTFrom = 0 ;
32- int cwAdcSTTo = 50 ;
33- int cwAdcBothFrom = 51 ;
34- int cwAdcBothTo = 300 ;
35- int cwAdcDotFrom = 301 ;
36- int cwAdcDotTo = 600 ;
37- int cwAdcDashFrom = 601 ;
38- int cwAdcDashTo = 800 ;
39- // byte cwKeyType = 0; //0: straight, 1 : iambica, 2: iambicb
32+ static const unsigned int cwAdcSTFrom = 0 ;
33+ static const unsigned int cwAdcSTTo = 50 ;
34+ static const unsigned int cwAdcBothFrom = cwAdcSTTo + 1 ;
35+ static const unsigned int cwAdcBothTo = 300 ;
36+ static const unsigned int cwAdcDotFrom = cwAdcBothTo + 1 ;
37+ static const unsigned int cwAdcDotTo = 600 ;
38+ static const unsigned int cwAdcDashFrom = cwAdcDotTo + 1 ;
39+ static const unsigned int cwAdcDashTo = 800 ;
4040
41- byte delayBeforeCWStartTime = 50 ;
41+ static const unsigned int delayBeforeCWStartTime = 50 ;
4242
43-
44-
45-
46- // in milliseconds, this is the parameter that determines how long the tx will hold between cw key downs
47- // #define CW_TIMEOUT (600l) //Change to CW Delaytime for value save to eeprom
48- #define PADDLE_DOT 1
49- #define PADDLE_DASH 2
50- #define PADDLE_BOTH 3
51- #define PADDLE_STRAIGHT 4
52-
53- // we store the last padde's character
54- // to alternatively send dots and dashes
55- // when both are simultaneously pressed
56- char lastPaddle = 0 ;
57-
58- /*
59- //reads the analog keyer pin and reports the paddle
60- byte getPaddle(){
61- int paddle = analogRead(ANALOG_KEYER);
62- //handle the ptt as the straight key
63-
64- if (digitalRead(PTT) == 0)
65- return PADDLE_STRAIGHT;
66-
67- if (paddle > 800) // above 4v is up
68- return 0;
69-
70- if (!Iambic_Key)
71- return PADDLE_STRAIGHT;
72-
73- if (paddle > 600) // 4-3v is dot
74- return PADDLE_DASH;
75- else if (paddle > 300) //1-2v is dash
76- return PADDLE_DOT;
77- else if (paddle > 50)
78- return PADDLE_BOTH; //both are between 1 and 2v
79- else
80- return PADDLE_STRAIGHT; //less than 1v is the straight key
81- }
82- */
8343/* *
8444 * Starts transmitting the carrier with the sidetone
8545 * It assumes that we have called cwTxStart and not called cwTxStop
8646 * each time it is called, the cwTimeOut is pushed further into the future
8747 */
8848void cwKeydown (){
49+ tone (CW_TONE, globalSettings.cwSideToneFreq );
50+ digitalWrite (CW_KEY, 1 );
8951
90- keyDown = 1 ; // tracks the CW_KEY
91- tone (CW_TONE, (int )sideTone);
92- digitalWrite (CW_KEY, 1 );
93-
94- // Modified by KD8CEC, for CW Delay Time save to eeprom
95- // cwTimeout = millis() + CW_TIMEOUT;
96- cwTimeout = millis () + cwDelayTime * 10 ;
52+ globalSettings.cwExpirationTimeMs = millis () + globalSettings.cwActiveTimeoutMs ;
9753}
9854
9955/* *
10056 * Stops the cw carrier transmission along with the sidetone
10157 * Pushes the cwTimeout further into the future
10258 */
10359void cwKeyUp (){
104- keyDown = 0 ; // tracks the CW_KEY
10560 noTone (CW_TONE);
106- digitalWrite (CW_KEY, 0 );
61+ digitalWrite (CW_KEY, 0 );
10762
108- // Modified by KD8CEC, for CW Delay Time save to eeprom
109- // cwTimeout = millis() + CW_TIMEOUT;
110- cwTimeout = millis () + cwDelayTime * 10 ;
63+ globalSettings.cwExpirationTimeMs = millis () + globalSettings.cwActiveTimeoutMs ;
11164}
11265
11366// Variables for Ron's new logic
@@ -119,16 +72,14 @@ void cwKeyUp(){
11972enum KSTYPE {IDLE, CHK_DIT, CHK_DAH, KEYED_PREP, KEYED, INTER_ELEMENT };
12073static unsigned long ktimer;
12174unsigned char keyerState = IDLE;
75+ uint8_t keyerControl = 0 ;
12276
12377// Below is a test to reduce the keying error. do not delete lines
12478// create by KD8CEC for compatible with new CW Logic
125- char update_PaddleLatch (byte isUpdateKeyState) {
79+ char update_PaddleLatch (bool isUpdateKeyState) {
12680 unsigned char tmpKeyerControl = 0 ;
12781
12882 int paddle = analogRead (ANALOG_KEYER);
129- // diagnostic, VU2ESE
130- // itoa(paddle, b, 10);
131- // printLine2(b);
13283
13384 // use the PTT as the key for tune up, quick QSOs
13485 if (digitalRead (PTT) == 0 )
@@ -138,18 +89,17 @@ char update_PaddleLatch(byte isUpdateKeyState) {
13889 else if (paddle >= cwAdcDotFrom && paddle <= cwAdcDotTo)
13990 tmpKeyerControl |= DIT_L;
14091 else if (paddle >= cwAdcBothFrom && paddle <= cwAdcBothTo)
141- tmpKeyerControl |= (DAH_L | DIT_L) ;
142- else
143- {
144- if (Iambic_Key)
92+ tmpKeyerControl |= (DAH_L | DIT_L) ;
93+ else {
94+ if (KeyerMode_e::KEYER_STRAIGHT != globalSettings.keyerMode )
14595 tmpKeyerControl = 0 ;
14696 else if (paddle >= cwAdcSTFrom && paddle <= cwAdcSTTo)
14797 tmpKeyerControl = DIT_L ;
14898 else
149- tmpKeyerControl = 0 ;
99+ tmpKeyerControl = 0 ;
150100 }
151101
152- if (isUpdateKeyState == 1 )
102+ if (isUpdateKeyState)
153103 keyerControl |= tmpKeyerControl;
154104
155105 return tmpKeyerControl;
@@ -160,22 +110,24 @@ char update_PaddleLatch(byte isUpdateKeyState) {
160110// modified by KD8CEC
161111******************************************************************************/
162112void cwKeyer (void ){
163- lastPaddle = 0 ;
164113 bool continue_loop = true ;
165114 unsigned tmpKeyControl = 0 ;
166115
167- if ( Iambic_Key ) {
168- while (continue_loop) {
169- switch (keyerState) {
116+ if (KeyerMode_e::KEYER_STRAIGHT != globalSettings. keyerMode ) {
117+ while (continue_loop){
118+ switch (keyerState){
170119 case IDLE:
171120 tmpKeyControl = update_PaddleLatch (0 );
172- if ( tmpKeyControl == DAH_L || tmpKeyControl == DIT_L ||
173- tmpKeyControl == (DAH_L | DIT_L) || (keyerControl & 0x03 )) {
174- update_PaddleLatch (1 );
121+ if ((tmpKeyControl == DAH_L)// Currently dah
122+ ||(tmpKeyControl == DIT_L)// Currently dit
123+ ||(tmpKeyControl == (DAH_L | DIT_L))// Currently both
124+ ||( keyerControl & (DAH_L | DIT_L))){// Resolving either
125+ update_PaddleLatch (true );
175126 keyerState = CHK_DIT;
176- }else {
177- if (0 < cwTimeout && cwTimeout < millis ()){
178- cwTimeout = 0 ;
127+ }
128+ else {
129+ if (0 < globalSettings.cwExpirationTimeMs && globalSettings.cwExpirationTimeMs < millis ()){
130+ globalSettings.cwExpirationTimeMs = 0 ;
179131 stopTx ();
180132 }
181133 continue_loop = false ;
@@ -185,7 +137,7 @@ void cwKeyer(void){
185137 case CHK_DIT:
186138 if (keyerControl & DIT_L) {
187139 keyerControl |= DIT_PROC;
188- ktimer = cwSpeed ;
140+ ktimer = globalSettings. cwDitDurationMs ;
189141 keyerState = KEYED_PREP;
190142 }else {
191143 keyerState = CHK_DAH;
@@ -194,7 +146,7 @@ void cwKeyer(void){
194146
195147 case CHK_DAH:
196148 if (keyerControl & DAH_L) {
197- ktimer = cwSpeed* 3 ;
149+ ktimer = 3 *globalSettings. cwDitDurationMs ;
198150 keyerState = KEYED_PREP;
199151 }else {
200152 keyerState = IDLE;
@@ -203,13 +155,11 @@ void cwKeyer(void){
203155
204156 case KEYED_PREP:
205157 // modified KD8CEC
206- if (!inTx ){
158+ if (!globalSettings. txActive ){
207159 // DelayTime Option
208160 active_delay (delayBeforeCWStartTime * 2 );
209-
210- keyDown = 0 ;
211- cwTimeout = millis () + cwDelayTime * 10 ; // + CW_TIMEOUT;
212- startTx (TX_CW);
161+ globalSettings.cwExpirationTimeMs = millis () + globalSettings.cwActiveTimeoutMs ;
162+ startTx (TuningMode_e::TUNE_CW);
213163 }
214164 ktimer += millis (); // set ktimer to interval end time
215165 keyerControl &= ~(DIT_L + DAH_L); // clear both paddle latch bits
@@ -220,10 +170,11 @@ void cwKeyer(void){
220170
221171 case KEYED:
222172 if (millis () > ktimer) { // are we at end of key down ?
223- cwKeyUp ();
224- ktimer = millis () + cwSpeed ; // inter-element time
173+ cwKeyUp ();
174+ ktimer = millis () + (globalSettings. cwDitDurationMs / 10 ) ; // inter-element time
225175 keyerState = INTER_ELEMENT; // next state
226- }else if (keyerControl & IAMBICB) {
176+ }
177+ else if (KeyerMode_e::KEYER_IAMBIC_B == globalSettings.keyerMode ){
227178 update_PaddleLatch (1 ); // early paddle latch in Iambic B mode
228179 }
229180 break ;
@@ -246,20 +197,19 @@ void cwKeyer(void){
246197 checkCAT ();
247198 } // end of while
248199 }
249- else {
200+ else {// KEYER_STRAIGHT
250201 while (1 ){
251202 char state = update_PaddleLatch (0 );
252- // Serial.println((int)state);
203+ // Serial.println((int)state);
253204 if (state == DIT_L) {
254205 // if we are here, it is only because the key is pressed
255- if (!inTx ){
256- startTx (TX_CW );
206+ if (!globalSettings. txActive ){
207+ startTx (TuningMode_e::TUNE_CW );
257208
258209 // DelayTime Option
259210 active_delay (delayBeforeCWStartTime * 2 );
260-
261- keyDown = 0 ;
262- cwTimeout = millis () + cwDelayTime * 10 ; // + CW_TIMEOUT;
211+
212+ globalSettings.cwExpirationTimeMs = millis () + globalSettings.cwDitDurationMs ;
263213 }
264214 cwKeydown ();
265215
@@ -269,24 +219,16 @@ void cwKeyer(void){
269219 cwKeyUp ();
270220 }
271221 else {
272- if (0 < cwTimeout && cwTimeout < millis ()){
273- cwTimeout = 0 ;
274- keyDown = 0 ;
222+ if (0 < globalSettings.cwExpirationTimeMs && globalSettings.cwExpirationTimeMs < millis ()){
223+ globalSettings.cwExpirationTimeMs = 0 ;
275224 stopTx ();
276225 }
277- // if (!cwTimeout) //removed by KD8CEC
278- // return;
279- // got back to the beginning of the loop, if no further activity happens on straight key
280- // we will time out, and return out of this routine
281- // delay(5);
282- // delay_background(5, 3); //removed by KD8CEC
283- // continue; //removed by KD8CEC
284- return ; // Tx stop control by Main Loop
226+ return ;// Tx stop control by Main Loop
285227 }
286228
287229 checkCAT ();
288230 } // end of while
289- } // end of elese
231+ }// end of else KEYER_STRAIGHT
290232}
291233
292234
0 commit comments