-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstormworks_connect_v1.3_free.py
1643 lines (1384 loc) · 81 KB
/
stormworks_connect_v1.3_free.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
import tkinter as tk
from tkinter import filedialog, StringVar, BooleanVar, DoubleVar, IntVar, Frame, Button, Label, Entry, Toplevel, Scrollbar, Listbox, messagebox
from tkinter import ttk
from PIL import Image, ImageTk, ImageSequence
from flask import Flask, request, jsonify
import threading
import logging
import webbrowser
import sys
import os
import pygame
import requests
from packaging import version
import TKinterModernThemes as TKMT
import ctypes
import darkdetect
import imageio
import time
import configparser
from bs4 import BeautifulSoup, SoupStrainer
import textwrap
from urllib.request import urlopen, Request
from io import BytesIO
import serial
import queue
import ast
# Enable High DPI support
try:
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
print(e)
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def open_download_link(url):
webbrowser.open_new(url)
class App(TKMT.ThemedTKinterFrame):
def __init__(self):
# Define current version
self.current_version = "1.3.0.0"
self.suppress_update_notification = 'False'
self.config_file = "config.ini"
# Detect system theme
system_theme = self.get_system_theme()
# Initialize with detected theme
super().__init__("Stormworks Connect", "sun-valley", system_theme)
# Flask app initialization
self.app = Flask(__name__)
self.current_image = None
self.original_image = None
self.original_gif = None
self.current_gif = None
self.selected_gif_size = (32, 32)
self.current_gif_animation_id = None
self.gif_frames_data = [] # Add this line to store GIF frames data
self.current_result_index = 0 # index to track the current result
self.results_per_page = 3 # number of results on one page
self.results_list = [] # a list to store all results
self.current_page = 0 # Initializing the current_page variable
self.result_text = [] # Initializing a variable to store search results
self.page_content = []
self.page_line_count = 10 # number of lines per page
self.search_complete = False
self.init_flask()
# Setting up logging
logging.basicConfig(level=logging.DEBUG)
# Initialize Pygame
pygame.init()
pygame.joystick.init()
self.joystick = None
# Variables for joystick control
self.joystick_name = tk.StringVar()
self.steering_angle = DoubleVar()
self.gas_pedal = DoubleVar()
self.brake_pedal = DoubleVar()
self.deadzone = DoubleVar(value=0.05)
self.swap_pedals = BooleanVar(value=False)
self.combined_pedals = BooleanVar(value=False)
self.steering_axis = tk.IntVar(value=0)
self.gas_axis = tk.IntVar(value=2)
self.brake_axis = tk.IntVar(value=1)
self.shift_up_button = tk.IntVar(value=1)
self.shift_down_button = tk.IntVar(value=0)
self.custom_button_1 = tk.IntVar(value=9)
self.custom_button_2 = tk.IntVar(value=8)
self.custom_button_3 = tk.IntVar(value=6)
self.custom_button_4 = tk.IntVar(value=7)
self.shift_up_status = tk.StringVar()
self.shift_down_status = tk.StringVar()
self.custom_button_1_status = tk.StringVar()
self.custom_button_2_status = tk.StringVar()
self.custom_button_3_status = tk.StringVar()
self.custom_button_4_status = tk.StringVar()
# Monitor sizes dictionary
self.monitor_sizes = {
"1x1": (32, 32),
"1x2": (64, 32),
"1x3": (96, 32),
"2x2": (64, 64),
"2x3": (96, 64),
"3x3": (96, 96),
"5x3": (160, 96),
"9x5": (288, 160)
}
self.selected_size = (288, 160)
self.fill_image = False
self.device_blocks = []
# Arrays for input and output values
self.digital_inputs = [0] * 4
self.digital_outputs = [0] * 4
self.boolean_inputs = [False] * 4
self.boolean_outputs = [False] * 4
self.arduino_handler = ArduinoHandler()
# Start a thread to read data from Arduino
self.read_arduino_thread = threading.Thread(target=self.read_arduino_data)
self.read_arduino_thread.daemon = True
self.read_arduino_thread.start()
self.update_device_blocks()
# Load images for tabs
self.image_transmit_icon = ImageTk.PhotoImage(Image.open(resource_path("picture.png")))
self.steering_wheel_icon = ImageTk.PhotoImage(Image.open(resource_path("steering-wheel.png")))
self.info_icon = ImageTk.PhotoImage(Image.open(resource_path("info.png")))
self.arduino_icon = ImageTk.PhotoImage(Image.open(resource_path("integrated.png")))
self.create_widgets()
self.poll_joystick()
self.load_config()
self.check_for_updates()
self.root.geometry("800x600")
self.root.update_idletasks()
scaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100
self.root.minsize(int(self.root.winfo_width() * scaleFactor), int(self.root.winfo_height() * scaleFactor))
self.root.resizable(True, True)
self.root.iconphoto(False, tk.PhotoImage(file=resource_path('SC_cover.png')))
self.run()
def load_config(self):
self.config = configparser.ConfigParser()
self.config.read(self.config_file)
if 'Settings' not in self.config.sections():
self.config.add_section('Settings')
if 'SteeringWheel' not in self.config.sections():
self.config.add_section('SteeringWheel')
if 'Arduino' not in self.config.sections():
self.config.add_section('Arduino')
# Load settings
self.suppress_update_notification = self.config.getboolean('Settings', 'suppress_update_notification', fallback=False)
self.deadzone.set(self.config.getfloat('SteeringWheel', 'deadzone', fallback=0.05))
self.swap_pedals.set(self.config.getboolean('SteeringWheel', 'swap_pedals', fallback=False))
self.combined_pedals.set(self.config.getboolean('SteeringWheel', 'combined_pedals', fallback=False))
self.steering_axis.set(self.config.getint('SteeringWheel', 'steering_axis', fallback=0))
self.gas_axis.set(self.config.getint('SteeringWheel', 'gas_axis', fallback=2))
self.brake_axis.set(self.config.getint('SteeringWheel', 'brake_axis', fallback=1))
self.shift_up_button.set(self.config.getint('SteeringWheel', 'shift_up_button', fallback=1))
self.shift_down_button.set(self.config.getint('SteeringWheel', 'shift_down_button', fallback=0))
self.custom_button_1.set(self.config.getint('SteeringWheel', 'custom_button_1', fallback=9))
self.custom_button_2.set(self.config.getint('SteeringWheel', 'custom_button_2', fallback=8))
self.custom_button_3.set(self.config.getint('SteeringWheel', 'custom_button_3', fallback=6))
self.custom_button_4.set(self.config.getint('SteeringWheel', 'custom_button_4', fallback=7))
self.update_swap_pedals_state()
# Load Arduino blocks
self.load_arduino_blocks()
def save_config(self, save_arduino=False):
self.config['Settings']['suppress_update_notification'] = str(self.suppress_update_notification)
self.config['SteeringWheel']['deadzone'] = str(self.deadzone.get())
self.config['SteeringWheel']['swap_pedals'] = str(self.swap_pedals.get())
self.config['SteeringWheel']['combined_pedals'] = str(self.combined_pedals.get())
self.config['SteeringWheel']['steering_axis'] = str(self.steering_axis.get())
self.config['SteeringWheel']['gas_axis'] = str(self.gas_axis.get())
self.config['SteeringWheel']['brake_axis'] = str(self.brake_axis.get())
self.config['SteeringWheel']['shift_up_button'] = str(self.shift_up_button.get())
self.config['SteeringWheel']['shift_down_button'] = str(self.shift_down_button.get())
self.config['SteeringWheel']['custom_button_1'] = str(self.custom_button_1.get())
self.config['SteeringWheel']['custom_button_2'] = str(self.custom_button_2.get())
self.config['SteeringWheel']['custom_button_3'] = str(self.custom_button_3.get())
self.config['SteeringWheel']['custom_button_4'] = str(self.custom_button_4.get())
if save_arduino:
# Save Arduino blocks
self.save_arduino_blocks()
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)
def save_arduino_blocks(self):
if 'Arduino' not in self.config.sections():
self.config.add_section('Arduino')
else:
self.config.remove_section('Arduino')
self.config.add_section('Arduino')
for idx, block in enumerate(self.device_blocks):
block_config = {
'type': block['type'],
'name': block['name']
}
if 'listbox_var' in block:
block_config['listbox_var'] = block['listbox_var'].get()
if 'listbox_var1' in block:
block_config['listbox_var1'] = block['listbox_var1'].get()
if 'listbox_var2' in block:
block_config['listbox_var2'] = block['listbox_var2'].get()
if 'assigned_output' in block:
block_config['assigned_output'] = block['assigned_output']
if 'line1_prefix' in block:
block_config['line1_prefix'] = block['line1_prefix'].get()
if 'line1_suffix' in block:
block_config['line1_suffix'] = block['line1_suffix'].get()
if 'line2_prefix' in block:
block_config['line2_prefix'] = block['line2_prefix'].get()
if 'line2_suffix' in block:
block_config['line2_suffix'] = block['line2_suffix'].get()
if 'tone_entry' in block:
block_config['tone_entry'] = block['tone_entry'].get()
if 'r_entry' in block:
block_config['r_entry'] = block['r_entry'].get()
if 'g_entry' in block:
block_config['g_entry'] = block['g_entry'].get()
if 'b_entry' in block:
block_config['b_entry'] = block['b_entry'].get()
self.config['Arduino'][f'block_{idx}'] = str(block_config)
with open(self.config_file, 'w') as configfile:
self.config.write(configfile)
def load_arduino_blocks(self):
if 'Arduino' in self.config.sections():
print("Arduino section found in config")
arduino_keys = sorted(self.config['Arduino'].keys())
print(f"Arduino keys: {arduino_keys}")
for key in arduino_keys:
try:
if key in self.config['Arduino']:
block_data = self.config['Arduino'][key]
print(f"Loading block: {key} with data: {block_data}")
block_config = ast.literal_eval(block_data)
block_type = block_config.pop('type')
block_name = block_config.pop('name')
print(f"Creating block: {block_type} with name: {block_name}")
# Temporarily disable trace while loading configuration
disable_trace = True
self.add_device_block(block_type, save_config=False, disable_trace=disable_trace)
block = self.device_blocks[-1]
for config_key, config_value in block_config.items():
print(f"Setting {config_key} to {config_value} in block {block_name}")
if config_key == 'listbox_var':
block['listbox_var'].set(config_value)
elif config_key == 'listbox_var1':
block['listbox_var1'].set(config_value)
elif config_key == 'listbox_var2':
block['listbox_var2'].set(config_value)
elif config_key == 'assigned_output':
block['assigned_output'] = config_value
block['frame'].children['!label2'].config(text=config_value)
elif config_key == 'line1_prefix':
block['line1_prefix'].delete(0, tk.END) # Clear any default text
block['line1_prefix'].insert(0, config_value)
elif config_key == 'line1_suffix':
block['line1_suffix'].delete(0, tk.END) # Clear any default text
block['line1_suffix'].insert(0, config_value)
elif config_key == 'line2_prefix':
block['line2_prefix'].delete(0, tk.END) # Clear any default text
block['line2_prefix'].insert(0, config_value)
elif config_key == 'line2_suffix':
block['line2_suffix'].delete(0, tk.END) # Clear any default text
block['line2_suffix'].insert(0, config_value)
elif config_key == 'tone_entry':
block['tone_entry'].delete(0, tk.END) # Clear any default text
block['tone_entry'].insert(0, config_value)
elif config_key == 'r_entry':
block['r_entry'].delete(0, tk.END) # Clear any default text
block['r_entry'].insert(0, config_value)
elif config_key == 'g_entry':
block['g_entry'].delete(0, tk.END) # Clear any default text
block['g_entry'].insert(0, config_value)
elif config_key == 'b_entry':
block['b_entry'].delete(0, tk.END) # Clear any default text
block['b_entry'].insert(0, config_value)
# Re-enable trace after loading configuration
disable_trace = False
self.reenable_trace(block)
else:
print(f"Key {key} not found in Arduino section")
except KeyError as ke:
print(f"KeyError loading Arduino block {key}: {ke}")
except SyntaxError as se:
print(f"SyntaxError loading Arduino block {key}: {se}")
except Exception as e:
print(f"Error loading Arduino block {key}: {e}")
def reenable_trace(self, block):
if 'listbox_var' in block:
block['listbox_var'].trace_add("write", lambda *args: self.save_config(save_arduino=True))
if 'listbox_var1' in block:
block['listbox_var1'].trace_add("write", lambda *args: self.save_config(save_arduino=True))
if 'listbox_var2' in block:
block['listbox_var2'].trace_add("write", lambda *args: self.save_config(save_arduino=True))
if 'line1_prefix' in block:
block['line1_prefix'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'line1_suffix' in block:
block['line1_suffix'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'line2_prefix' in block:
block['line2_prefix'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'line2_suffix' in block:
block['line2_suffix'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'tone_entry' in block:
block['tone_entry'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'r_entry' in block:
block['r_entry'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'g_entry' in block:
block['g_entry'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
if 'b_entry' in block:
block['b_entry'].bind("<FocusOut>", lambda e: self.save_config(save_arduino=True))
def get_system_theme(self):
try:
if darkdetect.isDark():
return 'dark'
else:
return 'light'
except Exception as e:
print(e)
return 'light'
def switch_to_dark_theme(self):
self.root.tk.call("set_theme", "dark")
self.mode = "dark"
def switch_to_light_theme(self):
self.root.tk.call("set_theme", "light")
self.mode = "light"
def check_for_updates(self):
if self.suppress_update_notification:
logging.debug("Update check is suppressed by user settings.")
return
try:
logging.debug("Checking for updates...")
response = requests.get("https://dilerfeed.github.io/Stormworks-Connect/version.json")
logging.debug(f"Response status code: {response.status_code}")
if response.status_code == 200:
version_info = response.json()
latest_version = version_info["version"]
download_url = version_info["download_url"]
logging.debug(f"Latest version available: {latest_version}")
logging.debug(f"Current version: {self.current_version}")
if version.parse(latest_version) > version.parse(self.current_version):
logging.debug("A new version is available. Showing update notification.")
self.show_update_notification(download_url)
else:
logging.debug("No new version available.")
else:
logging.error(f"Failed to fetch update information. Status code: {response.status_code}")
except Exception as e:
logging.error(f"Error checking for updates: {e}")
def show_update_notification(self, download_url):
update_window = tk.Toplevel(self.root)
update_window.title("Update Available")
update_window.geometry("350x250")
label = ttk.Label(update_window, text="A new version of the program is available!")
label.pack(pady=10)
download_button = ttk.Button(update_window, text="Download", command=lambda: self.open_download_link(download_url))
download_button.pack(pady=5)
def close_and_remember():
self.config['Settings']['suppress_update_notification'] = 'True'
self.suppress_update_notification = True
self.save_config()
update_window.destroy()
close_button = ttk.Button(update_window, text="Close", command=update_window.destroy)
close_button.pack(pady=5)
suppress_button = ttk.Button(update_window, text="Don't remind me again", command=close_and_remember)
suppress_button.pack(pady=5)
def create_custom_button(self, parent, text, command):
# Creating a custom button using a regular tkinter button
button = tk.Button(parent, text=text, command=command, bg='gold', fg='black', font=('Arial', 16, 'bold'),
activebackground='black', activeforeground='gold', relief='flat', borderwidth=2)
button.pack(pady=20, padx=20)
def on_enter(event):
button.config(bg='black', fg='gold')
def on_leave(event):
button.config(bg='gold', fg='black')
button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
return button
def create_widgets(self):
# Frames for different tabs
self.tab_control = ttk.Notebook(self.root)
self.image_transmit_frame = ttk.Frame(self.tab_control)
self.steering_wheel_frame = ttk.Frame(self.tab_control)
self.arduino_frame = ttk.Frame(self.tab_control) # Add new frame for Arduino settings
self.info_frame = ttk.Frame(self.tab_control)
self.tab_control.add(self.image_transmit_frame, text=" Image transmit", image=self.image_transmit_icon, compound='left')
self.tab_control.add(self.steering_wheel_frame, text=" Steering wheel", image=self.steering_wheel_icon, compound='left')
self.tab_control.add(self.arduino_frame, text=" Arduino", image=self.arduino_icon, compound='left') # Add Arduino tab
self.tab_control.add(self.info_frame, text=" Info", image=self.info_icon, compound='left')
self.tab_control.pack(expand=1, fill="both")
# Frame for image and gif transmit sections
image_gif_frame = ttk.Frame(self.image_transmit_frame)
image_gif_frame.pack(expand=1, fill="both")
# Image transmit section
image_frame = ttk.Frame(image_gif_frame)
image_frame.pack(side="left", fill="both", expand=True, padx=(10, 5))
ttk.Label(image_frame, text="Image Transmit", font=("Arial", 14)).pack(pady=10)
self.monitor_size_var = tk.StringVar(value="9x5")
monitor_size_menu = ttk.Combobox(image_frame, textvariable=self.monitor_size_var, values=list(self.monitor_sizes.keys()), state="readonly")
monitor_size_menu.bind("<<ComboboxSelected>>", self.on_monitor_size_change)
monitor_size_menu.pack(pady=10)
self.fill_var = tk.BooleanVar(value=False)
fill_checkbutton = ttk.Checkbutton(image_frame, text="Fill", variable=self.fill_var, command=self.on_fill_option_change)
fill_checkbutton.pack(pady=5)
upload_button = ttk.Button(image_frame, text="Upload image", command=self.open_file)
upload_button.pack(pady=10)
ttk.Label(image_frame, text="or upload image from URL:", font=("Arial", 10)).pack(pady=5)
self.image_url_var = tk.StringVar()
image_url_entry = ttk.Entry(image_frame, textvariable=self.image_url_var, width=50)
image_url_entry.pack(pady=5)
load_image_button = ttk.Button(image_frame, text="Upload image from URL", command=self.load_image_from_url_click)
load_image_button.pack(pady=5)
self.image_label = ttk.Label(image_frame)
self.image_label.pack(pady=10)
self.status_label = ttk.Label(image_frame, text="", font=("Arial", 12))
self.status_label.pack(pady=5)
# Separator (vertical)
ttk.Separator(image_gif_frame, orient='vertical').pack(side="left", fill='y', padx=5)
# GIF transmit section
gif_frame = ttk.Frame(image_gif_frame)
gif_frame.pack(side="left", fill="both", expand=True, padx=(5, 10))
ttk.Label(gif_frame, text="GIF Transmit", font=("Arial", 14)).pack(pady=10)
self.gif_monitor_size_var = tk.StringVar(value="1x1")
gif_monitor_size_menu = ttk.Combobox(gif_frame, textvariable=self.gif_monitor_size_var, values=list(self.monitor_sizes.keys()), state="readonly")
gif_monitor_size_menu.bind("<<ComboboxSelected>>", self.on_gif_monitor_size_change)
gif_monitor_size_menu.pack(pady=10)
self.gif_fill_var = tk.BooleanVar(value=False)
gif_fill_checkbutton = ttk.Checkbutton(gif_frame, text="Fill GIF", variable=self.gif_fill_var, command=self.on_gif_fill_option_change)
gif_fill_checkbutton.pack(pady=5)
upload_gif_button = ttk.Button(gif_frame, text="Upload GIF", command=self.open_gif_file)
upload_gif_button.pack(pady=10)
ttk.Label(gif_frame, text="or upload GIF from URL:", font=("Arial", 10)).pack(pady=5)
self.gif_url_var = tk.StringVar()
gif_url_entry = ttk.Entry(gif_frame, textvariable=self.gif_url_var, width=50)
gif_url_entry.pack(pady=5)
load_gif_button = ttk.Button(gif_frame, text="Upload GIF from URL", command=self.load_gif_from_url_click)
load_gif_button.pack(pady=5)
self.gif_label = ttk.Label(gif_frame)
self.gif_label.pack(pady=10)
self.gif_status_label = ttk.Label(gif_frame, text="", font=("Arial", 12))
self.gif_status_label.pack(pady=5)
# Call method to create Arduino tab widgets
self.create_arduino_widgets()
# Info tab with two sections: Info and Features
info_features_frame = ttk.Frame(self.info_frame)
info_features_frame.pack(expand=1, fill="both")
# Info section
info_frame = ttk.Frame(info_features_frame)
info_frame.pack(side="left", fill="both", expand=True, padx=(10, 5))
ttk.Label(info_frame, text="Info", font=("Arial", 14)).pack(pady=10)
server_status = ttk.Label(info_frame, text="Server is running on port 5000", foreground="green", font=("Arial", 12))
server_status.pack(pady=5)
program_title = ttk.Label(info_frame, text="Stormworks Connect v1.3.0.0", font=("Arial", 16))
program_title.pack(pady=5)
author_info = ttk.Label(info_frame, text="© Hlib Ishchenko 2024", font=("Arial", 10))
author_info.pack(pady=5)
steam_profile = ttk.Label(info_frame, text="Steam Profile", foreground="blue", cursor="hand2", font=("Arial", 12))
steam_profile.pack(pady=5)
steam_profile.bind("<Button-1>", self.open_steam_profile)
github_profile = ttk.Label(info_frame, text="GitHub Profile", foreground="blue", cursor="hand2", font=("Arial", 12))
github_profile.pack(pady=5)
github_profile.bind("<Button-1>", self.open_github_profile)
ttk.Button(info_frame, text="Switch to Dark Theme", command=self.switch_to_dark_theme).pack(pady=10)
ttk.Button(info_frame, text="Switch to Light Theme", command=self.switch_to_light_theme).pack(pady=10)
# Separator (vertical)
ttk.Separator(info_features_frame, orient='vertical').pack(side="left", fill='y', padx=5)
# Features section
features_frame = ttk.Frame(info_features_frame)
features_frame.pack(side="left", fill="both", expand=True, padx=(5, 10))
ttk.Label(features_frame, text="Features", font=("Arial", 14)).pack(pady=10)
features = [
("Image Transmit", "https://steamcommunity.com/sharedfiles/filedetails/?id=3256896125"),
("GIF Transmit", "https://steamcommunity.com/sharedfiles/filedetails/?id=3262978714"),
("Steering Wheel Support", "https://steamcommunity.com/sharedfiles/filedetails/?id=3261140680"),
("In-game text web browser", "https://steamcommunity.com/sharedfiles/filedetails/?id=3267509700"),
("Arduino Support", "https://steamcommunity.com/sharedfiles/filedetails/?id=3288246525"),
]
for feature, link in features:
feature_label = ttk.Label(features_frame, text=f"• {feature}", foreground="blue", cursor="hand2", font=("Arial", 12))
feature_label.pack(pady=2, anchor='w')
feature_label.bind("<Button-1>", lambda e, url=link: webbrowser.open_new(url))
small_notice = ttk.Label(features_frame, text="Clicking on a feature will open the relevant controller page in the browser.", font=("Arial", 8))
small_notice.pack(pady=5, anchor='w')
# Steering wheel tab
ttk.Label(self.steering_wheel_frame, text="Select Joystick:").grid(row=0, column=0, padx=10, pady=5, sticky="w")
self.joystick_menu = ttk.Combobox(self.steering_wheel_frame, textvariable=self.joystick_name, state="readonly")
self.joystick_menu.grid(row=0, column=1, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, text="Current Steering Angle:").grid(row=1, column=0, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, textvariable=self.steering_angle).grid(row=1, column=1, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, text="Current Gas Pedal:").grid(row=2, column=0, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, textvariable=self.gas_pedal).grid(row=2, column=1, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, text="Current Brake Pedal:").grid(row=3, column=0, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, textvariable=self.brake_pedal).grid(row=3, column=1, padx=10, pady=5, sticky="w")
ttk.Label(self.steering_wheel_frame, text="Deadzone:").grid(row=4, column=0, padx=10, pady=5, sticky="w")
deadzone_scale = ttk.Scale(self.steering_wheel_frame, from_=0, to=0.2, orient=tk.HORIZONTAL, variable=self.deadzone, command=lambda x: self.save_config())
deadzone_scale.grid(row=4, column=1, padx=10, pady=5, sticky="w")
deadzone_entry = ttk.Entry(self.steering_wheel_frame, textvariable=self.deadzone)
deadzone_entry.grid(row=4, column=2, padx=10, pady=5, sticky="w")
self.swap_pedals_checkbutton = ttk.Checkbutton(self.steering_wheel_frame, text="Swap Gas and Brake Pedals", variable=self.swap_pedals, command=self.save_config)
self.swap_pedals_checkbutton.grid(row=5, column=0, padx=10, pady=5, sticky="w")
ttk.Checkbutton(self.steering_wheel_frame, text="Combined Pedals", variable=self.combined_pedals, command=lambda: [self.update_swap_pedals_state(), self.save_config()]).grid(row=5, column=1, padx=10, pady=5, sticky="w")
self.create_joystick_buttons()
# Add labels for shift and custom button statuses
self.create_joystick_button("Shift Up Button", 6, self.shift_up_button, self.shift_up_status)
self.create_joystick_button("Shift Down Button", 7, self.shift_down_button, self.shift_down_status)
self.create_joystick_button("Custom Button 1", 8, self.custom_button_1, self.custom_button_1_status)
self.create_joystick_button("Custom Button 2", 9, self.custom_button_2, self.custom_button_2_status)
self.create_joystick_button("Custom Button 3", 10, self.custom_button_3, self.custom_button_3_status)
self.create_joystick_button("Custom Button 4", 11, self.custom_button_4, self.custom_button_4_status)
def create_arduino_widgets(self):
# Create a frame for the Arduino tab
arduino_tab_frame = ttk.Frame(self.arduino_frame)
arduino_tab_frame.pack(expand=1, fill="both")
# Add a '+' button to add new device blocks
add_device_button = ttk.Button(arduino_tab_frame, text="+", command=self.show_device_selection)
add_device_button.pack(pady=5)
self.arduino_warning_placeholder = tk.Frame(arduino_tab_frame)
self.arduino_warning_placeholder.pack(pady=5)
# Create a canvas and a vertical scrollbar
canvas = tk.Canvas(arduino_tab_frame)
scrollbar = ttk.Scrollbar(arduino_tab_frame, orient="vertical", command=canvas.yview)
self.device_blocks_frame = ttk.Frame(canvas)
# Bind the device_blocks_frame width to the canvas width
self.device_blocks_frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.bind("<Configure>", lambda e: canvas.itemconfig('frame', width=canvas.winfo_width()))
canvas.create_window((0, 0), window=self.device_blocks_frame, anchor="nw", tags='frame')
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# List to hold references to device blocks
self.device_blocks = []
def create_empty_icon(self, size):
return ImageTk.PhotoImage(Image.new("RGBA", size, (255, 255, 255, 0)))
def show_device_selection(self):
# Open a new window to select the device type
selection_window = Toplevel(self.root)
selection_window.title("Select Device")
selection_window.geometry("300x375") # Adjusted window size
# List of available devices
devices = ["LED", "Button", "Potentiometer", "Active Buzzer", "4 digit 7-segment", "1 digit 7-segment"]
# Use Treeview instead of Listbox
device_tree = ttk.Treeview(selection_window, show="tree")
device_tree.pack(padx=10, pady=10, fill="both", expand=True)
# Increase the font size for Treeview items
style = ttk.Style()
style.configure("Treeview", font=("Arial", 12)) # Set the font size to 16
# Create empty icon
self.empty_icon = self.create_empty_icon((24, 24))
# Add devices to Treeview
for device in devices:
device_tree.insert("", "end", text=device, image=self.empty_icon)
# Align the text and image to be next to each other
style.configure("Treeview", rowheight=24, font=("Arial", 12)) # Ensure the row height matches the icon
# Apply zebra-like pattern manually
for index, item in enumerate(device_tree.get_children()):
if 'disabled' in device_tree.item(item, 'tags'):
bg_color = 'darkgray' if index % 2 == 0 else 'black'
device_tree.tag_configure('disabled', background=bg_color)
def on_select_device():
selected_item = device_tree.selection()
if selected_item:
selected_device = device_tree.item(selected_item, "text")
self.add_device_block(selected_device)
selection_window.destroy()
select_button = ttk.Button(selection_window, text="Select", command=on_select_device)
select_button.pack(pady=5)
# Bind the treeview select event
device_tree.bind("<ButtonRelease-1>", lambda event: device_tree.selection()) # just select the element
# Custom tag configuration for aligning text and image
style.configure("Treeview.Item", compound="right") # Align image to the right of the text
def get_next_device_index(self, device_type):
# Get all current indices for the specified device type
current_indices = [int(device['name'].split()[-1]) for device in self.device_blocks if device['type'] == device_type]
# Find the smallest available index
index = 1
while index in current_indices:
index += 1
return index
def get_used_outputs(self):
used_boolean_outputs = set()
used_number_outputs = set()
for device in self.device_blocks:
if 'listbox_var' in device:
if 'Boolean Output' in device['listbox_var'].get():
used_boolean_outputs.add(device['listbox_var'].get())
elif 'Number Output' in device['listbox_var'].get():
used_number_outputs.add(device['listbox_var'].get())
elif 'listbox_vars' in device:
for var in device['listbox_vars']:
if 'Boolean Output' in var.get():
used_boolean_outputs.add(var.get())
elif 'Number Output' in var.get():
used_number_outputs.add(var.get())
elif 'assigned_output' in device:
if 'Boolean Output' in device['assigned_output']:
used_boolean_outputs.add(device['assigned_output'])
elif 'Number Output' in device['assigned_output']:
used_number_outputs.add(device['assigned_output'])
return used_boolean_outputs, used_number_outputs
def add_device_block(self, device_type, save_config=True, disable_trace=False):
if len(self.device_blocks) >= 16:
return # Limit to 16 blocks
# Get the next available index for the device type
device_index = self.get_next_device_index(device_type)
# Create a new frame for the device block
device_block_frame = ttk.Frame(self.device_blocks_frame, relief="raised", borderwidth=1)
device_block_frame.pack(fill="x", pady=5, padx=10)
# Configure grid layout for device_block_frame
device_block_frame.columnconfigure(0, weight=0)
device_block_frame.columnconfigure(1, weight=0)
device_block_frame.columnconfigure(2, weight=1) # This allows the frame to expand
device_block_frame.columnconfigure(3, weight=0)
device_block_frame.columnconfigure(4, weight=0)
device_block_frame.columnconfigure(5, weight=0)
device_block_frame.columnconfigure(6, weight=0)
device_block_frame.columnconfigure(7, weight=0)
device_block_frame.columnconfigure(8, weight=0)
device_block_frame.columnconfigure(9, weight=0)
# Add label for the device name and index
device_name_label = ttk.Label(device_block_frame, text=f"{device_type} {device_index}")
device_name_label.grid(row=0, column=0, rowspan=1, padx=5, pady=5, sticky="w")
# Add vertical separator
separator = ttk.Separator(device_block_frame, orient='vertical')
separator.grid(row=0, column=1, rowspan=1, sticky="ns", padx=2, pady=5)
# Add delete button for the device block
delete_button = ttk.Button(device_block_frame, text="-", width=2, command=lambda: self.delete_device_block(device_block_frame))
delete_button.grid(row=0, column=9, rowspan=2 if device_type == "LCD" else 1, padx=(0, 5), pady=5, sticky="e")
# Create a dictionary to store references to the widgets
block_widgets = {
"frame": device_block_frame,
"type": device_type,
"name": f"{device_type} {device_index}"
}
# Add functionality for each device type
if device_type == "LED":
status_label = ttk.Label(device_block_frame, text="Status: inactive")
status_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["status_label"] = status_label
separator1 = ttk.Separator(device_block_frame, orient='vertical')
separator1.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
test_button = ttk.Button(device_block_frame, text="Test", command=lambda: self.test_led(status_label, device_index))
test_button.grid(row=0, column=4, padx=(0, 5), pady=5, sticky="e")
separator2 = ttk.Separator(device_block_frame, orient='vertical')
separator2.grid(row=0, column=5, padx=2, pady=5, sticky="ns")
elif device_type == "Button":
status_label = ttk.Label(device_block_frame, text="Status: not pressed")
status_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["status_label"] = status_label
separator = ttk.Separator(device_block_frame, orient='vertical')
separator.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
elif device_type == "Potentiometer":
status_label = ttk.Label(device_block_frame, text="Value: 0")
status_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["value_label"] = status_label
separator = ttk.Separator(device_block_frame, orient='vertical')
separator.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
elif device_type == "Active Buzzer":
status_label = ttk.Label(device_block_frame, text="Status: inactive")
status_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["status_label"] = status_label
separator1 = ttk.Separator(device_block_frame, orient='vertical')
separator1.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
test_button = ttk.Button(device_block_frame, text="Test", command=lambda: self.test_buzzer(status_label, None, device_index, active=True))
test_button.grid(row=0, column=4, padx=(0, 5), pady=5, sticky="e")
separator2 = ttk.Separator(device_block_frame, orient='vertical')
separator2.grid(row=0, column=5, padx=2, pady=5, sticky="ns")
elif device_type == "4 digit 7-segment":
value_label = ttk.Label(device_block_frame, text="Value: 0")
value_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["value_label"] = value_label
separator = ttk.Separator(device_block_frame, orient='vertical')
separator.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
elif device_type == "1 digit 7-segment":
value_label = ttk.Label(device_block_frame, text="Value: 0")
value_label.grid(row=0, column=2, padx=(0, 5), pady=5, sticky="w")
block_widgets["value_label"] = value_label
separator = ttk.Separator(device_block_frame, orient='vertical')
separator.grid(row=0, column=3, padx=2, pady=5, sticky="ns")
# Add label for displaying assigned output/input
used_boolean_outputs, used_number_outputs = self.get_used_outputs()
if device_type in ["LED", "Active Buzzer"]:
io_options = ["Boolean Input 1", "Boolean Input 2", "Boolean Input 3", "Boolean Input 4"]
listbox_var = tk.StringVar(value=io_options[0])
listbox = ttk.Combobox(device_block_frame, textvariable=listbox_var, values=io_options, state="readonly")
listbox.grid(row=0, column=8, padx=(0, 5), pady=5, sticky="e")
block_widgets["listbox_var"] = listbox_var
if not disable_trace:
listbox_var.trace_add("write", lambda *args: self.save_config(save_arduino=True))
elif device_type in ["Button"]:
io_options = ["Boolean Output 1", "Boolean Output 2", "Boolean Output 3", "Boolean Output 4"]
available_outputs = [output for output in io_options if output not in used_boolean_outputs]
if not available_outputs:
tk.messagebox.showerror("Error", "All Boolean Outputs are used.")
device_block_frame.destroy()
return
assigned_output = available_outputs[0]
output_label = ttk.Label(device_block_frame, text=assigned_output)
output_label.grid(row=0, column=8, padx=(0, 5), pady=5, sticky="e")
block_widgets["assigned_output"] = assigned_output
# If there's a UI element to change assigned_output, add the save_config call there
elif device_type in ["Potentiometer"]:
io_options = ["Number Output 1", "Number Output 2", "Number Output 3", "Number Output 4"]
available_outputs = [output for output in io_options if output not in used_number_outputs]
if not available_outputs:
tk.messagebox.showerror("Error", "All Number Outputs are used.")
device_block_frame.destroy()
return
assigned_output = available_outputs[0]
output_label = ttk.Label(device_block_frame, text=assigned_output)
output_label.grid(row=0, column=8, padx=(0, 5), pady=5, sticky="e")
block_widgets["assigned_output"] = assigned_output
# If there's a UI element to change assigned_output, add the save_config call there
elif device_type in ["4 digit 7-segment", "1 digit 7-segment"]:
io_options = ["Number Input 1", "Number Input 2", "Number Input 3", "Number Input 4"]
listbox_var = tk.StringVar(value=io_options[0])
listbox = ttk.Combobox(device_block_frame, textvariable=listbox_var, values=io_options, state="readonly")
listbox.grid(row=0, column=4, padx=(0, 5), pady=5, sticky="e")
block_widgets["listbox_var"] = listbox_var
if not disable_trace:
listbox_var.trace_add("write", lambda *args: self.save_config(save_arduino=True))
# Store the device block information
self.device_blocks.append(block_widgets)
if save_config:
self.save_config(save_arduino=True)
def delete_device_block(self, device_block_frame):
for device in self.device_blocks:
if device['frame'] == device_block_frame:
if 'assigned_output' in device:
assigned_output = device['assigned_output']
if 'Boolean Output' in assigned_output:
used_boolean_outputs = self.get_used_outputs()[0]
used_boolean_outputs.remove(assigned_output)
elif 'Number Output' in assigned_output:
used_number_outputs = self.get_used_outputs()[1]
used_number_outputs.remove(assigned_output)
self.device_blocks.remove(device)
device_block_frame.destroy()
self.save_config(save_arduino=True)
break
def read_arduino_data(self):
while True:
data = self.arduino_handler.read_from_arduino()
if data:
self.process_arduino_data(data)
def process_arduino_data(self, data):
if data.startswith("BUTTON"):
button_index = int(data.split(":")[0][-1])
status = data.split(":")[1]
for block in self.device_blocks:
if block['type'] == 'Button' and block['name'].endswith(str(button_index)):
assigned_output = block['assigned_output']
output_index = int(assigned_output.split()[-1]) - 1
self.boolean_outputs[output_index] = (status == "PRESSED")
# Reset the button value after a second if a new value does not arrive
if hasattr(self, f"button_timer_{button_index}"):
self.root.after_cancel(getattr(self, f"button_timer_{button_index}"))
setattr(self, f"button_timer_{button_index}", self.root.after(1000, lambda: self.reset_button_output(output_index)))
break
elif data.startswith("POTENTIOMETER"):
pot_index = int(data.split(":")[0][-1])
value = int(data.split(":")[1])
for block in self.device_blocks:
if block['type'] == 'Potentiometer' and block['name'].endswith(str(pot_index)):
assigned_output = block['assigned_output']
output_index = int(assigned_output.split()[-1]) - 1
self.digital_outputs[output_index] = value
break
def reset_button_output(self, output_index):
self.boolean_outputs[output_index] = False
def reset_keypad_output(self, output_index):
self.digital_outputs[output_index] = 0
def reset_button_and_keypad_states(self):
for block in self.device_blocks:
if block['type'] == 'Button':
assigned_output = block['assigned_output']
output_index = int(assigned_output.split()[-1]) - 1
self.boolean_outputs[output_index] = False
def test_led(self, status_label, device_index):
self.arduino_handler.send_to_arduino(f"LED{device_index}:1")
status_label.config(text="Status: active")
self.root.after(1000, lambda: self.arduino_handler.send_to_arduino(f"LED{device_index}:0"))
self.root.after(1000, lambda: status_label.config(text="Status: inactive"))
def test_buzzer(self, status_label, tone_entry=None, device_index=None, active=False):
if active:
self.arduino_handler.send_to_arduino(f"ACTIVE_BUZZER{device_index}:1")
status_label.config(text="Status: active")
self.root.after(1000, lambda: self.arduino_handler.send_to_arduino(f"ACTIVE_BUZZER{device_index}:0"))
self.root.after(1000, lambda: status_label.config(text="Status: inactive"))
def update_device_blocks(self):
for block in self.device_blocks:
if block['type'] == 'LED':
assigned_input = block["listbox_var"].get()
index = int(assigned_input.split()[-1]) - 1
status = 'active' if self.boolean_inputs[index] else 'inactive'
block['status_label'].config(text=f"Status: {status}")
self.arduino_handler.send_to_arduino(f"LED{block['name'].split()[-1]}:{1 if self.boolean_inputs[index] else 0}")
elif block['type'] == 'Button':
assigned_output = block["assigned_output"]
index = int(assigned_output.split()[-1]) - 1
status = 'pressed' if self.boolean_outputs[index] else 'not pressed'
block['status_label'].config(text=f"Status: {status}")
elif block['type'] == 'Potentiometer':
assigned_output = block["assigned_output"]
index = int(assigned_output.split()[-1]) - 1
block['value_label'].config(text=f"Value: {self.digital_outputs[index]}")
elif block['type'] == 'Active Buzzer':
assigned_input = block["listbox_var"].get()
index = int(assigned_input.split()[-1]) - 1
status = 'active' if self.boolean_inputs[index] else 'inactive'
block['status_label'].config(text=f"Status: {status}")
self.arduino_handler.send_to_arduino(f"ACTIVE_BUZZER{block['name'].split()[-1]}:{1 if self.boolean_inputs[index] else 0}")
elif block['type'] == '4 digit 7-segment':
assigned_input = block["listbox_var"].get()
index = int(assigned_input.split()[-1]) - 1
value = self.digital_inputs[index]
# Converting the value for display
if value >= 0:
if value >= 1000:
display_value = f"{int(value)}" # Leave only the whole part
elif value >= 100:
display_value = f"{value:.1f}" # Leave one digit after the dot
else:
display_value = f"{value:.2f}" # Leave two digits after the dot
else:
if value <= -100:
display_value = f"{int(value)}"
elif value <= -10:
display_value = f"{value:.1f}"
else:
display_value = f"{value:.2f}"
block['value_label'].config(text=f"Value: {display_value}")
self.arduino_handler.send_to_arduino(f"4SEGMENT{block['name'].split()[-1]}:{display_value}")