diff --git a/CMakeLists.txt b/CMakeLists.txt index 3426338..23c0eea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,3 +10,6 @@ target_link_libraries(send_to_self PUBLIC sbuslib) add_executable(blocking_receiver examples/blocking_receiver.cpp) target_link_libraries(blocking_receiver PUBLIC sbuslib) + +add_executable(passthrough examples/passthrough.cpp) +target_link_libraries(passthrough PUBLIC sbuslib) diff --git a/examples/passthrough.cpp b/examples/passthrough.cpp new file mode 100644 index 0000000..51adc23 --- /dev/null +++ b/examples/passthrough.cpp @@ -0,0 +1,55 @@ +#include +#include +#include "SBUS.h" + +SBUS sbus; + +void onPacket(sbus_packet_t packet) +{ + static time_t lastPrint = time(nullptr); + time_t now = time(nullptr); + + sbus.write(packet); + + if (now > lastPrint) + { + lastPrint = now; + printf("ch1: %u\tch2: %u\tch3: %u\tch4: %u\t" + "ch5: %u\tch6: %u\tch7: %u\tch8: %u\t" + "ch9: %u\tch10: %u\tch11: %u\tch12: %u\t" + "ch13: %u\tch14: %u\tch15: %u\tch16: %u\tch17: %u\tch18: %u%s%s\n\r", + packet.channels[0], packet.channels[1], packet.channels[2], packet.channels[3], + packet.channels[4], packet.channels[5], packet.channels[6], packet.channels[7], + packet.channels[8], packet.channels[9], packet.channels[10], packet.channels[11], + packet.channels[12], packet.channels[13], packet.channels[14], packet.channels[15], + packet.ch17, packet.ch18, + packet.frameLost ? "\tFrame lost" : "", + packet.failsafe ? "\tFailsafe active" : ""); + } +} + +int main() +{ + printf("SBUS blocking passthrough example\n\r"); + + sbus.onPacket(onPacket); + + sbus_err_t err = sbus.install("/dev/ttyAMA0", true); + if (err != SBUS_OK) + { + fprintf(stderr, "SBUS install error: %d\n\r", err); + return err; + } + + while ((err = sbus.read()) != SBUS_FAIL) + { + if (err == SBUS_ERR_DESYNC) + { + fprintf(stderr, "SBUS desync\n\r"); + } + } + + fprintf(stderr, "SBUS error: %d\n\r", err); + + return err; +} \ No newline at end of file