1 /*
2  *      cafsk12.h -- AFSK1200 demodulator class
3  *
4  *      Copyright (C) 1996
5  *          Thomas Sailer (sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu)
6  *
7  *      Copyright (C) 2011 Alexandru Csete (oz9aec at gmail.com)
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 #ifndef CAFSK12_H
24 #define CAFSK12_H
25 
26 #include <QObject>
27 
28 extern const float costabf[0x400];
29 #define COS(x) costabf[(((x)>>6)&0x3ffu)]
30 #define SIN(x) COS((x)+0xc000)
31 
32 /*
33  * Standard TCM3105 clock frequency: 4.4336MHz
34  * Mark frequency: 2200 Hz
35  * Space frequency: 1200 Hz
36  */
37 #define FREQ_MARK  1200
38 #define FREQ_SPACE 2200
39 #define FREQ_SAMP  22050
40 #define BAUD       1200
41 #define SUBSAMP    2
42 
43 #define CORRLEN ((int)(FREQ_SAMP/BAUD))
44 #define SPHASEINC (0x10000u*BAUD*SUBSAMP/FREQ_SAMP)
45 
46 
47 struct demod_state {
48     const struct demod_param *dem_par;
49     union {
50         struct l2_state_hdlc {
51             unsigned char rxbuf[512];
52             unsigned char *rxptr;
53             unsigned int rxstate;
54             unsigned int rxbitstream;
55             unsigned int rxbitbuf;
56         } hdlc;
57 
58         struct l2_state_pocsag {
59             unsigned long rx_data;
60             struct l2_pocsag_rx {
61                 unsigned char rx_sync;
62                 unsigned char rx_word;
63                 unsigned char rx_bit;
64                 char func;
65                 unsigned long adr;
66                 unsigned char buffer[128];
67                 unsigned int numnibbles;
68             } rx[2];
69         } pocsag;
70     } l2;
71     union {
72         struct l1_state_poc5 {
73             unsigned int dcd_shreg;
74             unsigned int sphase;
75             unsigned int subsamp;
76         } poc5;
77 
78         struct l1_state_poc12 {
79             unsigned int dcd_shreg;
80             unsigned int sphase;
81             unsigned int subsamp;
82         } poc12;
83 
84         struct l1_state_poc24 {
85             unsigned int dcd_shreg;
86             unsigned int sphase;
87         } poc24;
88 
89         struct l1_state_afsk12 {
90             unsigned int dcd_shreg;
91             unsigned int sphase;
92             unsigned int lasts;
93             unsigned int subsamp;
94         } afsk12;
95 
96         struct l1_state_afsk24 {
97             unsigned int dcd_shreg;
98             unsigned int sphase;
99             unsigned int lasts;
100         } afsk24;
101 
102         struct l1_state_hapn48 {
103             unsigned int shreg;
104             unsigned int sphase;
105             float lvllo, lvlhi;
106         } hapn48;
107 
108         struct l1_state_fsk96 {
109             unsigned int dcd_shreg;
110             unsigned int sphase;
111             unsigned int descram;
112         } fsk96;
113 
114         struct l1_state_dtmf {
115             unsigned int ph[8];
116             float energy[4];
117             float tenergy[4][16];
118             int blkcount;
119             int lastch;
120         } dtmf;
121 
122         struct l1_state_zvei {
123             unsigned int ph[16];
124             float energy[4];
125             float tenergy[4][32];
126             int blkcount;
127             int lastch;
128         } zvei;
129 
130         struct l1_state_scope {
131             int datalen;
132             int dispnum;
133             float data[512];
134         } scope;
135     } l1;
136 };
137 
138 
139 struct demod_param {
140     const char *name;
141     unsigned int samplerate;
142     unsigned int overlap;
143     //void (*init)(struct demod_state *s);
144     //void (*demod)(struct demod_state *s, float *buffer, int length);
145 };
146 
147 
148 class CAfsk12 : public QObject
149 {
150     Q_OBJECT
151 public:
152     explicit CAfsk12(QObject *parent = 0);
153     ~CAfsk12();
154 
155     void demod(float *buffer, int length);
156     void reset();
157 
158 signals:
159     void newMessage(const QString &message);
160 
161 public slots:
162 
163 private:
164     float corr_mark_i[CORRLEN];
165     float corr_mark_q[CORRLEN];
166     float corr_space_i[CORRLEN];
167     float corr_space_q[CORRLEN];
168 
169     struct demod_state *state;
170 
171     /* HDLC functions */
172     void hdlc_init(struct demod_state *s);
173     void hdlc_rxbit(struct demod_state *s, int bit);
174     void verbprintf(int verb_level, const char *fmt, ...);
175     void ax25_disp_packet(unsigned char *bp, unsigned int len);
176 };
177 
178 #endif // CAFSK12_H
179