b15f.cpp uses only usart transmit & receive

This commit is contained in:
Tristan Krause 2019-06-04 10:28:01 +02:00
parent ab82f2f90e
commit b4996e84c8
72 changed files with 806 additions and 319 deletions

View file

@ -12,9 +12,7 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d13a1bae-0fd7-48fc-b53c-d6dd1aaf95b9" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/drv/usart.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usart.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/usart.h" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usart.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/usartexception.h" beforeDir="false" afterPath="$PROJECT_DIR$/drv/usartexception.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/drv/b15f.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/drv/b15f.cpp" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/cmake-build-debug/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -31,7 +29,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<state>
<caret line="6" column="8" selection-start-line="6" selection-start-column="8" selection-end-line="6" selection-end-column="8" />
</state>
</provider>
@ -40,7 +38,7 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/usart.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1140">
<state relative-caret-position="-1000">
<caret line="28" column="11" selection-start-line="28" selection-start-column="11" selection-end-line="28" selection-end-column="11" />
<folding>
<element signature="e#33#51#0" expanded="true" />
@ -52,8 +50,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-376">
<caret line="25" column="40" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="40" />
<state relative-caret-position="-25">
<caret line="70" column="1" lean-forward="true" selection-start-line="70" selection-start-column="1" selection-end-line="70" selection-end-column="1" />
</state>
</provider>
</entry>
@ -61,17 +59,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/backup.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="780">
<caret line="52" selection-start-line="52" selection-end-line="52" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="58">
<caret line="4" column="17" selection-start-line="4" selection-start-column="17" selection-end-line="4" selection-end-column="17" />
<state relative-caret-position="135">
<caret line="9" column="22" lean-forward="true" selection-start-line="9" selection-start-column="22" selection-end-line="9" selection-end-column="22" />
</state>
</provider>
</entry>
@ -79,17 +68,17 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/drv/b15f.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="267">
<caret line="232" column="43" selection-start-line="232" selection-start-column="39" selection-end-line="232" selection-end-column="43" />
<state relative-caret-position="285">
<caret line="34" column="41" selection-start-line="34" selection-start-column="41" selection-end-line="34" selection-end-column="41" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/drv/b15f.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="192">
<caret line="221" column="29" selection-start-line="221" selection-start-column="29" selection-end-line="221" selection-end-column="29" />
<state relative-caret-position="650">
<caret line="320" column="19" selection-start-line="320" selection-start-column="19" selection-end-line="320" selection-end-column="19" />
</state>
</provider>
</entry>
@ -115,11 +104,11 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/drv/b15f.cpp" />
<option value="$PROJECT_DIR$/drv/usart.cpp" />
<option value="$PROJECT_DIR$/drv/backup.cpp" />
<option value="$PROJECT_DIR$/drv/usart.h" />
<option value="$PROJECT_DIR$/drv/usartexception.h" />
<option value="$PROJECT_DIR$/drv/b15f.cpp" />
</list>
</option>
</component>
@ -202,12 +191,12 @@
<option name="presentableId" value="Default" />
<updated>1559118962147</updated>
<workItem from="1559118964304" duration="1913000" />
<workItem from="1559633658436" duration="1568000" />
<workItem from="1559633658436" duration="3165000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="3481000" />
<option name="totallyTimeSpent" value="5078000" />
</component>
<component name="ToolWindowManager">
<frame x="67" y="25" width="1853" height="1055" extended-state="6" />
@ -225,7 +214,7 @@
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Database Changes" order="7" />
<window_info anchor="bottom" id="Messages" order="8" weight="0.32937366" />
<window_info active="true" anchor="bottom" id="Terminal" order="9" visible="true" weight="0.44168466" />
<window_info active="true" anchor="bottom" id="Terminal" order="9" visible="true" weight="0.29049677" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="11" />
<window_info anchor="bottom" id="CMake" order="12" weight="0.33045357" />
@ -242,42 +231,42 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<state>
<caret line="6" column="8" selection-start-line="6" selection-start-column="8" selection-end-line="6" selection-end-column="8" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/b15f.cpp">
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="192">
<caret line="221" column="29" selection-start-line="221" selection-start-column="29" selection-end-line="221" selection-end-column="29" />
<state relative-caret-position="43">
<caret line="3" column="17" selection-start-line="3" selection-start-column="17" selection-end-line="3" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/b15f.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="267">
<caret line="232" column="43" selection-start-line="232" selection-start-column="39" selection-end-line="232" selection-end-column="43" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-376">
<caret line="25" column="40" selection-start-line="25" selection-start-column="32" selection-end-line="25" selection-end-column="40" />
<state relative-caret-position="285">
<caret line="34" column="41" selection-start-line="34" selection-start-column="41" selection-end-line="34" selection-end-column="41" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/backup.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="780">
<caret line="52" selection-start-line="52" selection-end-line="52" />
<state relative-caret-position="135">
<caret line="9" column="22" lean-forward="true" selection-start-line="9" selection-start-column="22" selection-end-line="9" selection-end-column="22" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-25">
<caret line="70" column="1" lean-forward="true" selection-start-line="70" selection-start-column="1" selection-end-line="70" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usart.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1140">
<state relative-caret-position="-1000">
<caret line="28" column="11" selection-start-line="28" selection-start-column="11" selection-end-line="28" selection-end-column="11" />
<folding>
<element signature="e#33#51#0" expanded="true" />
@ -285,10 +274,10 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/drv/usartexception.h">
<entry file="file://$PROJECT_DIR$/drv/b15f.cpp">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="58">
<caret line="4" column="17" selection-start-line="4" selection-start-column="17" selection-end-line="4" selection-end-column="17" />
<state relative-caret-position="650">
<caret line="320" column="19" selection-start-line="320" selection-start-column="19" selection-end-line="320" selection-end-column="19" />
</state>
</provider>
</entry>

View file

@ -1,6 +1,6 @@
#include "b15f.h"
B15F* B15F::instance = nullptr;
B15F *B15F::instance = nullptr;
errorhandler_t B15F::errorhandler = nullptr;
B15F::B15F()
@ -12,57 +12,57 @@ void B15F::init()
{
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)
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)
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--)
while (tries--)
{
// verwerfe Daten, die µC noch hat
//discard();
if(!testConnection())
if (!testConnection())
continue;
if(!testIntConv())
if (!testIntConv())
continue;
break;
}
if(tries == 0)
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;
std::cout << PRE << "AVR Firmware Version: " << info[0] << " um " << info[1] << " Uhr (" << info[2] << ")"
<< std::endl;
}
void B15F::reconnect()
{
uint8_t tries = RECONNECT_TRIES;
while(tries--)
while (tries--)
{
delay_ms(RECONNECT_TIMEOUT);
discard();
if(testConnection())
if (testConnection())
return;
}
@ -73,15 +73,20 @@ void B15F::discard(void)
{
try
{
usart.clearOutputBuffer();
for(uint8_t i = 0; i < 16; i++)
uint8_t rq[] =
{
usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
RQ_DISC
};
usart.clearOutputBuffer();
for (uint8_t i = 0; i < 16; i++)
{
usart.transmit(&rq[0], 0, sizeof(rq)); // sende discard Befehl (verwerfe input)
delay_ms(4);
}
usart.clearInputBuffer();
}
catch(std::exception& ex)
catch (std::exception &ex)
{
abort(ex);
}
@ -93,13 +98,17 @@ bool B15F::testConnection()
srand(time(NULL));
uint8_t dummy = rand() % 256;
usart.writeByte(RQ_TEST);
usart.writeByte(dummy);
uint8_t rq[] =
{
RQ_TEST,
dummy
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
uint8_t mirror = usart.readByte();
uint8_t aw[2];
usart.receive(&aw[0], 0, sizeof(aw));
return aw == MSG_OK && mirror == dummy;
return aw[0] == MSG_OK && aw[1] == dummy;
}
bool B15F::testIntConv()
@ -107,10 +116,17 @@ bool B15F::testIntConv()
srand(time(NULL));
uint16_t dummy = rand() % (0xFFFF / 3);
usart.writeByte(RQ_INT);
usart.writeInt(dummy);
uint8_t rq[] =
{
RQ_INT,
static_cast<uint8_t >(dummy & 0xFF),
static_cast<uint8_t >(dummy >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint16_t aw;
usart.receive(reinterpret_cast<uint8_t*>(&aw), 0, sizeof(aw));
uint16_t aw = usart.readInt();
return aw == dummy * 3;
}
@ -119,24 +135,30 @@ std::vector<std::string> B15F::getBoardInfo(void)
{
std::vector<std::string> info;
usart.writeByte(RQ_INFO);
uint8_t n = usart.readByte();
while(n--)
uint8_t rq[] =
{
uint8_t len = usart.readByte();
std::string str;
RQ_INFO
};
usart.transmit(&rq[0], 0, sizeof(rq));
while(len--)
{
str += static_cast<char>(usart.readByte());
}
uint8_t n;
usart.receive(&n, 0, sizeof(n));
info.push_back(str);
while (n--)
{
uint8_t len;
usart.receive(&len, 0, sizeof(len));
char str[len + 1];
str[len] = '\0';
usart.receive(reinterpret_cast<uint8_t *>(&str[0]), 0, len);
info.push_back(std::string(str));
}
uint8_t aw = usart.readByte();
if(aw != MSG_OK)
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
if (aw != MSG_OK)
abort("Board Info fehlerhalft: code " + std::to_string((int) aw));
return info;
@ -144,83 +166,121 @@ std::vector<std::string> B15F::getBoardInfo(void)
bool B15F::activateSelfTestMode()
{
usart.writeByte(RQ_ST);
uint8_t rq[] =
{
RQ_ST
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::digitalWrite0(uint8_t port)
{
usart.writeByte(RQ_BA0);
usart.writeByte(port);
uint8_t rq[] =
{
RQ_BA0,
port
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::digitalWrite1(uint8_t port)
{
usart.writeByte(RQ_BA1);
usart.writeByte(port);
uint8_t rq[] =
{
RQ_BA1,
port
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
uint8_t B15F::digitalRead0()
{
usart.clearInputBuffer();
usart.writeByte(RQ_BE0);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
uint8_t rq[] =
{
RQ_BE0
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
uint8_t B15F::digitalRead1()
{
usart.clearInputBuffer();
usart.writeByte(RQ_BE1);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
uint8_t rq[] =
{
RQ_BE1
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
uint8_t B15F::readDipSwitch()
{
usart.clearInputBuffer();
usart.writeByte(RQ_DSW);
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
uint8_t rq[] =
{
RQ_DSW
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
bool B15F::analogWrite0(uint16_t value)
{
usart.writeByte(RQ_AA0);
usart.writeInt(value);
uint8_t rq[] =
{
RQ_AA0,
static_cast<uint8_t >(value & 0xFF),
static_cast<uint8_t >(value >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::analogWrite1(uint16_t value)
{
usart.writeByte(RQ_AA1);
usart.writeInt(value);
uint8_t rq[] =
{
RQ_AA1,
static_cast<uint8_t >(value & 0xFF),
static_cast<uint8_t >(value >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
uint16_t B15F::analogRead(uint8_t channel)
{
usart.clearInputBuffer();
if(channel > 7)
if (channel > 7)
abort("Bad ADC channel: " + std::to_string(channel));
uint8_t rq[] =
@ -231,16 +291,20 @@ uint16_t B15F::analogRead(uint8_t channel)
usart.transmit(&rq[0], 0, sizeof(rq));
uint16_t adc = usart.readInt();
uint16_t aw;
usart.receive(reinterpret_cast<uint8_t*>(&aw), 0, sizeof(aw));
if(adc > 1023)
if (aw > 1023)
abort("Bad ADC data detected (1)");
return adc;
return aw;
}
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)
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)
{
// check pointers
/*
// prepare pointers
buffer_a += offset_a;
buffer_b += offset_b;
@ -253,38 +317,31 @@ void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
usart.writeInt(static_cast<uint16_t>(delta));
usart.writeInt(count);
for(uint16_t i = 0; i < count; i++)
{
if(buffer_a)
{
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
if (buffer_a[i] > 1023) // check for broken usart connection
abort("Bad ADC data detected (2)");
}
else
{
} else {
usart.readInt();
}
if(buffer_b)
{
if (buffer_b) {
buffer_b[i] = usart.readInt();
if(buffer_b[i] > 1023) // check for broken usart connection
if (buffer_b[i] > 1023) // check for broken usart connection
abort("Bad ADC data detected (3)");
}
else
{
} else {
usart.readInt();
}
}
uint8_t aw = usart.readByte();
if(aw != MSG_OK)
if (aw != MSG_OK)
abort("Sequenz unterbrochen");
delay_us(10);
delay_us(10);*/
}
uint8_t B15F::pwmSetFrequency(uint32_t freq)
@ -294,17 +351,17 @@ uint8_t B15F::pwmSetFrequency(uint32_t freq)
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 >> 0) & 0xFF),
static_cast<uint8_t>((freq >> 8) & 0xFF),
static_cast<uint8_t>((freq >> 16) & 0xFF),
static_cast<uint8_t>((freq >> 24) & 0xFF)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t byte = usart.readByte();
delay_us(10);
return byte;
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
bool B15F::pwmSetValue(uint8_t value)
@ -319,8 +376,8 @@ bool B15F::pwmSetValue(uint8_t value)
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
@ -337,9 +394,9 @@ bool B15F::setRegister(uint8_t adr, uint8_t val)
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t byte = usart.readByte();
delay_us(10);
return byte == val;
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == val;
}
uint8_t B15F::getRegister(uint8_t adr)
@ -354,8 +411,8 @@ uint8_t B15F::getRegister(uint8_t adr)
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
@ -370,9 +427,9 @@ void B15F::delay_us(uint16_t us)
std::this_thread::sleep_for(std::chrono::microseconds(us));
}
B15F& B15F::getInstance(void)
B15F &B15F::getInstance(void)
{
if(!instance)
if (!instance)
instance = new B15F();
return *instance;
@ -400,9 +457,10 @@ void B15F::abort(std::string msg)
DriverException ex(msg);
abort(ex);
}
void B15F::abort(std::exception& ex)
void B15F::abort(std::exception &ex)
{
if(errorhandler)
if (errorhandler)
errorhandler(ex);
else
{

View file

@ -0,0 +1,439 @@
#include "b15f.h"
B15F *B15F::instance = nullptr;
errorhandler_t B15F::errorhandler = nullptr;
B15F::B15F() {
init();
}
void B15F::init() {
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;
}
void B15F::reconnect() {
uint8_t tries = RECONNECT_TRIES;
while (tries--) {
delay_ms(RECONNECT_TIMEOUT);
discard();
if (testConnection())
return;
}
abort("Verbindung kann nicht repariert werden");
}
void B15F::discard(void) {
try {
uint8_t rq[] =
{
RQ_DISC
};
usart.clearOutputBuffer();
for (uint8_t i = 0; i < 16; i++) {
usart.transmit(&rq[0], 0, sizeof(rq)); // sende discard Befehl (verwerfe input)
delay_ms(4);
}
usart.clearInputBuffer();
}
catch (std::exception &ex) {
abort(ex);
}
}
bool B15F::testConnection() {
// erzeuge zufälliges Byte
srand(time(NULL));
uint8_t dummy = rand() % 256;
uint8_t rq[] =
{
RQ_TEST,
dummy
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw[2];
usart.receive(&aw[0], 0, sizeof(aw));
return aw[0] == MSG_OK && aw[1] == dummy;
}
bool B15F::testIntConv() {
srand(time(NULL));
uint16_t dummy = rand() % (0xFFFF / 3);
uint8_t rq[] =
{
RQ_INT,
static_cast<uint8_t >(dummy & 0xFF),
static_cast<uint8_t >(dummy >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint16_t aw;
usart.receive(reinterpret_cast<uint8_t*>(&aw), 0, sizeof(aw));
return aw == dummy * 3;
}
std::vector<std::string> B15F::getBoardInfo(void) {
std::vector<std::string> info;
uint8_t rq[] =
{
RQ_INFO
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t n;
usart.receive(&n, 0, sizeof(n));
while (n--) {
uint8_t len;
usart.receive(&len, 0, sizeof(len));
char str[len + 1];
str[len] = '\0';
usart.receive(reinterpret_cast<uint8_t *>(&str[0]), 0, len);
info.push_back(std::string(str));
}
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
if (aw != MSG_OK)
abort("Board Info fehlerhalft: code " + std::to_string((int) aw));
return info;
}
bool B15F::activateSelfTestMode() {
uint8_t rq[] =
{
RQ_ST
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::digitalWrite0(uint8_t port) {
uint8_t rq[] =
{
RQ_BA0,
port
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::digitalWrite1(uint8_t port) {
uint8_t rq[] =
{
RQ_BA1,
port
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
uint8_t B15F::digitalRead0() {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_BE0
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
uint8_t B15F::digitalRead1() {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_BE1
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
uint8_t B15F::readDipSwitch() {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_DSW
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
bool B15F::analogWrite0(uint16_t value) {
uint8_t rq[] =
{
RQ_AA0,
static_cast<uint8_t >(value & 0xFF),
static_cast<uint8_t >(value >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::analogWrite1(uint16_t value) {
uint8_t rq[] =
{
RQ_AA1,
static_cast<uint8_t >(value & 0xFF),
static_cast<uint8_t >(value >> 8)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
uint16_t B15F::analogRead(uint8_t channel) {
usart.clearInputBuffer();
if (channel > 7)
abort("Bad ADC channel: " + std::to_string(channel));
uint8_t rq[] =
{
RQ_ADC,
channel
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint16_t aw;
usart.receive(reinterpret_cast<uint8_t*>(&aw), 0, sizeof(aw));
if (aw > 1023)
abort("Bad ADC data detected (1)");
return aw;
}
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) {
/*
// prepare 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);*/
}
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)
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
bool B15F::pwmSetValue(uint8_t value) {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_PWM_SET_VALUE,
value
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == MSG_OK;
}
bool B15F::setRegister(uint8_t adr, uint8_t val) {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_SET_REG,
adr,
val
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw == val;
}
uint8_t B15F::getRegister(uint8_t adr) {
usart.clearInputBuffer();
uint8_t rq[] =
{
RQ_GET_REG,
adr
};
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw;
usart.receive(&aw, 0, sizeof(aw));
return aw;
}
void B15F::delay_ms(uint16_t ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
void B15F::delay_us(uint16_t us) {
std::this_thread::sleep_for(std::chrono::microseconds(us));
}
B15F &B15F::getInstance(void) {
if (!instance)
instance = new B15F();
return *instance;
}
// 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;
}
void B15F::abort(std::string msg) {
DriverException ex(msg);
abort(ex);
}
void B15F::abort(std::exception &ex) {
if (errorhandler)
errorhandler(ex);
else {
std::cerr << "NOTICE: B15F::errorhandler not set" << std::endl;
std::cout << ex.what() << std::endl;
throw DriverException(ex.what());
}
}
void B15F::setAbortHandler(errorhandler_t func) {
errorhandler = func;
}