]> git.rkrishnan.org Git - dttsp.git/blobdiff - jDttSP/keyb.c
Further keyer improvements: update commands for both physical and type-in keyers
[dttsp.git] / jDttSP / keyb.c
index 158b18f7a75506ad52e388661b145c62b1d74c02..14e128b8a9c4815f176a90223ea648fecedf4e28 100644 (file)
@@ -76,6 +76,12 @@ int ditspacesize, dahspacesize,
     risesize, fallsize;
 double riseincr, fallincr;
 
+#define MAX_ESC (512)
+#define ESC_L '<'
+#define ESC_R '>'
+
+void inlinecmd(char *, int);
+
 void jack_ringbuffer_clear(jack_ringbuffer_t *, int);
 void jack_ringbuffer_restart(jack_ringbuffer_t *, int);
 void send_sound(COMPLEX *, int);
@@ -98,6 +104,21 @@ reader_thread(void) {
   // keep reading 1 char at a time
   while ((c = getchar()) != EOF) {
     
+    // inline command?
+    if (c == ESC_L) {
+      int i = 0;
+      char buf[MAX_ESC];
+      while ((c = getchar()) != EOF) {
+       if (c == ESC_R) break;
+       buf[i] = c;
+       if (++i >= (MAX_ESC - 1)) break;
+      }
+      if (c == EOF) goto finish;
+      buf[i] = 0;
+      inlinecmd(buf, i);
+      continue;
+    }
+
     // is char mapped to morse?
     if (m = get_morse(c)) {
       
@@ -158,6 +179,7 @@ reader_thread(void) {
     }
   }
   
+ finish:
   // indicate EOF on input
   sem_wait(&reader);
   morsel.type = ME_EOF;
@@ -168,9 +190,7 @@ reader_thread(void) {
 void
 sound_thread(void) {
   int i, k = 0;
-  double ofreq = freq * 2.0 * M_PI / SAMP_RATE,
-         phase = 0.0,
-         scale = pow(10.0, gain / 20.0);
+  double ofreq, scale, phase = 0.0;
   COMPLEX z, delta_z;
 
   // keep looking for sub-element segments, one at a time
@@ -185,7 +205,10 @@ sound_thread(void) {
 
     // requires playing some tone?
     if (morsel.type != ME_ZERO) {
-      // yes, set up CORDIC tone generation
+      // yes, reset params and
+      // set up CORDIC tone generation
+      ofreq = freq * 2.0 * M_PI / SAMP_RATE;
+      scale = pow(10.0, gain / 20.0);
       if (phase > HUGE_PHASE) phase -= HUGE_PHASE;
       z = Cmplx(cos(phase), sin(phase));
       delta_z = Cmplx(cos(ofreq), sin(ofreq));
@@ -291,6 +314,33 @@ jack_callback(jack_nframes_t nframes, void *arg) {
   }
 }
 
+void
+resetparam(void) {
+  morsel.wpm = wpm;
+  morsel.rise = morsel.fall = ramp;
+  morsel.rate = SAMP_RATE;
+
+  ditspacesize = SAMP_RATE * 1.2 / morsel.wpm + 0.5;
+  dahspacesize = 3 * ditspacesize;
+  charspacesize = dahspacesize;
+  wordspacesize = 7 * ditspacesize;
+
+  risesize = SAMP_RATE * morsel.rise / 1e3 + 0.5;
+  if (risesize > 1)
+    riseincr = 1.0 / (risesize - 1);
+  else
+    riseincr = 1.0;
+
+  fallsize = SAMP_RATE * morsel.fall / 1e3 + 0.5;
+  if (fallsize > 1)
+    fallincr = -1.0 / (fallsize - 1);
+  else
+    fallincr = -1.0;
+
+  ditstdysize = ditspacesize - risesize - fallsize;
+  dahstdysize = dahspacesize - risesize - fallsize;
+}
+
 int
 main(int argc, char **argv) {
   int i;
@@ -321,29 +371,7 @@ main(int argc, char **argv) {
 
   //------------------------------------------------------------
 
-  morsel.wpm = wpm;
-  morsel.rise = morsel.fall = ramp;
-  morsel.rate = SAMP_RATE;
-
-  ditspacesize = SAMP_RATE * 1.2 / morsel.wpm + 0.5;
-  dahspacesize = 3 * ditspacesize;
-  charspacesize = dahspacesize;
-  wordspacesize = 7 * ditspacesize;
-
-  risesize = SAMP_RATE * morsel.rise / 1e3 + 0.5;
-  if (risesize > 1)
-    riseincr = 1.0 / (risesize - 1);
-  else
-    riseincr = 1.0;
-
-  fallsize = SAMP_RATE * morsel.fall / 1e3 + 0.5;
-  if (fallsize > 1)
-    fallincr = -1.0 / (fallsize - 1);
-  else
-    fallincr = -1.0;
-
-  ditstdysize = ditspacesize - risesize - fallsize;
-  dahstdysize = dahspacesize - risesize - fallsize;
+  resetparam();
 
   //------------------------------------------------------------
 
@@ -433,9 +461,12 @@ char *morse_table[128] = {
   /* 028  FS */ 0, /* 029  GS */ 0, /* 030  RS */ 0, /* 031  US */ 0,
   /* 032  SP */ 0,
   /* 033   ! */ "...-.",       // [SN]
-  /* 034   " */ 0, /* 035   # */ 0, /* 036   $ */ 0,
+  /* 034   " */ 0,
+  /* 035   # */ 0,
+  /* 036   $ */ 0,
   /* 037   % */ ".-...",       // [AS]
-  /* 038   & */ 0, /* 039   ' */ 0,
+  /* 038   & */ 0,
+  /* 039   ' */ 0,
   /* 040   ( */ "-.--.",       // [KN]
   /* 041   ) */ 0,
   /* 042   * */ "...-.-",      // [SK]
@@ -454,7 +485,9 @@ char *morse_table[128] = {
   /* 055   7 */ "--...",
   /* 056   8 */ "---..",
   /* 057   9 */ "----.",
-  /* 058   : */ 0, /* 059   ; */ 0, /* 060   < */ 0,
+  /* 058   : */ 0,
+  /* 059   ; */ 0,
+  /* 060   < */ 0,
   /* 061   = */ "-...-",       // [BT]
   /* 062   > */ 0,
   /* 063   ? */ "..__..",      // [IMI]
@@ -485,8 +518,12 @@ char *morse_table[128] = {
   /* 088   X */ "-..-",
   /* 089   Y */ "-.--",
   /* 090   Z */ "--..",
-  /* 091   [ */ 0, /* 092   \ */ 0, /* 093   ] */ 0, /* 094   ^ */ 0,
-  /* 095   _ */ 0, /* 096   ` */ 0,
+  /* 091   [ */ 0,
+  /* 092   \ */ 0,
+  /* 093   ] */ 0,
+  /* 094   ^ */ 0,
+  /* 095   _ */ 0,
+  /* 096   ` */ 0,
   /* 097   a */ ".-",
   /* 098   b */ "-...",
   /* 099   c */ "-.-.",
@@ -513,6 +550,24 @@ char *morse_table[128] = {
   /* 120   x */ "-..-",
   /* 121   y */ "-.--",
   /* 122   z */ "--..",
-  /* 123   { */ 0, /* 124   | */ 0, /* 125   } */ 0, /* 126   ~ */ 0,
+  /* 123   { */ 0,
+  /* 124   | */ 0,
+  /* 125   } */ 0,
+  /* 126   ~ */ 0,
   /* 127 DEL */ 0
 };
+
+void
+inlinecmd(char *buf, int len) {
+  if (!buf || len < 1) return;
+  if (!strncmp(buf, "wpm", 3)) {
+    wpm = atof(buf + 3);
+    resetparam();
+  } else if (!strncmp(buf, "ramp", 4)) {
+    ramp = atof(buf + 4);
+    resetparam();
+  } else if (!strncmp(buf, "freq", 4))
+    freq = atof(buf + 4);
+  else if (!strncmp(buf, "gain", 4))
+    gain = atof(buf + 4);
+}