-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstants.py
349 lines (279 loc) · 10.9 KB
/
constants.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
from tkinter import *
from tkinter import ttk
from collections import OrderedDict
# ALL
# speed constants
# bits 76
uart_parity = OrderedDict([
("8N1" , "00"), # default
("8O1" , "01"),
("8E1" , "10")])
#bits 543
uart_baudrate = OrderedDict([
("1200" , "000"),
("2400" , "001"),
("4800" , "010"),
("9600" , "011"),# default
("19200" , "100"),
("38400" , "101"),
("57600" , "110"),
("115200" , "111")])
#bits 210
air_baudrate = OrderedDict([
("300" , "000"),
("1200" , "001"),
("2400" , "010"), # default
("4800" , "011"),
("9600" , "100"),
("19200" , "101")])
# options constants
# bits 7
transmission_type = OrderedDict([
("TRANSP" , "0"),
("FIXED" , "1")]) # default
# bits 6
resistence_type = OrderedDict([
("WITH" , "1"), # default
("WHITHOUT" , "0")])
#bits 543
wake_time = OrderedDict([
("250ms" , "000" ), # default
("500ms" , "001"),
("1000ms" , "010"),
("1250ms" , "100"),
("1750ms" , "101"),
("2000ms" , "111")])
#bits 2
fec_switch = OrderedDict([
("ON" , "1"), # default
("OFF" , "0")])
#bits 10
power = OrderedDict([
("30dbi" , "00"), # default
("27dbi" , "01"),
("24dbi" , "10"),
("21dbi" , "11")])
def send_hex(h):
print("Sending to uart: ", end="")
for i in h:
print("{:02X}".format(i), end=" ")
print("")
def get_hex(l):
while True:
x = input("Getting data from uart: ")
result = []
try:
for i in x.split():
if len(i) != 2:
raise ValueError
else:
n = int(i, 16)
result.append(n)
result_b = bytearray(result)
return result_b
except ValueError:
print("Invalid Input!")
class Speed:
def __init__(self):
self.air_baud_rate = air_baudrate["2400"]
self.uart_baud_rate = uart_baudrate["9600"]
self.uart_parity_bit = uart_parity["8N1"]
def to_bytes(self):
# al reves pues son little endian
binary_string = self.uart_parity_bit + self.uart_baud_rate + self.air_baud_rate
#binary_string = binary_string[::-1]
b = bytes([int(binary_string, 2)])
return b
def from_bytes(self, bArray):
bString = "{0:0>8b}".format(bArray)
# bString = bString[::-1]
self.uart_parity_bit = bString[0:2]
self.uart_baud_rate = bString[2:5]
self.air_baud_rate = bString[5:8]
class Option:
def __init__(self):
self.fixed_transmission_enbled = transmission_type["TRANSP"]
self.io_resistences = resistence_type["WITH"]
self.wake_time = wake_time["250ms"]
self.fec_switch = fec_switch["ON"]
self.transmission_power = power["30dbi"]
def to_bytes(self):
# al reves pues son little endian
binary_string = self.fixed_transmission_enbled + self.io_resistences + self.wake_time + self.fec_switch + self.transmission_power
# binary_string = binary_string[::-1]
b = bytes([int(binary_string, 2)])
return b
def from_bytes(self, bArray):
bString = "{0:0>8b}".format(bArray)
# bString = bString[::-1]
self.fixed_transmission_enbled = bString[0]
self.io_resistences = bString[1]
self.wake_time = bString[2:5]
self.fec_switch = bString[5]
self.transmission_power = bString[6:8]
class Configuration:
def __init__(self):
self.head = 0xC0
self.addh = 0x00
self.addl = 0x00
self.sped = Speed()
self.chan = 0x17 # entre 0 y 1f
self.option = Option()
def to_bytes(self):
bArray = bytearray([self.head, self.addh, self.addl, self.sped.to_bytes()[0], self.chan, self.option.to_bytes()[0]])
return bArray
def from_bytes(self, bArray):
self.head = int(bArray[0])
self.addh = int(bArray[1])
self.addl = int(bArray[2])
self.sped.from_bytes(bArray[3])
self.chan = int(bArray[4])
self.option.from_bytes(bArray[5])
class LabeledEntry(Frame):
def __init__(self, parent, *args, **kargs):
text = kargs.pop("text")
self.txt = StringVar()
Frame.__init__(self, parent)
vcmd = (parent.register(self.validate),'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.l = Label(self, text=text, justify=LEFT, width=10).grid(sticky = W, column=0, row=0)
self.e =Entry(self, width=10, textvariable=self.txt, validate="key", validatecommand=vcmd)
self.e.grid(sticky = E, column=1, row=0)
def setText(self, txt):
self.txt.set(txt)
def getText(self):
return self.e.get()
def validate(self, action, index, value_if_allowed, prior_value, text, validation_type, trigger_type, widget_name):
#print(value_if_allowed)
if value_if_allowed[:2] == "0x":
try:
x = int(value_if_allowed[2:], 16)
if x > 0xFF:
return False
except ValueError:
return False
return True
else:
return False
class LabeledLabel(Frame):
def __init__(self, parent, *args, **kargs):
text = kargs.pop("text")
self.text2 = StringVar()
Frame.__init__(self, parent)
self.l1 = Label(self, text=text, justify=LEFT).grid(sticky=W, column=0, row=0)
self.l2 = Label(self, textvariable=self.text2).grid(sticky=E, column=1, row=0)
def settext(self, s):
self.text2.set(str(s))
class LabeledComboBox(Frame):
def __init__(self, parent, *args, **kargs):
text = kargs.pop("text")
val = kargs.pop("values")
Frame.__init__(self, parent)
self.l = Label(self, text=text, justify=LEFT, width=10)
self.l.grid(sticky = W, column=0, row=0)
self.c =ttk.Combobox(self,values=val,state="readonly",width=10)
self.c.grid(sticky = E, column=1, row=0)
def current(self, cur):
self.c.current(cur)
def get_current(self):
return self.c.get()
class Config_view(Frame):
def __init__(self, parent, configOBJ, *args, **kwags):
self.config = configOBJ
Frame.__init__(self, parent)
self.head = LabeledComboBox(self, text="HEAD: ", values=["0xC0"])
#self.head.pack(side = LEFT)
self.head.grid(row=0, column=0)
self.addh = LabeledEntry(self, text="ADDH: ")
#self.addh.pack(side = LEFT)
self.addh.grid(row=0, column=1)
self.addl = LabeledEntry(self, text="ADDL: ")
#self.addl.pack(side = LEFT)
self.addl.grid(row=0, column=2)
# sped
self.airBr = LabeledComboBox(self, text="AirBR: ", values=list(air_baudrate))
self.airBr.grid(row=1, column=0)
self.uartBr = LabeledComboBox(self, text="uartBR", values=list(uart_baudrate))
self.uartBr.grid(row=1, column=1)
self.parity = LabeledComboBox(self, text="uartParity", values=list(uart_parity))
self.parity.grid(row=1, column=2)
self.chan = LabeledEntry(self, text="CHAN: ")
# self.chan.pack()
self.chan.grid(row=2, column=0)
# option
self.transmission_power = LabeledComboBox(self, text="Tpower: ", values=list(power))
self.transmission_power.grid(row=3, column=0)
self.fec_switch = LabeledComboBox(self, text="Fec: ", values=list(fec_switch))
self.fec_switch.grid(row=3,column=1)
self.wake_time =LabeledComboBox(self, text="WakeT: ", values=list(wake_time))
self.wake_time.grid(row=3, column=2)
self.io_resistences = LabeledComboBox(self, text="Resistences: ", values=list(resistence_type))
self.io_resistences.grid(row=4, column=0)
self.fixed_transmission_enbled =LabeledComboBox(self, text = "TMode: ", values=list(transmission_type))
self.fixed_transmission_enbled.grid(row=4,column=1)
def ReadData(self):
send_hex(bytes([0xc1] * 3))
x = get_hex(6)
self.config.from_bytes(x)
self.updateGUI()
def updateGUI(self): # put info from config object to view
self.head.current(0) # CONSTANT
addh = self.config.addh
self.addh.setText("0x{:0>2X}".format(addh))
addl = self.config.addl
self.addl.setText("0x{:0>2X}".format(addl))
chan = self.config.chan
self.chan.setText("0x{:0>2X}".format(chan))
# sped
i = self.getIndexFromValue(air_baudrate, self.config.sped.air_baud_rate)
self.airBr.current(i)
i = self.getIndexFromValue(uart_baudrate, self.config.sped.uart_baud_rate)
self.uartBr.current(i)
i = self.getIndexFromValue(uart_parity, self.config.sped.uart_parity_bit)
self.parity.current(i)
#options
i = self.getIndexFromValue(transmission_type, self.config.option.fixed_transmission_enbled)
self.fixed_transmission_enbled.current(i)
i = self.getIndexFromValue(resistence_type, self.config.option.io_resistences)
self.io_resistences.current(i)
i = self.getIndexFromValue(wake_time, self.config.option.wake_time)
self.wake_time.current(i)
i = self.getIndexFromValue(fec_switch, self.config.option.fec_switch)
self.fec_switch.current(i)
i = self.getIndexFromValue(power, self.config.option.transmission_power)
self.transmission_power.current(i)
def getIndexFromValue(self, oDict, val):
r = 0
for k, v in oDict.items():
if v == val:
return r
r += 1
return None
def readGUI(self): # read info from view and put it in config object
addh = self.addh.getText()[2:]
self.config.addh = int(addh, 16)
addl = self.addl.getText()[2:]
self.config.addl = int(addl, 16)
chan = self.chan.getText()[2:]
self.config.chan = int(chan, 16)
# Speed
i = self.airBr.get_current()
self.config.sped.air_baud_rate = air_baudrate[i]
i = self.uartBr.get_current()
self.config.sped.uart_baud_rate = uart_baudrate[i]
i = self.parity.get_current()
self.config.sped.uart_parity_bit = uart_parity[i]
# Options
i = self.fixed_transmission_enbled.get_current()
self.config.option.fixed_transmission_enbled = transmission_type[i]
i = self.io_resistences.get_current()
self.config.option.io_resistences = resistence_type[i]
i = self.wake_time.get_current()
self.config.option.wake_time = wake_time[i]
i = self.fec_switch.get_current()
self.config.option.fec_switch = fec_switch[i]
i = self.transmission_power.get_current()
self.config.option.transmission_power = power[i]
def SendData(self):
self.readGUI()
x = self.config.to_bytes()
send_hex(x)