oopsie dumb file
[teletype.git] / python / teletype-test.py
1 import time
2 import binascii
3 import urllib # for url unquote
4 import threading
5 import pigpio
6
7 pi = pigpio.pi() # connect to local Pi
8
9 pi.set_mode(2, pigpio.OUTPUT)
10
11 gpio8period      = 20 # period of 1 bit to achieve 45bps was 20
12
13 ColumnCurrentPosition = 1
14 ColumnMax             = 68
15
16 # first we map ascii to the possible ascii chars
17 ascii_to_baudot_char = {
18   'a':'A',
19   'b':'B',
20   'c':'C',
21   'd':'D',
22   'e':'E',
23   'f':'F',
24   'g':'G',
25   'h':'H',
26   'i':'I',
27   'j':'J',
28   'k':'K',
29   'l':'L',
30   'm':'M',
31   'n':'N',
32   'o':'O',
33   'p':'P',
34   'q':'Q',
35   'r':'R',
36   's':'S',
37   't':'T',
38   'u':'U',
39   'v':'V',
40   'w':'W',
41   'x':'X',
42   'y':'Y',
43   'z':'Z',
44   'A':'A',
45   'B':'B',
46   'C':'C',
47   'D':'D',
48   'E':'E',
49   'F':'F',
50   'G':'G',
51   'H':'H',
52   'I':'I',
53   'J':'J',
54   'K':'K',
55   'L':'L',
56   'M':'M',
57   'N':'N',
58   'O':'O',
59   'P':'P',
60   'Q':'Q',
61   'R':'R',
62   'S':'S',
63   'T':'T',
64   'U':'U',
65   'V':'V',
66   'W':'W',
67   'X':'X',
68   'Y':'Y',
69   'Z':'Z',
70   '1':'1',
71   '2':'2',
72   '3':'3',
73   '4':'4',
74   '5':'5',
75   '6':'6',
76   '7':'7',
77   '8':'8',
78   '9':'9',
79   '0':'0',
80   '-': '-',
81   '?': '?',
82   ':': ':',
83   '$': '$',
84   '!': '!',
85   '&': '&',
86   '#': '#',
87   '(': '(',
88   ')': '(',
89   '.': '.',
90   ',': ',',
91   '\'': '\'',
92   '/': '/',
93   '"': '"',
94   ' ': ' ' 
95 }
96
97 # then we map limted set to baudot
98 # see http://rabbit.eng.miami.edu/info/baudot.html
99 ascii_to_binstr = {
100   'A'  : '00011',
101   'B'  : '11001',
102   'C'  : '01110',
103   'D'  : '01001',
104   'E'  : '00001',
105   'F'  : '01101',
106   'G'  : '11010',
107   'H'  : '10100',
108   'I'  : '00110',
109   'J'  : '01011',
110   'K'  : '01111',
111   'L'  : '10010',
112   'M'  : '11100',
113   'N'  : '01100',
114   'O'  : '11000',
115   'P'  : '10110',
116   'Q'  : '10111',
117   'R'  : '01010',
118   'S'  : '00101',
119   'T'  : '10000',
120   'U'  : '00111',
121   'V'  : '11110',
122   'W'  : '10011',
123   'X'  : '11101',
124   'Y'  : '10101',
125   'Z'  : '10001',
126   '1'  : '10111',
127   '2'  : '10011',
128   '3'  : '00001',
129   '4'  : '01011',
130   '5'  : '10000',
131   '6'  : '10101',
132   '7'  : '00111',
133   '8'  : '00110',
134   '9'  : '11000',
135   '0'  : '10110',
136   '-'  : '00011',
137   '?'  : '11001',
138   ':'  : '01110',
139   '$'  : '01001',
140   '!'  : '01101',
141   '&'  : '11010',
142   '#'  : '10100',
143   '('  : '01111',
144   ')'  : '10010',
145   '.'  : '11100',
146   ','  : '01100',
147   '\'' : '01010',
148   '/'  : '11101',
149   '"'  : '11101',
150   ' '  : '00100'
151 }
152
153 needs_shift_up = (
154   '1',
155   '2',
156   '3',
157   '4',
158   '5',
159   '6',
160   '7',
161   '8',
162   '9',
163   '0',
164   '-',
165   '?',
166   ':',
167   '$',
168   '!',
169   '&',
170   '#',
171   '(',
172   ')',
173   '.',
174   ',',
175   '\'',
176   '/',
177   '"'
178 )
179
180 #def init(gpio_arg):
181 def init():
182   """
183   initialize teletype i/o
184   """
185   txbaudot("01000") # cr
186   time.sleep(1.0)
187   txbaudot("00010") # lf
188
189
190 def finish():
191   txbaudot("11111")
192   init()
193
194 # establish timer for TTY Motor
195
196 def motor_start(time_secs=0):
197   """
198   turn on motor
199   """
200   global MotorTimerCtr, MotorTimerVal
201
202   if (not MotorTimerCtr) :
203     gpio.output(PWR_RLY,gpio.LOW)
204     time.sleep(.25)
205   
206   if not time_secs:
207     MotorTimerCtr = MotorTimerVal
208   else:
209     MotorTimerCtr = time_secs
210
211 def motor_stop():
212   """
213   turn off motor, turn off data relay
214   """
215   global MotorTimerCtr
216   gpio.output(PWR_RLY,gpio.HIGH)
217   time.sleep(2.0)
218   gpio.output(DATA_RLY,gpio.HIGH)
219   MotorTimerCtr = 0
220
221 def test(s):
222   """
223   various tests
224   """
225   if (s == 'allpats'):
226     """
227     test mapping tables by attempt to print out all possible codes
228     """
229     for i in range(0,256):
230       if (ascii_to_baudot_char.has_key(chr(i))): # if first reduce mapping table has key
231         a = ascii_to_baudot_char[chr(i)]
232         if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
233           b = ascii_to_binstr[a]
234           if (b != '00000'):
235             txbaudot(b)
236    
237 def txbaudot(c):
238   """
239   transmit one character to the teletype
240   """
241
242   global pi
243   gpio=2
244
245   pi.wave_clear()
246   wf=[]
247   micros = gpio8period*1000
248
249   wf.append(pigpio.pulse(0, 1<<gpio, 20000))
250
251   for b in reversed(c):
252     if (int(b) == 1):
253       wf.append(pigpio.pulse(1<<gpio, 0, micros))
254     else:
255       wf.append(pigpio.pulse(0, 1<<gpio, micros))
256
257   wf.append(pigpio.pulse(1<<gpio, 0, micros))
258   pi.wave_add_generic(wf)
259
260   wid = pi.wave_create()
261
262   if wid >= 0:
263     pi.wave_send_once(wid)
264
265   while pi.wave_tx_busy():
266     time.sleep(0.2)
267
268   time.sleep(0.1)
269
270 def txbin(s):
271   txbaudot(s)
272
273 def tx_keycode(s):
274   k = int(s)
275   if (ascii_to_baudot_char.has_key(chr(k))):
276       a = ascii_to_baudot_char[chr(k)]
277       if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
278         b = ascii_to_binstr[a]
279         if (b != '00000'):
280           txbaudot(b)
281
282 shifted = False
283
284 def update_column_position():
285   """
286   keep track of column position so we can insert cr lf when necessary
287   """
288   global ColumnCurrentPosition, ColumnMax
289   ColumnCurrentPosition = ColumnCurrentPosition + 1
290   if ColumnCurrentPosition > ColumnMax:
291     #print "update_column_position(): col 0"
292     tx_ctl('cr')
293     tx_ctl('lf')
294     ColumnCurrentPosition = 0; 
295
296 def shift_up():
297   """
298   Shift up to figures
299   """
300   global shifted
301   if not shifted:
302     tx_ctl('figs')
303     shifted = True
304
305 def shift_down():
306   """
307   Shift down to letters
308   """
309   global shifted
310   if shifted:
311     tx_ctl('ltrs')
312     shifted = False
313
314
315 def tx_ascii_chr(c):
316   """
317   send an ascii character
318   """
319   if (ascii_to_baudot_char.has_key(c)):
320       a = ascii_to_baudot_char[c]
321       if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
322         b = ascii_to_binstr[a]
323         if (b != '00000'):
324           if (a in needs_shift_up):
325             shift_up()
326           else:
327             shift_down()
328           txbaudot(b)
329           update_column_position()
330
331 def tx(c):
332   """
333   send an ascii character
334   """
335   tx_keycode(c)
336
337 def tx_str(s):
338   """
339   transmit an ascii string
340   """
341   de_uried_str = urllib.unquote(s)
342   for i in range(len(de_uried_str)):
343     tx_ascii_chr(de_uried_str[i])
344
345
346 def tx_ctl(c):
347   """
348   transmit a control code 'lf' = line feed, 'cr' = carriage return, etc.
349   """
350   global ColumnCurrentPosition
351   if (c == 'cr'):
352     txbaudot('01000')
353     ColumnCurrentPosition = 0
354   elif (c == 'lf'):
355     txbaudot('00010')
356   elif (c == 'figs'):
357     txbaudot('11011')
358   elif (c == 'ltrs'):
359     txbaudot('11111')
360   elif (c == 'bell'):
361     txbaudot('11011') # shift up
362     txbaudot('00101')
363     txbaudot('11111') # shift down
364     txbaudot('01000') # cr
365     txbaudot('00100') # space
366     txbaudot('00100')
367     txbaudot('00100')
368     txbaudot('00100')
369     txbaudot('00100')
370     txbaudot('00100')
371     txbaudot('00100')
372     txbaudot('00100')
373     txbaudot('01000') # cr
374     ColumnCurrentPosition = 0
375   elif (c == 'null'):
376     txbaudot('00000')
377   elif (c == 'space'):
378     txbaudot('00100')
379     update_column_position()
380
381 init()
382 tx_str("the quick brown fox jumps over the lazy dog!")