-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTractorGUI.py
More file actions
264 lines (208 loc) · 9.24 KB
/
TractorGUI.py
File metadata and controls
264 lines (208 loc) · 9.24 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
'''import pygame
import asyncio
import uuid
from bleak import BleakClient
#Bluetooth module address and UUID
address = "a0:6c:65:cf:7f:8f"
MODEL_NBR_UUID = "0000FFE1-0000-1000-8000-00805F9B34FB"
SERVICE_NBR_UUID = "0000FFE0-0000-1000-8000-00805F9B34FB"
#Set screen width and Height
SCREEN_HEIGHT = 750
SCREEN_WIDTH = 1500
#Initialize the screen and set a screen caption
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Button Demo')
#Load each button image from the PNG file
START_BUTTON = pygame.image.load('START.PNG')
STOP_BUTTON = pygame.image.load('STOP.PNG')
TRIP_REPORT_BUTTON = pygame.image.load('Trip_Report.PNG')
BLACK_TAPE_COUNT_BUTTON = pygame.image.load('Black_Tape_Count.PNG')
ELAPSED_TIME_BUTTON = pygame.image.load('Elapsed_Time.PNG')
DISTANCE_TRAVELED_BUTTON = pygame.image.load('Distance_Traveled.PNG')
SPEED_BUTTON = pygame.image.load('Speed.PNG')
class Button():
#This function is similar to a constructor
def __init__(self, x, y, image, scale):
width = image.get_width() #gets the image width and height
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) #scales the image based on the user input
self.rect = self.image.get_rect()
self.rect.topleft = (x, y) #sets the top left of the rectangle to the user's x and y inputs
self.clicked = False
def draw(self):
action = False
position = pygame.mouse.get_pos()
if self.rect.collidepoint(position):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
screen.blit(self.image, (self.rect.x, self.rect.y))
return action
#Creating the buttons neccessary
start_button = Button(547.5, 50, START_BUTTON, 1.0)
stop_button = Button(466.5, 200, STOP_BUTTON, 1.0)
trip_report_button = Button(555, 350, TRIP_REPORT_BUTTON, 1.0)
black_tape_count_button = Button(613.5, 475, BLACK_TAPE_COUNT_BUTTON, 1.0)
elapsed_time_button = Button(1150, 475, ELAPSED_TIME_BUTTON, 1.0)
distance_traveled_button = Button(100, 475, DISTANCE_TRAVELED_BUTTON, 1.0)
speed_button = Button(100, 100, SPEED_BUTTON, 1.0)
async def main(address):
client = BleakClient(address)
def callback(sender: client, data: bytearray):
black_tape_count = data.decode()
print("hello")
print(black_tape_count)
try:
await client.connect()
await client.start_notify(MODEL_NBR_UUID, callback)
model_number = await client.read_gatt_char(MODEL_NBR_UUID)
print("Model Number: {0}".format("".join(map(chr, model_number))))
#async with BleakClient(address) as client:
run = True
while run:
screen.fill((229, 229, 229)) #Used to achieve the soft white background
#if black_tape_count_button.draw() == False:
#await client.start_notify(MODEL_NBR_UUID, callback)
#black_tape_count = await client.read_gatt_char(MODEL_NBR_UUID)
#print(black_tape_count.decode())
#textTBD = textfont.render(black_tape_count)
#draws all the buttons on the screen
if start_button.draw() == True:
print("START")
start = '1'
bytes_start = bytes(start, 'ascii', errors = 'ignore')
print('Byte converstion:', bytes_start)
await client.write_gatt_char(MODEL_NBR_UUID, bytes(start, 'ascii', errors = 'ignore'), response = False)
if stop_button.draw() == True:
print("STOP")
stop = '0'
bytes_stop = bytes(stop, 'ascii', errors = 'ignore')
print('Byte converstion:', bytes_stop)
await client.write_gatt_char(MODEL_NBR_UUID, bytes(stop, 'ascii', errors = 'ignore'), response = False)
trip_report_button.draw()
black_tape_count_button.draw()
elapsed_time_button.draw()
distance_traveled_button.draw()
speed_button.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update() #Updates the events from the user
pygame.quit()
except Exception as e:
print(e)
finally:
await client.disconnect()
asyncio.run(main(address))
'''
import pygame
import asyncio
import uuid
from bleak import BleakClient
#Bluetooth module address and UUID
address = "a0:6c:65:cf:7f:8f"
MODEL_NBR_UUID = "0000FFE1-0000-1000-8000-00805F9B34FB"
SERVICE_NBR_UUID = "0000FFE0-0000-1000-8000-00805F9B34FB"
#Set screen width and Height
SCREEN_HEIGHT = 750
SCREEN_WIDTH = 1500
#Initialize the screen and set a screen caption
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Button Demo')
pygame.font.init()
textfont = pygame.font.SysFont("monospace", 100)
#Load each button image from the PNG file
START_BUTTON = pygame.image.load('START.PNG')
STOP_BUTTON = pygame.image.load('STOP.PNG')
TRIP_REPORT_BUTTON = pygame.image.load('Trip_Report.PNG')
BLACK_TAPE_COUNT_BUTTON = pygame.image.load('Black_Tape_Count.PNG')
ELAPSED_TIME_BUTTON = pygame.image.load('Elapsed_Time.PNG')
DISTANCE_TRAVELED_BUTTON = pygame.image.load('Distance_Traveled.PNG')
SPEED_BUTTON = pygame.image.load('Speed.PNG')
BLANK = pygame.image.load("Blank.PNG")
class Button():
#This function is similar to a constructor
def __init__(self, x, y, image, scale):
width = image.get_width() #gets the image width and height
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) #scales the image based on the user input
self.rect = self.image.get_rect()
self.rect.topleft = (x, y) #sets the top left of the rectangle to the user's x and y inputs
self.clicked = False
def draw(self):
action = False
position = pygame.mouse.get_pos()
if self.rect.collidepoint(position):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
screen.blit(self.image, (self.rect.x, self.rect.y))
return action
#Creating the buttons neccessary
start_button = Button(547.5, 50, START_BUTTON, 1.0)
stop_button = Button(466.5, 200, STOP_BUTTON, 1.0)
trip_report_button = Button(555, 350, TRIP_REPORT_BUTTON, 1.0)
black_tape_count_button = Button(613.5, 475, BLACK_TAPE_COUNT_BUTTON, 1.0)
elapsed_time_button = Button(1150, 475, ELAPSED_TIME_BUTTON, 1.0)
distance_traveled_button = Button(100, 475, DISTANCE_TRAVELED_BUTTON, 1.0)
speed_button = Button(100, 100, SPEED_BUTTON, 1.0)
blank = Button(665, 550, BLANK, 1.0)
screen.fill((229, 229, 229))
global_black_tape_count = '999'
async def main(address):
client = BleakClient(address)
def callback(sender: client, data: bytearray):
black_tape_count = data.decode()
if ()
print("hello")
print(black_tape_count)
textTBD = textfont.render(black_tape_count, 1, (0, 0, 0))
screen.blit(textTBD, (665, 550))
try:
await client.connect()
#await client.start_notify(MODEL_NBR_UUID, callback)
model_number = await client.read_gatt_char(MODEL_NBR_UUID)
print("Model Number: {0}".format("".join(map(chr, model_number))))
#async with BleakClient(address) as client:
run = True
while run:
#Used to achieve the soft white background
#textTBD = textfont.render(black_tape_count, 1, (0, 0, 0))
<<<<<<< Updated upstream
#screen.blit(textTBD, (300, 700))
=======
#creen.blit(textTBD, (400, 500))
await client.start_notify(MODEL_NBR_UUID, callback)
>>>>>>> Stashed changes
#draws all the buttons on the screen
if start_button.draw() == True:
print("START")
start = '1'
bytes_start = bytes(start, 'ascii', errors = 'ignore')
print('Byte converstion:', bytes_start)
await client.write_gatt_char(MODEL_NBR_UUID, bytes(start, 'ascii', errors = 'ignore'), response = False)
if stop_button.draw() == True:
print("STOP")
stop = '0'
bytes_stop = bytes(stop, 'ascii', errors = 'ignore')
print('Byte converstion:', bytes_stop)
await client.write_gatt_char(MODEL_NBR_UUID, bytes(stop, 'ascii', errors = 'ignore'), response = False)
trip_report_button.draw()
black_tape_count_button.draw()
elapsed_time_button.draw()
distance_traveled_button.draw()
speed_button.draw()
blank.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update() #Updates the events from the user
pygame.quit()
except Exception as e:
print(e)
await client.disconnect()
asyncio.run(main(address))