File: elbug.c

package info (click to toggle)
hf 0.7.3-4etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,684 kB
  • ctags: 3,156
  • sloc: ansic: 26,447; cpp: 4,909; sh: 3,785; makefile: 309
file content (185 lines) | stat: -rw-r--r-- 4,887 bytes parent folder | download | duplicates (2)
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
/* serial elbug = electronic morse key. */
/* by Günther Montag dl4mge */
/* elbug is distributed alone and as part of the hf package. */
/* Just compile me by 'gcc elbug.c -o elbug' */
/* and copy elbug to user/local/bin. */
/* Call me as root. */
/* How to cable your homebrew elbug? See below! */

/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 */

#include <stdio.h>

#if HAVE_SYS_IO_H
  #include <sys/io.h>
#endif

#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

char *cable = "\n"
	"* * * elbug - electronic morse key by dl4mge. How to cable: * * *\n"
	"*  Middle pad: +9V via Resistor 2 k                             *\n"                           
	"*  Left  contact: -> DCD (9-pin plug: 1) (25-pin plug: 8)       *\n"
	"*  Right contact: -> CTS (9-pin plug: 8) (25-pin plug: 5)       *\n"
	"*  Ground:               (9-pin plug: 5) (25-pin plug: 7)       *\n"
	"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" ;


/* default parameters */
int pin = TIOCM_RTS;
char* name_ptt = "/dev/ttyS0";
int port = 0x3f8;
int invert_ptt = 0;
unsigned int wpm = 12;

int fd_ptt;
int dotus;

void wait(int us) {
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = us;
    select (0, NULL, NULL, NULL, &tv);
}

void output_elbug_serial(int ptt)
{
	if (fd_ptt < 0) {
	    printf("no serial port open for output.\n");
	    exit (1);
	}
	if (ioctl(fd_ptt, 
	    ((ptt && !invert_ptt) || (!ptt && invert_ptt)) ? 
	    TIOCMBIS : TIOCMBIC, &pin)) {
	    printf ("ioctl: TIOCMBI[CS]\n");
	    printf ( "serial port can not be accessed!\n");
	    exit (1);
	}
}

void elbug_send_dit() {
    output_elbug_serial(1);
//    printf(".");
    wait(dotus);
    output_elbug_serial(0);
    wait(dotus);
}

void elbug_send_dah() {
    output_elbug_serial(1);
//    printf("_");    
    wait(dotus * 3);
    output_elbug_serial(0);
    wait(dotus);
}

int main(int argc, char *argv[]) {
    int c, err = 0;
    unsigned short int dcd = 0, cts = 0;
    
    while ((c = getopt(argc, argv, "w:s:di")) != -1) {
	switch (c) {
	    case 's':
		name_ptt = optarg;
		if (! strcmp (name_ptt, "/dev/ttyS0")) {
		    port = 0x03F8;
		    break;
		}
		if (! strcmp (name_ptt, "/dev/ttyS1")) {
		    port = 0x02F8;
		    break;
		}
		if (! strcmp (name_ptt, "/dev/ttyS2")) {
		    port = 0x03E8;
		    break;
		}
		if (! strcmp (name_ptt, "/dev/ttyS3")) {
		    port = 0x02E8;
		    break;
		}
		break;
    	    case 'd':
		pin = TIOCM_DTR;
    		break;
            case 'i':
	        invert_ptt = 1;
    	        break;
    	    case 'w':
		wpm = strtoul(optarg, NULL, 0);
		if (wpm < 3 || wpm > 90) {
		    printf("Speed %d out of range 3...90 wpm\n", wpm);
    		    err++;
		}

		break;
	    default:
        	err++;
        	break;
    	}
    	if (err) {
    	    printf("/* dl4mge's elbug.*/\n"
		"usage: elbug [-w <speed wpm>] [-s <ptt comm port>] [-d] [-i] \n"
			"  -w: speed in wpm              (default: 12 words per minute)\n"
			"  -s: serial port for in/output (default: /dev/ttyS0)\n"
			"  -d: output through DTR pin    (default is RTS)\n"
			"  -i: invert PTT                (default: PTT = positive)\n"
			"%s", cable);
	exit (0);
	}
    }
#if HAVE_SYS_IO_H
    if ((err = ioperm(port, 8, 1))) {
	printf("permission problem for serial port %04x: ioperm = %d\n", 
	    port, err);
	printf("This program has to be called with root permissions.\n");
    }
#endif
    if ((fd_ptt = open(name_ptt, O_RDWR, 0)) < 0) {
	printf("error in opening ptt device %s - maybe try another one?\n", 
	    name_ptt);
    }
    dotus = 1000000 / (wpm * 50 / 60);

    printf("%s",cable);
    printf("\nelbug: %d wpm at %s, address %04x, %s %s\n",
	wpm, name_ptt, port,
	invert_ptt ? "inverted ptt output," : "",
	pin == TIOCM_DTR ? "DTR output" : "RTS output");
    printf("A dit will last %d ms.\n", dotus/1000);
    printf("See options by elbug -h. Stop me by <strg>c.\n");
    
    output_elbug_serial(0);

    /* main loop */
    for (;;) {
#if HAVE_SYS_IO_H
	dcd = inb(port + 6) & 128;
	cts = inb(port + 6) & 16;
#endif
	if (cts) elbug_send_dit();
	if (dcd) elbug_send_dah();

	wait(3000);
    }
}