Skip to content

Commit

Permalink
Struct Data Serial Test
Browse files Browse the repository at this point in the history
  • Loading branch information
TekuConcept committed Mar 16, 2017
1 parent d428464 commit bd9dd25
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Tests/SerialTest/PIO_StructDataType/lib/readme.txt
Original file line number Diff line number Diff line change
@@ -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 <Foo.h>
#include <Bar.h>

// 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
19 changes: 19 additions & 0 deletions Tests/SerialTest/PIO_StructDataType/platformio.ini
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions Tests/SerialTest/PIO_StructDataType/src/Arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Arduino.h>
#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() { }
76 changes: 76 additions & 0 deletions Tests/SerialTest/Test_StructDataType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <string>
#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;
}

0 comments on commit bd9dd25

Please sign in to comment.