This is a simple program that drives a servo controller via UART to move the RC servo. You can use it for practicing the cross-compiling. Below is the complete C++ source code. The Arduino nano was program  to function as a servo controller, more details can be found here.  It is connected to UART pins of IOT+.

 

Serial.h

#ifndef SERIAL_H
#define	SERIAL_H
#include <cstdio>
#include <cstdlib>
#include <cstring>
#ifdef	__cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <syslog.h>

#ifdef	__cplusplus
}
#endif

class Serial {
protected:
    int fd;
public:

    Serial(const char tty_port[]);

    ~Serial() {
        if (fd > -1) {
            close(fd);
            fd = -1;
        }
    }

    void msleep(unsigned int ms);
private:
    void init(const char tty_port[]);

};

#endif	/* SERIAL_H */

 

Serial.cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>


#include "Serial.h"

Serial::Serial(const char tty_port[]) {
    init(tty_port);
}

void Serial::init(const char tty_port[]) {
    struct termios newtio;
    struct termios *termios_p = &newtio;

    fd = open(tty_port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd < 0) {
        syslog(LOG_NOTICE, "Failed to open port %s \n", tty_port);
        exit(EXIT_FAILURE);
    }
    bzero(&newtio, sizeof (newtio));
    cfsetispeed(&newtio, B115200);
    cfsetospeed(&newtio, B115200);

    newtio.c_cc[VTIME] = 0; /* inter-character timer unused while VMIN > 1*/
    newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */

    termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
            | INLCR | IGNCR | ICRNL | IXON);
    termios_p->c_oflag &= ~OPOST;
    termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    termios_p->c_cflag &= ~(CSIZE | PARENB);
    termios_p->c_cflag |= CS8;

    //tcflush(fd, TCIFLUSH);
    tcflush(fd, TCIOFLUSH);
    tcsetattr(fd, TCSANOW, &newtio);

}

void Serial::msleep(unsigned int ms) {
    usleep(ms * 1000);
}

 

Servo.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#ifdef	__cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#ifdef	__cplusplus
}
#endif
#include "Serial.h"


#define SERVO_MIN 500
#define SERVO_MAX 2500
#define PWM(angle) ((int) (SERVO_MIN + (SERVO_MAX - SERVO_MIN) * angle / 180.0))
using namespace std;

/*
 * 
 */
class Servo : public Serial {
public:
    Servo(const char tty_port[]);

    virtual ~Servo() {

    };
    void run(int start, int len, int angle, int delay);
    void run(int start, int angle, int delay);

};

Servo::Servo(const char tty_port[]) : Serial(tty_port) {

}

void Servo::run(int start, int angle, int delay) {
    run(start, 1, angle, delay);
}

void Servo::run(int start, int len, int angle, int delay) {
    std::string cmd;
    char buf[64];
    for (int i = 0; i < len; i++) {
        snprintf(buf, 64, "#%d P%d ", i, PWM(angle));
        cmd += buf;
    }
    snprintf(buf, 64, "T%d\r", delay);
    cmd += buf;
    //printf("%s\n", cmd.c_str());
    write(fd, cmd.c_str(), cmd.length());
    msleep(delay);
}
static std::string argsString = "\n"
        "  --port: serial port to use, e.g /dev/ttyUSB0\n\n";

int main(int argc, char** argv) {
    int c, option_index = 0;
    std::string optionName, tty_port;
    static struct option long_options[] = {
        {"port", required_argument, 0, 0},
        {0, 0, 0, 0}
    };
    opterr = 0;
    while ((c = getopt_long(argc, argv, "?",
            long_options, &option_index)) != -1) {
        switch (c) {
            case 0:
                /* If this option set a flag, do nothing else now. */
                if (long_options[option_index].flag != 0) break;
                optionName = long_options[option_index].name;
                if (optionName == "port") {
                    tty_port = optarg;
                }
                break;
            case '?':
                std::cout << "Usage: " << argv[0] << argsString;
                exit(0);
                break;
            default:
                break;
        }
    }
    if (tty_port == "") {
        std::cout << "Usage: " << argv[0] << argsString;
        exit(0);
    }
    Servo servo(tty_port.c_str());
    while (1) {
        servo.run(0, 0, 1000);
        servo.run(0, 180, 1000);
    }
    servo.~Servo();
    return 0;
}