-
Notifications
You must be signed in to change notification settings - Fork 3
/
h19term.py
executable file
·2536 lines (2212 loc) · 89.9 KB
/
h19term.py
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python3
# coding=utf-8
#
#-------------------------------------------------------------------------------
# H19term - Heathkit H19 Terminal emulator
#
# Copyright (c) 2014 George Farris - [email protected]
#
# 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 3 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#-------------------------------------------------------------------------------
# Release history
# Dec 27, 2014 - V1.0 Initial release
# Dec 28, 2014 - V1.1 Fixed serial port open error not reporting
# Jan 01, 2015 - V1.2 Add popup on CTRL-A, Now CTRL-A x to exit
# Add serial port logging - CTRL-A l to toggle
# Add popup window with help and ascii table
# Add introduction screen
# Changed name to H19term
# Jan 04, 2015 - V1.3 Fix clear to end of line and erase line bug
# Fix reset of status line display
# (Ywing2 works now) Add discard at end of line logic to addchar
# Add cursor on and off to mode setting
# Jan 15, 2015 - V1.4 Add loop timer to stop 100% CPU (who needs threads)
# Add F12 offline and F11 ERASE key function
# Add Auto time and date when booting HDOS or CP/M
# Add CTRL keys for DEL and BREAK key
# Pass TAB char through
# Add preload font to setup.
# Speed up serial I/O for Raspbery Pi
# Jan 20, 2015 - V1.5 Add configuration file (.h19termrc) support for settings
# Add file prefix support for installing in any directory
# Fix erase to beginning of line
# Fix reverse linefeed
# Fix backspace when not expecting BS echo
# Tested PIE editor which now works.
# Added CTRL-A CTRL-A to send CTRL-A through to application.
# Jan 29, 2015 V1.6 Fix get config file, was using get instead of getboolean
# Change h19term to 27x80 instead of 28x80.
# Add probe and selection of serial port on first run.
# Added 10x20, 12x24 and 14x28 font.
# Fix linefeed indexing - it fixed HDOS MAPLE.
# Added configuration of serial port on first run.
# Added timer to keyboard repeat to stop over runs
# Feb 07, 2015 V1.7 Added colour support
# Feb 24, 2015 V1.8 Fix keyboard repeat code to not miss char typed
# Add change baud rate functions - with Super-19 baud rates
# 19200 and 38400 added.
# Jan 22, 2016 V1.9 Fix bug in heath_escape_seq - thanks Jason Kersten
# Nov 28, 2019 V1.10 Add ESC-Z function, identify as VT52
# Jan 03, 2020 V1.11 Fix ANSI shifted keypad mode - fixes Sasix
# Jan 16, 2020 V1.12 Fix backspace, it's a move not a delete
# Also better format for serial port logging, each character
# output is on a new line with < > around it.
# Apr 02, 2020 V1.13 Add setting of baud rate
# Add striping of high bit on incoming characters.
# Apr 03, 2020 V1.14 Add changing the port on the fly.
# Apr 11, 2020 V2.0 Python 3 version
# May 02, 2020 V2.1 Added Heathkit-H19-bitmap font for desktop use.
# Many fixes.
# May 16, 2020 V2.2 Added Xmodem support.
# May 30, 2020 V2.3 Fix window sizes on Raspberry pi/ Linux console
# and only use 16x32 font.
# Jun 01, 2020 V2.4 Added Xmodem transfer.
# Aug 22, 2020 V2.5 Add toggling window box drawing characters for Linux copy and paste.
# Add keypad associations to help screen.
import os
import re
import sys
import time
import curses
import locale
import serial
import string
import datetime
import configparser
from pysinewave import SineWave
# This must be set to output unicode characters
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()
# ************* MODIFIABLE SETTINGS ************************************
# Do not modify these settings here any more, edit the .h19termrc file instead,
# you will find it in your home directory.
SERIAL_PORT = '/dev/ttyUSB0'
XMODEM_PORT = '/dev/ttyUSB1'
BAUD_RATE = 9600
XMODEM_RATE = 9600
# Set the autorun mode for xmodem transfers. Auto run can only be used with the
# RX.COM companion application that comes with H19term. The "USER" mode can be
# used with any xmodem program.
#
# Modes are:
# USER - User selectable via a popup window, this is the default
# AUTO - H19term will automatically run "RX -n <filename> on the CP/M side.
# ANY - Choose this for using ZMP or Maple etc.
AUTORUN_MODE = "USER"
# This is the root directory for h19term except for .h19termrc which is in your
# home directory. All the help files etc live here.
INSTALL_PATH = '/usr/local/share/h19term/'
# This is a storage area for the remembered path when choosing a file. It
# starts as your home dir
RUN_PATH = os.environ['HOME']
PRELOAD_FONT = True
FONT = 'H19term16x32.psfu.gz' # This font works on Raspberry Pi
BEEP = 'beep1.wav'
AUTO_CPM_DATE = False
AUTO_HDOS_DATE = False
CPM_DATE_FORMAT = "Enter today's date (MM/DD/YY): "
CPM_TIME_FORMAT = "Enter the time (HH:MM:SS): "
HDOS_DATE_FORMAT = r"^Date.(\d\d-\w\w\w-\d\d)?." # Date (01-Jan-77)?
# Haven't tested this as I don't have the date patches in for HDOS
# Colours defined when running on Raspberry Pi or Linux Console. This does not
# set the colours when running under X11.
LC_WHITE = 'FFFFFF'
LC_GREEN = '00AA00'
LC_YELLOW = 'FFA400'
LC_BLUE = '0000AA'
LC_CYAN = '00AAAA'
LC_MAGENTA = 'AA00AA'
LC_RED = 'AA0000'
BAUD_RATES=[1200,2400,4800,9600,19200,38400,57600]
# Set default colour
# 0 - white, 1 - green, 2 - yellow, 3 - blue, 4 - cyan, 5 - magenta, 6 - red
DEFAULT_COLOUR = 0
# ************ END OF USER MODIFIABLE SETTINGS *****************************
VERSION = 'V2.5 - Python 3'
VERSION_DATE = 'Aug 22, 2020'
CONFIG_FILE = os.path.join(os.environ['HOME'], '.h19termrc')
LOG_FILE = os.path.join(os.environ['HOME'], 'h19term.log')
SIO_WAIT = None
SIO_NO_WAIT = 0
SHIFT_FKEY = curses.KEY_F9
ENTER_FKEY = curses.KEY_F10
ERASE_FKEY = curses.KEY_F11
OFFLINE_FKEY = curses.KEY_F12
CURSOR_INVISIBLE = 0 # no cursor
CURSOR_NORMAL = 1 # Underline cursor
CURSOR_BLOCK = 2 # Block cursor
NORM = 0 # Index for numkeys array
H_ALT = 1
A_ALT = 2
SHIFT = 3
ASHIFT = 4
LF = '\x0a'
CR = '\x0d'
BS = '\x08'
DEL = '\x7f'
BEL = '\x07'
TAB = '\x09'
ESC = '\x1b'
NUL = '\x00'
KEY = 1 # Used in backspace to tell if key or incoming serial char
BLANK_LINE = " "
NOCHAR = -1
CURSOR = [0,0]
SAVED_CURSOR = [0,0]
KEY_REPEAT_RATE = 0.09 #10ish CPS, more than this and PIE editor has char overflows
class H19Keys:
fnkeys = {
# Key Heath Ansi
#-------------------------------------------------
curses.KEY_F1: 'S', # F1 ESC S ESC O S
curses.KEY_F2: 'T', # F2 ESC T ESC O T
curses.KEY_F3: 'U', # F3 ESC U ESC O U
curses.KEY_F4: 'V', # F4 ESC V ESC O V
curses.KEY_F5: 'W', # F5 ESC W ESC O W
curses.KEY_F6: 'P', # BLUE ESC P ESC O P
curses.KEY_F7: 'Q', # RED ESC Q ESC O Q
curses.KEY_F8: 'R', # WHITE ESC R ESC O R
curses.KEY_F9: '*', # Shift key to change keypad
curses.KEY_F11: '*', # ERASE key
curses.KEY_F12: '*', # Terminal OFFLINE key
}
numkeys = {
# Keypad group: Note F12 is the ENTER key because we can't distinguish
# between the two ENTER keys, they both return ctrl-j
# UNSHIFTED HEATH ANSI SHIFTED ANSI
# UNSHIFTED UNSHIFTED SHIFTED
# ALTERNATE ALTERNATE
#----------------------------------------------------------------
curses.KEY_IC: ['0', '\x1b?p', '\x1bOp', '0', '0'],
curses.KEY_END: ['1', '\x1b?q', '\x1bOq', '\x1bL', '\x1b[L'],
curses.KEY_DOWN: ['2', '\x1b?r', '\x1bOr', '\x1bB', '\x1b[B'],
curses.KEY_NPAGE: ['3', '\x1b?s', '\x1bOs', '\x1bM', '\x1b[M'],
curses.KEY_LEFT: ['4', '\x1b?t', '\x1bOt', '\x1bD', '\x1b[D'],
curses.KEY_B2: ['5', '\x1b?u', '\x1bOu', '\x1bH', '\x1b[H'],
curses.KEY_RIGHT: ['6', '\x1b?v', '\x1bOv', '\x1bC', '\x1b[C'],
curses.KEY_HOME: ['7', '\x1b?w', '\x1bOw', '\x1bO'],
curses.KEY_UP: ['8', '\x1b?x', '\x1bOx', '\x1bA', '\x1b[A'],
curses.KEY_PPAGE: ['9', '\x1b?y', '\x1bOy', '\x1bN', '\x1b[P'],
curses.KEY_DC: ['.', '\x1b?n', '\x1bOn', '.', '.'],
curses.KEY_F10: ['\x13', '\x1b?M', '\x1bOM', '\x13', '\x13'],
}
class H19Screen:
def __init__(self, scn, stat):
self.screen = scn
self.status = stat
h19_graphics = [
'\u25CF', # ^ '⚫' BLACK CIRCLE.
'\u25E5', # - '◥' UPPER RIGHT TRIANGLE.
'\u2503', # ` '│' LIGHT VERTICLE LINE.
'\u2501', # a '─' LIGHT HORIZONTAL LINE.
'\u254B', # b '┼' LIGHT PLUS.
'\u2513', # c '┐' LIGHT UPPER RIGHT CORNER.
'\u251B', # d '┘' LIGHT LOWER RIGHT CORNER.
'\u2517', # e '└' LIGHT LOWER LEFT CORNER.
'\u250F', # f '┌' LIGHT UPPER LEFT CORNER.
'\u00B1', # g '±' LIGHT PLUS MINUS.
'\u279C', # h '→' RIGHT ARROW.
'\u2591', # i '▒' MEDIUM SHADE.
'\u259A', # j '▚' QUADRANT UPPER LEFT LOWER R.
'\u2B07', # k '↓' DOWN ARROW.
'\u2597', # l '▗' QUADRANT LOWER RIGHT.
'\u2596', # m '▖' QUADRANT LOWER LEFT.
'\u2598', # n '▘' QUADRANT UPPER LEFT.
'\u259D', # o '▝' QUADRANT UPPER RIGHT.
'\u2580', # p '▀' UPPER HALF BLOCK.
'\u2590', # q '▐' RIGHT HALF BLOCK.
'\u25E4', # r '◤' UPPER LEFT TRIANGLE.
'\u2533', # s '┬' LIGHT DOWN HORIZONTAL.
'\u252B', # t '┤' LIGHT VERTICAL LEFT.
'\u253B', # u '┴' LIGHT UP HORIZONTAL.
'\u2523', # v '├' LIGHT VERTICAL RIGHT.
'\u2573', # w '╳' LIGHT CROSS.
'\u2571', # x '╱' LIGHT RIGHT.
'\u2572', # y '╲' LIGHT LEFT.
'\u2594', # z '▔' UPPER HORIZONTAL LINE.
'\u2581', # { '▁' LOWER HORIZONTAL LINE.
'\u258F', # | '▏' LEFT VERTICAL LINE.
'\u2595', # } '▕' RIGHT VERTICAL LINE.
'\u00B6', # ~ '¶' PILCROW SIGN
]
def update_cursor(self):
y,x = self.screen.getyx()
CURSOR = [y,x]
# Control keys
#
def bell(self):
if time.time() - self.bell_start_time > 1.0 or self.bell_start_time == 0.0:
try:
self.sinewave.play()
self.bell_start_time = time.time()
time.sleep(0.16)
self.sinewave.stop()
except:
pass # sometimes this fails with ugly python throw up
def backspace(self, sio, ch): # H8 will return ^H <SPACE> ^H when we
y,x = self.screen.getyx() # send it a ^H key
if ch == KEY:
if x > 0:
self.sio_write(sio, chr(8))
c = self.sio_read(sio, TIMEOUT=0.01) # ^H or timeout
if c == chr(8):
c = self.sio_read(sio, TIMEOUT=0.01) # <SPACE>
if c == ' ':
c = self.sio_read(sio, TIMEOUT=0.01) # ^H
self.screen.delch(y, x-1)
else:
self.screen.move(y,x-1) # sometimes we only get 1 ^H
else:
return(c)
else:
if x == 0:
return('')
#self.screen.delch(y, x-1)
self.screen.move(y, x-1)
def tab(self): # so far don't need this
pass
def linefeed(self):
y,x = self.screen.getyx()
if y == 24: # if we are in the 25th line don't scroll
return
elif y >= 23:
self.screen.scrollok(True)
self.screen.scroll(1)
y = 23
self.screen.move(y,x)
else:
self.screen.move(y+1,x)
if self.autoCarriageReturnMode:
self.screen.move(y,0)
def carriage_return(self):
y,x = self.screen.getyx()
if y == 24: # 25th line
self.screen.move(24,0)
return
#self.log("[Y->%s, X->%s]" % (y,x))
self.screen.move(y, 0)
if self.autoLinefeedMode:
#self.log("[Y->%s, X->%s]" % (y,x))
self.screen.move(y+1,0)
def escape(self):
pass
def delete(self, sio):
self.sio_write(sio, chr(177))
def rubout(self):
#self.screen.delch()
#self.screen.addch(' ')
pass
def scroll_key(self):
pass
def break_key(self, sio):
sio.sendBreak()
# Cursor functions
def cursor_home(self):
y, x = self.screen.getyx()
if x == 24:
self.screen.move(0,x)
else:
self.screen.move(0,0)
def cursor_forward(self, n=1):
y,x = self.screen.getyx()
if n == 0:
n = 1
if x == 79:
return
self.screen.move(y, x+n)
def cursor_backward(self, n=1):
y,x = self.screen.getyx()
if n == 0:
n = 1
if x == 0:
return
self.screen.move(y, x-n)
def cursor_down(self, n=1):
y,x = self.screen.getyx()
if n == 0:
n = 1
if y == 23:
return
self.screen.move(y+n, x)
def cursor_up(self, n=1):
y,x = self.screen.getyx()
if n == 0:
n = 1
if y == 0:
return
self.screen.move(y-n, x)
def reverse_linefeed(self):
self.screen.scrollok(True)
y,x = self.screen.getyx()
if y == 0:
self.screen.insertln()
else:
self.screen.move(y-1,x)
#self.screen.scrollok(False)
def cursor_position_report(self, sio):
if self.ansiMode:
y,x = self.screen.getyx()
sio.write(ESC.encode())
sio.write(b'[')
sio.write((chr(x + 32)).encode())
sio.write(b';')
sio.write((chr(y + 32)).encode())
sio.write(b'R')
else:
y,x = self.screen.getyx()
sio.write(ESC.encode())
sio.write(b'Y')
sio.write((chr(x + 32)).encode())
sio.write((chr(y + 32)).encode())
def save_cursor_position(self):
y,x = self.screen.getyx()
SAVED_CURSOR[0] = y
SAVED_CURSOR[1] = x
def goto_saved_cursor_position(self):
self.screen.move(SAVED_CURSOR[0],SAVED_CURSOR[1])
self.screen.refresh()
def set_cursor_position(self, line, col):
if line == 24: # heath line 25
self.screen.scrollok(False)
else:
self.screen.scrollok(True)
if line > 24:
return
if col > 79:
self.screen.move(line, 79)
return
self.screen.move(line, col)
def can_perform_as_vt52(self,sio):
sio.write(ESC)
sio.write('/')
sio.write('K')
# Erasing and editing
def clear_display(self, reset=False):
if reset: # reset clears both 24x80 and 25th line
self.screen.erase()
# if self.ansiMode and self.enable25thLine:
# self.screen.erase()
y, x = self.screen.getyx()
if y < 24:
for i in range(24):
self.screen.move(i, 0)
self.screen.clrtoeol()
self.screen.move(0, 0)
if y == 24:
self.screen.move(24, 0)
self.screen.clrtoeol()
#self.screen.move(24, 0) not sure we need this
self.screen.refresh()
def erase_to_beginning_of_display(self):
y,x = self.screen.getyx()
self.screen.move(y,0) # first clear beginning of line to cursor
self.screen.addnstr(BLANK_LINE, x + 1)
self.screen.move(0,0) # now home and clear remaining lines
if y > 0:
lines = y
for i in range(lines):
self.screen.move(i,0)
self.screen.clrtoeol()
self.screen.move(0,0) # set cursor at beginning of display or maybe
def erase_to_end_of_page(self):
y,x = self.screen.getyx()
self.screen.clrtobot()
def erase_line(self):
y,x = self.screen.getyx()
self.screen.move(y,0)
self.screen.clrtoeol()
self.screen.move(y,0)
def erase_beginning_of_line(self):
y,x = self.screen.getyx()
self.screen.move(y,0)
self.screen.addnstr(BLANK_LINE,x)
def erase_to_end_of_line(self):
y,x = self.screen.getyx()
self.screen.clrtoeol()
if x > 0:
self.screen.move(y,x-1)
# self.screen.refresh()
def insert_line(self):
self.screen.insertln()
def delete_line(self):
self.screen.deleteln()
def delete_character(self):
y,x = self.screen.getyx()
self.screen.delch()
def enter_insert_mode(self):
self.insertMode = True
def exit_insert_mode(self):
self.insertMode = False
class H19Term(H19Keys, H19Screen):
""" H19 terminal.
Supports I/O using a serial port.
"""
def __init__(self):
self.screen = None
self.status = None
self.logio = False
self.showbox = True
self.baudrate = [110, 150, 300, 600, 1200, 1800, 2000, 2400, 3600, 4800, 7200, 9600, 19200, 38400]
H19Screen.__init__(self, self.screen, self.status)
def get_h19config(self):
global SERIAL_PORT, XMODEM_PORT, BAUD_RATE, PRELOAD_FONT, FONT, BEEP
global AUTO_CPM_DATE, CPM_DATE_FORMAT, CPM_TIME_FORMAT, XMODEM_RATE
global AUTO_HDOS_DATE, HDOS_DATE_FORMAT, INSTALL_PATH, AUTORUN_MODE
global KEY_REPEAT_RATE, DEFAULT_COLOUR, RUN_PATH
global LC_WHITE, LC_GREEN, LC_YELLOW
global LC_BLUE, LC_CYAN, LC_MAGENTA, LC_RED
Config = configparser.ConfigParser(allow_no_value = True)
updateFile = True
# If it exists get the settings otherwise write them to a file
if os.path.isfile(CONFIG_FILE):
updateFile = False
try:
Config.read(CONFIG_FILE)
if Config.has_option('General', 'RunPath'):
RUN_PATH = Config.get('General', 'RunPath')
else: updateFile = True
if Config.has_option('General','KeyRepeatRate'):
KEY_REPEAT_RATE = Config.getfloat('General','KeyRepeatRate')
else: updateFile = True
if Config.has_option('General','SoundFile'):
BEEP = Config.get('General','SoundFile')
else: updateFile = True
if Config.has_option('SerialComms', 'port'):
SERIAL_PORT = Config.get('SerialComms', 'port')
else: updateFile = True
if Config.has_option('SerialComms', 'baudRate'):
BAUD_RATE = Config.getint('SerialComms', 'baudRate')
else: updateFile = True
if Config.has_option('SerialComms', 'xmodemport'):
XMODEM_PORT = Config.get('SerialComms', 'xmodemport')
else: updateFile = True
if Config.has_option('SerialComms', 'xmodembaudrate'):
XMODEM_RATE = Config.getint('SerialComms', 'xmodembaudrate')
else: updateFile = True
if Config.has_option('AutoRun', 'autorunmode'):
AUTORUN_MODE = Config.get('AutoRun', 'autorunmode')
else: updateFile = True
if Config.has_option('Fonts','Preload'):
PRELOAD_FONT = Config.getboolean('Fonts','Preload')
else: updateFile = True
if Config.has_option('Fonts','Font'):
FONT = Config.get('Fonts','Font')
else: updateFile = True
if Config.has_option('Colours','DefaultColour'):
DEFAULT_COLOUR = Config.getint('Colours','DefaultColour')
else: updateFile = True
if Config.has_option('Colours','White'):
LC_WHITE = Config.get('Colours','White')
else: updateFile = True
if Config.has_option('Colours','Green'):
LC_GREEN = Config.get('Colours','Green')
else: updateFile = True
if Config.has_option('Colours','Yellow'):
LC_YELLOW = Config.get('Colours','Yellow')
else: updateFile = True
if Config.has_option('Colours','Blue'):
LC_BLUE = Config.get('Colours','Blue')
else: updateFile = True
if Config.has_option('Colours','Cyan'):
LC_CYAN = Config.get('Colours','Cyan')
else: updateFile = True
if Config.has_option('Colours','Magenta'):
LC_MAGENTA = Config.get('Colours','Magenta')
else: updateFile = True
if Config.has_option('Colours','Red'):
LC_RED = Config.get('Colours','Red')
else: updateFile = True
if Config.has_option('Date','AutoCpmDate'):
AUTO_CPM_DATE = Config.getboolean('Date','AutoCpmDate')
else: updateFile = True
if Config.has_option('Date','CpmDate'):
CPM_DATE_FORMAT = Config.get('Date','CpmDate')
else: updateFile = True
if Config.has_option('Date','CpmTime'):
CPM_TIME_FORMAT = Config.get('Date','CpmTime')
else: updateFile = True
if Config.has_option('Date','AutoHdosDate'):
AUTO_HDOS_DATE = Config.getboolean('Date','AutoHdosDate')
else: updateFile = True
if Config.has_option('Date','HdosDate'):
HDOS_DATE_FORMAT = Config.get('Date','HdosDate')
else: updateFile = True
except:
print("Problem reading configuration file .h19termrc, skipping...")
if updateFile:
print("\nConfiguration file ~/.h19termrc not found:")
print(" A new one will be created after serial port selection...")
print(" You may edit this file to set your defaults...\n")
print("\nChecking serial ports...")
enterOne = -1
availablePorts = []
try:
from serial.tools.list_ports import comports
print(" These are a list of ports found on this system, there may be others")
print(" but they didn't show up in the probe...\n")
i = 0
for port, desc, hwid in sorted(comports()):
if hwid != 'n/a':
i += 1
availablePorts.append(port)
print('%s. %-15s %s' % (i,port, desc))
print('%s. %-15s %s' % (i+1,'Enter one', ''))
enterOne = i+1
except:
print("Your python serial package doesn't have the serial port probe")
print("We'll have to work around it and just offer a couple of standard")
print("ports without knowing if they actually exist...\n")
print('%s. %-15s %s' % ('1','/dev/ttyS0', 'Builtin port '))
print('%s. %-15s %s' % ('2','/dev/ttyUSB0', 'USB to RS232 cable'))
print('%s. %-15s %s' % ('3','Enter one', ''))
enetrOne = 3
availablePorts.append('/dev/ttyS0')
availablePorts.append('/dev/ttyUSB0')
print("\n Please select one of these, or press <Enter> if you would")
print(" like to enter your own by editing your ~/.h19termrc configuration file.")
print("\n Python doesn't always find all available ports on your computer.")
print(" A default baud rate of 9600 will be choosen.\n")
portno = input("Enter number: ")
if len(portno) > 0:
if int(portno) == enterOne:
while True:
print('\nEnter a port in the form of /dev/<devicename> or <enter> to quit')
s = input('Enter port: ')
if s == '':
print("Using default port -> %s" % SERIAL_PORT)
break
if not os.path.exists(s):
print('That port device doesn\'t exist...')
else:
SERIAL_PORT = s
print("Setting port to -> %s" % SERIAL_PORT)
break
else:
SERIAL_PORT = availablePorts[int(portno)-1]
print("Setting port to -> %s" % SERIAL_PORT)
else:
print("Using default port -> %s" % SERIAL_PORT)
print("Writing new ~/.h19termrc file...")
self.write_h19config(True) # True if it's a new file not a rewrite
def write_h19config(self, new=False):
cfgfile = ''
Config = configparser.ConfigParser(allow_no_value = True)
try:
cfgfile = open(CONFIG_FILE, 'w')
except:
print("Problem opening configuration file .h19term, skipping...")
# add the settings to the structure of the file, and lets write it out...
Config.add_section('General')
Config.add_section('SerialComms')
Config.add_section('AutoRun')
Config.add_section('Fonts')
Config.add_section('Colours')
Config.add_section('Date')
Config.set('General','SoundFile', BEEP)
Config.set('General','RunPath', str(RUN_PATH))
Config.set('General','# Use caution when increasing repeat rate to avoid overruns')
Config.set('General','KeyRepeatRate', str(KEY_REPEAT_RATE))
Config.set('SerialComms','Port', SERIAL_PORT)
Config.set('SerialComms','BaudRate', str(BAUD_RATE))
Config.set('SerialComms','XmodemPort', XMODEM_PORT)
Config.set('SerialComms','XmodemBaudRate', str(XMODEM_RATE))
Config.set('AutoRun', 'AutoRunMode', str(AUTORUN_MODE))
Config.set('Fonts','Preload',str(PRELOAD_FONT))
Config.set('Fonts','Font',FONT)
Config.set('Colours','# Custom colours used only for Linux console, see manual')
Config.set('Colours', 'White', LC_WHITE)
Config.set('Colours', 'Green', LC_GREEN)
Config.set('Colours', 'Yellow', LC_YELLOW)
Config.set('Colours', 'Blue', LC_BLUE)
Config.set('Colours', 'Cyan', LC_CYAN)
Config.set('Colours', 'Magenta', LC_MAGENTA)
Config.set('Colours', 'Red', LC_RED)
Config.set('Colours','# Default colour of H19term on console or Xterm, see manual')
Config.set('Colours','DefaultColour', str(DEFAULT_COLOUR))
Config.set('Date','AutoCpmDate',str(AUTO_CPM_DATE))
Config.set('Date','CpmDate',CPM_DATE_FORMAT)
Config.set('Date','CpmTime',CPM_TIME_FORMAT)
Config.set('Date','AutoHdosDate',str(AUTO_HDOS_DATE))
Config.set('Date','HdosDate',HDOS_DATE_FORMAT)
Config.write(cfgfile)
cfgfile.close()
if new:
sys.exit()
def setup_screen(self):
self.cur = curses.initscr() # Initialize curses.
curses.start_color()
if curses.termname() == b'linux':
if PRELOAD_FONT:
os.system("setfont %s" % os.path.join(INSTALL_PATH, FONT))
# setup colours for console only, X11 colours are set in the
# terminal application such as gnome-terminal
#
# a hex colour number such as FFA400 is converted as follows
# curses uses 0-1000 so hex 0xAA = 170 dec which then converts to
# roughly to int(170*3.92) = 666
# My amber is FFA400
r = int(int(LC_WHITE[0:2],16) * 3.92)
g = int(int(LC_WHITE[2:4],16) * 3.92)
b = int(int(LC_WHITE[4:6],16) * 3.92)
curses.init_color(curses.COLOR_WHITE, r,g,b)
r = int(int(LC_GREEN[0:2],16) * 3.92)
g = int(int(LC_GREEN[2:4],16) * 3.92)
b = int(int(LC_GREEN[4:6],16) * 3.92)
curses.init_color(curses.COLOR_GREEN, r,g,b)
r = int(int(LC_YELLOW[0:2],16) * 3.92)
g = int(int(LC_YELLOW[2:4],16) * 3.92)
b = int(int(LC_YELLOW[4:6],16) * 3.92)
curses.init_color(curses.COLOR_YELLOW,r,g,b)
r = int(int(LC_BLUE[0:2],16) * 3.92)
g = int(int(LC_BLUE[2:4],16) * 3.92)
b = int(int(LC_BLUE[4:6],16) * 3.92)
curses.init_color(curses.COLOR_BLUE, r,g,b)
r = int(int(LC_CYAN[0:2],16) * 3.92)
g = int(int(LC_CYAN[2:4],16) * 3.92)
b = int(int(LC_CYAN[4:6],16) * 3.92)
curses.init_color(curses.COLOR_CYAN, r,g,b)
r = int(int(LC_MAGENTA[0:2],16) * 3.92)
g = int(int(LC_MAGENTA[2:4],16) * 3.92)
b = int(int(LC_MAGENTA[4:6],16) * 3.92)
curses.init_color(curses.COLOR_MAGENTA,r,g,b)
r = int(int(LC_RED[0:2],16) * 3.92)
g = int(int(LC_RED[2:4],16) * 3.92)
b = int(int(LC_RED[4:6],16) * 3.92)
curses.init_color(curses.COLOR_RED,r,g,b)
self.X0 = 0
self.Y0 = 0
else:
self.X0 = 1
self.Y0 = 1
self.cur.box()
self.cur.refresh()
y, x = self.cur.getmaxyx()
if y < 31 or x < 82:
curses.endwin()
print("\nYour screen is to small to run h19term, it must be")
print("at least 31 lines by 82 columns...\n")
print("Consider changing your terminal profile so you do not have")
print("to change this all the time...\n")
sys.exit(1)
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
#curses.resize_term(31,82)
curses.cbreak()
curses.raw()
curses.noecho()
curses.nonl()
self.cur.refresh()
self.screen = curses.newwin(25,80,self.X0, self.Y0)
self.status = curses.newwin(4,80,26,self.Y0)
self.screen.attrset(curses.color_pair(1))
self.status.attrset(curses.color_pair(1))
self.set_colour(DEFAULT_COLOUR) # set our default colour
self.screen.refresh()
self.screen.nodelay(1)
self.status.nodelay(1)
self.screen.keypad(1)
self.status.keypad(1)
self.screen.scrollok(True)
self.screen.idlok(1)
self.screen.setscrreg(0,23)
self.status.scrollok(False)
return self.screen, self.status
def reset(self):
self.enter_heath_mode()
self.enable25thLine = False
self.noKeyClick = False
self.insertMode = False
self.holdScreenMode = False
self.reverseVideoMode = False
self.graphicsMode = False
self.keypadShiftedMode = False
self.keypadAlternateMode = False
self.status.addstr(1, 40, " ")
self.status.addstr(1, 41, "-")
self.status.refresh()
self.autoLinefeedMode = False
self.visibleCursor = True
self.autoCarriageReturnMode = False
self.autoLineFeedMode = False
self.keyboardDisabled = False
self.wrapAtEndOfLine = False
self.linesSinceBoot = 0 # used to auto set date
self.clear_display(True)
self.cursor_home()
def terminate(self):
curses.endwin() # End screen (ready to draw new one, but instead we exit)
def open_port(self):
try:
sp = serial.Serial(SERIAL_PORT, BAUD_RATE, xonxoff=True, timeout=0)
return sp
except:
print("\nATTENTION!! - Could not open serial port...\n\n")
print("Please edit the ~/.h19termrc configuration file in your home directory")
print("and set your serial port. Most Linux installations will use either")
print("/dev/ttyS0 or /dev/ttyS1 for the built in motherboard ports or ")
print("/dev/ttyUSB0 if you have a USB to RS232 converter.\n")
sys.exit(1)
def sio_write(self,sio, c):
if self.offline:
return
if self.logio:
self.log("\n{{%s}}" % c)
#pass
while sio.out_waiting > 0:
pass
sio.write(str.encode(c))
#sio.write(c)
# Sometimes we need to wait for a character so we use this function
def sio_read(self, sio, TIMEOUT=SIO_WAIT):
if not self.offline:
if TIMEOUT == 0:
c = sio.read(1)
else:
sio.timeout = TIMEOUT
c = sio.read(1)
sio.timeout = SIO_NO_WAIT
if len(c) > 0:
c = chr(ord(c) & 0x7F)
if self.logio:
self.log("%s" % c)
return(c)
else:
return('')
else:
return('')
def log(self, s):
try:
log = open(LOG_FILE, 'a')
except:
self.bell()
self.popup_error("Can't open %s for writing" % LOG_FILE)
return
log.write(s)
log.close()
def process_escape_seq(self, sio):
if self.ansiMode:
self.ansi_escape_seq(sio)
else:
self.heath_escape_seq(sio)
def heath_escape_seq(self, sio):
c = self.sio_read(sio)
if c == 'A':
self.cursor_up()
elif c == 'B':
self.cursor_down()
elif c == 'C':
self.cursor_forward()
elif c == 'D':
self.cursor_backward()
elif c == 'E':
self.clear_display()
elif c == 'F':
self.enter_graphics_mode()
elif c == 'G':
self.exit_graphics_mode()
elif c == 'H':
self.cursor_home()
elif c == 'I':
self.reverse_linefeed()
elif c == 'J':
self.erase_to_end_of_page()
elif c == 'K':
self.erase_to_end_of_line()
elif c == 'L':
self.insert_line()
elif c == 'M':
self.delete_line()
elif c == 'N':
self.delete_character()
elif c == 'O':
self.exit_insert_mode()
elif c == 'Y': #H19 starts at line & col 1, curses at line & col 0
ch = self.sio_read(sio)
line = (ord(ch) - 31) -1
ch = self.sio_read(sio)
col = (ord(ch) - 31) -1
c = ''
self.set_cursor_position(line,col)
elif c == 'Z':
self.can_perform_as_vt52(sio)
elif c == 'b':
self.erase_to_beginning_of_display()
elif c == 'j':
self.save_cursor_position()
elif c == 'k':
self.goto_saved_cursor_position()
elif c == 'l':
self.erase_line()
elif c == 'n':
self.cursor_position_report(sio)
elif c == 'o':
self.erase_beginning_of_line()
elif c == 'p':
self.enter_reverse_video_mode()
elif c == 'q':
self.exit_reverse_video_mode()
elif c == 'r':
rate = ord(self.sio_read(sio))