From 9de92bf4db4e76aabc4c8051adfa0301da134744 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:10:01 -0600 Subject: [PATCH 1/6] Serial Test Readme --- Tests/README.txt | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Tests/README.txt diff --git a/Tests/README.txt b/Tests/README.txt new file mode 100644 index 0000000..561fcb3 --- /dev/null +++ b/Tests/README.txt @@ -0,0 +1,39 @@ +In order to quickly test Arduino code without unpluging and repluging the +Arduino, these tests have been writen to use PlatformIO. PlatformIO enables +combiling and uploading Arduino code, directly from the terminal without ever +leaving your chair. + + + +* Installing PlatformIO * + +In a Linux terminal, run the following commands: +> python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)" +> python get-platformio.py + +To confirm PlatformIO is installed sucessfully, run: +> platformio --help + + + +* Compiling and Uploading with PlatformIO * + +> # list serial ports +> platformio serialports list + +> # upload code +> platformio run -e due -t upload + +> # compile code +> platformio run -e due + + + +* Running Tests * + +Step 1. Upload the corresponding Arduino code with PlatformIO (as needed) +Step 2. Compile test w/dependencies: +> g++ --std=c++11 Test_.cpp [dependencies] -o run +Step 3. Run the test: +> ./run + From db98b1eeef98a020c8a55c35b17d85026115392f Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:32:36 -0600 Subject: [PATCH 2/6] Microcontroller Serial Inter-Communication --- .../PIO_ArduinoToBone/src/SerialTools.h | 145 ++++++++++++++ .../PIO_BoneToArduino/src/SerialTools.h | 145 ++++++++++++++ .../PIO_StructDataType/src/SerialTools.h | 145 ++++++++++++++ Tests/SerialTest/Serial.cpp | 179 ++++++++++++++++++ Tests/SerialTest/Serial.h | 58 ++++++ 5 files changed, 672 insertions(+) create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/src/SerialTools.h create mode 100644 Tests/SerialTest/PIO_BoneToArduino/src/SerialTools.h create mode 100644 Tests/SerialTest/PIO_StructDataType/src/SerialTools.h create mode 100644 Tests/SerialTest/Serial.cpp create mode 100644 Tests/SerialTest/Serial.h diff --git a/Tests/SerialTest/PIO_ArduinoToBone/src/SerialTools.h b/Tests/SerialTest/PIO_ArduinoToBone/src/SerialTools.h new file mode 100644 index 0000000..c1fe220 --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/src/SerialTools.h @@ -0,0 +1,145 @@ +/** + * Created by TekuConcept on 8/3/2016. + */ +#ifndef SERIAL_TOOLS_H +#define SERIAL_TOOLS_H + +/** + * Extend the functionality of the Serial object, + * providing various useful type value streams. + */ +class SerialTools{ +public: + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * READ FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /* reading strings is not yet implemented here */ + static uint8_t readByte() { + uint8_t result = 0; + readData((char*)&result, sizeof(uint8_t)); + return result; + } + + static float readFloat() { + float result = 0; + readData((char*)&result, sizeof(float)); + return result; + } + + static double readDouble() { + double result = 0; + readData((char*)&result, sizeof(double)); + return result; + } + + static int32_t readInt() { + int result = 0; + readData((char*)&result, sizeof(int32_t)); + return result; + } + + static uint32_t readUInt() { + uint32_t result = 0; + readData((char*)&result, sizeof(uint32_t)); + return result; + } + + static int16_t readShort() { + int16_t result = 0; + readData((char*)&result, sizeof(int16_t)); + return result; + } + + static uint16_t readUShort() { + uint16_t result = 0; + readData((char*)&result, sizeof(uint16_t)); + return result; + } + + static void readData(char* ptr, int size) { + while(Serial.available() < size); + for(int i = 0; i < size; i++) { + ptr[i] = Serial.read(); + } + } + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * WRITE FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /** + * Writes a null-terminated string to the Serial's + * out buffer. + */ + static void writeString(const char* data, int size) { + for(int i = 0; i < size; i++) { + Serial.print(data[i]); + } + Serial.print('\0'); + } + + static void writeByte(uint8_t out) { + writeData((char*)&out, sizeof(uint8_t)); + } + + static void writeFloat(float out) { + writeData((char*)&out, sizeof(float)); + } + + static void writeDouble(double out) { + writeData((char*)&out, sizeof(double)); + } + + static void writeInt(int32_t out) { + writeData((char*)&out, sizeof(int32_t)); + } + + static void writeUInt(uint32_t out) { + writeData((char*)&out, sizeof(uint32_t)); + } + + static void writeShort(int16_t out) { + writeData((char*)&out, sizeof(int16_t)); + } + + static void writeUShort(uint16_t out) { + writeData((char*)&out, sizeof(uint16_t)); + } + + static void writeData(char* ptr, int size) { + for(int i = 0; i < size; i++){ + // #ifdef DEBUG + // printHex((uint8_t)ptr[i]); + // #else + Serial.print(ptr[i]); + // #endif + } + } + + static void printHex(uint8_t c) { + printHexChar((uint8_t)((c&0xF0) >> 4)); + printHexChar((uint8_t)(c&0x0F)); + } + +private: + SerialTools(){} + static void printHexChar(uint8_t val) { + char res = '0'; + if(val < 10) + Serial.print(val); + else + switch(val-10) { + case 0: res = 'A'; break; + case 1: res = 'B'; break; + case 2: res = 'C'; break; + case 3: res = 'D'; break; + case 4: res = 'E'; break; + case 5: res = 'F'; break; + } + Serial.print(res); + } +}; + +#endif \ No newline at end of file diff --git a/Tests/SerialTest/PIO_BoneToArduino/src/SerialTools.h b/Tests/SerialTest/PIO_BoneToArduino/src/SerialTools.h new file mode 100644 index 0000000..c1fe220 --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/src/SerialTools.h @@ -0,0 +1,145 @@ +/** + * Created by TekuConcept on 8/3/2016. + */ +#ifndef SERIAL_TOOLS_H +#define SERIAL_TOOLS_H + +/** + * Extend the functionality of the Serial object, + * providing various useful type value streams. + */ +class SerialTools{ +public: + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * READ FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /* reading strings is not yet implemented here */ + static uint8_t readByte() { + uint8_t result = 0; + readData((char*)&result, sizeof(uint8_t)); + return result; + } + + static float readFloat() { + float result = 0; + readData((char*)&result, sizeof(float)); + return result; + } + + static double readDouble() { + double result = 0; + readData((char*)&result, sizeof(double)); + return result; + } + + static int32_t readInt() { + int result = 0; + readData((char*)&result, sizeof(int32_t)); + return result; + } + + static uint32_t readUInt() { + uint32_t result = 0; + readData((char*)&result, sizeof(uint32_t)); + return result; + } + + static int16_t readShort() { + int16_t result = 0; + readData((char*)&result, sizeof(int16_t)); + return result; + } + + static uint16_t readUShort() { + uint16_t result = 0; + readData((char*)&result, sizeof(uint16_t)); + return result; + } + + static void readData(char* ptr, int size) { + while(Serial.available() < size); + for(int i = 0; i < size; i++) { + ptr[i] = Serial.read(); + } + } + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * WRITE FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /** + * Writes a null-terminated string to the Serial's + * out buffer. + */ + static void writeString(const char* data, int size) { + for(int i = 0; i < size; i++) { + Serial.print(data[i]); + } + Serial.print('\0'); + } + + static void writeByte(uint8_t out) { + writeData((char*)&out, sizeof(uint8_t)); + } + + static void writeFloat(float out) { + writeData((char*)&out, sizeof(float)); + } + + static void writeDouble(double out) { + writeData((char*)&out, sizeof(double)); + } + + static void writeInt(int32_t out) { + writeData((char*)&out, sizeof(int32_t)); + } + + static void writeUInt(uint32_t out) { + writeData((char*)&out, sizeof(uint32_t)); + } + + static void writeShort(int16_t out) { + writeData((char*)&out, sizeof(int16_t)); + } + + static void writeUShort(uint16_t out) { + writeData((char*)&out, sizeof(uint16_t)); + } + + static void writeData(char* ptr, int size) { + for(int i = 0; i < size; i++){ + // #ifdef DEBUG + // printHex((uint8_t)ptr[i]); + // #else + Serial.print(ptr[i]); + // #endif + } + } + + static void printHex(uint8_t c) { + printHexChar((uint8_t)((c&0xF0) >> 4)); + printHexChar((uint8_t)(c&0x0F)); + } + +private: + SerialTools(){} + static void printHexChar(uint8_t val) { + char res = '0'; + if(val < 10) + Serial.print(val); + else + switch(val-10) { + case 0: res = 'A'; break; + case 1: res = 'B'; break; + case 2: res = 'C'; break; + case 3: res = 'D'; break; + case 4: res = 'E'; break; + case 5: res = 'F'; break; + } + Serial.print(res); + } +}; + +#endif \ No newline at end of file diff --git a/Tests/SerialTest/PIO_StructDataType/src/SerialTools.h b/Tests/SerialTest/PIO_StructDataType/src/SerialTools.h new file mode 100644 index 0000000..c1fe220 --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/src/SerialTools.h @@ -0,0 +1,145 @@ +/** + * Created by TekuConcept on 8/3/2016. + */ +#ifndef SERIAL_TOOLS_H +#define SERIAL_TOOLS_H + +/** + * Extend the functionality of the Serial object, + * providing various useful type value streams. + */ +class SerialTools{ +public: + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * READ FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /* reading strings is not yet implemented here */ + static uint8_t readByte() { + uint8_t result = 0; + readData((char*)&result, sizeof(uint8_t)); + return result; + } + + static float readFloat() { + float result = 0; + readData((char*)&result, sizeof(float)); + return result; + } + + static double readDouble() { + double result = 0; + readData((char*)&result, sizeof(double)); + return result; + } + + static int32_t readInt() { + int result = 0; + readData((char*)&result, sizeof(int32_t)); + return result; + } + + static uint32_t readUInt() { + uint32_t result = 0; + readData((char*)&result, sizeof(uint32_t)); + return result; + } + + static int16_t readShort() { + int16_t result = 0; + readData((char*)&result, sizeof(int16_t)); + return result; + } + + static uint16_t readUShort() { + uint16_t result = 0; + readData((char*)&result, sizeof(uint16_t)); + return result; + } + + static void readData(char* ptr, int size) { + while(Serial.available() < size); + for(int i = 0; i < size; i++) { + ptr[i] = Serial.read(); + } + } + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - *\ + * WRITE FUNCTIONS * + \* - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + /** + * Writes a null-terminated string to the Serial's + * out buffer. + */ + static void writeString(const char* data, int size) { + for(int i = 0; i < size; i++) { + Serial.print(data[i]); + } + Serial.print('\0'); + } + + static void writeByte(uint8_t out) { + writeData((char*)&out, sizeof(uint8_t)); + } + + static void writeFloat(float out) { + writeData((char*)&out, sizeof(float)); + } + + static void writeDouble(double out) { + writeData((char*)&out, sizeof(double)); + } + + static void writeInt(int32_t out) { + writeData((char*)&out, sizeof(int32_t)); + } + + static void writeUInt(uint32_t out) { + writeData((char*)&out, sizeof(uint32_t)); + } + + static void writeShort(int16_t out) { + writeData((char*)&out, sizeof(int16_t)); + } + + static void writeUShort(uint16_t out) { + writeData((char*)&out, sizeof(uint16_t)); + } + + static void writeData(char* ptr, int size) { + for(int i = 0; i < size; i++){ + // #ifdef DEBUG + // printHex((uint8_t)ptr[i]); + // #else + Serial.print(ptr[i]); + // #endif + } + } + + static void printHex(uint8_t c) { + printHexChar((uint8_t)((c&0xF0) >> 4)); + printHexChar((uint8_t)(c&0x0F)); + } + +private: + SerialTools(){} + static void printHexChar(uint8_t val) { + char res = '0'; + if(val < 10) + Serial.print(val); + else + switch(val-10) { + case 0: res = 'A'; break; + case 1: res = 'B'; break; + case 2: res = 'C'; break; + case 3: res = 'D'; break; + case 4: res = 'E'; break; + case 5: res = 'F'; break; + } + Serial.print(res); + } +}; + +#endif \ No newline at end of file diff --git a/Tests/SerialTest/Serial.cpp b/Tests/SerialTest/Serial.cpp new file mode 100644 index 0000000..3660af4 --- /dev/null +++ b/Tests/SerialTest/Serial.cpp @@ -0,0 +1,179 @@ +// +// Created by Nathan Copier on 1/28/2016. +// Refactored by TekuConcept on 2/13/2017. +// + +#include "Serial.h" + +std::mutex Serial::serialLock_; + +Serial::Serial(std::string device) { +#ifdef DEBUG + LOG("\nReceived Device Name: " << device); + LOG("\nEntering Serial Debug Mode\n"); + fd = 0; +#else + if((fd = open(device.c_str(), O_RDWR)) < 0) { + LOG("Device failed to open... entering dummy mode.\n"); + fd = 0; + return; + } + + configure(); +#endif +} + +Serial::~Serial() { + if(fd != 0) close(fd); +} + +void Serial::configure() { + struct termios topts; + if(tcgetattr(fd, &topts)) { + LOG("Failed to get terminal ios options from file descriptor.\n"); + throw 1; + } + if(cfsetispeed(&topts, B115200)) { + LOG("Failed to set input baud rate.\n"); + throw 1; + } + if(cfsetospeed(&topts, B115200)) { + LOG("Failed to set output baud rate.\n"); + throw 1; + } + topts.c_cflag &= ~(PARENB|CSTOPB|CSIZE|CRTSCTS); + topts.c_cflag |= CS8|CREAD|CLOCAL; + topts.c_iflag &= ~(IXON | IXOFF | IXANY); + topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + topts.c_oflag &= ~OPOST; + topts.c_cc[VMIN] = 1; + topts.c_cc[VTIME] = 0; + if(tcsetattr(fd, TCSANOW, &topts)) { + LOG("Failed to set terminal ios options for file descriptor.\n"); + throw 1; + } + if(tcflush(fd, TCIFLUSH)) { + LOG("Failed to flush file descriptor.\n"); + throw 1; + } +} + +std::string Serial::readString() { +#ifdef DEBUG + return "Dummy String\0"; +#else + if(fd==0) return ""; + std::stringstream ss; + char c[1]; + + do{ + read(fd, c, 1); + ss << c[0]; + }while(c[0] != '\0'); + return ss.str(); +#endif +} + +unsigned char Serial::readByte() { + unsigned char result = 0; + readData((char*)&result, 1); + return result; +} + +float Serial::readFloat() { + float result = 0; + readData((char*)&result, 4); + return result; +} + +double Serial::readDouble() { + double result = 0; +#ifdef DUE + readData((char*)&result, 8); +#else + float data = 0; + readData((char*)&data, 4); + result = (double)data; +#endif + return result; +} + +int Serial::readInt() { + int result = 0; + readData((char*)&result, 4); + return result; +} + +unsigned int Serial::readUInt() { + unsigned int result = 0; + readData((char*)&result, 4); + return result; +} + +short Serial::readShort() { + short result = 0; + readData((char*)&result, 2); + return result; +} + +unsigned short Serial::readUShort() { + unsigned short result = 0; + readData((char*)&result, 2); + return result; +} + +void Serial::readData(char* ptr, size_t size) { + std::lock_guard guard(serialLock_); + if(fd!=0) read(fd, ptr, size); + else for(unsigned int i = 0; i < size; i++) ptr[i] = 0; +} + + + + +void Serial::writeFloat(float value) { + writeData((char*)&value, 4); +} + +void Serial::writeDouble(double value) { +#ifdef DUE + writeData((char*)&value, 8); +#else + float val2 = (float)value; + writeData((char*)&value, 4); +#endif +} + +void Serial::writeInt(int value) { + writeData((char*)&value, 4); +} + +void Serial::writeUInt(unsigned int value) { + writeData((char*)&value, 4); +} + +void Serial::writeShort(short value) { + writeData((char*)&value, 2); +} + +void Serial::writeUShort(unsigned short value) { + writeData((char*)&value, 2); +} + +void Serial::writeByte(unsigned char value) { + writeData((char*)&value, 1); +} + +void Serial::writeData(char* ptr, size_t size) { +#ifdef DEBUG + LOG("Serial Write: " << std::hex << std::setw(2)); + for(size_t i = 0; i < size; i++) { + LOG((unsigned short)ptr[i]); + } + LOG(std::dec << std::endl); +#else + std::lock_guard guard(serialLock_); + if(fd == 0) return; + write(fd, ptr, size); +#endif +} \ No newline at end of file diff --git a/Tests/SerialTest/Serial.h b/Tests/SerialTest/Serial.h new file mode 100644 index 0000000..f3aa4ed --- /dev/null +++ b/Tests/SerialTest/Serial.h @@ -0,0 +1,58 @@ +// +// Created by Nathan Copier on 1/28/2016. +// Refactored by TekuConcept on 8/3/2016. +// + +#ifndef PERIPHERALS_SERIAL_H +#define PERIPHERALS_SERIAL_H + +#define DUE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOG(x) std::cerr << x + +class Serial { +private: + static std::mutex serialLock_; + int fd; + + void configure(); + +public: + Serial(std::string device); + ~Serial(); + + std::string readString(); + unsigned char readByte(); + float readFloat(); + double readDouble(); + int readInt(); + unsigned int readUInt(); + short readShort(); + unsigned short readUShort(); + void readData(char* ptr, size_t size); + + void writeFloat(float value); + void writeDouble(double value); + void writeInt(int value); + void writeUInt(unsigned int value); + void writeShort(short value); + void writeUShort(unsigned short value); + void writeByte(unsigned char value); + void writeData(char* ptr, size_t size); +}; + + +#endif //PERIPHERALS_SERIAL_H \ No newline at end of file From 370c833d059869cac09de77ea0be59f48f54fe65 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:38:16 -0600 Subject: [PATCH 3/6] Arduino To Bone Test --- .../PIO_ArduinoToBone/lib/readme.txt | 36 +++++++++++++ .../PIO_ArduinoToBone/platformio.ini | 19 +++++++ .../PIO_ArduinoToBone/src/Arduino.ino | 23 +++++++++ Tests/SerialTest/Test_ArduinoToBone.cpp | 51 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/lib/readme.txt create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/platformio.ini create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/src/Arduino.ino create mode 100644 Tests/SerialTest/Test_ArduinoToBone.cpp diff --git a/Tests/SerialTest/PIO_ArduinoToBone/lib/readme.txt b/Tests/SerialTest/PIO_ArduinoToBone/lib/readme.txt new file mode 100644 index 0000000..dbadc3d --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/lib/readme.txt @@ -0,0 +1,36 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Tests/SerialTest/PIO_ArduinoToBone/platformio.ini b/Tests/SerialTest/PIO_ArduinoToBone/platformio.ini new file mode 100644 index 0000000..b4fb831 --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +framework = arduino +board = uno + +[env:due] +platform = atmelsam +framework = arduino +board = due \ No newline at end of file diff --git a/Tests/SerialTest/PIO_ArduinoToBone/src/Arduino.ino b/Tests/SerialTest/PIO_ArduinoToBone/src/Arduino.ino new file mode 100644 index 0000000..38d4756 --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/src/Arduino.ino @@ -0,0 +1,23 @@ +#include +#include "SerialTools.h" + +#define B115200 115200 +#define GARBAGE 0 + +void setup() { + Serial.begin(B115200); + SerialTools::writeString("Ready!", 6); + while((!Serial.available())||(Serial.read()==GARBAGE)); + + int test = 0x0A0B0C0D; + SerialTools::writeData((char*)&test, 4); + SerialTools::writeByte(0xFF); + SerialTools::writeFloat(0.1F); + SerialTools::writeDouble(0.2); + SerialTools::writeInt(-3); + SerialTools::writeUInt(4); + SerialTools::writeShort(-5); + SerialTools::writeUShort(6); +} + +void loop() { } \ No newline at end of file diff --git a/Tests/SerialTest/Test_ArduinoToBone.cpp b/Tests/SerialTest/Test_ArduinoToBone.cpp new file mode 100644 index 0000000..f1f6dc6 --- /dev/null +++ b/Tests/SerialTest/Test_ArduinoToBone.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "Serial.h" + +#define MSG(x) std::cout << x +#define MSGN(x) std::cout << x << "\n" +#define ERR(x) std::cerr << x << "\n" +#define m_assert(x,y) (x) != (y) ? MSGN("FAIL") : MSGN("PASS") + +int main() { + MSGN("-- BEGIN TEST --"); + + MSG("\nCreating Serial Object... "); + Serial serial("/dev/ttyACM0"); + MSGN("Done!\n"); + + std::string res = serial.readString(); + MSG("Message: "); MSGN(res); + serial.writeByte('R'); + + MSGN("Receiving Static Values from Arduino..."); + + int bytes = 0; + MSG("> Receiving Bytes...\t\t"); + serial.readData((char*)&bytes, 4); + m_assert(bytes, 0x0A0B0C0D); + + MSG("> Receiving Byte...\t\t"); + m_assert(serial.readByte(), 0xFF); + + MSG("> Receiving Float...\t\t"); + m_assert(serial.readFloat(), 0.1F); + + MSG("> Receiving Double...\t\t"); + m_assert(serial.readDouble(), 0.2); + + MSG("> Receiving Int...\t\t"); + m_assert(serial.readInt(), -3); + + MSG("> Receiving Unsigned Int...\t"); + m_assert(serial.readUInt(), 4); + + MSG("> Receiving Short...\t\t"); + m_assert(serial.readShort(), -5); + + MSG("> Receiving Unsigned Short...\t"); + m_assert(serial.readUShort(), 6); + + MSGN("\n-- END TEST --"); + return 0; +} \ No newline at end of file From d42846484d699155772c278fb95601575d314b04 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:39:08 -0600 Subject: [PATCH 4/6] Bone To Arduino Test --- .../PIO_BoneToArduino/lib/readme.txt | 36 ++++++++++++++ .../PIO_BoneToArduino/platformio.ini | 19 ++++++++ .../PIO_BoneToArduino/src/Arduino.ino | 29 +++++++++++ Tests/SerialTest/Test_BoneToArduino.cpp | 48 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 Tests/SerialTest/PIO_BoneToArduino/lib/readme.txt create mode 100644 Tests/SerialTest/PIO_BoneToArduino/platformio.ini create mode 100644 Tests/SerialTest/PIO_BoneToArduino/src/Arduino.ino create mode 100644 Tests/SerialTest/Test_BoneToArduino.cpp diff --git a/Tests/SerialTest/PIO_BoneToArduino/lib/readme.txt b/Tests/SerialTest/PIO_BoneToArduino/lib/readme.txt new file mode 100644 index 0000000..dbadc3d --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/lib/readme.txt @@ -0,0 +1,36 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Tests/SerialTest/PIO_BoneToArduino/platformio.ini b/Tests/SerialTest/PIO_BoneToArduino/platformio.ini new file mode 100644 index 0000000..b4fb831 --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +framework = arduino +board = uno + +[env:due] +platform = atmelsam +framework = arduino +board = due \ No newline at end of file diff --git a/Tests/SerialTest/PIO_BoneToArduino/src/Arduino.ino b/Tests/SerialTest/PIO_BoneToArduino/src/Arduino.ino new file mode 100644 index 0000000..179ff4a --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/src/Arduino.ino @@ -0,0 +1,29 @@ +#include +#include "SerialTools.h" + +#define m_assert(T,V) if(T != V) { result = (result<<1)|1; } else { result <<= 1; } +#define B115200 115200 +#define GARBAGE 0 + +void setup() { + Serial.begin(B115200); + SerialTools::writeString("Ready!", 6); + while((!Serial.available())||(Serial.read()==GARBAGE)); + + uint16_t result = 0; + int test = 0; + + SerialTools::readData((char*)&test, sizeof(int)); + m_assert(test, 0x0A0B0C0D) + m_assert(SerialTools::readByte(), 0xFF) + m_assert(SerialTools::readFloat(), 0.1F) + m_assert(SerialTools::readDouble(), 0.2) + m_assert(SerialTools::readInt(), -3) + m_assert(SerialTools::readUInt(), 4) + m_assert(SerialTools::readShort(), -5) + m_assert(SerialTools::readUShort(), 6) + + SerialTools::writeInt(result); +} + +void loop() { } \ No newline at end of file diff --git a/Tests/SerialTest/Test_BoneToArduino.cpp b/Tests/SerialTest/Test_BoneToArduino.cpp new file mode 100644 index 0000000..5a73ca7 --- /dev/null +++ b/Tests/SerialTest/Test_BoneToArduino.cpp @@ -0,0 +1,48 @@ +#include +#include +#include "Serial.h" + +#define MSG(x) std::cout << x +#define MSGN(x) std::cout << x << "\n" +#define ERR(x) std::cerr << x << "\n" +#define m_assert(x) (x)&0x01 == 1 ? MSGN("FAIL") : MSGN("PASS") + +int main() { + MSGN("-- BEGIN TEST --"); + + MSG("\nCreating Serial Object... "); + Serial serial("/dev/ttyACM0"); + MSGN("Done!\n"); + + std::string res = serial.readString(); + MSG("Message: "); MSGN(res); + serial.writeByte('R'); + + MSGN("Sending Static Values to Arduino..."); + int bytes = 0x0A0B0C0D; + + serial.writeData((char*)&bytes, 4); + serial.writeByte(0xFF); + serial.writeFloat(0.1F); + serial.writeDouble(0.2); + serial.writeInt(-3); + serial.writeUInt(4); + serial.writeShort(-5); + serial.writeUShort(6); + + MSG("Results: "); + int ires = serial.readInt(); + + MSGN(ires); + MSG("> Sending Bytes\t\t\t"); m_assert(ires>>7); + MSG("> Sending Byte\t\t\t"); m_assert(ires>>6); + MSG("> Sending Float\t\t\t"); m_assert(ires>>5); + MSG("> Sending Double\t\t"); m_assert(ires>>4); + MSG("> Sending Int\t\t\t"); m_assert(ires>>3); + MSG("> Sending Unsigned Int\t\t"); m_assert(ires>>2); + MSG("> Sending Short\t\t\t"); m_assert(ires>>1); + MSG("> Sending Unsigned Short\t"); m_assert(ires>>0); + + MSGN("\n-- END TEST --"); + return 0; +} \ No newline at end of file From bd9dd25e3d8bd063ec42c5d68df2f6b4e5938f14 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:39:53 -0600 Subject: [PATCH 5/6] Struct Data Serial Test --- .../PIO_StructDataType/lib/readme.txt | 36 +++++++++ .../PIO_StructDataType/platformio.ini | 19 +++++ .../PIO_StructDataType/src/Arduino.ino | 47 ++++++++++++ Tests/SerialTest/Test_StructDataType.cpp | 76 +++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 Tests/SerialTest/PIO_StructDataType/lib/readme.txt create mode 100644 Tests/SerialTest/PIO_StructDataType/platformio.ini create mode 100644 Tests/SerialTest/PIO_StructDataType/src/Arduino.ino create mode 100644 Tests/SerialTest/Test_StructDataType.cpp diff --git a/Tests/SerialTest/PIO_StructDataType/lib/readme.txt b/Tests/SerialTest/PIO_StructDataType/lib/readme.txt new file mode 100644 index 0000000..dbadc3d --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/lib/readme.txt @@ -0,0 +1,36 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Tests/SerialTest/PIO_StructDataType/platformio.ini b/Tests/SerialTest/PIO_StructDataType/platformio.ini new file mode 100644 index 0000000..b4fb831 --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +framework = arduino +board = uno + +[env:due] +platform = atmelsam +framework = arduino +board = due \ No newline at end of file diff --git a/Tests/SerialTest/PIO_StructDataType/src/Arduino.ino b/Tests/SerialTest/PIO_StructDataType/src/Arduino.ino new file mode 100644 index 0000000..200f4ff --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/src/Arduino.ino @@ -0,0 +1,47 @@ +#include +#include "SerialTools.h" + +#define B115200 115200 +#define GARBAGE 0 +#define m_assert(T,V) if(T != V) { result = (result<<1)|1; } else { result <<= 1; } + +typedef struct Data { + float a; + double b; + int32_t c; + uint32_t d; + int16_t e; + uint16_t f; +} Data; + +void setup() { + Serial.begin(B115200); + SerialTools::writeString("Ready!", 6); + while((!Serial.available())||(Serial.read()==GARBAGE)); + + uint16_t result = 0; + + Data data; + data.a = 0.1F; + data.b = 0.2; + data.c = -3; + data.d = 4; + data.e = -5; + data.f = 6; + + SerialTools::writeData((char*)&data, sizeof(Data)); + + Data dmod; + SerialTools::readData((char*)&dmod, sizeof(Data)); + + m_assert(dmod.a, data.a*2) + m_assert(dmod.b, data.b*2) + m_assert(dmod.c, data.c*2) + m_assert(dmod.d, data.d*2) + m_assert(dmod.e, data.e*2) + m_assert(dmod.f, data.f*2) + + SerialTools::writeShort(result); +} + +void loop() { } \ No newline at end of file diff --git a/Tests/SerialTest/Test_StructDataType.cpp b/Tests/SerialTest/Test_StructDataType.cpp new file mode 100644 index 0000000..2f2c652 --- /dev/null +++ b/Tests/SerialTest/Test_StructDataType.cpp @@ -0,0 +1,76 @@ +#include +#include +#include "Serial.h" + +#define MSG(x) std::cout << x +#define MSGN(x) std::cout << x << "\n" +#define ERR(x) std::cerr << x << "\n" +#define m_assert(x,y) (x) != (y) ? MSGN("FAIL") : MSGN("PASS") + +typedef struct Data { + float a; + double b; + int c; + unsigned d; + short e; + unsigned short f; +} Data; + + + +int main() { + MSGN("-- BEGIN TEST --"); + + MSG("\nCreating Serial Object... "); + Serial serial("/dev/ttyACM0"); + MSGN("Done!\n"); + + std::string res = serial.readString(); + MSG("Message: "); MSGN(res); + serial.writeByte('R'); + + Data data; + data.a = 0.0F; + data.b = 0.0; + data.c = 0; + data.d = 0; + data.e = 0; + data.f = 0; + + MSGN("Receiving Static Values from Arduino..."); + serial.readData((char*)&data, sizeof(Data)); + // MSG("> "); MSGN(data.a); + // MSG("> "); MSGN(data.b); + // MSG("> "); MSGN(data.c); + // MSG("> "); MSGN(data.d); + // MSG("> "); MSGN(data.e); + // MSG("> "); MSGN(data.f); + MSG("> Float:\t"); m_assert(data.a, 0.1F); + MSG("> Double:\t"); m_assert(data.b, 0.2); + MSG("> Int:\t\t"); m_assert(data.c, -3); + MSG("> UInt:\t\t"); m_assert(data.d, 4); + MSG("> Short:\t"); m_assert(data.e, -5); + MSG("> UShort:\t"); m_assert(data.f, 6); + + data.a *= 2; + data.b *= 2; + data.c *= 2; + data.d *= 2; + data.e *= 2; + data.f *= 2; + + MSGN("Sending Modified Static Values to Arduino..."); + serial.writeData((char*)&data, sizeof(Data)); + short err = serial.readShort(); + MSG("Result: "); MSGN(err); + + MSG("> Float:\t"); m_assert((err>>5)&1, 0); + MSG("> Double:\t"); m_assert((err>>4)&1, 0); + MSG("> Int:\t\t"); m_assert((err>>3)&1, 0); + MSG("> UInt:\t\t"); m_assert((err>>2)&1, 0); + MSG("> Short:\t"); m_assert((err>>1)&1, 0); + MSG("> UShort:\t"); m_assert((err>>0)&1, 0); + + MSGN("\n-- END TEST --"); + return 0; +} \ No newline at end of file From ab3dc3bfbbd010c7925075e5dbfb4a287724c450 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 15 Mar 2017 18:41:58 -0600 Subject: [PATCH 6/6] Project Management --- Tests/SerialTest/PIO_ArduinoToBone/.gitignore | 2 + .../SerialTest/PIO_ArduinoToBone/.travis.yml | 65 +++++++++++++++++++ Tests/SerialTest/PIO_BoneToArduino/.gitignore | 2 + .../SerialTest/PIO_BoneToArduino/.travis.yml | 65 +++++++++++++++++++ .../SerialTest/PIO_StructDataType/.gitignore | 2 + .../SerialTest/PIO_StructDataType/.travis.yml | 65 +++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/.gitignore create mode 100644 Tests/SerialTest/PIO_ArduinoToBone/.travis.yml create mode 100644 Tests/SerialTest/PIO_BoneToArduino/.gitignore create mode 100644 Tests/SerialTest/PIO_BoneToArduino/.travis.yml create mode 100644 Tests/SerialTest/PIO_StructDataType/.gitignore create mode 100644 Tests/SerialTest/PIO_StructDataType/.travis.yml diff --git a/Tests/SerialTest/PIO_ArduinoToBone/.gitignore b/Tests/SerialTest/PIO_ArduinoToBone/.gitignore new file mode 100644 index 0000000..6c69f4c --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/.gitignore @@ -0,0 +1,2 @@ +.pioenvs +.piolibdeps diff --git a/Tests/SerialTest/PIO_ArduinoToBone/.travis.yml b/Tests/SerialTest/PIO_ArduinoToBone/.travis.yml new file mode 100644 index 0000000..2c4ff5c --- /dev/null +++ b/Tests/SerialTest/PIO_ArduinoToBone/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choice one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/Tests/SerialTest/PIO_BoneToArduino/.gitignore b/Tests/SerialTest/PIO_BoneToArduino/.gitignore new file mode 100644 index 0000000..6c69f4c --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/.gitignore @@ -0,0 +1,2 @@ +.pioenvs +.piolibdeps diff --git a/Tests/SerialTest/PIO_BoneToArduino/.travis.yml b/Tests/SerialTest/PIO_BoneToArduino/.travis.yml new file mode 100644 index 0000000..2c4ff5c --- /dev/null +++ b/Tests/SerialTest/PIO_BoneToArduino/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choice one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/Tests/SerialTest/PIO_StructDataType/.gitignore b/Tests/SerialTest/PIO_StructDataType/.gitignore new file mode 100644 index 0000000..6c69f4c --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/.gitignore @@ -0,0 +1,2 @@ +.pioenvs +.piolibdeps diff --git a/Tests/SerialTest/PIO_StructDataType/.travis.yml b/Tests/SerialTest/PIO_StructDataType/.travis.yml new file mode 100644 index 0000000..2c4ff5c --- /dev/null +++ b/Tests/SerialTest/PIO_StructDataType/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choice one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N