-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorArrayTest.py
More file actions
52 lines (43 loc) · 1.36 KB
/
SensorArrayTest.py
File metadata and controls
52 lines (43 loc) · 1.36 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
import pygame, serial, time, sys
from pygame.locals import *
puerto = '/dev/ttyACM0'
velocidad = 115200
numSensors = 8
pixelSize = 100
SCREEN_WIDTH = numSensors*pixelSize
SCREEN_HEIGHT = pixelSize
def main():
print("Configuración actual:")
print("Puerto: %s" % puerto)
print("BPS = %d" % velocidad)
print("Nº de sensores: %d" %numSensors)
try:
arduino = serial.Serial(puerto, baudrate=velocidad, timeout=1.0)
arduino.setDTR(False)
time.sleep(1)
arduino.flushInput()
arduino.setDTR(True)
except (ImportError, serial.SerialException):
print('ERROR: Placa no encontrada.\nConfigura tu puerto editando este archivo.')
sys.exit()
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Sensor array test (en %s)" % puerto)
while True:
line = arduino.readline()
sensorValues = line.decode('ascii', errors='replace').split(',')
if(len(sensorValues) >= 8):
for i in range(numSensors):
if(sensorValues[i] != '\r\n' and sensorValues[i] != ''):
value = int(sensorValues[i])
color=(value,value,value)
pygame.draw.rect(screen,color,(i*pixelSize,0,pixelSize,pixelSize))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
sys.exit()
pygame.display.update()
if __name__ == "__main__":
main()