Commit bf70fa43 authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Add Arduino module using Alisson's protocol

parent 18fc550e
# Este arquivo define a interface a ser utilizada
device:
type: mock
location: null
type: arduino
location: "/dev/ttyUSB0"
channels:
- id: 0
name: "Tensão"
......
from time import sleep
from serial import Serial
class Arduino(object):
def __init__(self, logger, location, channels):
logger("[ARDUINO]", "Opening serial port...")
try:
self.ser = Serial(location, 115200)
except:
logger("[ARDUINO]", "ERROR: Could not open serial port")
logger("[ARDUINO]", "Starting board...")
self._reset() # Reset do arduino para reinicio da leitura
self.ser.readline() # Espera o Arduino inicializar
self.ser.write(bytes("press",'utf-8'))
self.ser.readline()
logger("[ARDUINO]", "Board started, good luck!")
def read(self):
return 10
def readAll(self):
self.ser.flushInput() # Limpeza do buffer de entrada
data = self.ser.readline().split(bytes(" ", "utf-8")) # Leitura de todos os valores em uma linha
return [int(x) for x in data]
def _reset(self):
self.ser.setDTR(False)
sleep(0.05)
self.ser.setDTR(True)
\ No newline at end of file
from random import random
class Mock(object):
def __init__(self, location, channels):
def __init__(self, logger, location, channels):
self.channels = len(channels)
def read(self):
logger("[MOCK]", "Generating fake data...")
def read(self, channel):
return random() * 20
def readAll(self):
return [self.read() for i in range(self.channels)]
\ No newline at end of file
return [self.read(0) for i in range(self.channels)]
\ No newline at end of file
# Comente fora as interfaces não utilizadas para reduzir o tamanho do programa
from interfaces.Mock import *
from interfaces.Arduino import *
INTERFACES = {
'mock': Mock
'mock': Mock,
'arduino': Arduino
}
\ No newline at end of file
......@@ -19,7 +19,7 @@ class Graph(QThread):
self.config = Config().data
self._configurePlots()
device = self.config["device"]
self.interface = INTERFACES[device["type"]](device["location"], device["channels"])
self.interface = INTERFACES[device["type"]](logger, device["location"], device["channels"])
def run(self):
while self.running:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment