From c800ee85b25be2880be304bbb9b6a05f66efcd36 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 6 Dec 2022 21:05:18 +0100 Subject: [PATCH] add dip switch reading --- control/examples/wrapper/main.c | 8 ++++++++ control/src/drv/commit_hash.h | 2 +- control/src/wrapper/b15f.cpp | 35 +++++++++++++++++++++++++++------ control/src/wrapper/b15f.h | 23 +++++++++++++++++++++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/control/examples/wrapper/main.c b/control/examples/wrapper/main.c index 46a0f68..dbdf9c7 100644 --- a/control/examples/wrapper/main.c +++ b/control/examples/wrapper/main.c @@ -10,5 +10,13 @@ int main() { return -1; } + uint8_t dip = read_dip_switch(instance, NULL, 0); + + printf("DIP switch reads: "); + for(uint8_t mask = 1; mask != 0; mask <<= 1) { + printf("%d", (dip & mask) == mask ? 1 : 0); + } + printf("\n"); + return 0; } \ No newline at end of file diff --git a/control/src/drv/commit_hash.h b/control/src/drv/commit_hash.h index 91bf536..b8791b9 100644 --- a/control/src/drv/commit_hash.h +++ b/control/src/drv/commit_hash.h @@ -1,4 +1,4 @@ #ifndef COMMIT_HASH_H #define COMMIT_HASH_H -const char COMMIT_HASH[] = "c9b75ce062eab34b7b19a69e8b3206abf4ddfa3a"; +const char COMMIT_HASH[] = "ac85394396b3757b39c81e6010b1d0b76442f74c"; #endif // COMMIT_HASH_H diff --git a/control/src/wrapper/b15f.cpp b/control/src/wrapper/b15f.cpp index c3539c9..1f7105a 100644 --- a/control/src/wrapper/b15f.cpp +++ b/control/src/wrapper/b15f.cpp @@ -1,14 +1,37 @@ #include "b15f.h" #include "../drv/b15f.h" +#define wrap(x) reinterpret_cast(x) +#define unwrap(x) reinterpret_cast(x) + +#define default_catch catch(DriverException& e) {\ + if (err) { \ + strncpy(err, e.what(), len); \ + } \ +} + + b15f_t get_instance(char* err, size_t len) { try { - return reinterpret_cast(&B15F::getInstance()); - } catch(DriverException& e) { - if (err) { - strncpy(err, e.what(), len); - } - } + return wrap(&B15F::getInstance()); + } default_catch; return NULL; +} + +int reconnect(b15f_t drv, char* err, size_t len) { + try { + unwrap(drv).reconnect(); + return 0; + } default_catch; + + return -1; +} + +uint8_t read_dip_switch(b15f_t drv, char* err, size_t len) { + try { + return unwrap(drv).readDipSwitch(); + } default_catch; + + return 0; } \ No newline at end of file diff --git a/control/src/wrapper/b15f.h b/control/src/wrapper/b15f.h index 024ea10..7813f8f 100644 --- a/control/src/wrapper/b15f.h +++ b/control/src/wrapper/b15f.h @@ -2,6 +2,7 @@ #define _B15F_H_ #include +#include #ifdef __cplusplus extern "C" { @@ -10,7 +11,7 @@ extern "C" { typedef void* b15f_t; /** - * @brief Returns an instance of the B15F driver + * @brief Returns an instance of the B15 driver * * @param err Buffer to write the exception message to (can be NULL) * @param len Length of the error buffer @@ -18,6 +19,26 @@ extern "C" { */ b15f_t get_instance(char* err, size_t len); + /** + * @brief Tries to reconnect to the B15 + * + * @param drv Instance of the B15 driver + * @param err Error buffer + * @param len Size of the error buffer + * @return int 0 on success + */ + int reconnect(b15f_t drv, char* err, size_t len); + + /** + * @brief Reads the values of the DIP switch + * + * @param drv Instance of the B15 driver + * @param err Error buffer + * @param len Size of the error buffer + * @return uint8_t values of the DIP switch as a bitfield + */ + uint8_t read_dip_switch(b15f_t drv, char* err, size_t len); + #ifdef __cplusplus } #endif