Skip to content

Commit

Permalink
passthrough example
Browse files Browse the repository at this point in the history
  • Loading branch information
Carbon225 committed Oct 29, 2020
1 parent 5e3a6e4 commit f80de13
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 55 additions & 0 deletions examples/passthrough.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cstdio>
#include <ctime>
#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;
}

0 comments on commit f80de13

Please sign in to comment.