diff --git a/drv/b15f.cpp b/drv/b15f.cpp index fc4ef28..a11139d 100644 --- a/drv/b15f.cpp +++ b/drv/b15f.cpp @@ -6,10 +6,10 @@ B15F::B15F() { } -void B15F::init(void) +void B15F::init() { - std::cout << PRE << "Stelle Verbindung mit Adapter her... "; + std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush; int code = system(std::string("stty 38400 -F " + SERIAL_DEVICE).c_str()); if(code) { @@ -29,9 +29,11 @@ void B15F::init(void) std::cout << "OK" << std::endl; + // verwerfe Daten, die µC noch hat + discard(); // Verbindungstest muss dreimal erfolgreich sein - std::cout << PRE << "Teste Verbindung... "; + std::cout << PRE << "Teste Verbindung... " << std::flush; for(uint8_t i = 0; i < 3; i++) { if(!testConnection()) @@ -40,21 +42,30 @@ void B15F::init(void) } } std::cout << "OK" << std::endl; + + std::cout << "int: " << testIntConv() << std::endl; + std::cout << "int: " << testIntConv() << std::endl; + std::cout << "int: " << testIntConv() << std::endl; - writeByte(0xFF); - writeByte(0x01); - writeByte(0x02); - std::cout << readByte() << std::endl; - std::cout << readByte() << std::endl; } -bool B15F::testConnection(void) +void B15F::discard(void) +{ + for(uint8_t i = 0; i < 8; i++) + { + writeByte(RQ_DISC); // sende discard Befehl (verwerfe input) + delay(1); + tcflush(usart, TCIFLUSH); // leere Puffer + } +} + +bool B15F::testConnection() { // erzeuge zufälliges Byte - srand (time(NULL)); + srand(time(NULL)); uint8_t dummy = rand() % 256; - writeByte(0); // echo / dummy command for testing + writeByte(RQ_TEST); writeByte(dummy); uint8_t aw = readByte(); @@ -63,25 +74,28 @@ bool B15F::testConnection(void) return aw == MSG_OK && mirror == dummy; } +bool B15F::testIntConv() +{ + srand(time(NULL)); + uint16_t dummy = rand() % (0xFFFF / 3); + + writeByte(RQ_INT); + writeInt(dummy); + + uint16_t aw = readInt(); + return aw == dummy * 3; +} + void B15F::writeByte(uint8_t b) { if(write(usart, &b, 1) != 1) - throw DriverException("Fehler beim Senden."); + throw DriverException("Fehler beim Senden. (byte)"); } void B15F::writeInt(uint16_t v) { - // static_cast(static_cast(&x)); - writeByte((v >> 8) & 0xFF); - writeByte((v >> 0) & 0xFF); -} - -void B15F::writeLong(uint32_t v) -{ - writeByte((v >> 24) & 0xFF); - writeByte((v >> 16) & 0xFF); - writeByte((v >> 8) & 0xFF); - writeByte((v >> 0) & 0xFF); + if(write(usart, reinterpret_cast(&v), 2) != 2) + throw DriverException("Fehler beim Senden. (int)"); } uint8_t B15F::readByte() @@ -113,15 +127,10 @@ uint8_t B15F::readByte() uint16_t B15F::readInt() { - return readByte() << 8 | readByte(); + return readByte() | readByte() << 8; } -uint32_t B15F::readLong() -{ - return readByte() << 24 | readByte() << 16 | readByte() << 8 | readByte(); -} - -void B15F::sleep(uint16_t ms) +void B15F::delay(uint16_t ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } diff --git a/drv/b15f.h b/drv/b15f.h index 2294934..7c71e0b 100644 --- a/drv/b15f.h +++ b/drv/b15f.h @@ -20,16 +20,16 @@ private: B15F(void); // privater Konstruktor public: void init(void); + void discard(void); bool testConnection(void); + bool testIntConv(void); inline void writeByte(uint8_t); inline void writeInt(uint16_t); - inline void writeLong(uint32_t); inline uint8_t readByte(void); inline uint16_t readInt(void); - inline uint32_t readLong(void); - void sleep(uint16_t); + void delay(uint16_t); static B15F& getInstance(void); @@ -45,6 +45,12 @@ private: const std::string SERIAL_DEVICE = "/dev/ttyUSB0"; constexpr static uint8_t MSG_OK = 0xFF; constexpr static uint8_t MSG_FAIL = 0xFE; + +// REQUESTS + constexpr static uint8_t RQ_DISC = 0; + constexpr static uint8_t RQ_TEST = 1; + constexpr static uint8_t RQ_INFO = 2; + constexpr static uint8_t RQ_INT = 3; }; #endif // B15F_h diff --git a/drv/b15f.o b/drv/b15f.o index 54c9087..1d44b6d 100644 Binary files a/drv/b15f.o and b/drv/b15f.o differ diff --git a/main b/main index 02f913e..064be73 100755 Binary files a/main and b/main differ