-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathradio_edit.py.in
executable file
·4090 lines (3662 loc) · 122 KB
/
radio_edit.py.in
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/env python3
#
# radio_edit - Edit radio clone files
# Copyright (C) 2009 Corey Minyard
#
# 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., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
import sys
import traceback
import tkinter.tix
import tkinter.dialog
import os
import locale
radiodir = "@YCONF@"
decimal_digits = "0123456789"
class ParseException(Exception):
def __init__(self, filename, lineno, err):
self.filename = filename
self.lineno = lineno.get()
self.err = err
pass
def __str__(self):
return self.filename + "(" + str(self.lineno) + "): " + self.err
pass
class DataException(Exception):
def __init__(self, err):
self.err = err
pass
def __str__(self):
return self.err
pass
class LineNum:
def __init__(self):
self.val = 0
return
def get(self):
return self.val
def inc(self):
self.val += 1
return
pass
###############################################################################
#
# Radio information parsing and storage.
#
###############################################################################
class RadioInfo:
def __init__(self, name):
self.name = name;
self.headercmp = []
return
pass
radios = []
def find_radio(data):
for r in radios:
if (len(data) < len(r.headercmp)):
continue
found = True
i = 0
for b in r.headercmp:
if (b != data[i]):
found = False
break
i += 1
pass
if (found):
return r.name
pass
return None
# Throws IOError and ParseException
def read_radios():
filename = os.path.join(radiodir, "radios")
f = open(filename, "r");
try:
lineno = LineNum()
in_radio = False
l = ""
while True:
l += f.readline()
if (not l):
break;
lineno.inc()
if l[len(l) - 1] == '\\':
continue
if l[0] == '#':
l = ""
continue
v = l.split();
l = ""
if (not v):
continue
if (v[0] == "radio"):
if (in_radio):
raise ParseException(filename, lineno,
"Got radio start inside a radio")
if (len(v) < 2):
raise ParseException(filename, lineno,
"No radio specified")
curr_radio = RadioInfo(v[1])
in_radio = True
continue
if (v[0] == "endradio"):
if (not in_radio):
raise ParseException(filename, lineno,
"Got radio end outside a radio")
if (len(curr_radio.headercmp) == 0):
raise ParseException(filename, lineno,
"No headercmp for radio")
radios.append(curr_radio)
in_radio = False
continue
if (v[0] == "headercmp"):
if (not in_radio):
raise ParseException(filename, lineno,
"Got headercmp outside a radio")
try:
i = 0
found = True
for ns in v[1:]:
n = int(ns, 16)
if (n > 255):
raise TypeError("Number too large")
curr_radio.headercmp.append(n)
i += 1
pass
pass
except TypeError as e:
raise ParseException(filename, lineno,
"invalid hexadecimal 8-bit number")
except ValueError as e:
raise ParseException(filename, lineno,
"invalid hexadecimal 8-bit number")
pass
pass
pass
except:
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
f.close()
raise exceptionType(exceptionValue).with_traceback(exceptionTraceback)
return
###############################################################################
#
# Handling of repeater offsets
#
###############################################################################
def getHz(s, filename, lineno):
v = 0
numdec = 0
postdec = 0
units = None
l = 0
for c in s:
if (c == '.'):
if (numdec > 0):
if (postdec != 3):
raise ParseException(filename, lineno,
"Invalid number %s" % s)
pass
postdec = 0
numdec += 1
l += 1
continue
d = decimal_digits.find(c)
if (d == -1):
units = s[l:]
break
postdec += 1
v *= 10
v += d
l += 1
pass
if (numdec > 0):
if (postdec > 3):
raise ParseException(filename, lineno,
"Invalid number %s" % s)
v *= (10 ** (3 - postdec)) # Extend n.n to n.nnn
pass
if (units):
if (units == "MHz"):
while (numdec < 2):
v *= 1000
numdec += 1
pass
pass
elif (units == "kHz"):
while (numdec < 1):
v *= 1000
numdec += 1
pass
pass
elif (units == "Hz"):
pass
else:
raise ParseException(filename, lineno,
"Invalid units %s" % units)
pass
return v
def getCountry():
l = locale.getdefaultlocale()
if (not l):
return None
l = l[0].split("_")
if len(l) < 2:
return None
return l[1]
class RepeaterOffsets:
def __init__(self, country=None):
self.offsets = []
if (country is None):
country = getCountry()
if (not country):
return
pass
filename = os.path.join(radiodir, country + ".rpt")
try:
fd = open(filename, "r")
except IOError as e:
print("Can't load repeater offsets: " + str(e))
return
try:
lineno = LineNum()
while True:
l = fd.readline();
if (not l):
break
lineno.inc()
if l[0] == '#':
continue
v = l.split();
if not v:
continue
if len(v) != 4:
raise ParseException(filename, lineno,
"Invalid number of elements on line")
if v[2] != "+" and v[2] != "-":
raise ParseException(filename, lineno,
"Invalid offset, must be + or -")
self.offsets.append((getHz(v[0], filename, lineno),
getHz(v[1], filename, lineno),
v[2],
getHz(v[3], filename, lineno)))
pass
pass
except ParseException as e:
print(e)
return
def calcOffset(self, freq):
for e in self.offsets:
if ((freq >= e[0]) and (freq <= e[1])):
return (e[2], e[3])
pass
return ("smplx", 0)
pass
###############################################################################
#
# Configuration information
#
###############################################################################
class GenConfig:
def __init__(self):
self.doRptOffsets = True
self.rptoff = RepeaterOffsets()
return
pass
###############################################################################
#
# Raw file data handling
#
###############################################################################
class Address:
"""This holds an address in a radio file. It is a set of
locations (entries), each holding a 4-tuple: start byte, start bit,
length (in bits), and a offset between succeeding entries."""
def __init__(self, c, s, base=0):
l = len(s)
if (l == 0):
raise ParseException(c.filename, c.lineno,
"Empty location");
if (s[0] != '('):
raise ParseException(c.filename, c.lineno,
"Address doesn't start with (");
if (s[l-1] != ')'):
raise ParseException(c.filename, c.lineno,
"Address doesn't end with )");
self.entries = []
self.revbits = False # Bits are numbered from 0 by default
i = 1
l -= 1
pos = 0
start = i
v = [0, 0, 0, 0]
self.entries.append(v)
while (i < l):
if (s[i] == ','):
v[pos] = self.parseNum(c, s[start:i], base)
start = i + 1
pos += 1
if (pos > 3):
raise ParseException(c.filename, c.lineno,
"Address has too many values"
+ " in a field");
pass
elif (s[i] == ':'):
if (pos < 2 or pos > 3):
raise ParseException(c.filename, c.lineno,
"Address has too few values"
+ " in a field");
v[pos] = self.parseNum(c, s[start:i], base)
start = i + 1
pos = 0
v = [0, 0, 0, 0]
self.entries.append(v)
pass
i += 1
pass
if (pos < 2 or pos > 3):
raise ParseException(c.filename, c.lineno,
"Address has too few values"
+ " in a field");
if ((pos == 3) and (s[start] == 'r')):
# Number bits starting with the high order.
start += 1
self.revbits = True
pass
v[pos] = self.parseNum(c, s[start:i], base)
return
def parseNum(self, c, s, base):
l = len(s)
if (s[l-1] == '*'):
v = c.toNum(s[0:l-1]) + base
else:
v = c.toNum(s[0:l])
pass
return v
def numbits(self):
numbits = 0
for a in self.entries:
numbits += a[2]
pass
return numbits
pass
class RadioFileData:
# Throws IOError
def __init__(self, filename):
self.filename = filename
f = open(filename, "rb")
strdata = f.read();
f.close()
self.data = []
for c in strdata:
self.data.append(c)
self.changed = False
pass
def write(self, filename=None):
if (filename == None):
filename = self.filename
else:
self.filename = filename
pass
try:
f = open(filename, "wb")
f.write(bytes(self.data))
finally:
f.close()
pass
self.changed = False
pass
# The get and set routine throw IndexError if out of range.
def get_bits(self, addr, num):
v = 0
for a in addr.entries:
byte_off = a[0]
bit_off = a[1] + a[3] * num
byte_off += bit_off // 8
bit_off %= 8
numbits = a[2]
v <<= numbits
if (addr.revbits):
# FIXME - This doesn't work for multi-byte revbits
x = self.data[byte_off] >> (8 - numbits - bit_off)
else:
x = self.data[byte_off] >> bit_off
pass
bitsleft = numbits - (8 - bit_off)
while (bitsleft >= 8):
byte_off += 1
x <<= 8
x |= self.data[byte_off]
bitsleft -= 8
pass
if (bitsleft > 0):
byte_off += 1
x <<= bitsleft
if (addr.revbits):
x1 = self.data[byte_off] >> (8 - bitsleft)
x1 &= ~(0xffffffff << bitsleft)
x |= x1
else:
x |= self.data[byte_off] & ~(0xffffffff << bitsleft)
pass
pass
v |= x & ~(0xffffffff << numbits)
pass
return v
def set_bits(self, vt, addr, num):
self.changed = True
bitsleft = 0
for a in addr.entries:
bitsleft += a[2]
pass
for a in addr.entries:
byte_off = a[0]
bit_off = a[1] + a[3] * num
byte_off += bit_off // 8
bit_off %= 8
numbits = a[2]
bitsleft -= numbits
v = vt >> bitsleft
v = v & ~(0xffffffff << numbits)
x = self.data[byte_off]
if ((bit_off + numbits) <= 8):
# Special case, all in one byte
if (addr.revbits):
mask = (0xff >> (8 - numbits)) << (8 - numbits - bit_off)
else:
mask = (0xff >> (8 - numbits)) << bit_off
pass
x &= ~mask
if (addr.revbits):
x |= (v << (8 - numbits - bit_off)) & mask
else:
x |= (v << bit_off) & mask
pass
self.data[byte_off] = x
continue
# FIXME - no support for the 'r' flag in the following.
mask = 0xff << bit_off
x &= ~mask
self.data[byte_off] = x | ((v >> (numbits - (8 -bit_off))) & mask)
self.data[byte_off] &= 0xff
byte_off += 1
numbits -= 8 - bit_off
while (numbits >= 8):
self.data[byte_off] = (v >> (numbits - 8)) & 0xff
self.data[byte_off] &= 0xff
byte_off += 1
numbits -= 8
pass
if (numbits > 0):
mask = 0xff << numbits
x = self.data[byte_off]
x &= mask
x |= v & ~mask;
self.data[byte_off] = x & 0xff
pass
pass
pass
def get_bytes(self, addr, num):
byte_pos = addr.entries[0][0] + (addr.entries[0][3] * num // 8)
numbytes = addr.entries[0][2] // 8
return self.data[byte_pos:byte_pos+numbytes]
def set_byte(self, v, addr, num, offset):
byte_pos = addr.entries[0][0] + (addr.entries[0][3] * num // 8)
self.changed = True
self.data[byte_pos + offset] = v
pass
pass
###############################################################################
#
#
#
###############################################################################
class HandlerTie:
def __init__(self, handler, tie):
self.handler = handler
self.tie = tie
return
pass
class Handler:
"""This is an internal type that eases handling events for a type.
The type objects do not own the data about the particular object
they are used to represent, since they are reused for many
different objects. Instead, when a widget is allocated to handle
a particular type, one of these is allocated to hold that data and
deliver it to the type object."""
def __init__(self, handler, data, wtype):
self.handler = handler
self.data = data
self.type = wtype
self.widget = None # Will be filled in later by the type
self.foreground = None # Filled in if needed
# The set of widgets/handlers we are tied to. When the widget
# associated with this handler changes, we call everything in
# the tie list. This is a list of HandlerTie object.
self.tielist = []
# These are ties to me that change the way I behave. We keep these
# as lists becaue there may be more than one tie to me, and only
# one of them is required to be valid, so we may have to scan them.
self.offtie = []
self.basefreqtie = []
self.offfreqtie = []
self.lastbfreq = None
self.enabletie = None
self.enableoffsettie = None
pass
def addTie(self, h, tie):
self.tielist.append(HandlerTie(h, tie))
# Inform the widget we are tied to them, in case they care
h.newTie(self, tie)
return
def newTie(self, fromh, tie):
self.type.newTie(self, fromh, tie)
if (fromh.widget):
fromh.modified(newtie=True)
return
def tieChanged(self, h, newtie):
self.type.tieChanged(self, h, newtie)
return
# Called when the user modifies a value. Not required for setSelect
# because that does a full redisplay. Causes any tied values to be
# redisplayed.
def modified(self, newtie=False):
for ht in self.tielist:
ht.tie.tieChanged(ht.handler, newtie)
pass
return
def getValue(self, num):
return self.type.getValue(self, num)
def getSelect(self, num):
return self.type.getSelect(self, num)
def setSelect(self, v, num):
return self.type.setSelect(v, self, num)
def redisplay(self, h):
self.type.redisplay(h)
return
def set(self):
self.handler(self)
pass
def set_event(self, event):
return self.handler(self, event)
pass
###############################################################################
#
#
#
###############################################################################
def rewriteEntry(w, v):
"""Rewrite the value in an entry widget, setting the state to normal so
it is writable, the setting the value, then setting the state back"""
oldstate = w["state"]
if (oldstate != "normal"):
w["state"] = "normal"
pass
w.delete(0, 'end')
w.insert(0, v)
if (oldstate != "normal"):
w["state"] = oldstate
pass
return
class YType:
"""This is the base class for all the different types that may be
declared in a radio file."""
def __init__(self, name):
self.name = name
self.displayheader = True
pass
def matchName(self, c, string):
if (self.name == string):
return self
return None
def getWidgetHandler(self, parent, t, num):
h = Handler(None, None, self)
h.num = num
h.widget = None
return h
def renumWidget(self, h, num):
if h.enabletie:
v = int(h.enabletie.getValue(num))
if v:
h.widget["state"] = "normal"
else:
h.widget["state"] = "disabled"
pass
pass
elif h.enableoffsettie:
v = h.enableoffsettie.getValue(num)
if (v == "+" or v == "-"):
h.widget["state"] = "normal"
else:
h.widget["state"] = "disabled"
pass
pass
pass
def redisplay(self, h):
self.renumWidget(h, h.num)
return
def tieChanged(self, h, newtie):
self.redisplay(h)
return
def checkAddrOk(self, c, addr):
return True
def getSelect(self, h, num):
return ""
def getValue(self, h, num):
# Most types are the same for select and value
return self.getSelect(h, num)
def setSelect(self, v, h, num, default=None):
return
def newTie(self, myh, fromh, tie):
if (tie.tietype == "enable"):
myh.enabletie = fromh
elif (tie.tietype == "enableoff"):
myh.enableoffsettie = fromh
elif (tie.tietype != "normal"):
print(("Warning: Unknown type %s tie to %s"
% (tie.tietype, tie.value)))
pass
if (myh.widget):
self.redisplay(myh)
pass
return
pass
class BISpecial(YType):
def __init__(self, name="Special"):
YType.__init__(self, name)
self.displayheader = False
pass
def getWidgetHandler(self, parent, t, num):
h = Handler(None, t, self)
# This is not actually displayed.
h.widget = None
h.num = num
return h
def getSelect(self, h, num):
t = h.data
return str(t.data.get_bits(t.addr, num))
def getValue(self, h, num):
t = h.data
return str(t.data.get_bits(t.addr, num))
def setSelect(self, v, h, num, default=None):
t = h.data
try:
t.data.set_bits(int(v), t.addr, num)
except Exception as e:
if default is not None:
try:
t.data.set_bits(int(default), t.addr, num)
except:
pass
pass
pass
return
pass
class BISpecial857A(BISpecial):
def __init__(self, name="Special857A"):
BISpecial.__init__(self, name)
return
def newTie(self, myh, fromh, tie):
if (tie.tietype == "offset"):
myh.offtie.append(fromh)
elif (tie.tietype == "basefreq"):
myh.basefreqtie.append(fromh)
pass
if (myh.widget):
self.redisplay(myh)
pass
return
def renumWidget(self, h, num):
h.num = num
t = h.data
off = None
for m in h.offtie:
off = m.getValue(num)
if (off):
break
pass
oldv = t.data.get_bits(t.addr, num)
if (off and off != "split"):
if (oldv != 0):
t.data.set_bits(0, t.addr, num)
return
bfreq = None
for m in h.basefreqtie:
bfreq = m.getValue(num)
if (bfreq):
break
pass
if (not bfreq):
# bfreq should be coming later, don't get it now.
return
bfreq = int(bfreq)
if (bfreq < 4000000):
if (oldv != 0):
t.data.set_bits(0, t.addr, num)
pass
elif (bfreq < 8000000):
if (oldv != 1):
t.data.set_bits(1, t.addr, num)
pass
elif (bfreq < 11000000):
if (oldv != 2):
t.data.set_bits(2, t.addr, num)
pass
elif (bfreq < 14000000):
if (oldv != 3):
t.data.set_bits(3, t.addr, num)
pass
elif (bfreq < 40000000):
if (oldv != 4):
t.data.set_bits(4, t.addr, num)
pass
else:
if (oldv != 5):
t.data.set_bits(5, t.addr, num)
pass
pass
pass
pass
class BISpecial857B(BISpecial):
def __init__(self, name="Special857B"):
BISpecial.__init__(self, name)
return
def newTie(self, myh, fromh, tie):
if (tie.tietype == "basefreq"):
myh.basefreqtie.append(fromh)
pass
return
def renumWidget(self, h, num):
h.num = num
t = h.data
bfreq = None
for m in h.basefreqtie:
bfreq = m.getValue(num)
if (bfreq):
break
pass
oldv = t.data.get_bits(t.addr, num)
bfreq = int(bfreq)
if (not bfreq):
if (oldv != 0):
t.data.set_bits(0, t.addr, num)
return
if (bfreq < 0x4000000):
if (oldv != 1):
t.data.set_bits(1, t.addr, num)
else:
if (oldv != 0):
t.data.set_bits(0, t.addr, num)
pass
pass
pass
class BISpecialTMV71(BISpecial):
"""There is a location that is either VHF (0x05) or UHF (0x08) depending
on the frequency set."""
def __init__(self, name="SpecialTMV71"):
BISpecial.__init__(self, name)
return
def newTie(self, myh, fromh, tie):
if (tie.tietype == "basefreq"):
myh.basefreqtie.append(fromh)
pass
return
def renumWidget(self, h, num):
h.num = num
t = h.data
bfreq = None
for m in h.basefreqtie:
bfreq = m.getValue(num)
if (bfreq):
break
pass
bfreq = int(bfreq)
oldv = t.data.get_bits(t.addr, num)
if (bfreq > 400000000):
t.data.set_bits(0x05, t.addr, num)
else:
t.data.set_bits(0x08, t.addr, num)
pass
pass
pass
class BISetToZero(BISpecial):
def __init__(self, name="SetToZero"):
BISpecial.__init__(self, name)
return
def newTie(self, myh, fromh, tie):
if (tie.tietype == "normal"):
myh.offtie.append(fromh)
pass
return
def renumWidget(self, h, num):
off = None
for m in h.offtie:
off = m.getValue(num)
if (off):
break
pass
if off is None:
off = True
else:
off = int(off)
pass
if (off):
t = h.data
oldv = t.data.get_bits(t.addr, num)
if (oldv != 0):
t.data.set_bits(0, t.addr, num)
pass
pass
pass
pass
class BIInt(YType):
valid_digits = decimal_digits
conv_val = 10
def __init__(self, name="Int"):
YType.__init__(self, name)
return
def setup(self, minval, maxval, offset, multi, units):
self.minval = minval
self.maxval = maxval
self.unsigned = minval >= 0
self.offset = offset
self.multi = multi
self.units = units
self.unitslen = len(units)
return
def newInst(self, name):
return BIInt(name=name)
def matchName(self, c, string):
if not string.startswith(self.name + "("):
return None
if not string.endswith(")"):
raise ParseException(c.filename, c.lineno,
self.name + " does not end with ')'")
spec = string[4:len(string)-1].split(",")
if (len(spec) != 5):
raise ParseException(c.filename, c.lineno,
"Invalid %s specifier" % self.name)
minval = c.toNum(spec[0])
maxval = c.toNum(spec[1])
if (minval >= maxval):
raise ParseException(c.filename, c.lineno,
"min must be < max value")
offset = c.toNum(spec[2])
multi = c.toNum(spec[3])
if (multi <= 0):
raise ParseException(c.filename, c.lineno,
"Multiplier must be > 0")
units = spec[4]
b = self.newInst(name=string)
b.setup(minval, maxval, offset, multi, units)
return b
def getWidgetHandler(self, parent, t, num):
v = self.getInt(t.data, t.addr, num)
h = Handler(self.set, t, self)
w = tkinter.tix.Entry(parent, readonlybackground="tan")
h.widget = w
h.num = num
w.bind("<Key>", h.set_event)
w.delete(0, 'end')
w.insert(0, self.convFromNum(v)+self.units)
return h
def renumWidget(self, h, num):
YType.renumWidget(self, h, num)
h.num = num
t = h.data
v = t.data.get_bits(t.addr, num)
rewriteEntry(h.widget, str(v)+self.units)
pass
def getInt(self, data, addr, num):
v = data.get_bits(addr, num)
if (not self.unsigned):
n = addr.numbits()
if ((1 << n) & v):
# Negative value
v |= (-1 << n) | v
pass
pass
v += self.offset