3 B15F *B15F::instance =
nullptr;
4 errorhandler_t B15F::errorhandler =
nullptr;
14 instance =
new B15F();
38 abort(
"Verbindung kann nicht repariert werden");
51 for (uint8_t i = 0; i < 16; i++)
53 usart.
transmit(&rq[0], 0,
sizeof(rq));
58 catch (std::exception &ex)
68 uint8_t dummy = rand() % 256;
76 assertRequestLength(rq, RQ_TEST);
77 usart.
transmit(&rq[0], 0,
sizeof(rq));
80 usart.
receive(&aw[0], 0,
sizeof(aw));
83 assertCode(aw[1], dummy);
89 uint16_t dummy = rand() % (0xFFFF / 3);
94 static_cast<uint8_t >(dummy & 0xFF),
95 static_cast<uint8_t >(dummy >> 8)
98 assertRequestLength(rq, RQ_INT_TEST);
99 usart.
transmit(&rq[0], 0,
sizeof(rq));
102 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
104 assertCode(aw, dummy * 3);
110 std::vector<std::string> info;
117 assertRequestLength(rq, RQ_INFO);
118 usart.
transmit(&rq[0], 0,
sizeof(rq));
121 usart.
receive(&n, 0,
sizeof(n));
125 usart.
receive(&len, 0,
sizeof(len));
128 usart.
receive(reinterpret_cast<uint8_t *>(&str[0]), 0, len);
131 info.push_back(std::string(str));
135 usart.
receive(&aw, 0,
sizeof(aw));
137 abort(
"Board Info fehlerhalft: code " + std::to_string((
int) aw));
144 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
149 std::this_thread::sleep_for(std::chrono::microseconds(us));
154 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
155 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
156 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
162 std::array<char, 128> buffer;
164 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(),
"r"), pclose);
167 throw std::runtime_error(
"popen() failed!");
169 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr)
171 result += buffer.data();
188 std::cout << ex.what() << std::endl;
213 assertRequestLength(rq, RQ_SELF_TEST);
214 usart.
transmit(&rq[0], 0,
sizeof(rq));
217 usart.
receive(&aw, 0,
sizeof(aw));
231 assertRequestLength(rq, RQ_DIGITAL_WRITE_0);
232 usart.
transmit(&rq[0], 0,
sizeof(rq));
235 usart.
receive(&aw, 0,
sizeof(aw));
249 assertRequestLength(rq, RQ_DIGITAL_WRITE_1);
250 usart.
transmit(&rq[0], 0,
sizeof(rq));
253 usart.
receive(&aw, 0,
sizeof(aw));
265 assertRequestLength(rq, RQ_DIGITAL_READ_0);
266 usart.
transmit(&rq[0], 0,
sizeof(rq));
269 usart.
receive(&aw, 0,
sizeof(aw));
284 assertRequestLength(rq, RQ_DIGITAL_READ_1);
285 usart.
transmit(&rq[0], 0,
sizeof(rq));
288 usart.
receive(&aw, 0,
sizeof(aw));
303 assertRequestLength(rq, RQ_READ_DIP_SWITCH);
304 usart.
transmit(&rq[0], 0,
sizeof(rq));
307 usart.
receive(&aw, 0,
sizeof(aw));
319 static_cast<uint8_t >(value & 0xFF),
320 static_cast<uint8_t >(value >> 8)
323 assertRequestLength(rq, RQ_ANALOG_WRITE_0);
324 usart.
transmit(&rq[0], 0,
sizeof(rq));
327 usart.
receive(&aw, 0,
sizeof(aw));
336 static_cast<uint8_t >(value & 0xFF),
337 static_cast<uint8_t >(value >> 8)
340 assertRequestLength(rq, RQ_ANALOG_WRITE_1);
341 usart.
transmit(&rq[0], 0,
sizeof(rq));
344 usart.
receive(&aw, 0,
sizeof(aw));
352 abort(
"Bad ADC channel: " + std::to_string(channel));
360 assertRequestLength(rq, RQ_ANALOG_READ);
361 usart.
transmit(&rq[0], 0,
sizeof(rq));
364 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
367 abort(
"Bad ADC data detected (1)");
371 void B15F::analogSequence(uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b,
372 uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
375 buffer_a += offset_a;
376 buffer_b += offset_b;
385 static_cast<uint8_t >(start & 0xFF),
386 static_cast<uint8_t >(start >> 8),
387 static_cast<uint8_t >(delta & 0xFF),
388 static_cast<uint8_t >(delta >> 8),
389 static_cast<uint8_t >(count & 0xFF),
390 static_cast<uint8_t >(count >> 8)
393 assertRequestLength(rq, RQ_ADC_DAC_STROKE);
394 usart.
transmit(&rq[0], 0,
sizeof(rq));
396 for (uint16_t i = 0; i < count; i++)
400 usart.
receive(reinterpret_cast<uint8_t *>(&buffer_a[i]), 0, 2);
402 if (buffer_a[i] > 1023)
403 abort(
"Bad ADC data detected (2)");
412 usart.
receive(reinterpret_cast<uint8_t *>(&buffer_b[i]), 0, 2);
414 if (buffer_b[i] > 1023)
415 abort(
"Bad ADC data detected (3)");
424 usart.
receive(&aw, 0,
sizeof(aw));
435 static_cast<uint8_t>((freq >> 0) & 0xFF),
436 static_cast<uint8_t>((freq >> 8) & 0xFF),
437 static_cast<uint8_t>((freq >> 16) & 0xFF),
438 static_cast<uint8_t>((freq >> 24) & 0xFF)
441 assertRequestLength(rq, RQ_PWM_SET_FREQ);
442 usart.
transmit(&rq[0], 0,
sizeof(rq));
445 usart.
receive(&aw, 0,
sizeof(aw));
459 assertRequestLength(rq, RQ_PWM_SET_VALUE);
460 usart.
transmit(&rq[0], 0,
sizeof(rq));
463 usart.
receive(&aw, 0,
sizeof(aw));
474 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
475 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8),
479 assertRequestLength(rq, RQ_SET_MEM_8);
480 usart.
transmit(&rq[0], 0,
sizeof(rq));
483 usart.
receive(&aw, 0,
sizeof(aw));
494 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
495 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8)
498 assertRequestLength(rq, RQ_GET_MEM_8);
499 usart.
transmit(&rq[0], 0,
sizeof(rq));
502 usart.
receive(&aw, 0,
sizeof(aw));
513 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
514 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8),
515 static_cast<uint8_t >(val & 0xFF),
516 static_cast<uint8_t >(val >> 8)
519 assertRequestLength(rq, RQ_SET_MEM_16);
520 usart.
transmit(&rq[0], 0,
sizeof(rq));
523 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
534 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) & 0xFF),
535 static_cast<uint8_t >(reinterpret_cast<size_t>(adr) >> 8)
538 assertRequestLength(rq, RQ_GET_MEM_16);
539 usart.
transmit(&rq[0], 0,
sizeof(rq));
542 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
565 assertRequestLength(rq, RQ_COUNTER_OFFSET);
566 usart.
transmit(&rq[0], 0,
sizeof(rq));
569 usart.
receive(reinterpret_cast<uint8_t *>(&aw), 0,
sizeof(aw));
570 return reinterpret_cast<uint16_t*>(aw);
582 assertRequestLength(rq, RQ_SERVO_ENABLE);
583 usart.
transmit(&rq[0], 0,
sizeof(rq));
586 usart.
receive(&aw, 0,
sizeof(aw));
599 assertRequestLength(rq, RQ_SERVO_DISABLE);
600 usart.
transmit(&rq[0], 0,
sizeof(rq));
603 usart.
receive(&aw, 0,
sizeof(aw));
610 throw DriverException(
"Impulslänge ist zu lang: " + std::to_string(pos));
617 static_cast<uint8_t >(pos & 0xFF),
618 static_cast<uint8_t >(pos >> 8)
621 assertRequestLength(rq, RQ_SERVO_SET_POS);
622 usart.
transmit(&rq[0], 0,
sizeof(rq));
625 usart.
receive(&aw, 0,
sizeof(aw));
647 std::string device =
exec(
"bash -c 'ls /dev/ttyAMA* 2> /dev/null'");
650 std::string device =
exec(
"bash -c 'ls /dev/ttyUSB* 2> /dev/null'");
653 while (device.find(
' ') != std::string::npos || device.find(
'\n') != std::string::npos ||
654 device.find(
'\t') != std::string::npos)
657 if (device.length() == 0)
658 abort(
"Adapter nicht gefunden");
660 std::cout <<
PRE <<
"Verwende Adapter: " << device << std::endl;
663 std::cout <<
PRE <<
"Stelle Verbindung mit Adapter her... " << std::flush;
666 std::cout <<
"OK" << std::endl;
669 std::cout <<
PRE <<
"Teste Verbindung... " << std::flush;
696 abort(
"Verbindungstest fehlgeschlagen. Neueste Version im Einsatz?");
698 std::cout <<
"OK" << std::endl;
703 std::cout <<
PRE <<
"AVR Firmware Version: " << info[0] <<
" um " << info[1] <<
" Uhr (" << info[2] <<
")" 707 std::string& avr_commit_hash = info[3];
708 if(avr_commit_hash.compare(COMMIT_HASH))
710 std::cout <<
PRE <<
"Unterschiedliche commit hashes: " << std::endl;
711 std::cout << std::string(
PRE.length(),
' ') <<
"AVR: " << avr_commit_hash << std::endl;
712 std::cout << std::string(
PRE.length(),
' ') <<
"Control: " << COMMIT_HASH << std::endl << std::endl;
713 abort(
"Versionen inkompatibel. Bitte Software aktualisieren!");