Commit 28c992a0 authored by Nelso Jost's avatar Nelso Jost

ADD: serial port select via config file

parent 1aa99a79
......@@ -2,3 +2,4 @@
paigamu
firmware/Makefile
firmware/.scripts
build
[serial]
baud = 9600
port = /dev/ttyACM0
......@@ -35,7 +35,7 @@
"name": "platformiowizard",
"check": "[ -f firmware/.scripts/platformio_wizard.py ]",
"install":
["cd firmware && python2 2-c \"$(wget -O- https://git.cta.if.ufrgs.br/ctautils/platformiowizard/raw/master/get-platformiowizard.py)\""]
["cd firmware && python -c \"$(wget -O- https://git.cta.if.ufrgs.br/ctautils/platformiowizard/raw/master/get-platformiowizard.py)\""]
}
]
}
......@@ -9,11 +9,5 @@ void setup()
void loop()
{
if (Serial.available()) {
command = Serial.read();
if (command == 'p')
{
Serial.println(analogRead(APIN));
}
}
Serial.println(analogRead(APIN));
}
from paigamu import *
import serial
import configparser
class AppLabirintoGame(GameApp):
RESOLUTION = (640, 480)
FRAMERATE = 0
CONFIG_FILENAME = 'config.ini'
def on_setup(self):
self.config = configparser.ConfigParser()
self.config.read('config.ini')
self.main_scene = MainScene(app=self, surface=self.screen)
self.timer_fps = Timer(dt=1,
callback=lambda: pygame.display.set_caption(
......@@ -36,7 +42,7 @@ class MainScene(Scene):
centery=self.rect.centery,
accel=Vec2D(0, 4*self.maze_speed),
keymap={'up': 'K_w', 'down': 'K_s'},
serial_joystick={'port': '/dev/ttyACM0', 'baud': 9600})
serial_joystick=dict(self.app.config['serial']))
self['top_maze'] = MazeSprite(
points=[(0, 200), (200, 0), (400, 200), (600, 0), (800, 200)],
......@@ -156,10 +162,11 @@ class JoystickRespirometro:
self._state = 0
self._last_read = 0
try:
self._sercon = serial.Serial(port, rate, timeout=1)
self._sercon = serial.Serial(port, baud, timeout=1)
self._enabled = True
self.timer = Timer(dt=0.2, callback=self.refresh)
except:
except Exception as e:
print('[SerialOpenError] {}: {}'.format(e.__class__.__name__, e))
self._enabled = False
@property
......@@ -174,20 +181,18 @@ class JoystickRespirometro:
def refresh(self):
if not self._enabled: return
self._sercon.write(b'p')
try:
raw_read = self._sercon.readline()
raw_read = self._sercon.read_all().split(b'\r\n')[-2]
read = int(raw_read.decode('ascii'))
except:
print('[FAIL] unable to read from serial on refresh')
except Exception as e:
print('[SerialReadError] {}: {}'.format(e.__class__.__name__, e))
return
dv = (read - self._last_read)
if dv > self._AMPLITUDE:
self._state = self._STATES['in']
elif dv < -self._AMPLITUDE:
self._state = self._STATES['ex']
elif dv < -self._AMPLITUDE:
self._state = self._STATES['in']
else:
self._state = self._STATES['idle']
......
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