b15f/drv/b15f.cpp

350 lines
6.4 KiB
C++
Raw Normal View History

2019-03-26 08:57:01 +00:00
#include "b15f.h"
B15F* B15F::instance = nullptr;
B15F::B15F()
{
}
2019-03-26 14:02:41 +00:00
void B15F::init()
2019-03-26 08:57:01 +00:00
{
2019-03-26 12:28:29 +00:00
2019-03-26 14:02:41 +00:00
std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
2019-03-27 10:45:14 +00:00
int code = system(std::string("stty " + std::to_string(BAUDRATE) + " -F " + SERIAL_DEVICE).c_str());
2019-03-26 10:35:20 +00:00
if(code)
{
throw DriverException("Konnte serielle Verbindung nicht initialisieren. Ist der Adapter angeschlossen?");
}
2019-03-26 12:28:29 +00:00
usart = open(SERIAL_DEVICE.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
struct termios options;
tcgetattr(usart, &options);
2019-03-27 11:57:51 +00:00
options.c_cflag = CS8 | CLOCAL | CREAD;
2019-03-26 12:28:29 +00:00
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
2019-03-27 10:45:14 +00:00
options.c_cc[VTIME]=1; // timeout in Dezisekunden
cfsetspeed(&options, BAUDRATE);
2019-03-26 12:28:29 +00:00
tcsetattr(usart, TCSANOW, &options);
tcflush(usart, TCIFLUSH);
std::cout << "OK" << std::endl;
2019-03-27 11:57:51 +00:00
delay(1);
2019-03-26 12:28:29 +00:00
2019-03-27 11:57:51 +00:00
std::cout << PRE << "Teste Verbindung... " << std::flush;
uint8_t tries = 3;
while(tries--)
2019-03-27 09:49:08 +00:00
{
// verwerfe Daten, die µC noch hat
discard();
2019-03-26 12:28:29 +00:00
if(!testConnection())
2019-03-27 11:57:51 +00:00
continue;
if(!testIntConv())
continue;
break;
2019-03-27 09:49:08 +00:00
}
2019-03-27 11:57:51 +00:00
if(tries == 0)
throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
2019-03-26 12:28:29 +00:00
std::cout << "OK" << std::endl;
2019-03-27 14:48:24 +00:00
// Gib board info aus
std::vector<std::string> info = getBoardInfo();
std::cout << PRE << "AVR Firmware Version: " << info[0] << " um " << info[1] << " Uhr (" << info[2] << ")" << std::endl;
2019-03-26 14:02:41 +00:00
2019-03-27 08:18:21 +00:00
}
void B15F::reconnect()
{
std::cout << PRE << "Verbindung unterbrochen, stelle Verbindung neu her: " << std::flush;
uint8_t tries = RECONNECT_TRIES;
while(tries--)
{
delay(RECONNECT_TIMEOUT);
discard();
if(testConnection())
{
std::cout << "OK" << std::endl << std::flush;
return;
2019-03-26 15:27:21 +00:00
}
2019-03-27 08:18:21 +00:00
2019-03-26 15:27:21 +00:00
}
2019-03-26 12:28:29 +00:00
2019-03-27 08:18:21 +00:00
throw DriverException("Verbindung kann nicht repariert werden");
2019-03-26 08:57:01 +00:00
}
2019-03-26 14:02:41 +00:00
void B15F::discard(void)
{
2019-03-27 11:57:51 +00:00
tcflush(usart, TCIFLUSH); // leere Puffer
2019-03-26 14:02:41 +00:00
for(uint8_t i = 0; i < 8; i++)
{
writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
2019-03-27 11:57:51 +00:00
delay(10);
2019-03-26 14:02:41 +00:00
}
2019-03-27 11:57:51 +00:00
delay(100);
tcflush(usart, TCIFLUSH); // leere Puffer
2019-03-26 14:02:41 +00:00
}
bool B15F::testConnection()
2019-03-26 10:35:20 +00:00
{
2019-03-26 12:28:29 +00:00
// erzeuge zufälliges Byte
2019-03-26 14:02:41 +00:00
srand(time(NULL));
2019-03-26 10:35:20 +00:00
uint8_t dummy = rand() % 256;
2019-03-26 12:28:29 +00:00
2019-03-26 14:02:41 +00:00
writeByte(RQ_TEST);
2019-03-26 10:35:20 +00:00
writeByte(dummy);
uint8_t aw = readByte();
uint8_t mirror = readByte();
return aw == MSG_OK && mirror == dummy;
}
2019-03-26 14:02:41 +00:00
bool B15F::testIntConv()
2019-03-26 10:35:20 +00:00
{
2019-03-26 14:02:41 +00:00
srand(time(NULL));
uint16_t dummy = rand() % (0xFFFF / 3);
writeByte(RQ_INT);
writeInt(dummy);
uint16_t aw = readInt();
return aw == dummy * 3;
2019-03-26 12:28:29 +00:00
}
2019-03-26 15:27:21 +00:00
2019-03-27 14:48:24 +00:00
std::vector<std::string> B15F::getBoardInfo(void)
{
try
{
std::vector<std::string> info;
writeByte(RQ_INFO);
uint8_t n = readByte();
while(n--)
{
uint8_t len = readByte();
std::string str;
while(len--)
str += static_cast<char>(readByte());
info.push_back(str);
}
uint8_t aw = readByte();
if(aw != MSG_OK)
throw DriverException("Board Info fehlerhalft");
return info;
}
catch(DriverException& de)
{
reconnect();
return getBoardInfo();
}
}
2019-03-26 15:27:21 +00:00
bool B15F::digitaleAusgabe0(uint8_t port)
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_BA0);
writeByte(port);
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return digitaleAusgabe0(port);
}
2019-03-26 15:27:21 +00:00
}
bool B15F::digitaleAusgabe1(uint8_t port)
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_BA1);
writeByte(port);
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return digitaleAusgabe1(port);
}
2019-03-26 15:27:21 +00:00
}
uint8_t B15F::digitaleEingabe0()
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_BE0);
return readByte();
}
catch(DriverException& de)
{
reconnect();
return digitaleEingabe0();
}
2019-03-26 15:27:21 +00:00
}
uint8_t B15F::digitaleEingabe1()
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_BE1);
return readByte();
}
catch(DriverException& de)
{
reconnect();
return digitaleEingabe1();
}
2019-03-26 15:27:21 +00:00
}
bool B15F::analogeAusgabe0(uint16_t value)
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_AA0);
writeInt(value);
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return analogeAusgabe0(value);
}
2019-03-26 15:27:21 +00:00
}
bool B15F::analogeAusgabe1(uint16_t value)
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_AA1);
writeInt(value);
uint8_t aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
return analogeAusgabe1(value);
}
2019-03-26 15:27:21 +00:00
}
uint16_t B15F::analogeEingabe(uint8_t channel)
{
2019-03-27 08:18:21 +00:00
try
{
writeByte(RQ_ADC);
writeByte(channel);
return readInt();
}
catch(DriverException& de)
{
reconnect();
return analogeEingabe(channel);
}
2019-03-26 15:27:21 +00:00
}
2019-03-27 09:49:08 +00:00
bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
2019-03-27 09:33:16 +00:00
{
try
{
2019-03-27 09:49:08 +00:00
writeByte(RQ_ADC_DAC_STROKE);
writeByte(channel_a);
writeByte(channel_b);
2019-03-27 09:33:16 +00:00
writeInt(start);
2019-03-27 09:49:08 +00:00
writeInt(static_cast<uint16_t>(delta));
2019-03-27 09:33:16 +00:00
writeInt(count);
uint8_t aw = readByte();
if(aw != MSG_OK)
throw DriverException("Mikrocontroller nicht synchronisiert");
for(uint16_t i = 0; i < count; i++)
{
buffer_a[offset_a + i] = readInt();
buffer_b[offset_b + i] = readInt();
2019-03-28 10:11:19 +00:00
if(buffer_a[offset_a + i] > 1023 || buffer_b[offset_b + i] > 1023)
std::cout << "schlechte Werte gefunden" << std::endl;
2019-03-27 11:57:51 +00:00
//std::cout << "(" << i << ") " << buffer_a[offset_a + i] << " \t| " << buffer_b[offset_b + i] << std::endl;
2019-03-27 09:33:16 +00:00
}
aw = readByte();
return aw == MSG_OK;
}
catch(DriverException& de)
{
reconnect();
2019-03-27 09:49:08 +00:00
return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count);
2019-03-27 09:33:16 +00:00
}
}
2019-03-26 15:27:21 +00:00
2019-03-26 14:02:41 +00:00
void B15F::writeByte(uint8_t b)
2019-03-26 12:28:29 +00:00
{
2019-03-26 14:02:41 +00:00
if(write(usart, &b, 1) != 1)
throw DriverException("Fehler beim Senden. (byte)");
2019-03-26 12:28:29 +00:00
}
2019-03-26 14:02:41 +00:00
void B15F::writeInt(uint16_t v)
2019-03-26 12:28:29 +00:00
{
2019-03-26 14:02:41 +00:00
if(write(usart, reinterpret_cast<char*>(&v), 2) != 2)
throw DriverException("Fehler beim Senden. (int)");
2019-03-26 10:35:20 +00:00
}
uint8_t B15F::readByte()
{
char b;
2019-03-26 12:28:29 +00:00
auto start = std::chrono::steady_clock::now();
auto end = start;
uint16_t elapsed = 0;
while(elapsed < timeout)
2019-03-26 10:35:20 +00:00
{
2019-03-26 12:28:29 +00:00
int n = read(usart, &b, 1);
if (n > 0)
2019-03-26 10:35:20 +00:00
return static_cast<uint8_t>(b);
2019-03-26 12:28:29 +00:00
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
2019-03-26 10:35:20 +00:00
}
2019-03-26 12:28:29 +00:00
if(elapsed >= timeout)
throw DriverException("Verbindung unterbrochen. (timeout)");
}
uint16_t B15F::readInt()
{
2019-03-26 14:02:41 +00:00
return readByte() | readByte() << 8;
2019-03-26 12:28:29 +00:00
}
2019-03-26 14:02:41 +00:00
void B15F::delay(uint16_t ms)
2019-03-26 12:28:29 +00:00
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
2019-03-26 10:35:20 +00:00
}
2019-03-26 08:57:01 +00:00
B15F& B15F::getInstance(void)
{
if(!instance)
instance = new B15F();
return *instance;
}