b15f/driver/drv/b15f.cpp

299 lines
5.9 KiB
C++
Raw Normal View History

2019-03-26 09:57:01 +01:00
#include "b15f.h"
B15F* B15F::instance = nullptr;
B15F::B15F()
{
2019-03-29 08:32:11 +01:00
init();
2019-03-26 09:57:01 +01:00
}
2019-03-26 15:02:41 +01:00
void B15F::init()
2019-03-26 09:57:01 +01:00
{
2019-03-26 13:28:29 +01:00
2019-03-29 14:54:38 +01:00
std::string device = exec("bash -c 'ls /dev/ttyUSB*'");
while(device.find(' ') != std::string::npos || device.find('\n') != std::string::npos || device.find('\t') != std::string::npos)
device.pop_back();
if(device.length() == 0)
throw DriverException("Adapter nicht gefunden");
std::cout << PRE << "Verwende Adapter: " << device << std::endl;
2019-03-26 15:02:41 +01:00
std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
2019-03-29 11:50:16 +01:00
usart.setBaudrate(BAUDRATE);
2019-03-29 14:54:38 +01:00
usart.openDevice(device);
2019-03-26 13:28:29 +01:00
std::cout << "OK" << std::endl;
2019-03-27 12:57:51 +01:00
2019-03-29 13:35:06 +01:00
2019-04-02 11:18:36 +02:00
// Temporärer Test
/*
2019-04-02 10:59:37 +02:00
uint8_t block[16];
2019-04-01 08:36:45 +02:00
while(1)
2019-04-02 08:46:42 +02:00
{
2019-04-01 08:36:45 +02:00
usart.writeBlock(&block[0], 0, sizeof(block));
2019-04-02 08:46:42 +02:00
usart.printStatistics();
2019-04-02 10:59:37 +02:00
usleep(1000);
2019-04-02 08:46:42 +02:00
}
2019-04-02 11:18:36 +02:00
throw std::runtime_error("SCHLUSS");*/
2019-04-01 08:36:45 +02:00
2019-03-26 13:28:29 +01:00
2019-03-27 12:57:51 +01:00
std::cout << PRE << "Teste Verbindung... " << std::flush;
uint8_t tries = 3;
while(tries--)
2019-03-27 10:49:08 +01:00
{
// verwerfe Daten, die µC noch hat
2019-04-02 11:18:36 +02:00
//discard();
2019-03-27 10:49:08 +01:00
2019-03-26 13:28:29 +01:00
if(!testConnection())
2019-03-27 12:57:51 +01:00
continue;
2019-04-02 11:53:38 +02:00
2019-03-27 12:57:51 +01:00
if(!testIntConv())
continue;
break;
2019-03-27 10:49:08 +01:00
}
2019-03-27 12:57:51 +01:00
if(tries == 0)
throw DriverException("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
2019-03-26 13:28:29 +01:00
std::cout << "OK" << std::endl;
2019-03-27 15:48:24 +01:00
2019-04-02 11:18:36 +02:00
2019-03-27 15:48:24 +01: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-27 09:18:21 +01:00
}
void B15F::reconnect()
{
std::cout << PRE << "Verbindung unterbrochen, stelle Verbindung neu her: " << std::flush;
uint8_t tries = RECONNECT_TRIES;
while(tries--)
{
2019-03-29 11:12:31 +01:00
delay_ms(RECONNECT_TIMEOUT);
2019-03-27 09:18:21 +01:00
discard();
if(testConnection())
{
std::cout << "OK" << std::endl << std::flush;
return;
2019-03-26 16:27:21 +01:00
}
2019-03-27 09:18:21 +01:00
2019-03-26 16:27:21 +01:00
}
2019-03-26 13:28:29 +01:00
2019-03-27 09:18:21 +01:00
throw DriverException("Verbindung kann nicht repariert werden");
2019-03-26 09:57:01 +01:00
}
2019-03-26 15:02:41 +01:00
void B15F::discard(void)
{
2019-04-02 08:46:42 +02:00
usart.clearOutputBuffer();
2019-03-26 15:02:41 +01:00
for(uint8_t i = 0; i < 8; i++)
{
2019-03-29 11:38:11 +01:00
usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
2019-03-29 11:12:31 +01:00
delay_ms((16000 / BAUDRATE) + 1); // warte mindestens eine Millisekunde, gegebenenfalls mehr
2019-03-26 15:02:41 +01:00
}
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-26 15:02:41 +01:00
}
bool B15F::testConnection()
2019-03-26 11:35:20 +01:00
{
2019-03-26 13:28:29 +01:00
// erzeuge zufälliges Byte
2019-03-26 15:02:41 +01:00
srand(time(NULL));
2019-03-26 11:35:20 +01:00
uint8_t dummy = rand() % 256;
2019-03-26 13:28:29 +01:00
2019-03-29 11:38:11 +01:00
usart.writeByte(RQ_TEST);
usart.writeByte(dummy);
2019-04-02 11:18:36 +02:00
2019-03-29 11:38:11 +01:00
uint8_t aw = usart.readByte();
uint8_t mirror = usart.readByte();
2019-03-26 11:35:20 +01:00
return aw == MSG_OK && mirror == dummy;
}
2019-03-26 15:02:41 +01:00
bool B15F::testIntConv()
2019-03-26 11:35:20 +01:00
{
2019-03-26 15:02:41 +01:00
srand(time(NULL));
uint16_t dummy = rand() % (0xFFFF / 3);
2019-03-29 11:38:11 +01:00
usart.writeByte(RQ_INT);
usart.writeInt(dummy);
2019-03-26 15:02:41 +01:00
2019-03-29 11:38:11 +01:00
uint16_t aw = usart.readInt();
2019-03-26 15:02:41 +01:00
return aw == dummy * 3;
2019-03-26 13:28:29 +01:00
}
2019-03-26 16:27:21 +01:00
2019-03-27 15:48:24 +01:00
std::vector<std::string> B15F::getBoardInfo(void)
{
2019-03-29 11:42:52 +01:00
std::vector<std::string> info;
usart.writeByte(RQ_INFO);
uint8_t n = usart.readByte();
while(n--)
2019-03-27 15:48:24 +01:00
{
2019-04-02 13:34:59 +02:00
uint8_t len = usart.readByte();
2019-03-29 11:42:52 +01:00
std::string str;
2019-04-02 13:34:59 +02:00
2019-04-03 08:40:14 +02:00
while(len--) {
2019-03-29 11:42:52 +01:00
str += static_cast<char>(usart.readByte());
2019-04-03 08:40:14 +02:00
}
2019-03-29 11:42:52 +01:00
info.push_back(str);
2019-03-27 15:48:24 +01:00
}
2019-03-29 11:42:52 +01:00
uint8_t aw = usart.readByte();
if(aw != MSG_OK)
2019-03-29 13:35:06 +01:00
throw DriverException("Board Info fehlerhalft: code " + std::to_string((int) aw));
2019-03-29 11:42:52 +01:00
return info;
2019-03-27 15:48:24 +01:00
}
2019-03-26 16:27:21 +01:00
2019-04-03 11:59:15 +02:00
bool B15F::activateSelfTestMode()
{
usart.clearInputBuffer();
usart.writeByte(RQ_ST);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
}
2019-03-28 16:04:38 +01:00
bool B15F::digitalWrite0(uint8_t port)
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_BA0);
usart.writeByte(port);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
2019-03-26 16:27:21 +01:00
}
2019-03-28 16:04:38 +01:00
bool B15F::digitalWrite1(uint8_t port)
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_BA1);
usart.writeByte(port);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
2019-03-26 16:27:21 +01:00
}
2019-03-28 16:04:38 +01:00
uint8_t B15F::digitalRead0()
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_BE0);
return usart.readByte();
2019-03-26 16:27:21 +01:00
}
2019-03-28 16:04:38 +01:00
uint8_t B15F::digitalRead1()
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_BE1);
return usart.readByte();
2019-03-26 16:27:21 +01:00
}
2019-04-03 09:34:22 +02:00
uint8_t B15F::readDipSwitch()
{
usart.clearInputBuffer();
usart.writeByte(RQ_DSW);
return usart.readByte();
}
2019-03-28 16:04:38 +01:00
bool B15F::analogWrite0(uint16_t value)
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_AA0);
usart.writeInt(value);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
2019-03-26 16:27:21 +01:00
}
2019-03-28 16:04:38 +01:00
bool B15F::analogWrite1(uint16_t value)
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_AA1);
usart.writeInt(value);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
2019-03-26 16:27:21 +01:00
}
2019-03-28 16:04:38 +01:00
uint16_t B15F::analogRead(uint8_t channel)
2019-03-26 16:27:21 +01:00
{
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_ADC);
usart.writeByte(channel);
return usart.readInt();
2019-03-26 16:27:21 +01:00
}
2019-04-03 08:40:14 +02:00
void B15F::analogSequence(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 10:33:16 +01:00
{
2019-03-29 13:35:06 +01:00
buffer_a += offset_a;
buffer_b += offset_b;
2019-04-02 08:46:42 +02:00
usart.clearInputBuffer();
2019-03-29 11:42:52 +01:00
usart.writeByte(RQ_ADC_DAC_STROKE);
usart.writeByte(channel_a);
usart.writeByte(channel_b);
usart.writeInt(start);
usart.writeInt(static_cast<uint16_t>(delta));
usart.writeInt(count);
2019-04-02 11:53:38 +02:00
/*
2019-03-29 11:42:52 +01:00
uint8_t aw = usart.readByte();
2019-03-28 15:22:06 +01:00
2019-03-29 11:42:52 +01:00
if(aw != MSG_OK)
2019-03-29 14:54:38 +01:00
throw DriverException("Mikrocontroller nicht synchron");
2019-04-02 11:53:38 +02:00
*/
2019-03-28 15:22:06 +01:00
2019-03-29 11:42:52 +01:00
for(uint16_t i = 0; i < count; i++)
2019-03-27 10:33:16 +01:00
{
2019-03-29 13:35:06 +01:00
buffer_a[i] = usart.readInt();
buffer_b[i] = usart.readInt();
2019-04-03 08:40:14 +02:00
if(buffer_a[i] > 1023 || buffer_b[i] > 1023)
std::cout << PRE << "bad data detected" << std::endl;
2019-03-27 10:33:16 +01:00
}
2019-03-29 11:42:52 +01:00
2019-04-02 11:53:38 +02:00
uint8_t aw = usart.readByte();
2019-04-03 08:40:14 +02:00
if(aw != MSG_OK)
throw DriverException("Sequenz unterbrochen");
delay_us(1);
2019-03-27 10:33:16 +01:00
}
2019-03-26 16:27:21 +01:00
2019-03-29 11:12:31 +01:00
void B15F::delay_ms(uint16_t ms)
2019-03-26 13:28:29 +01:00
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
2019-03-26 11:35:20 +01:00
}
2019-03-29 11:12:31 +01:00
void B15F::delay_us(uint16_t us)
{
std::this_thread::sleep_for(std::chrono::microseconds(us));
}
2019-03-29 14:54:38 +01:00
// https://stackoverflow.com/a/478960
std::string B15F::exec(std::string cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
2019-03-26 11:35:20 +01:00
2019-03-26 09:57:01 +01:00
B15F& B15F::getInstance(void)
{
if(!instance)
instance = new B15F();
return *instance;
}