b15f/control/src/drv/b15f.cpp

389 lines
7.9 KiB
C++
Raw Normal View History

2019-03-26 08:57:01 +00:00
#include "b15f.h"
B15F* B15F::instance = nullptr;
2019-05-09 11:37:15 +00:00
errorhandler_t B15F::errorhandler = nullptr;
2019-03-26 08:57:01 +00:00
B15F::B15F()
{
2019-05-23 13:08:54 +00:00
init();
2019-03-26 08:57:01 +00:00
}
2019-03-26 14:02:41 +00:00
void B15F::init()
2019-03-26 08:57:01 +00:00
{
2019-05-23 13:08:54 +00: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)
abort("Adapter nicht gefunden");
std::cout << PRE << "Verwende Adapter: " << device << std::endl;
std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
usart.setBaudrate(BAUDRATE);
usart.openDevice(device);
std::cout << "OK" << std::endl;
std::cout << PRE << "Teste Verbindung... " << std::flush;
uint8_t tries = 3;
while(tries--)
{
// verwerfe Daten, die µC noch hat
//discard();
if(!testConnection())
continue;
if(!testIntConv())
continue;
break;
}
if(tries == 0)
abort("Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
std::cout << "OK" << std::endl;
// 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 08:18:21 +00:00
}
void B15F::reconnect()
{
2019-05-23 13:08:54 +00:00
uint8_t tries = RECONNECT_TRIES;
while(tries--)
{
delay_ms(RECONNECT_TIMEOUT);
discard();
if(testConnection())
return;
}
abort("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-05-23 13:08:54 +00:00
try
{
usart.clearOutputBuffer();
for(uint8_t i = 0; i < 16; i++)
{
usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
delay_ms(4);
}
usart.clearInputBuffer();
}
catch(std::exception& ex)
{
abort(ex);
}
2019-03-26 14:02:41 +00:00
}
bool B15F::testConnection()
2019-03-26 10:35:20 +00:00
{
2019-05-23 13:08:54 +00:00
// erzeuge zufälliges Byte
srand(time(NULL));
uint8_t dummy = rand() % 256;
usart.writeByte(RQ_TEST);
usart.writeByte(dummy);
uint8_t aw = usart.readByte();
uint8_t mirror = usart.readByte();
return aw == MSG_OK && mirror == dummy;
2019-03-26 10:35:20 +00:00
}
2019-03-26 14:02:41 +00:00
bool B15F::testIntConv()
2019-03-26 10:35:20 +00:00
{
2019-05-23 13:08:54 +00:00
srand(time(NULL));
uint16_t dummy = rand() % (0xFFFF / 3);
usart.writeByte(RQ_INT);
usart.writeInt(dummy);
uint16_t aw = usart.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)
{
2019-05-23 13:08:54 +00:00
std::vector<std::string> info;
usart.writeByte(RQ_INFO);
uint8_t n = usart.readByte();
while(n--)
{
uint8_t len = usart.readByte();
std::string str;
while(len--)
{
str += static_cast<char>(usart.readByte());
}
info.push_back(str);
}
uint8_t aw = usart.readByte();
if(aw != MSG_OK)
abort("Board Info fehlerhalft: code " + std::to_string((int) aw));
return info;
2019-03-27 14:48:24 +00:00
}
2019-03-26 15:27:21 +00:00
2019-04-03 09:59:15 +00:00
bool B15F::activateSelfTestMode()
{
2019-05-23 13:08:54 +00:00
usart.writeByte(RQ_ST);
uint8_t aw = usart.readByte();
return aw == MSG_OK;
2019-04-03 09:59:15 +00:00
}
2019-03-28 15:04:38 +00:00
bool B15F::digitalWrite0(uint8_t port)
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.writeByte(RQ_BA0);
usart.writeByte(port);
uint8_t aw = usart.readByte();
delay_us(10);
return aw == MSG_OK;
2019-03-26 15:27:21 +00:00
}
2019-03-28 15:04:38 +00:00
bool B15F::digitalWrite1(uint8_t port)
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.writeByte(RQ_BA1);
usart.writeByte(port);
uint8_t aw = usart.readByte();
delay_us(10);
return aw == MSG_OK;
2019-03-26 15:27:21 +00:00
}
2019-03-28 15:04:38 +00:00
uint8_t B15F::digitalRead0()
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.clearInputBuffer();
usart.writeByte(RQ_BE0);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
2019-03-26 15:27:21 +00:00
}
2019-03-28 15:04:38 +00:00
uint8_t B15F::digitalRead1()
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.clearInputBuffer();
usart.writeByte(RQ_BE1);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
2019-03-26 15:27:21 +00:00
}
2019-04-03 07:34:22 +00:00
uint8_t B15F::readDipSwitch()
{
2019-05-23 13:08:54 +00:00
usart.clearInputBuffer();
usart.writeByte(RQ_DSW);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
2019-04-03 07:34:22 +00:00
}
2019-03-28 15:04:38 +00:00
bool B15F::analogWrite0(uint16_t value)
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.writeByte(RQ_AA0);
usart.writeInt(value);
uint8_t aw = usart.readByte();
delay_us(10);
return aw == MSG_OK;
2019-03-26 15:27:21 +00:00
}
2019-03-28 15:04:38 +00:00
bool B15F::analogWrite1(uint16_t value)
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.writeByte(RQ_AA1);
usart.writeInt(value);
uint8_t aw = usart.readByte();
delay_us(10);
return aw == MSG_OK;
2019-03-26 15:27:21 +00:00
}
2019-03-28 15:04:38 +00:00
uint16_t B15F::analogRead(uint8_t channel)
2019-03-26 15:27:21 +00:00
{
2019-05-23 13:08:54 +00:00
usart.clearInputBuffer();
if(channel > 7)
abort("Bad ADC channel: " + std::to_string(channel));
uint8_t rq[] =
{
RQ_ADC,
channel
};
int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
if(n_sent != sizeof(rq))
abort("Sent failed");
uint16_t adc = usart.readInt();
if(adc > 1023)
abort("Bad ADC data detected (1)");
return adc;
2019-03-26 15:27:21 +00:00
}
2019-04-03 06:40:14 +00: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 09:33:16 +00:00
{
2019-05-23 13:08:54 +00:00
// check pointers
buffer_a += offset_a;
buffer_b += offset_b;
usart.clearInputBuffer();
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);
for(uint16_t i = 0; i < count; i++)
{
if(buffer_a)
{
buffer_a[i] = usart.readInt();
if(buffer_a[i] > 1023) // check for broken usart connection
abort("Bad ADC data detected (2)");
}
else
{
usart.readInt();
}
if(buffer_b)
{
buffer_b[i] = usart.readInt();
if(buffer_b[i] > 1023) // check for broken usart connection
abort("Bad ADC data detected (3)");
}
else
{
usart.readInt();
}
}
uint8_t aw = usart.readByte();
if(aw != MSG_OK)
abort("Sequenz unterbrochen");
delay_us(10);
2019-03-27 09:33:16 +00:00
}
2019-03-26 15:27:21 +00:00
2019-05-28 10:32:31 +00:00
uint8_t B15F::pwmSetFrequency(uint32_t freq)
{
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_PWM_SET_FREQ,
static_cast<uint8_t>((freq >> 0) & 0xFF),
static_cast<uint8_t>((freq >> 8) & 0xFF),
static_cast<uint8_t>((freq >> 16) & 0xFF),
static_cast<uint8_t>((freq >> 24) & 0xFF)
};
int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
if(n_sent != sizeof(rq))
abort("Sent failed");
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
}
bool B15F::pwmSetValue(uint8_t value)
{
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_PWM_SET_VALUE,
value
};
int n_sent = usart.write_timeout(&rq[0], 0, sizeof(rq), 1000);
if(n_sent != sizeof(rq))
abort("Sent failed");
uint8_t aw = usart.readByte();
delay_us(10);
return aw == MSG_OK;
}
2019-03-29 10:12:31 +00:00
void B15F::delay_ms(uint16_t ms)
2019-03-26 12:28:29 +00:00
{
2019-05-23 13:08:54 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
2019-03-26 10:35:20 +00:00
}
2019-03-29 10:12:31 +00:00
void B15F::delay_us(uint16_t us)
{
2019-05-23 13:08:54 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(us));
2019-03-29 10:12:31 +00:00
}
2019-05-23 13:08:54 +00:00
2019-05-09 11:37:15 +00:00
B15F& B15F::getInstance(void)
{
2019-05-23 13:08:54 +00:00
if(!instance)
instance = new B15F();
2019-05-09 11:37:15 +00:00
2019-05-23 13:08:54 +00:00
return *instance;
2019-05-09 11:37:15 +00:00
}
2019-03-29 13:54:38 +00:00
// https://stackoverflow.com/a/478960
2019-05-23 13:08:54 +00:00
std::string B15F::exec(std::string cmd)
{
2019-03-29 13:54:38 +00:00
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
2019-05-23 13:08:54 +00:00
if (!pipe)
{
2019-03-29 13:54:38 +00:00
throw std::runtime_error("popen() failed!");
}
2019-05-23 13:08:54 +00:00
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
{
2019-03-29 13:54:38 +00:00
result += buffer.data();
}
return result;
}
2019-05-09 11:37:15 +00:00
void B15F::abort(std::string msg)
2019-03-26 08:57:01 +00:00
{
2019-05-23 13:08:54 +00:00
DriverException ex(msg);
abort(ex);
2019-05-09 11:37:15 +00:00
}
void B15F::abort(std::exception& ex)
{
2019-05-23 13:08:54 +00:00
if(errorhandler)
errorhandler(ex);
else
{
std::cerr << "NOTICE: B15F::errorhandler not set" << std::endl;
std::cout << ex.what() << std::endl;
throw DriverException(ex.what());
}
2019-05-09 11:37:15 +00:00
}
void B15F::setAbortHandler(errorhandler_t func)
{
2019-05-23 13:08:54 +00:00
errorhandler = func;
2019-03-26 08:57:01 +00:00
}