-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlbe142x.py
More file actions
executable file
·650 lines (489 loc) · 16.8 KB
/
Copy pathlbe142x.py
File metadata and controls
executable file
·650 lines (489 loc) · 16.8 KB
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
#!/bin/env python
#
# Configuration Utility für Leo Bodnar GPSDO
#
# Copyright (C) 2020-2026 Mario Haustein
#
# 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, see <http://www.gnu.org/licenses/>.
#
import argparse
import hid
import struct
import sys
USBIDS = \
[
( 0x1dd2, 0x2444 ), # LBE 1421
]
class GPSDODevice(object):
"""
GPSDO device bound to an USB device.
"""
@classmethod
def enumerate(cls):
"""
Returns USB device information descriptors.
The method returns the USB device information descriptors of all
detected GPSDO devices as they are returned by the HID library.
:returns: USB device information descriptors
:rtype: list of dict
"""
for d in hid.enumerate():
if ( d['vendor_id'], d['product_id'] ) in USBIDS:
yield d
@classmethod
def filter(cls, serial = None, device = None):
"""
Filters the result of the `enumerate()`.
The method filters the detected devices by serial number or device
path. Only devices which math all parameters are returned. If a
parameter is `None` it will not be treated as filter criteria.
:param serial: Serial Number of the device
:type serial: str | None
:param device: Device path
:type device: bytes | None
:returns: USB device information descriptors
:rtype: list of dict
"""
for d in cls.enumerate():
if serial is not None and serial != d['serial_number']:
continue
if device is not None and device != d['path']:
continue
yield d
@classmethod
def open(cls, serial = None, device = None):
"""
Opens a device and returns an instance.
This method opens an USB device. The parameters are the same like for
the `filter()` method. If, after applying the filter, no or more than
one device was found, an exception is raised.
:param serial: Serial Number of the device
:type serial: str | None
:param device: Device path
:type device: bytes | None
:raises: :class:`ValueError`: Filter criterias are not unique.
:returns: Device instance
:rtype: GPSDODevice
"""
ds = list(cls.filter(serial = serial, device = device))
if len(ds) == 0:
raise ValueError("No GPSDO device found")
elif len(ds) > 1:
raise ValueError("More than one GPSDO device found. Please specifiy device path or serial number.")
return cls(ds[0])
@classmethod
def openall(cls, serial = None, device = None):
"""
Opens all devices mathing the filter and return a list of instances.
This method opens all devices matching the specified filter. The
parameters are the same like for the `filter()` method.
:param serial: Serial Number of the device
:type serial: str | None
:param device: Device path
:type device: bytes | None
:returns: List of device instance
:rtype: list of GPSDODevice
"""
ds = list(cls.filter(serial = serial, device = device))
for dinfo in ds:
yield cls(dinfo)
def __init__(self, dinfo):
"""
Opens a device.
DO NOT USE THIS METHOD DIRECTLY! Use the methods `open()` or
`openall()` instead.
:param dinfo: USB device information descriptor
:type dinfo: dict
:returns: Device instance
:rtype: GPSDODevice
"""
self.device = None
super().__init__()
# Open device.
self.path = dinfo['path'].decode()
self.vid = dinfo['vendor_id']
self.pid = dinfo['product_id']
self.manufacturer = dinfo['manufacturer_string']
self.product = dinfo['product_string']
self.serial = dinfo['serial_number']
self.version_major = (dinfo['release_number'] & 0xff00) >> 8
self.version_minor = dinfo['release_number'] & 0x00ff
self.device = hid.Device(path = dinfo['path'])
self.read()
def __del__(self):
if self.device is not None:
self.device.close()
def read(self):
"""
Read status and configuration from the device.
"""
buf = self.device.get_feature_report(0x4b, 60)
# self.loss_count = buf[0]
self.sat_lock = bool(buf[1] & 0x01)
self.pll_lock = bool(buf[1] & 0x02)
self.ant_ok = bool(buf[1] & 0x04)
# self.led1 = bool(buf[1] & 0x08)
# self.led2 = bool(buf[1] & 0x10)
self.out1 = bool(buf[1] & 0x20)
self.out2 = bool(buf[1] & 0x40)
self.pps1 = bool(buf[1] & 0x80)
self.f1 = struct.unpack("<I", buf[6:10])[0]
self.f2 = struct.unpack("<I", buf[14:18])[0]
self.fll = bool(buf[18])
self.out1low = bool(buf[19])
self.out2low = bool(buf[20])
def enable(self, out1, out2):
"""
Enable or disable outputs.
This method enables or disables output drivers for the first channel
(`out1`) and second channel (`out2`). The driver will be enabled when
the parameter is set to `True` and disabled when `False`. If the
parameter is `None`, the state is not changed.
The 1-PPS-mode takes precedence over this setting. Enabling the
1-PPS-mode provides the clock pulse even with a disabled driver.
:param out1: configuration for output 1
:type out1: bool | None
:param out2: configuration for output 2
:type out2: bool | None
"""
buf = 60 * [ 0 ]
buf[0] = 1
# As of firmware version 1.9 the flags are swapped in regards of output
# assignment. 0x02 will configure output 1, but sets LED 2. 0x01 will
# configure output 2, but sets LED 1.
if out1 is None and self.out1 or out1:
buf[1] |= 0x02
if out2 is None and self.out2 or out2:
buf[1] |= 0x01
self.device.send_feature_report(bytes(buf))
def identify(self):
"""
Identify device.
Blink LEDs of the device.
"""
buf = 60 * [ 0 ]
buf[0] = 2
self.device.send_feature_report(bytes(buf))
def set_freq(self, chann, f, save):
"""
Set channel frequency.
Sets frequency of channel `chann` (0 for first channel, 1 for second
channel) to value `f` in Hz. When `f` is `None`, the function returns
without doing anything.
If `save` is true, the frequency is saved into the flash. Otherwise the
setting is temporary until poweroff.
:param chann: channel number
:type chann: int
:param f: frequency
:type f: int | None
:param save: save to flash
:type save: bool
:raises: :class:`ValueError`: frequency out of range.
"""
buf = 60 * [ 0 ]
if f is None:
return
# TODO: 1420: 1600000000
if f > 1400000000:
raise ValueError("Frequency for output %d too high. Maximal 1400000000 Hz allowed." % ( chann + 1 ))
if chann == 0:
# TODO: 1420 3, 4
if save:
buf[0] = 6
else:
buf[0] = 5
elif chann == 1:
if save:
buf[0] = 10
else:
buf[0] = 9
else:
return
buf[5:8] = struct.pack("<I", f)[0:4]
self.device.send_feature_report(bytes(buf))
def set_pll(self, pll):
"""
Set PLL mode.
When `pll` evaluates to true, the device operates in PLL mode (phase
locked loop), otherwise in FLL mode (frequency locked loop).
:param pll: PLL mode
:type pll: bool
"""
if pll is None:
return
buf = 60 * [ 0 ]
buf[0] = 11
buf[1] = 0 if pll else 1
self.device.send_feature_report(bytes(buf))
def set_pps(self, pps):
"""
Enable 1-PPS-mode on first channel.
When `pps` evaluates to true, the 1-PPS-mode is enabled. In 1-PPS-mode
the device provides a 1 Hz pulse at the start of every UTC second on
the first channel. The 1-PPS-mode takes precedence over frequency
mode.
:param pps: 1-PPS-mode
:type pps: bool
"""
if pps is None:
return
buf = 60 * [ 0 ]
buf[0] = 12
buf[1] = 1 if pps else 0
self.device.send_feature_report(bytes(buf))
def set_level(self, chann, lowlevel):
"""
Set drive level.
This method sets the drive level of channel `chann` (0 for first
channel, 1 for second channel). When `level` evaluates to true, the
driver is set to low level, otherwise to normal power level.
:param chann: channel number
:type chann: int
:param lowlevel: driver level
:type lowlevel: bool
"""
if lowlevel is None:
return
buf = 60 * [ 0 ]
if chann == 0:
# TODO: 1420 7
buf[0] = 13
elif chann == 1:
buf[0] = 14
else:
return
buf[1] = 1 if lowlevel else 0
self.device.send_feature_report(bytes(buf))
def infotext(self):
"""
Returns current device status and configuration as formatted text.
:returns: Status
:rtype: str
"""
result = ""
result += "Device information\n"
result += "------------------\n"
result += "VID, PID: 0x%04x:0x%04x\n" % ( self.vid, self.pid )
result += "Device: %s\n" % self.path
result += "Product: %s\n" % self.product
result += "Manufacturer: %s\n" % self.manufacturer
result += "S/N: %s\n" % self.serial
result += "Firmware: %d.%d\n" % ( self.version_major, self.version_minor )
result += "\n"
result += "Device status\n"
result += "-------------\n"
# result += "Loss count: %d\n" % self.loss_count
result += "SAT lock: %s\n" % ( "LOCKED" if self.sat_lock else "unlocked" )
result += "PLL lock: %s\n" % ( "LOCKED" if self.pll_lock else "unlocked" )
result += "Antenna: %s\n" % ( "OK" if self.ant_ok else "short-circuit" )
result += "Mode: %s\n" % ( "FLL" if self.fll else "PLL" )
result += "\n"
result += "Output settings\n"
result += "---------------\n"
result += "Output 1: "
if self.pps1:
result += "1 PPS"
elif not self.out1:
result += " --- "
else:
result += "%10d Hz" % self.f1
result += " level: %s\n" % ( "LOW" if self.out1low else "NORMAL" )
result += "Output 2: "
if not self.out2:
result += " --- "
else:
result += "%10d Hz" % self.f2
result += " level: %s\n" % ( "LOW" if self.out1low else "NORMAL" )
result += "\n"
result = result.strip("\n")
if result:
result += "\n"
return result
#
# Command Callbacks
#
def command_list(args):
for d in GPSDODevice.enumerate():
sys.stdout.write("%04x:%04x %-16s %s %s\n" % \
(
d['vendor_id'],
d['product_id'],
d['path'].decode(),
d['serial_number'],
d['product_string'],
))
def command_detail(args):
first = True
for d in GPSDODevice.openall(serial = args.serial, device = args.device):
if first:
first = False
else:
sys.stdout.write("\n\n")
sys.stdout.write(d.infotext())
def command_modify(args):
d = GPSDODevice.open(serial = args.serial, device = args.device)
d.enable(args.out1, args.out2)
d.set_freq(0, args.f1, args.save)
d.set_freq(1, args.f2, args.save)
d.set_pll(args.pll)
d.set_pps(args.pps)
d.set_level(0, args.out1low)
d.set_level(1, args.out2low)
def command_identify(args):
d = GPSDODevice.open(serial = args.serial, device = args.device)
d.identify()
#
# Command Line Parser Helper Functions
#
def parser_add_device(p):
p.add_argument(
'-s', '--serial',
dest = 'serial',
metavar = 'S/N',
help = "Serial number of GPS device")
p.add_argument(
'-d', '--device',
dest = 'device',
metavar = 'PATH',
help = "Path specification of the USB HID device")
def parser_add_config(p):
parser_config = p.add_argument_group(
title = "Configuration")
parser_config.add_argument(
'--f1',
dest = 'f1',
metavar = 'HZ',
type = int,
help = "Output 1 frequency")
parser_config.add_argument(
'--f2',
dest = 'f2',
metavar = 'HZ',
type = int,
help = "Output 2 frequency")
parser_config.add_argument(
'--save',
dest = 'save',
action = 'store_true',
help = "Save frequency to flash memory")
parser_config.add_argument(
'--pll',
dest = 'pll',
action = 'store_const',
const = True,
help = "Set PLL mode")
parser_config.add_argument(
'--fll',
dest = 'pll',
action = 'store_const',
const = False,
help = "Set FLL mode")
parser_config.add_argument(
'--enable1',
dest = 'out1',
action = 'store_const',
const = True,
help = "Enable output 1")
parser_config.add_argument(
'--disable1',
dest = 'out1',
action = 'store_const',
const = False,
help = "Disable output 1")
parser_config.add_argument(
'--pps-enable',
dest = 'pps',
action = 'store_const',
const = True,
help = "Enable PPS signal on output 1")
parser_config.add_argument(
'--pps-disable',
dest = 'pps',
action = 'store_const',
const = False,
help = "Enable PPS signal on output 1")
parser_config.add_argument(
'--level1-low',
dest = 'out1low',
action = 'store_const',
const = True,
help = "Output 1 drive low level")
parser_config.add_argument(
'--level1-normal',
dest = 'out1low',
action = 'store_const',
const = False,
help = "Output 1 drive normal level")
parser_config.add_argument(
'--enable2',
dest = 'out2',
action = 'store_const',
const = True,
help = "Enable output 2")
parser_config.add_argument(
'--disable2',
dest = 'out2',
action = 'store_const',
const = False,
help = "Disable output 2")
parser_config.add_argument(
'--level2-low',
dest = 'out2low',
action = 'store_const',
const = True,
help = "Output 2 drive low level")
parser_config.add_argument(
'--level2-normal',
dest = 'out2low',
action = 'store_const',
const = False,
help = "Output 2 drive normal level")
#
# Command Line Parser Definition
#
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser_list = subparsers.add_parser(
'list',
aliases = [ 'l' ],
help = "List devices")
parser_list.set_defaults(func = command_list)
parser_detail = subparsers.add_parser(
'detail',
aliases = [ 'd' ],
help = "Show details of a device")
parser_add_device(parser_detail)
parser_detail.set_defaults(func = command_detail)
parser_modify = subparsers.add_parser(
'modify',
aliases = [ 'm' ],
help = "Change configuration of a single device")
parser_add_device(parser_modify)
parser_add_config(parser_modify)
parser_modify.set_defaults(func = command_modify)
parser_identify = subparsers.add_parser(
'identify',
aliases = [ 'i' ],
help = "Identify output channel of a device")
parser_add_device(parser_identify)
parser_identify.set_defaults(func = command_identify)
#
# Here we go.
#
if __name__ == '__main__':
args = parser.parse_args()
if 'func' in args:
args.func(args)
else:
parser.print_help()