initial commit
[teletype.git] / python / teletype-weather.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 def motor_start(time_secs=0):
195   """
196   turn on motor
197   """
198   global MotorTimerCtr, MotorTimerVal
199
200   if (not MotorTimerCtr) :
201     gpio.output(PWR_RLY,gpio.LOW)
202     time.sleep(.25)
203   
204   if not time_secs:
205     MotorTimerCtr = MotorTimerVal
206   else:
207     MotorTimerCtr = time_secs
208
209 def motor_stop():
210   """
211   turn off motor, turn off data relay
212   """
213   global MotorTimerCtr
214   gpio.output(PWR_RLY,gpio.HIGH)
215   time.sleep(2.0)
216   gpio.output(DATA_RLY,gpio.HIGH)
217   MotorTimerCtr = 0
218
219 def test(s):
220   """
221   various tests
222   """
223   if (s == 'allpats'):
224     """
225     test mapping tables by attempt to print out all possible codes
226     """
227     for i in range(0,256):
228       if (ascii_to_baudot_char.has_key(chr(i))): # if first reduce mapping table has key
229         a = ascii_to_baudot_char[chr(i)]
230         if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
231           b = ascii_to_binstr[a]
232           if (b != '00000'):
233             txbaudot(b)
234    
235 def txbaudot(c):
236   """
237   transmit one character to the teletype
238   """
239
240   global pi
241   gpio=2
242
243   pi.wave_clear()
244   wf=[]
245   micros = gpio8period*1000
246
247   wf.append(pigpio.pulse(0, 1<<gpio, 20000))
248
249   for b in reversed(c):
250     if (int(b) == 1):
251       wf.append(pigpio.pulse(1<<gpio, 0, micros))
252     else:
253       wf.append(pigpio.pulse(0, 1<<gpio, micros))
254
255   wf.append(pigpio.pulse(1<<gpio, 0, micros))
256   pi.wave_add_generic(wf)
257
258   wid = pi.wave_create()
259
260   if wid >= 0:
261     pi.wave_send_once(wid)
262
263   while pi.wave_tx_busy():
264     time.sleep(0.2)
265
266   time.sleep(0.1)
267
268 def txbin(s):
269   txbaudot(s)
270
271 def tx_keycode(s):
272   k = int(s)
273   if (ascii_to_baudot_char.has_key(chr(k))):
274       a = ascii_to_baudot_char[chr(k)]
275       if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
276         b = ascii_to_binstr[a]
277         if (b != '00000'):
278           txbaudot(b)
279
280 shifted = False
281
282 def update_column_position():
283   """
284   keep track of column position so we can insert cr lf when necessary
285   """
286   global ColumnCurrentPosition, ColumnMax
287   ColumnCurrentPosition = ColumnCurrentPosition + 1
288   if ColumnCurrentPosition > ColumnMax:
289     tx_ctl('cr')
290     tx_ctl('lf')
291     ColumnCurrentPosition = 0; 
292
293 def shift_up():
294   """
295   Shift up to figures
296   """
297   global shifted
298   if not shifted:
299     tx_ctl('figs')
300     shifted = True
301
302 def shift_down():
303   """
304   Shift down to letters
305   """
306   global shifted
307   if shifted:
308     tx_ctl('ltrs')
309     shifted = False
310
311
312 def tx_ascii_chr(c):
313   """
314   send an ascii character
315   """
316   if (ascii_to_baudot_char.has_key(c)):
317       a = ascii_to_baudot_char[c]
318       if (ascii_to_binstr.has_key(a)): # and 2nd reduce mapping table has key
319         b = ascii_to_binstr[a]
320         if (b != '00000'):
321           if (a in needs_shift_up):
322             shift_up()
323           else:
324             shift_down()
325           txbaudot(b)
326           update_column_position()
327
328
329
330 def tx(c):
331   """
332   send an ascii character
333   """
334   tx_keycode(c)
335
336 def tx_str(s):
337   """
338   transmit an ascii string
339   """
340   de_uried_str = urllib.unquote(s)
341   for i in range(len(de_uried_str)):
342     tx_ascii_chr(de_uried_str[i])
343
344
345 def tx_ctl(c):
346   """
347   transmit a control code 'lf' = line feed, 'cr' = carriage return, etc.
348   """
349   global ColumnCurrentPosition
350   if (c == 'cr'):
351     txbaudot('01000')
352     ColumnCurrentPosition = 0
353   elif (c == 'lf'):
354     txbaudot('00010')
355   elif (c == 'figs'):
356     txbaudot('11011')
357   elif (c == 'ltrs'):
358     txbaudot('11111')
359   elif (c == 'bell'):
360     txbaudot('11011') # shift up
361     txbaudot('00101')
362     txbaudot('11111') # shift down
363     txbaudot('01000') # cr
364     txbaudot('00100') # space
365     txbaudot('00100')
366     txbaudot('00100')
367     txbaudot('00100')
368     txbaudot('00100')
369     txbaudot('00100')
370     txbaudot('00100')
371     txbaudot('00100')
372     txbaudot('01000') # cr
373     ColumnCurrentPosition = 0
374   elif (c == 'null'):
375     txbaudot('00000')
376   elif (c == 'space'):
377     txbaudot('00100')
378     update_column_position()
379
380 init()
381 file = open("paz001.txt", "r")
382
383 lines = file.readlines()
384 for line in lines:
385   tx_str(line.strip())
386