]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/win/keyerio.c
Upgraded windows alternatives
[dttsp.git] / jDttSP / win / keyerio.c
1 #include <keyer.h>
2
3 //========================================================================
4
5 /* Read a straight key connected to a serial port, do debouncing, then
6    return the key state */
7
8 BOOLEAN
9 read_straight_key(KeyerState ks, BOOLEAN keyed) {
10   int i, j;
11   static BOOLEAN keystate = 0;
12   static int debounce_buf_i = 0,
13              debounce_buf[DEBOUNCE_BUF_MAX_SIZE];
14   debounce_buf[debounce_buf_i] = keyed;
15   debounce_buf_i++;
16
17   //
18   //***************************************************
19   // back to business as usual
20   //***************************************************
21
22   /* If the debounce buffer is full, determine the state of the key */
23   if (debounce_buf_i >= ks->debounce) {
24     debounce_buf_i = 0;
25
26     j = 0;
27     for (i = 0; i < ks->debounce; i++)
28       if (debounce_buf[i])
29         j++;
30     keystate = (j > ks->debounce / 2) ? 1 : 0;
31   }
32
33   return keystate;
34 }
35
36 //------------------------------------------------------------------------
37
38 /* Read an iambic key connected to a serial port, do debouncing, emulate a
39    straight key, then return the emulated key state */
40
41 BOOLEAN
42 read_iambic_key(KeyerState ks, BOOLEAN dash, BOOLEAN dot, KeyerLogic kl, double ticklen) {
43   int i, j;
44   static BOOLEAN dah_debounce_buf[DEBOUNCE_BUF_MAX_SIZE],
45                  dit_debounce_buf[DEBOUNCE_BUF_MAX_SIZE];
46   static int dah = 0, debounce_buf_i = 0, dit = 0;
47   
48   if (ks->flag.revpdl) {
49         dah_debounce_buf[debounce_buf_i] = dot;
50         dit_debounce_buf[debounce_buf_i] = dash;
51   } else {
52         dah_debounce_buf[debounce_buf_i] = dash;
53         dit_debounce_buf[debounce_buf_i] = dot;
54   }
55   debounce_buf_i++;
56  
57   //
58   //***************************************************
59   // back to business as usual
60   //***************************************************
61
62   /* If the debounce buffer is full, determine the state of the keys */
63   if (debounce_buf_i >= ks->debounce) {
64     debounce_buf_i = 0;
65
66     j = 0;
67     for (i = 0; i < ks->debounce; i++)
68       if (dah_debounce_buf[i]) j++;
69     dah = (j > ks->debounce / 2) ? 1 : 0;
70
71     j = 0;
72     for (i = 0; i < ks->debounce; i++)
73       if (dit_debounce_buf[i]) j++;
74     dit = (j > ks->debounce / 2) ? 1 : 0;
75   }
76
77   return klogic(kl,
78                 dit,
79                 dah,
80                 ks->wpm,
81                 ks->mode,
82                 ks->flag.mdlmdB,
83                 ks->flag.memory.dit,
84                 ks->flag.memory.dah,
85                 ks->flag.autospace.khar,
86                 ks->flag.autospace.word,
87                 ks->weight,
88                 ticklen);
89 }
90
91 //========================================================================