Skip to content

Instantly share code, notes, and snippets.

Last active November 7, 2020 21:08
Show Gist options
  • Save emdete/2ee81f637c9a2108ef5f2adf65b51b8a to your computer and use it in GitHub Desktop.
Save emdete/2ee81f637c9a2108ef5f2adf65b51b8a to your computer and use it in GitHub Desktop.
package de.emdete.roberta;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.fhg.iais.roberta.connection.IRobot;
import de.fhg.iais.roberta.connection.IConnector;
import de.fhg.iais.roberta.connection.wired.arduino.Arduino;
import de.fhg.iais.roberta.connection.IDetector;
import de.fhg.iais.roberta.connection.wired.WiredRobotType;
import de.fhg.iais.roberta.connection.IConnector.State;
import de.fhg.iais.roberta.connection.SerialLoggingTask;
class Main {
static final Logger LOG = LoggerFactory.getLogger(Main.class);
static IConnector<?> connector = null;
static final ExecutorService executorService = Executors.newSingleThreadExecutor();
static Future<Void> serialLoggingFuture = null;
static String portName = null;
static public void setState(State state) {"State is {}", state);
switch ( state ) {
case WAIT_FOR_CONNECT_BUTTON_PRESS: // ready to connect
case WAIT_FOR_SERVER: // token available"Connected," + connector.getRobot().getName() + ", token=" + connector.getToken());
case WAIT_FOR_CMD: // the token was succesfully used, we are connected, start serial monitor
try {
serialLoggingFuture = executorService.submit(new SerialLoggingTask(Main::appendSerial, Main.portName, 9600));
catch (Exception e) {"No arduino found");
case WAIT_UPLOAD: // arduino is beeing flashed, stop serial monitor
if (serialLoggingFuture != null)
static void appendSerial(byte[] readBuffer) {
String[] lines = new String(readBuffer).split("\n");
for (String line: lines) {
line = line.strip();
if (line.length() > 0)"From serial: '{}'", line);
public static void main(String args[]) throws Exception {
try {
portName = args[3];
connector = new Arduino(WiredRobotType.fromString(args[2]), args[3]).createConnector(); // TODO support all other types
connector.updateCustomServerAddress(args[0] + ":" + args[1]);
connector.registerListener(Main::setState);; // kick off flow;
finally {
if (connector != null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment