-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
244 lines (221 loc) · 7.61 KB
/
main.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
"""Module for monitoring humidity and temperature"""
from json import JSONDecodeError, loads, dumps
from time import sleep
from threading import Thread
from flet import (
Text,
TextField,
Dropdown,
Row,
Column,
Page,
app,
MainAxisAlignment,
CupertinoSlider,
FilledButton,
dropdown,
ControlEvent,
)
from serial import SerialTimeoutException, SerialException, PortNotOpenError, Serial
DELAY = 5.0
def setup_serial(port:str="COM1") -> dict:
"""Returns a serial object with serial and timeout already set"""
serial_instance = {
"error": None,
"new_serial": None
}
try:
new_serial = Serial(port,9600,timeout=DELAY)
serial_instance.update("new_serial",new_serial)
except PortNotOpenError:
serial_instance.update("error","Error al abrir puerto")
except SerialTimeoutException:
serial_instance.update("error","Tiempo de espera excedido")
except SerialException:
serial_instance.update("error","Error en el puerto serial")
return serial_instance
def update_serial_instance(
old_instance:dict,
new_instance:dict
) -> dict:
"""Updates the old serial object and sets new values from another serial object"""
new_instance.update("error",old_instance.get("error"))
new_instance.update("new_serial",old_instance.get("new_serial"))
return new_instance
def setup_threads(func) -> Thread:
"""Returns an initialized thread targeting a function"""
arduino_thread = Thread(target=func)
arduino_thread.start()
return arduino_thread
def change_field_status(fields:list, status=True) -> None:
"""Changes the status of a field"""
map(lambda field: field.disabled == status, fields)
def update_arduino_values(
serial_object:dict,
humidity_field:TextField,
temperature_field:TextField,
page:Page
):
"""Updates the humidity and temperature fields with the values from the arduino"""
serial_line = serial_object.get("new_serial")
while True:
if serial_object.get("error") is not None:
continue
try:
serial_data = serial_line.readline().decode("utf-8")
decoded_data = loads(serial_data)
print(decoded_data)
humidity_field.value = decoded_data["temperatura"]
temperature_field.value = decoded_data["humedad"]
page.update()
except JSONDecodeError:
pass
sleep(0.5)
def rgb_component(
page:Page,
serial_object:dict
):
"""Returns a component with RGB sliders and text fields"""
red_text = Text("0",color="red")
green_text = Text("0",color="green")
blue_text = Text("0",color="blue")
def change_red_value(event:ControlEvent):
print(event.control.value)
red_text.value = str(int(event.control.value)).zfill(3)
page.update()
def change_green_value(event:ControlEvent):
print(event.control.value)
green_text.value = str(int(event.control.value)).zfill(3)
page.update()
def change_blue_value(event:ControlEvent):
print(event.control.value)
blue_text.value = str(int(event.control.value)).zfill(3)
page.update()
def change_color():
"""Changes the color of the LED"""
serial_line = serial_object.get("new_serial")
json_data = dumps({
"red":red_text.value,
"green":green_text.value,
"blue":blue_text.value
})
if not serial_line is None:
serial_line.write(json_data.encode("utf-8"))
component = Column(
[
CupertinoSlider(
on_change=change_red_value,
value=0,
divisions=255,
min=0,
max=255,
active_color="red",
thumb_color="red",
),
CupertinoSlider(
on_change=change_green_value,
value=0,
divisions=255,
min=0,
max=255,
active_color="green",
thumb_color="green",
),
CupertinoSlider(
on_change=change_blue_value,
value=0,
divisions=255,
min=0,
max=255,
active_color="blue",
thumb_color="blue",
),
FilledButton(
text='Enviar color',
icon="send",
icon_color="white",
on_click=lambda e: change_color()
)
],
)
rgb_text_fields = [red_text,green_text,blue_text]
return component, rgb_text_fields
def set_title(page:Page):
"""Sets the title and the alignment of the page"""
page.title = "Valores de arduino"
page.vertical_alignment = MainAxisAlignment.CENTER
def main(page: Page) -> None:
"""This function setup the page and manages the page content"""
serial_object = setup_serial(port="COM3")
arduino_status = Text("Conecta el arduino y selecciona el puerto serial")
rgb_controls, rgb_text_fields = rgb_component(page,serial_object)
def add_error_message(error_message="Algo salió mal"):
"""Adds an error message to the page"""
arduino_status.value = error_message
page.update()
set_title(page=page)
txt_humidity = TextField(value="Humedad",read_only=True,disabled=True)
txt_temperature = TextField(value="Temperatura",read_only=True,disabled=True)
if not serial_object.get("error") is None:
change_field_status([txt_temperature, txt_humidity], status=False)
add_error_message(error_message=serial_object.get("error"))
def change_serial_port(serial_obj:dict,event:ControlEvent):
"""Changes the serial port to the selected one"""
new_instance = update_serial_instance(
old_instance=serial_obj,
new_instance=setup_serial(event.control.value)
)
if not new_instance.get("error") is None:
change_field_status([txt_temperature, txt_humidity], status=False)
add_error_message(error_message=new_instance.get("error"))
else:
change_field_status([txt_temperature,txt_humidity], status=True)
add_error_message(error_message="Conectado al arduino")
print(f"Serial port changed to {event.control.value}")
page.add(
Row(
[
arduino_status
],
alignment=MainAxisAlignment.CENTER,
),
Row(
[
Dropdown(
width=200,
options=[
dropdown.Option("COM1", "COM1"),
dropdown.Option("COM2", "COM2"),
dropdown.Option("COM3", "COM3"),
dropdown.Option("COM4", "COM4"),
],
label="Puerto serial",
on_change=lambda e:
change_serial_port(serial_obj=serial_object,event=e)
)
],
alignment=MainAxisAlignment.CENTER,
),
Row(
[
txt_humidity,
txt_temperature
],
alignment=MainAxisAlignment.CENTER,
),
Row(
rgb_text_fields,
alignment=MainAxisAlignment.CENTER,
),
Row(
[
rgb_controls
],
alignment=MainAxisAlignment.CENTER,
)
)
setup_threads(
func=lambda:
update_arduino_values(serial_object,txt_humidity,txt_temperature,page)
)
app(main)