-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
282 lines (218 loc) · 9.93 KB
/
main.py
File metadata and controls
282 lines (218 loc) · 9.93 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
# -*- coding: utf-8 -*-
"""
Created on Wednesday July 17th 2024
Based on: https://www.hackster.io/ansh2919/serial-communication-between-python-and-arduino-e7cce0
Modified by: Jorn Steen
Interactive command-line script for communication with an Arduino
in MOD Mode
Using the Adafruit MCP4725 breakout board for digital-to-analog (DAC) conversion
Operation:
- make sure that the correct script is uploaded to the Arduino
(ArduinoCode_DAC_0to5V.ino)
- execute this .py script in the PowerShell
GUI:
- Choose an LED -- the twelvebit_max gets adjusted from 4095 to the corrected
value according to the allowed maximum current of the LED
- IMPORTANT: The Current Limit on the LED Driver should be set to the max (1.2 A)
- Input a percentage using the slider
-- this gets converted to a 12-bit string and sent to the Arduino
!!! TO DO
[] make one function for turnLED_off and turnLED_on
"""
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import (QApplication, QMessageBox, QMainWindow)
from PyQt5.QtCore import pyqtSignal
import serial ## for communication with Arduino COM port
import time
import tools.constants as Constants
import tools.settings as Settings
import tools.functions as Functions # test version
import IrrKin.IrrKin as IrrKin
##############
# Constants.MODE = "TEST" ##!!! TURN OFF WHEN NOT TESTING
##############
############## define Arduino write-read function ##############
MaxCurrents = Constants.MaxCurrents
twelvebit_zero = Constants.twelvebit_zero
twelvebit_max_default = Constants.twelvebit_max_default
MaxCurrent_default = Constants.MaxCurrent_default
########################################################
def AdjustMaxCurrent(LED):
MaxCurrent = MaxCurrents[LED]
fraction = MaxCurrent / MaxCurrent_default
twelvebit_max_thisLED = round(fraction * twelvebit_max_default)
return MaxCurrent, twelvebit_max_thisLED
def percent_to_12bit(twelvebit_max, percent):
fraction = percent / 100
twelvebit_adj = twelvebit_max * fraction
twelvebit_adj_round = round(twelvebit_adj)
return twelvebit_adj_round
########################################################
######################### GUI ##########################
########################################################
class MainWindow(QMainWindow):
close_signal = pyqtSignal()
def __init__(self):
# super().__init__()
super(MainWindow, self).__init__()
uic.loadUi('UIs/MainWindow.ui', self) # Load the UI file you provided
#### Initialize instance variables ####
self.selected_option = None
self.percentage = 0 # start with 0%
Settings.clickedSC = None
Settings.twelvebit_max_thisLED = None
Settings.twelvebit_adjusted = None
self.current = 0
################
#### drop-down menu ####
self.comboBox_LEDs.addItems(list(MaxCurrents.keys()))
self.comboBox_LEDs.currentIndexChanged.connect(self.update_dropdown)
################
#### Create textfield for Percentage ####
self.textEdit_Percentage.setPlainText(str(self.percentage)) #
self.textEdit_Percentage.textChanged.connect(self.update_percentage)
#### slide bar ####
self.horizontalSlider.setMinimum(0)
self.horizontalSlider.setMaximum(100)
self.horizontalSlider.setValue(self.percentage)
self.horizontalSlider.valueChanged.connect(self.update_slider)
################
#### buttons ####
self.pushButton_LED_ON.clicked.connect(self.show_popup_ON)
self.pushButton_LED_OFF.clicked.connect(self.turnLED_OFF)
#### Save, Close and Continue button ####
self.pushButton_SaveContinue.clicked.connect(self.SaveContinue)
#### Cancel button ####
self.pushButton_Cancel.clicked.connect(self.cancel_gui)
################################################
################ INSTANCE VARIABLES ############
################################################
self.initialise_Arduino(Constants.MODE)
self.update_dropdown() # Initial calculation
self.turnLED_OFF() # extra caution
def initialise_Arduino(self, MODE):
""" Start COM port communication with Arduino (unless TEST MODE is on) """
if MODE == "TEST":
### TEST ###
pass
elif MODE == "FORREAL":
Settings.arduino = serial.Serial(port='COM4', baudrate=115200, timeout=.1) ## fibirr laptop
# Settings.arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1) ## my laptop
time.sleep(2) ## need to wait a bit after opening the communication
else:
print("wrong value for MODE")
################################################
################################################
def update_slider(self):
self.percentage = self.horizontalSlider.value()
self.textEdit_Percentage.setPlainText(str(self.percentage)) #
self.update_calculation()
def update_dropdown(self):
self.selected_option = self.comboBox_LEDs.currentText()
self.MaxCurrent, Settings.twelvebit_max_thisLED = AdjustMaxCurrent(self.selected_option) ## use currently selected LED
self.update_label_MaxCurrent()
def update_calculation(self):
Settings.twelvebit_adjusted = str(percent_to_12bit(Settings.twelvebit_max_thisLED,int(self.percentage)))
print(f"update_calculation twelvebit_adjusted: {Settings.twelvebit_adjusted}")
self.current = round((int(self.percentage) / 100) * self.MaxCurrent)
self.update_label_CurrentCurrent()
def update_label_MaxCurrent(self):
self.textEdit_MaxCurrent.setText(str(self.MaxCurrent))
def update_label_CurrentCurrent(self):
if self.current is None:
self.current = ''
self.textEdit_CurrentCurrent.setText(str(self.current))
def update_label_LEDstatus(self):
self.textEdit_LEDstatus.setText(Settings.LEDstatus)
def update_percentage(self):
if self.textEdit_Percentage.toPlainText() == '':
self.current = ''
Settings.twelvebit_adjusted = None
self.update_label_CurrentCurrent()
elif 0 <= int(self.textEdit_Percentage.toPlainText()) <= 100:
self.percentage = int(self.textEdit_Percentage.toPlainText()) # Convert the input to an integer
self.horizontalSlider.setValue(self.percentage)
self.update_calculation()
else:
self.current = "Wrong Percentage"
Settings.twelvebit_adjusted = None
self.update_label_CurrentCurrent()
################
def show_popup_ON(self):
msg = QMessageBox()
msg.setWindowTitle("Please Confirm")
msg.setText("Are you sure you want to turn ON the LED?")
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg.buttonClicked.connect(self.turnLED_ON)
msg.exec_() # Execute the dialog
def turnLED_ON(self, i):
if i.text() == "&Yes":
if Settings.twelvebit_adjusted is None:
QMessageBox.warning(self, "Error", "Wrong input percentage")
return
print("======= MainWindow =======")
Functions.turnLED_ON()
self.update_label_LEDstatus()
else:
Functions.turnLED_OFF()
def turnLED_OFF(self):
print("======= MainWindow =======")
Functions.turnLED_OFF()
self.update_label_LEDstatus()
################
def SaveContinue(self):
""" Save, Close and Continue with IrrKin mode controlled by .ahk script """
if Settings.twelvebit_adjusted is None:
QMessageBox.warning(self, "Error", "Don't forget to choose LED and percentage")
return
else:
QMessageBox.warning(self, "Just so you know", "Saving values, closing GUI, and continuing with IrrKin")
Settings.clickedSC = "YES"
self.close_signal.emit()
self.close() # Call close event
def cancel_gui(self):
""" Cancel button """
print("=== CANCEL BUTTON === Closing application...")
print(f"Settings.clickedSC: {Settings.clickedSC}")
Functions.SetToZero_twelvebitadjusted()
self.close() # Call close event
def closeEvent(self, event):
""" Close event: associated with X button by default"""
print(f"Settings.clickedSC: {Settings.clickedSC}")
Functions.turnLED_OFF()
print("=== Closing application... ===")
################
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = MainWindow()
def on_close():
app.quit()
gui.close_signal.connect(on_close)
gui.show()
app.exec_()
###################################
if Settings.clickedSC is not None:
twelvebit_adjusted = Settings.twelvebit_adjusted
print(f"Script continues with IrrKin part: twelvebit_adjusted = {twelvebit_adjusted}")
#### Continue with the rest of the script using twelvebit_adjusted
################ START ON-OFF LOOP ################
while True:
command = input("Enter a command (on/off/stop/help): ").lower() ## makes all characters non-capitalised
if command == "on":
print("LED is on")
IrrKin.turnLED_ON()
print(f"Cycle: {Settings.count}")
Settings.count+=1
elif command == "off":
print("LED is off")
IrrKin.turnLED_OFF()
elif command == "stop":
print("Turning off LED and stopping the program...")
IrrKin.StopIrrKin()
break ## Exit the loop to stop the program
elif command == "help":
print("Available commands: on/off/stop/help")
else:
print("Invalid command. Type 'help' for a list of commands.")
################################################################################