Backup before switching to flexx for GUI
Added serial communication class that can succesfully communicate with the arduino. Started work on pygame GUI but I've decided to switch direction to using Flexx instead.
This commit is contained in:
34
Interface/Button.py
Normal file
34
Interface/Button.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import pygame
|
||||
class Button:
|
||||
pos = [0,0]
|
||||
size = [0,0]
|
||||
color = (0, 80, 80)
|
||||
def __init__(self, position, size, screen, function, argument):
|
||||
self.pos = position
|
||||
self.size = size
|
||||
self.function = function
|
||||
self.argument = argument
|
||||
self.screen = screen
|
||||
return
|
||||
|
||||
def isClicked(self, mouse_x, mouse_y, mouse_state):
|
||||
x, y = mouse_x, mouse_y
|
||||
if x >= self.pos[0] and x <= self.pos[0]+self.size[0]:
|
||||
if y >= self.pos[1] and y <= self.pos[1]+self.size[1]:
|
||||
if mouse_state == True:
|
||||
self.function(self.argument)
|
||||
return True
|
||||
return False
|
||||
|
||||
def setPosition(self, x, y):
|
||||
self.pos[0], self.pos[1] = x, y
|
||||
return
|
||||
|
||||
def setArgument(self, argument):
|
||||
self.argument = argument
|
||||
return
|
||||
|
||||
def drawButton(self):
|
||||
pygame.draw.rect(self.screen, self.color,
|
||||
pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]))
|
||||
return
|
||||
60
Interface/Comm.py
Normal file
60
Interface/Comm.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import serial
|
||||
from time import sleep
|
||||
|
||||
class Comm:
|
||||
current_pressure = -1
|
||||
target_pressure = -1
|
||||
motor_speed = -1
|
||||
def __init__(self, port, baud_rate):
|
||||
# Setup serial
|
||||
self.comm = serial.Serial(port, baud_rate)
|
||||
#read a line from the serial bus to sync arduino with python script
|
||||
for x in range(0,5):
|
||||
self.comm.readline()
|
||||
self.getPID()
|
||||
return
|
||||
|
||||
def getPID(self):
|
||||
message = self.comm.readline().decode().strip()
|
||||
#message = 'Current Pressure: 80, Target Pressure: 15, Motor Speed: 255'
|
||||
#print(message)
|
||||
if message[0:7] == 'Current':
|
||||
startIndex = message.find(':')
|
||||
endIndex = message.find(',')
|
||||
self.current_pressure = float(message[startIndex+1:endIndex])
|
||||
startIndex = message.find(':', endIndex)
|
||||
endIndex = message.find(',', endIndex+1)
|
||||
self.target_pressure = float(message[startIndex+1:endIndex])
|
||||
startIndex = message.find(':', endIndex)
|
||||
self.motor_speed = float(message[startIndex+1:len(message)])
|
||||
#print("1", self.current_pressure, "2", self.target_pressure, "3", self.motor_speed)
|
||||
return (self.current_pressure, self.target_pressure, self.motor_speed)
|
||||
else:
|
||||
return (self.current_pressure, self.target_pressure, self.motor_speed)
|
||||
|
||||
def setPressure(self, pressure):
|
||||
pressure = float(pressure)
|
||||
msg = 'PRSR ' + str(pressure) + '\r\n'
|
||||
while self.getPID()[1] != pressure:
|
||||
self.comm.write(msg.encode())
|
||||
return
|
||||
|
||||
def setHeartRate(self, BPM):
|
||||
msg = 'BEAT ' + str(BPM) + '\r\n'
|
||||
response = 'Heart Rate set to ' + str(BPM) + ' BPM.'
|
||||
ans = ''
|
||||
while ans != response:
|
||||
self.comm.write(msg.encode())
|
||||
ans = self.comm.readline().decode().strip()
|
||||
return
|
||||
|
||||
def fill(self, time):
|
||||
msg = 'FILL ' + str(time) + '\r\n'
|
||||
response = 'Filling for ' + str(time) + ' seconds.'
|
||||
ans = ''
|
||||
while ans != response:
|
||||
self.comm.write(msg.encode())
|
||||
ans = self.comm.readline().decode().strip()
|
||||
return
|
||||
|
||||
#comm = Comm('COM11', 115200)
|
||||
BIN
Interface/__pycache__/Button.cpython-36.pyc
Normal file
BIN
Interface/__pycache__/Button.cpython-36.pyc
Normal file
Binary file not shown.
@@ -1,5 +1,6 @@
|
||||
import pygame
|
||||
import serial
|
||||
from Button import Button
|
||||
from Comm import Comm
|
||||
|
||||
def createText(font, text, position):
|
||||
text = font.render(text, True, (0,0,0), (255,255,255))
|
||||
@@ -23,14 +24,17 @@ texts.append(createText(font, 'Pressure (PSI)', positions[0]))
|
||||
texts.append(createText(font, 'Heart Rate (BPM)', positions[1]))
|
||||
texts.append(createText(font, '0', positions[2]))
|
||||
texts.append(createText(font, '0', positions[3]))
|
||||
def test(nothing):
|
||||
|
||||
comm = Comm('COM10', 115200)
|
||||
def test():
|
||||
return
|
||||
testButton = Button([10,10], [50,50], pygame, test, "Hello")
|
||||
testButton = Button([10,10], [50,50], screen, test, "Hello")
|
||||
|
||||
def main():
|
||||
done = False
|
||||
while not done:
|
||||
screen.fill((255, 255, 255))
|
||||
testButton.drawButton()
|
||||
for text in texts:
|
||||
screen.blit(text[0], text[1])
|
||||
for event in pygame.event.get():
|
||||
@@ -42,33 +46,5 @@ def main():
|
||||
pygame.quit()
|
||||
return
|
||||
|
||||
class Button:
|
||||
pos = [0,0]
|
||||
size = [0,0]
|
||||
def __init__(self, position, size, pygame, function, argument):
|
||||
self.pos = position
|
||||
self.size = size
|
||||
self.function = function
|
||||
self.argument = argument
|
||||
self.pygame = pygame
|
||||
return
|
||||
|
||||
def isClicked(self):
|
||||
x, y = self.pygame.get_pos()[0], self.pygame.get_pos()[1]
|
||||
if x >= self.pos[0] and x <= self.pos[0]+self.size[0]:
|
||||
if y >= self.pos[1] and y <= self.pos[1]+self.size[1]:
|
||||
if pygame.mouse.get_pressed()[0] == True:
|
||||
self.function(self.argument)
|
||||
return True
|
||||
return False
|
||||
|
||||
def setPosition(self, x, y):
|
||||
self.pos[0], self.pos[1] = x, y
|
||||
return
|
||||
|
||||
def setArgument(self, argument):
|
||||
self.argument = argument
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user