File: pileup.c

package info (click to toggle)
pileup 1.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 268 kB
  • ctags: 113
  • sloc: ansic: 698; makefile: 47
file content (620 lines) | stat: -rwxr-xr-x 15,336 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/************************************************************************/
/* sccw - Soundcard CW for Linux   Steven J. Merrifield			*/
/*			 	   VK3ESM  sjm@ee.latrobe.edu.au	*/
/*				   http://livewire.ee.latrobe.edu.au	*/
/*									*/
/* Feb 92 - First MS-DOS release using PC speaker (Turbo Pascal)	*/
/* Oct 92 - Added punctuation support, varying character delay		*/
/* Aug 95 - Added mouse support for sending practice			*/
/* Jul 96 - Added soundcard support using FM chip			*/
/* Aug 96 - Complete rewrite in ANSI-C, first Linux release	 	*/
/* Dec 96 - Modified by Richard Everitt G4ZFE for use as a Morse Code	*/
/*          pileup program.						*/
/* Apr 97 - Added a competition mode. Implemented each voice as a	*/
/*          Kernel thread using LinuxThreads (by Xavier Leroy,		*/
/*          Xavier.Leroy@inria.fr)					*/
/*          Richard Everitt, G4ZFE. For score results see:		*/
/*		http://www.babbage.demon.co.uk/pileup.html		*/
/************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h> 
#include <signal.h>
#include <asm/io.h>
#include <sys/types.h>
#include <time.h>
#include "pileup.h"
#include "pthread.h"

extern int AdLib_found();
extern int Play_sound();
extern void Cleanup ();

/* Function prototypes	*/
void *start_thread (void*);
void stop_thread (void*);
void Code_2_snd(int,int,int,int);
int Read_data ();
int send_callsign (int,int,int,int);
void controlc_handler (int);
void decrement_count (int);
void *keyboard_thread (void*);
int near_character (char*,char);
int check_call (char*);

pthread_mutex_t global_data_mutex = PTHREAD_MUTEX_INITIALIZER;

char *Code[] = 
		{".-","-...","-.-.","-..",".","..-.","--.",
		"....","..",".---","-.-",".-..","--","-.","---",
		".--.","--.-",".-.","...","-","..-","...-",
		".--","-..-","-.--","--..",  /* A..Z */
		"-----",".----","..---","...--","....-",
		".....","-....","--...","---..","----.", /* 0..9 */
		"..--..","-..-.",""}; /*  q-mark, slant, space */

char *callsign[MAX_CALLS];		/* Calls to be sent		*/

char *calls_tx[MAX_CALLS];		/* Calls sent in this session	*/
char *calls_rx[MAX_CALLS];		/* Calls copied in this session	*/

int  number_of_calls;			/* Max. number to be sent	*/

int  max_speed = 0;			/* Maximum speed (WPM) reached	*/
int  rx_count = 0;			/* Number of calls sent in sess	*/
int  tx_count = 0;			/* Number of calls rx'ed in ses	*/
int  score=0;				/* Cumulative score		*/

int  finished = FALSE;			/* Flag to indicate if user     */
					/* wants to give up!		*/

int  channel = 0;			/* Voice to send callsign on	*/
int  Morse_gap = 3;
int  AdLib_vol = 48;

int  competition_mode = FALSE;		/* Competition mode/Practice 	*/
					/* mode				*/
int  number_session = NUMBER_COMPETITION_CALLSIGNS;
					/* Number of callsigns sent in	*/
					/* Competition mode		*/
int  number_voices;			/* Maximum number of voices	*/
int  base_speed;			/* Slowest CW speed to be used	*/

pthread_t threads [9];			/* One thread for each voice	*/

/************************************************************************/
/* Convert the string into sound					*/
/************************************************************************/
void Code_2_snd(int a, int voice, int speed, int tone)
{
	int i;
	double dit,dah,wait;
	/* Voice 0 is the loudest, 8 the quietist			*/
	int volume_decrease = voice * 4;

	dit = (double)(60.0/(speed*50.0)) * 1000000.0; 	/* delay in us (PARIS method) */
	dah = 3 * dit;
	wait = Morse_gap * dit;

	for (i=0;i<strlen(Code[a]);i++)
	{
		if (Code[a][i] == '.') 
		{
			Play_sound(voice,tone,dit,AdLib_vol - volume_decrease);
		}
		if (Code[a][i] == '-')
		{
			Play_sound(voice,tone,dah,AdLib_vol-volume_decrease);
		}
		usleep(dit);	/* delay between each dit or dah */
	}
	usleep(wait);	 	/* delay between each each characte4 */
}


/************************************************************************/
/* Read a file, and store all the callsigns read			*/
/************************************************************************/
int Read_data ()
{
	FILE *f;
	int i=0;

	if ((f = fopen("/var/lib/pileup/calls.dat","rt")) == NULL)
	{
		fprintf(stderr,"Error opening file.\n");
		return(1);
	}

	while (!feof(f) && i < MAX_CALLS)
	{
		callsign[i] = malloc (12);
		fgets (callsign[i], 12, f);

		/* Remove the \n (if any)				*/
		if (callsign [i] [strlen(callsign[i]) - 1] == 0x0A)
			callsign [i] [strlen(callsign[i]) - 1] = '\0'; 

		i++;
	}

	number_of_calls = i - 1;

	fclose(f);
	return(0);
}

/************************************************************************/
/* Send a callsign on a given voice at specified speed and tone		*/
/************************************************************************/
int send_callsign (int number, int voice, int speed,int tone)
{
	int i, ch;

	Code_2_snd (38,voice,speed,tone);

	/* Keep a track of the fastest speed reached		*/
	if (speed > max_speed)
		max_speed = speed;

	/* Display the callsign about to be sent		*/
	if (!competition_mode)
	{
		printf ("%s ",callsign[number]);
	 	fflush (stdout);	
	}

	/* send callsign					*/
	for (i=0; i < strlen (callsign[number]) ; i++)
	{
		ch = callsign [number] [i];
		if (ch == ' ')
			continue;	

		if (isalpha(ch)) ch = toupper(ch); 
		if (((ch >= 'A') && (ch <= 'Z')) ||
			((ch >= '0' ) && (ch <= '9')) ||
			(ch == '?') || (ch == '/') || (ch == ' ') ||
			(ch == 0x0A))
		{
			if ((ch >= 'A') && (ch <= 'Z')) 
				ch = ch - 65;
			else if ((ch >= '0') && (ch <= '9')) 
				ch = ch - 22;
			else if (ch == '?') 
				ch = 36;
			else if (ch == '/') 
				ch = 37;
			else if (ch == ' ') 
				ch = 38;
			Code_2_snd(ch,voice,speed,tone);
		}
	}

	number_of_calls--;

	return (0);
}


/************************************************************************/
/* On a control C event stop all SoundBlaster voices              	*/
/************************************************************************/
void controlc_handler (int sig)
{
	int i;
	signal (sig, SIG_IGN);

	for (i=0; i < number_voices; i++)
		pthread_cancel (threads[i]);

	for (i=0; i < number_voices; i++)
		pthread_join (threads[i], NULL);

	printf ("\n");

	exit (1);
}

/************************************************************************/
/* Called on a control C event for each thread  			*/
/************************************************************************/
void stop_thread (void *arg)
{
	Cleanup ();

	ioperm(reg_port,17,0);
	ioperm(data_port,17,0);
}

/************************************************************************/
/* Store the callsign being sent for checking at the end		*/
/************************************************************************/
void decrement_count (int call)
{
	pthread_mutex_lock (&global_data_mutex);

	number_session--;

	/* Save the callsign just sent.			*/
	calls_tx[tx_count++] = strdup (callsign [call]);

	if (finished == TRUE)
		number_session = 0;

	pthread_mutex_unlock (&global_data_mutex);
}

/************************************************************************/
/* (Competition mode only). A thrad to read user input. The callsign	*/
/* entered is stored for checking at the end.				*/
/************************************************************************/
void *keyboard_thread (void *arg)
{
	char call[15];

	while (finished == FALSE)
	{
		scanf ("%s", call);

		/* Store the callsign entered from the	*/
		/* keyboard				*/
		calls_rx[rx_count++] = strdup (call);

		/* '0' is the abort now flag		*/
		if (!strcmp (call,"0"))
			finished = TRUE;
	}
	return NULL;
}

/************************************************************************/
/* Start a thread for each voice.                                   	*/
/************************************************************************/
void *start_thread (void *arg)
{
	int call;
	int r_speed, r_tone, k;
	pthread_t tid;

	/* Use the thread ID and process ID as seed	*/
	tid = pthread_self();

	/* Seed the random generator			*/
	srand((int)tid+getpid());	

	ioperm(reg_port,17,1);
	ioperm(data_port,17,1);

	/* Allow the thread to be cancelled immediatedly	*/
	pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
	pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

	/* On thread cancellation, clear the soundblaster voice	*/	
	pthread_cleanup_push ((void*) stop_thread, (void*) NULL);

	while (number_session > 0)          
	{
		call = rand () % number_of_calls;
		r_speed = rand () % 20;
		r_tone = (rand () % 300) + r_speed;
		k = rand () % 3;

		decrement_count(call);

		send_callsign (call,(int)arg,r_speed+base_speed,r_tone+BASE_TONE);

		sleep (k);
	}

	pthread_cleanup_pop (0);

	return NULL;
}

/************************************************************************/
/* Attempt to match partials in a string, e.g call sent is g4zfe	*/
/* copied as g4zfi.							*/
/************************************************************************/
int near_character (char *call, char letter)
{
	char	c;
	int	i;

	for (i=0; i < strlen(call); i++)
	{
		c = call[i];

		if (c == 'E' && letter == 'I')
			return (TRUE);

		if (c == 'I' && letter == 'E')
			return (TRUE);

		if (c == 'S' && letter == 'H')
			return (TRUE);

		if (c == 'H' && letter == 'S')
			return (TRUE);

		if (c == 'D' && letter == 'B')
			return (TRUE);

		if (c == 'B' && letter == 'D')
			return (TRUE);

		if (c >= '0' && c <= '9')
			if (letter >= '0' && letter <= '9')
				if (c > letter)
					if (c - letter == 1)
						return (TRUE);
				else if (letter - c == 1)
					return (TRUE);
	}

	return (FALSE);
}

/************************************************************************/
/* Find the best match to the received callsign.			*/
/************************************************************************/
int check_call (char *rx_call)
{
	int i,j;		/* Local counters		*/
	int match;		/* Characters match		*/
	int partial;		/* Partial character match	*/
	char *p;
	char *ptr;
	char temp_call [15];
	int possible_match = -1;
	int possible_match_score = 0;
	float possible_match_rating = 0;
	float rating = 0;

	/* Check the received call against each of the 		*/
	/* transmitted calls in order to find the best match	*/
	for (i=0; i < tx_count; i++)
	{
		strcpy (temp_call, calls_tx[i]);
		ptr = rx_call;
		match = 0;
		partial = 0;

		/* Ignore empty callsigns			*/
		if (strlen (temp_call) == 0 || strlen (rx_call) == 0)
			continue;
		
		/* Check for correct callsign match		*/
		if (!strcmp (rx_call, temp_call))
		{
			score += strlen(rx_call);
			return (i);
		}

		/* Find exact character matches			*/
		for (j=0; j < strlen(temp_call); j++)
		{
			if (calls_tx[i][j] != '\0')
			{
				p = strchr (ptr, temp_call[j]);

				if (p != NULL)
				{
					/* Increment pointer 	*/
					/* to search rest of str*/
					ptr = p++;
					match++;

					/* Mark characeters so	*/
					/* it will not be 	*/
					/* checked again	*/
					temp_call[j] = '.';
				}
			}
		}

		if (match > 0)
		{
			/* Find partial matches			*/
			for (j=0; j < strlen(temp_call); j++)
				if (near_character (rx_call, temp_call[j]))
					partial++;
		}

		/* Rate this match. Partial matches are only 	*/
		/* worth half as much as a complete match	*/
		rating = match + (partial/2);

		/* Determine if this match is the "best one"	*/
		if (rating > possible_match_rating)
		{
			possible_match = i;
			possible_match_rating = match + (partial/2);
			possible_match_score = match;
		}
	}

	/* If we've found a match then return it		*/
	if (possible_match != -1)
	{
		score += possible_match_score;
		return (possible_match);
	}

	return (-1);
}


/************************************************************************/
/* Setup the IO ports, and start sending, then reset the IO ports on	*/ 
/* exit. Note that this requires the setuid bit to be set, since we are	*/
/* playing with the hardware directly.					*/
/************************************************************************/
int main(int argc, char *argv[])
{
	pthread_t keyb_thread;
	int i;
	int j;
	int retcode;
	int max_score = 0;
	int results [MAX_CALLS];
	char *tx_string[MAX_CALLS];
	char mode [12];

	if (!(AdLib_found))
	{
		fprintf(stderr,"\nAdLib/SoundBlaster card not found.\n");
		exit(1);
	}

	Read_data ();

	printf ("How many voices (1 to 9)\n");
	fscanf (stdin,"%d",&number_voices);

	if (number_voices < 0 || number_voices > 9)
	{
		printf ("Only 9 voices allowed ...\n");
		exit (1);
	}

	printf ("Starting speed (wpm)\n");
	fscanf (stdin,"%d",&base_speed);

	printf ("(C)ompetion mode or (P)ractice mode\n");
	fscanf (stdin,"%s",mode);

	if (mode[0] == 'C' || mode[0] == 'c')
	{
		number_session = NUMBER_COMPETITION_CALLSIGNS;
		competition_mode = TRUE;
	}
	else
	{
		number_session = number_of_calls;
		competition_mode = FALSE;
	}

	signal (SIGINT, controlc_handler);

	/* Create a thread to handle the keyboard		*/
	if (competition_mode)
	{
		retcode = pthread_create (&keyb_thread, NULL, keyboard_thread, (void*) NULL );
		printf ("Enter '0' to abort the session! GL..\n");
		sleep (1);
	}

	/* Create a thread for each voice requested		*/
	for (i=0; i< number_voices; i++)
	{
		retcode = pthread_create (&threads[i], 
					  NULL, 
					  start_thread,
					  (void*) i);
		if (retcode != 0)
			fprintf (stderr,"Creation of thread %d failed \n", i);
	}

	/* Wait for all the threads to finish			*/
	for (i=0; i< number_voices; i++)
	{
		pthread_join (threads[i], NULL);
	}

	if (competition_mode)
	{
		/* wait for the last call to be copied			*/
		sleep (2);

		/* Convert everything to upper case			*/
		for (i=0; i < tx_count; i++)
		{
			for (j=0; j < strlen (calls_tx[i]); j++)
				calls_tx [i][j] = toupper (calls_tx[i][j]);

			tx_string [i] = strdup (calls_tx[i]);
		}

		for (i=0; i < rx_count; i++)
			for (j=0; j < strlen (calls_rx[i]); j++)
				calls_rx [i][j] = toupper (calls_rx[i][j]);

		/* Pad out rx array					*/
		for (i=rx_count; i< tx_count; i++)
			calls_rx [i] = strdup ("");

		/* Determine maximum score				*/
		for (i=0; i < tx_count; i++)
		{
			max_score += strlen (calls_tx[i]);
			results[i] = -1;
		}
	
		/* Check calls received against those sent		*/
		/* and store the position of any matches		*/
		for (i=0; i < rx_count; i++)
		{
			retcode = check_call (calls_rx[i]);
			if (retcode != -1)
			{
				results [retcode] = i;
				strcpy (calls_tx[retcode],"");
			}
		}

		/* Display the results					*/
		printf ("\n");
		printf ("TX\t\tRX\t   TX\t\tRX\n");
		printf ("--\t\t--\t   --\t\t--\n");

		for (j=0; j < tx_count/2; j++) 
		{
			printf ("%-12s ",tx_string[j]);
			if (results[j] != -1)	
				printf ("%-12s",calls_rx[results[j]]);
			else
				printf ("%-12s"," ");

			printf ("%-12s ",tx_string[j+tx_count/2]);
			if (results[j+tx_count/2] != -1)
				printf ("%-12s\n",calls_rx[results[j+tx_count/2]]);
			else
				printf ("%-12s\n"," ");
		}


		printf ("\n");
		printf ("Accuracy: %d/%d. Max speed: %d\n",score,max_score,max_speed);
		printf ("Score: %d\n", score * max_speed * number_voices);

		/* Free up as we're about to exit		*/
		for (i=0; i < tx_count; i++)
		{
			free (tx_string[i]);
		}

		for (i=0; i < rx_count; i++)
			free (calls_rx[i]);

	}

	for (i=0; i < tx_count; i++)
	{
		free (calls_tx[i]);
	}

	/* Free up as we're about to exit		*/
	for (i=0; i < number_of_calls; i++)
		free (callsign [i]);

	ioperm(reg_port,17,0);
	ioperm(data_port,17,0);

	return(0);
}