-
Notifications
You must be signed in to change notification settings - Fork 4
/
EcuZoneLineEdit.py
184 lines (163 loc) · 6.14 KB
/
EcuZoneLineEdit.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
"""
EcuZoneLineEdit.py
Copyright (C) 2024 - 2025 Marc Postema (mpostema09 -at- gmail.com)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
"""
import json
from PySide6.QtGui import QKeyEvent
from PySide6.QtCore import Qt, QEvent
from PySide6.QtWidgets import QLineEdit
class EcuZoneLineEdit(QLineEdit):
"""
"""
zoneObject = {}
valueType = ""
initialValue = ""
initialRaw = ""
def __init__(self, parent, zoneObject: dict, readOnly: bool):
super(EcuZoneLineEdit, self).__init__(parent)
self.setReadOnly(readOnly)
self.zoneObject = zoneObject
def event(self, event: QEvent):
if event.type() == QEvent.KeyPress:
keyEvent = QKeyEvent(event)
# When ESC -> Clear focus
if keyEvent.key() == Qt.Key_Escape:
# @TODO: Maybe give option to undo changes?
self.clearFocus()
return True
return super().event(event)
def getCorrespondingByte(self):
return self.zoneObject["byte"]
def __setText(self, val):
self.initialValue = val;
super().setText(val)
def updateText(self, val):
super().setText(val)
def isLineEditChanged(self):
return self.isEnabled() and self.initialValue != self.text()
def getValuesAsCSV(self):
value = "Disabled"
if self.isEnabled():
if self.valueType == "string_ascii":
value = self.text().encode().hex()
elif self.valueType == "string_date":
value = self.initialRaw
elif self.valueType == "int":
value = "%0.2X" % int(self.text())
else:
value = self.text()
return value
def getZoneAndHex(self):
value = "None"
if self.isLineEditChanged():
if self.valueType == "string_ascii":
value = self.text().encode().hex()
elif self.valueType == "int":
value = "%0.2X" % int(self.text())
else:
value = self.text()
return value
def __shift(self, mask: int):
# Code snippet by Sean Eron Anderson
v = mask
c = 32
v &= -v
if v:
c -= 1
if v & 0x0000FFFF:
c -= 16
if v & 0x00FF00FF:
c -= 8
if v & 0x0F0F0F0F:
c -= 4
if v & 0x33333333:
c -= 2
if v & 0x55555555:
c -= 1
return c
def update(self, byte: str):
if "mask" in self.zoneObject:
text = self.text()
mask = int(self.zoneObject["mask"], 2)
if text != None and text != "":
newByte = int(text) << self.__shift(mask)
value = (int(byte, 16) & ~mask) | newByte
byte = "%0.2X" % value
return byte
def __convertStringToDate(self, data: str):
self.initialDate = data
day = int(data[0:2], 16)
month = int(data[2:4], 16)
year = int(data[4:6], 16)
# Check if date is not sane, then give ASCII
if year >= 30 or day > 31 or month > 12:
day = data[0:2]
month = data[2:4]
year = data[4:6]
else:
day = ("%0.2d" % int(data[0:2], 16))
month = ("%0.2d" % int(data[2:4], 16))
year = ("%0.2d" % int(data[4:6], 16))
txt = day + "." + month + "." + year
return txt
def changeZoneOption(self, data: str, valueType: str):
self.valueType = valueType
self.initialRaw = data
if "mask" in self.zoneObject:
byteData = []
for i in range(0, len(data), 2):
byteData.append(data[i:i + 2])
byteNr = self.zoneObject["byte"]
mask = int(self.zoneObject["mask"], 2)
byte = 0
if mask < 256:
byte = (int(byteData[byteNr], 16) & mask) >> self.__shift(mask)
else:
print("Bigger")
print(valueType)
self.__setText(str(byte))
elif "byte_range" in self.zoneObject:
byteNr = self.zoneObject["byte"] * 2
ran = self.zoneObject["byte_range"] * 2
txt = data[byteNr:byteNr + ran]
if "type" in self.zoneObject:
if "zi_cal" == self.zoneObject["type"]:
txt = "98" + txt + "80"
elif "zi_sup" == self.zoneObject["type"]:
file = open("./data/ECU_SUPPLIERS.json", 'r', encoding='utf-8')
jsonFile = file.read()
supplierList = json.loads(jsonFile.encode("utf-8"))
if txt in supplierList:
txt = supplierList[str(txt)]
elif "zi_sys" == self.zoneObject["type"]:
txt = txt
elif "string_date" == self.zoneObject["type"]:
txt = self.__convertStringToDate(txt)
self.__setText(txt)
else:
if valueType == "string_ascii":
try:
txt = bytes.fromhex(data).decode("utf-8")
except:
valueType = "string"
txt = data
elif valueType == "string_date":
txt = self.__convertStringToDate(data)
elif valueType == "int":
txt = str(int(data, 16))
else:
txt = data
self.__setText(txt)
return True