switch(command)
{
case 'I':
- Serial.print(F("LivingScreen\0\n"));
- Serial.flush();
+ Serial.println(F("LivingScreen"));
+ //Serial.flush();
break;
case 'T':
Serial.print("Translate ");
break;
default:
Serial.print("Unknown command ");
- Serial.println(command);
+ Serial.write(command);
+ Serial.write('\n');
}
}
init_motors();
//Calibration
- calibrate();
+ //calibrate();
}
void loop()
}
//other initialization.
init_process_string();
- init_steppers();
- process_string("G90",3);//Absolute Position
+ //init_steppers();
+ //process_string("G90",3);//Absolute Position
//Serial.println("start");
}
+void loop()
+{
+
+ char c;
+ //read in characters if we got them.
+ if (Serial.available() > 0)
+ {
+ c = Serial.read();
+
+ if (c == '\n' || c == '\r')
+ {
+ //process our command!
+ process_string(commands, serial_count);
+ //clear command.
+ init_process_string();
+ }
+ else if (serial_count < COMMAND_SIZE)
+ {
+ commands[serial_count] = c;
+ serial_count++;
+ }
+ }
+ else
+ delayMicroseconds(100);
+}
+
+
+/*
void loop()
{
//newlines are ends of commands.
if (c != '\n')
{
- if(c=='I')
- Serial.print(F("XYPlotter\0\n"));
+ if (c == 'I')
+ Serial.println(F("XYPlotter"));
+ else if (c == 'C')
+ {
+ init_steppers();
+ process_string("G90",3);//Absolute Position
+ }
else
{
if (c == '(')
// }
-}
+}*/
//did we get a gcode?
- if (!has_command('$', instruction, size)&&(
+ if (has_command('I', instruction, size))
+ {
+ Serial.println(F("XYPlotter"));
+ }
+ else if (has_command('C', instruction, size))
+ {
+ init_steppers();
+ process_string("G90",3);//Absolute Position
+ }
+ else if (!has_command('$', instruction, size)&&(
has_command('G', instruction, size) ||
has_command('X', instruction, size) ||
has_command('Y', instruction, size) ||
//#define QIODEVICE_DEBUG
char const *LivingDevice::serialports[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/ttyUSB4"};
+bool LivingDevice::usedports[] = {false, false, false, false, false};
LivingDevice::LivingDevice(QString name, int timeout)
: serialPort(NULL)
qDebug() << "Discovering" << name;
for (int i = 0 ; i < 5 && !serialPort ; i++)
{
- try{
- serialPort = new SerialLinux(serialports[i], 9600);
-
+ if (usedports[i])
+ continue;
+ try {
qDebug() << "Trying port" << serialports[i];
+ serialPort = new SerialLinux(serialports[i], 9600);
- //ask fo his name
- sendCommand('I');
- QThread::msleep(500);
- QString answer = readAnswer();
+ int tries = 0;
+ QString answer;
+ qDebug() << "Identify";
+ do
+ {
+ qDebug() << "Try" << tries;
+ //ask fo his name
+ if (!sendCommand('I'))
+ {
+ qDebug() << "Cannot speak with port";
+ closeSerialPort();
+ continue;
+ }
+ answer = readAnswer(timeout);
+ } while (answer == "" && tries++ < 5);
if (answer == name)
- qDebug() << "Found LivingKeyboard on port" << serialports[i];
+ {
+ qDebug() << "Found " << name << " on port" << serialports[i];
+ usedports[i] = true;
+ }
else
{
qDebug() << "Not" << name;
serialPort = NULL;
}
-void LivingDevice::sendCommand(char command)
+bool LivingDevice::sendCommand(char command)
{
if (serialPort)
{
char buffer[] = {command, '\n'};
- serialPort->WriteData(buffer, 2);
+ return serialPort->WriteData(buffer, 2);
}
+ return false;
}
-void LivingDevice::sendCommand(char command, int value)
+bool LivingDevice::sendCommand(char command, int value)
{
if (serialPort)
{
QString buffer = command + QString::number(value) + "\n";
- serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+ return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
}
+ return false;
}
-void LivingDevice::sendCommand(char *command, int *value, int nb)
+bool LivingDevice::sendCommand(char *command, int *value, int nb)
{
QString buffer;
if (serialPort)
for (int i = 0 ; i < nb ; i++)
buffer += command[i] + QString::number(value[i]);
buffer += '\n';
- serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+ return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
}
+ return false;
}
-QString LivingDevice::readAnswer()
+QString LivingDevice::readAnswer(int timeout)
{
char buffer[128];
if (serialPort)
{
- int nb = serialPort->ReadData(buffer, 127);
+ int tries = 0;
+ int nb = 0;
+ do
+ {
+ QThread::msleep(100);
+ nb = serialPort->ReadData(buffer, 127);
+ } while (nb == 0 && tries++ < timeout / 100);
+ if (nb <= 0)
+ {
+ qDebug() << "No response";
+ return ""; //TODO: do something else
+ }
buffer[nb] = '\0';
+ while(buffer[strlen(buffer)-1] == '\n' || buffer[strlen(buffer)-1] == '\r')
+ buffer[strlen(buffer)-1] = '\0';
qDebug() << "Read" << buffer;
return QString(buffer);
}
LivingDevice(QString name, int timeout=5000);
virtual ~LivingDevice();
- void discover(QString name, int timeout=5000);
-
- void closeSerialPort();
+ virtual void calibrate() = 0;
- void sendCommand(char command);
- void sendCommand(char command, int value);
- void sendCommand(char *command, int *value, int nb);
-
- QString readAnswer();
+ QString readAnswer(int timeout);
protected:
Serial *serialPort;
static char const *serialports[];
+ static bool usedports[];
+
+ void discover(QString name, int timeout=5000);
+ void closeSerialPort();
+
+ bool sendCommand(char command);
+ bool sendCommand(char command, int value);
+ bool sendCommand(char *command, int *value, int nb);
+
};
#endif // LIVINGDEVICE_H
#include <QDebug>
LivingKeyboard::LivingKeyboard()
-: LivingDevice("LivingKeyboard", 500)
+: LivingDevice("LivingKeyboard", 5000)
{
}
dis *= -1;
sendCommand('T', dis);
}
+
+void LivingKeyboard::calibrate()
+{
+}
void rotation(tilt t, unsigned int angle);
void translation(direction d, unsigned int distance);
+
+ void calibrate();
};
#endif // LIVINGKEYBOARD_H
#include <QDebug>
LivingScreen::LivingScreen()
-: LivingDevice("LivingScreen", 30000)
+: LivingDevice("LivingScreen", 5000)
{
}
dis *= -1;
sendCommand('T', dis);
}
+
+void LivingScreen::calibrate()
+{
+ sendCommand('C');
+}
void rotation(direction t, unsigned int angle);
void translation(direction d, unsigned int distance);
+
+ void calibrate();
};
#endif // LIVINGSCREEN_H
#include "SerialLinux.h"
+#include <ErrorMessage.h>
+
#include <termios.h>
#include <errno.h>
#include <unistd.h>
{
_arduinowaittime = 1;
//Try to connect to the given port throuh CreateFile
- _hSerial = open(portName, O_RDWR | O_NOCTTY);// | O_NDELAY);
+ //_hSerial = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
+ _hSerial = open(portName, O_RDWR | O_NDELAY);
//Check if the connection was successfull
if(_hSerial < 0)
{
char buffer[256];
sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno));
- throw buffer;
+ throw ErrorMessage(buffer);
}
struct termios serialParams;
const int XYPlotter::toolDistance = 70;
XYPlotter::XYPlotter()
-: LivingDevice("XYPlotter", 13000)
+: LivingDevice("XYPlotter", 5000)
{
}
int values[] = {0, x, y, toolup * toolDistance};
sendCommand(commands, values, 4);
}
+
+void XYPlotter::calibrate()
+{
+ sendCommand('C');
+}
void moveTo(int x, int y, bool toolup);
+ void calibrate();
+
private:
unsigned int posX, posY;
bool toolUp;
#include "ui_mainwindow.h"
#include <ErrorMessage.h>
+#include <QThread>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
connect(ui->keyboardrotatebutton, SIGNAL(clicked()), this, SLOT(rotateKeyboard()));
ui->keyboardmovebutton->setEnabled(true);
ui->keyboardrotatebutton->setEnabled(true);
+ keyboard->calibrate();
}
catch (ErrorMessage e)
{
connect(ui->screenrotatebutton, SIGNAL(clicked()), this, SLOT(rotateScreen()));
ui->screenmovebutton->setEnabled(true);
ui->screenrotatebutton->setEnabled(true);
+ screen->calibrate();
}
catch (ErrorMessage e)
{
- mouse = NULL;
+ screen = NULL;
qDebug() << QString(e.what());
}
qDebug("Connect mouse");
mouse = new XYPlotter();
connect(ui->mousebutton, SIGNAL(clicked()), this, SLOT(moveMouse()));
ui->mousebutton->setEnabled(true);
+ mouse->calibrate();
}
catch (ErrorMessage e)
{