18 #ifndef HAND_BRIDGE_SDHX_H_
19 #define HAND_BRIDGE_SDHX_H_
22 #include "boost/format.hpp"
23 #include "boost/thread.hpp"
35 const size_t MAX_LINE = 1024;
37 char buffer[MAX_LINE+1] = {0};
39 int a = 0,
r = 1, offset = 0;
40 while(a >= 0 &&
r > 0){
43 while(
char * extra = strchr(line,
'\n')){
53 offset = (buffer + offset +
r - line);
54 strncpy(buffer, line, offset);
57 std::cerr <<
"stopped reading" << std::endl;
64 if(sscanf(line,
"rc=%hhx", &res) == 1){
73 template<
typename T>
bool tryReadValues(
const char *line, T (&val)[2],
const char *format,
bool track_time =
false) {
75 if(sscanf(line, format, &val1, &val2) == 2){
81 last_time = boost::chrono::steady_clock::now();
88 bool send(
const std::string &command){
93 bool setPWM(
const int16_t &val,
const char *format) {
94 return val == 0 ||
send(boost::str(boost::format(format) % val));
107 bool init(
const char* port, int16_t min_pwm0, int16_t min_pwm1, int16_t max_pwm0, int16_t max_pwm1){
108 std::string dev(port);
118 if(
setPWM(min_pwm0,
"set min_pwm0 %1%\r\n") &&
119 setPWM(min_pwm1,
"set min_pwm1 %1%\r\n") &&
120 setPWM(max_pwm0,
"set max_pwm0 %1%\r\n") &&
121 setPWM(max_pwm1,
"set max_pwm1 %1%\r\n")){
133 template <
typename Dur>
bool getData(int16_t (&p)[2], int16_t (&
v)[2], int16_t (&
c)[2],
const Dur &max_age){
135 if((boost::chrono::steady_clock::now() -
last_time) > max_age)
return false;
153 bool move(
const int16_t (&p)[2],
const int16_t (&
v)[2],
const int16_t (&
c)[2]){
154 static boost::format
command(
"m %hd,%hd %hd,%hd %hd,%hd\r\n");
167 #endif // HAND_BRIDGE_SDHX_H_