1 // ----------------------------------------------------------------------------
2 //    thor.h  --  thor modem
3 //
4 // Copyright (C) 2008-2012
5 //     David Freese <w1hkj@w1hkj.com>
6 //     John Douyere <vk2eta@gmail.com>
7 //     John Phelps  <kl4yfd@gmail.com>
8 //
9 // This file is part of fldigi.
10 //
11 // Fldigi is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // Fldigi is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with fldigi.  If not, see <http://www.gnu.org/licenses/>.
23 // ----------------------------------------------------------------------------
24 
25 #ifndef _thor_H
26 #define _thor_H
27 
28 #include <string>
29 
30 #include "complex.h"
31 #include "modem.h"
32 #include "globals.h"
33 #include "filters.h"
34 #include "fftfilt.h"
35 #include "dominovar.h"
36 #include "mbuffer.h"
37 
38 #include "picture.h"
39 #include <FL/Fl_Shared_Image.H>
40 
41 // NASA coefficients for viterbi encode/decode algorithms
42 #define	THOR_K	7
43 #define	THOR_POLY1	0x6d
44 #define	THOR_POLY2	0x4f
45 
46 //VK2ETA high speed modes
47 // IEEE coefficients for viterbi encode/decode algorithms
48 #define	THOR_K15	15
49 #define	K15_POLY1	044735
50 #define	K15_POLY2	063057
51 
52 //#include "mfskvaricode.h"
53 #include "interleave.h"
54 
55 #include "viterbi.h"
56 
57 
58 #define THORNUMTONES 18
59 #define THORMAXFFTS  8
60 #define THORBASEFREQ 1500.0
61 #define THORFIRSTIF  2000.0
62 
63 #define THORSCOPESIZE 64
64 
65 #define THORSLOWPATHS 3
66 #define THORFASTPATHS 5
67 
68 #define THOR_IMAGESPP			10
69 
70 // the following constant changes if a mode with more tones than 25x4 is
71 // created
72 #define MAXPATHS (8 * THORFASTPATHS * THORNUMTONES )
73 
74 struct THORrxpipe {
75 	cmplx vector[THORMAXFFTS * THORNUMTONES * 6];
76 };
77 
78 class thor : public modem {
79 public:
80 	enum {
81 		TX_STATE_PREAMBLE,
82 		TX_STATE_START,
83 		TX_STATE_DATA,
84 		TX_STATE_END,
85 		TX_STATE_IMAGE,
86 		TX_STATE_AVATAR,
87 		TX_STATE_FLUSH,
88 		TX_STATE_RECEIVE
89 	};
90 	enum THOR_STATE {
91 		TEXT,
92 		IMAGE_START,
93 		IMAGE_SYNC,
94 		IMAGE
95 	};
96 protected:
97 // common variables
98 	double	phase[THORMAXFFTS + 1];
99 	double	txphase;
100 	int		symlen;
101 	int		doublespaced;
102 	double	tonespacing;
103 	int		counter;
104 	unsigned int	twosym;
105 	int		paths;
106 	int		numbins;
107 	bool	slowcpu;
108 	int		basetone;
109 	int		lotone;
110 	int		hitone;
111 	int		extones;
112 
113 // rx variables
114 	C_FIR_filter	*hilbert;
115 	sfft			*binsfft[THORMAXFFTS];
116 	fftfilt			*fft;
117 	Cmovavg			*vidfilter[THORSCOPESIZE];
118 	Cmovavg			*syncfilter;
119 
120 	THORrxpipe		*pipe;
121 	unsigned int	pipeptr;
122 	unsigned int	datashreg;
123 	mbuffer<double, 0, 2>	scopedata;
124 	mbuffer<double, 0, 2>	videodata;
125 
126 	cmplx currvector;
127 
128 	int currsymbol;
129 	int prev1symbol;
130 	int prev2symbol;
131 
132 	double currmag;
133 	double prev1mag;
134 	double prev2mag;
135 
136 	double met1;
137 	double met2;
138 	double sig;
139 	double noise;
140 	double s2n;
141 
142 	int synccounter;
143 
144 	unsigned char symbolbuf[MAX_VARICODE_LEN];
145 	int symcounter;
146 
147 	int symbolbit;
148 
149 	bool filter_reset;
150 	bool staticburst;
151 
152 	int fec_confidence;
153 
154 // tx variables
155 	int txstate;
156 	int txprevtone;
157 	unsigned int bitshreg;
158 	std::string strSecXmtText;
159 	unsigned int cptr;
160 
161 	viterbi		*Dec;
162 	interleave	*Rxinlv;
163 	encoder		*Enc;
164 	interleave	*Txinlv;
165 	int			bitstate;
166 	unsigned char symbolpair[2];
167 
168 	int flushlength;
169 
170 
171 private:
172 	cmplx	mixer(int n, const cmplx& in);
173 
174 // Rx
175 	void	recvchar(int c);
176 	void	decodesymbol();
177 	void	softdecodesymbol();
178 	int		harddecode();
179 	int		softdecode();
180 	void	update_syncscope();
181 	void	synchronize();
182 	void	reset_afc();
183 	void	eval_s2n();
184 	int		get_secondary_char();
185 	void	reset_filters();
186 	void	decodePairs(unsigned char symbol);
187 	bool	preambledetect(int c);
188 	void	softflushrx();
189 
190 	void	parse_pic(int);
191 
192 // Tx
193 	void	sendtone(int tone, int duration);
194 	void	sendsymbol(int sym);
195 	void	sendchar(unsigned char c, int secondary);
196 	void	sendidle();
197 	void	sendsecondary();
198 	void	flushtx();
199 	void	Clearbits();
200 
201 protected:
202 	void	s2nreport(void);
203 
204 public:
205 	thor (trx_mode md);
206 	~thor ();
207 	void	init();
208 	void	rx_init();
209 	void	tx_init();
210 	void	restart();
211 	int		rx_process(const double *buf, int len);
212 	int		tx_process();
213 
214 // support for thor image transfers
215 private:
216 	double amplitude;
217 	double pixel;
218 	double sync;
219 	double img_phase;
220 	unsigned char tx_pixel;
221 	int tx_pixelnbr;
222 	int image_mode;
223 public:
224 	int				byte;
225 	double			picf;
226 	double			picpeak;
227 	C_FIR_filter	*picfilter;
228 	Cmovavg 		*pixfilter;
229 	Cmovavg			*pixsyncfilter;
230 	double			phidiff;
231 	double			pic_phase;
232 	cmplx			prevz;
233 	cmplx			currz;
234 	double			image_freq[10];
235 	int				image_counter;
236 	int				picW;
237 	int				picH;
238 	int				row;
239 	int				col;
240 	int				rgb;
241 	int				pixelnbr;
242 	static int		IMAGEspp;
243 	int				TXspp;
244 	static std::string		imageheader;
245 	static std::string		avatarheader;
246 	std::string		pic_str;
247 	THOR_STATE		state;
248 	bool			b_ava;
249 
250 	void			recvpic(double smpl);
251 	void			send_image();
252 	void			send_avatar();
253 	void			thor_send_avatar();
254 	void			m_thor_send_avatar();
255 	void			thor_send_image(std::string s = "", bool gray = false);
256 
257 };
258 
259 #endif
260