Fix invalid DHCP server socket creation in the Webserver project.
[pub/USBasp.git] / Projects / TempDataLogger / TempLogHostApp_Python / temp_log_config.py
1 #!/usr/bin/env python
2
3 """
4 LUFA Library
5 Copyright (C) Dean Camera, 2018.
6
7 dean [at] fourwalledcubicle [dot] com
8 www.lufa-lib.org
9 """
10
11 """
12 Front-end configuration app for the TempDataLogger project. This script
13 configures the logger to the current system time and date, with a user
14 defined logging interval.
15
16 The logging interval should be specified in milliseconds and is rounded to
17 a multiple of 500ms.
18
19 Usage:
20 python temp_log_config.py <Log_Interval>
21
22 Example:
23 python temp_log_config.py 500
24
25 Requires the pywinusb library (https://pypi.python.org/pypi/pywinusb/).
26 """
27
28 import sys
29 from datetime import datetime
30 import hid
31
32
33 def get_hid_device_handle():
34 all_hid_devices = hid.enumerate()
35
36 lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x2063]
37
38 if len(lufa_hid_devices) is 0:
39 return None
40
41 device_handle = hid.device()
42 device_handle.open_path(lufa_hid_devices[0]['path'])
43 return device_handle
44
45
46 def configure_temp_log_device(device, time_date, log_interval_500ms):
47 report_data = bytearray(8)
48
49 # Report data for the demo is the report ID (always zero)
50 report_data[0] = 0
51
52 # Followed by the time/date data
53 report_data[1] = time_date.hour
54 report_data[2] = time_date.minute,
55 report_data[3] = time_date.second
56 report_data[4] = time_date.day,
57 report_data[5] = time_date.month
58 report_data[6] = time_date.year - 2000
59
60 # Lastly the log interval in 500ms units of time
61 report_data[7] = log_interval_500ms
62
63 # Send the generated report to the device
64 device.write(report_data)
65
66
67 def main(time_date, log_interval_500ms):
68 hid_device = get_hid_device_handle()
69
70 if hid_device is None:
71 print("No valid HID device found.")
72 sys.exit(1)
73
74 try:
75 print("Connected to device.", flush=True)
76
77 configure_temp_log_device(hid_device, time_date, log_interval_500ms)
78
79 print("Time/Date is now set to %s" % time_date)
80 print("Log interval is now set to every %0.1fs" % (log_interval_500ms * (500.0 / 1000.0)))
81
82 finally:
83 hid_device.close()
84
85
86 if __name__ == '__main__':
87 time_date = datetime.now()
88 log_interval_500ms = (int(sys.argv[1]) / 500) if len(sys.argv) > 1 else 2
89
90 # Clamp the log interval to the allowable range
91 log_interval_500ms = max(log_interval_500ms, 0x01)
92 log_interval_500ms = min(log_interval_500ms, 0xFF)
93
94 main(time_date, log_interval_500ms)