Skip to content

Commit

Permalink
Include arduino project and enable all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jputcu committed Mar 8, 2013
1 parent b8289ff commit d1f9892
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 20 deletions.
22 changes: 16 additions & 6 deletions README.markdown
@@ -1,15 +1,25 @@
Objectives
----------
==========
* Cross platform: at least Linux, Windows and Mac OS.

Tests
=====

Setup
-----
Arduino Leonardo + FTDI breakout board
* [Arduino Leonardo](http://arduino.cc/en/Main/arduinoBoardLeonardo) + [Sparkfun FTDI breakout board](https://www.sparkfun.com/products/718).
* Connections: TX, RX and GND

Upload arduino code
Prepare Arduino
---------------
* Upload arduino code using Arduino IDE or avrdude

Configure cabal to build the tests: cabal configure --enable-tests.
Prepare haskell test program
----------------------------
* Configure cabal to build the tests: cabal configure --enable-tests.
* Build: cabal build

Build: cabal build
Running the tests
-----------------
* Run the tests: cabal test --test-options="/dev/ttyACM0 /dev/ttyUSB0"

Run the tests: cabal test --test-options="/dev/ttyACM0 /dev/ttyUSB0"
27 changes: 13 additions & 14 deletions tests/Tests.hs
Expand Up @@ -65,18 +65,15 @@ testHandle cs test_port control = TestCase $ do

testDelay :: String -> SerialPort -> Test
testDelay test_port control = TestCase $ do
s <- openSerial test_port defaultSerialSettings
s <- openSerial test_port defaultSerialSettings { timeout = 6 }
let control_char = 'h'
send control $ B.pack [control_char]
assertEqual "configure serial port" [control_char] . B.unpack =<< recv control 100

send s $ B.pack "a"
--threadDelay 1000
assertEqual "immediatly" "a" . B.unpack =<< recv s 10
assertEqual "immediatly" "A" . B.unpack =<< recv s 10
assertEqual "immediatly" "aA" . B.unpack =<< recv s 10

send s $ B.pack "b"
--threadDelay 100000
assertEqual "delay 50 ms" "bB" . B.unpack =<< recv s 10

send s $ B.pack "c"
Expand All @@ -91,15 +88,16 @@ tests :: String -> SerialPort -> Test
tests test_port control = TestList $ map (\(descr,fun) -> TestLabel descr (fun test_port control)) testCases
where
testCases = [
--("b1200 Serialport", testSerialport CS1200),
--("b2400 Serialport", testSerialport CS2400),
--("b4800 Serialport", testSerialport CS4800),
--("b9600 Serialport", testSerialport CS9600),
--("b19200 Serialport", testSerialport CS19200),
--("b57600 Serialport", testSerialport CS57600),
--("b115200 Serialport",testSerialport CS115200),
("test delay", testDelay)]
--("b9600 Handle", testHandle CS9600)]
("b1200 Serialport", testSerialport CS1200),
("b2400 Serialport", testSerialport CS2400),
("b4800 Serialport", testSerialport CS4800),
("b9600 Serialport", testSerialport CS9600),
("b19200 Serialport", testSerialport CS19200),
("b57600 Serialport", testSerialport CS57600),
("b115200 Serialport",testSerialport CS115200),
("b9600 Handle", testHandle CS9600),
("test delay", testDelay)
]


main :: IO ExitCode
Expand All @@ -109,3 +107,4 @@ main = do
if failures cnts == 0
then exitSuccess
else exitFailure

166 changes: 166 additions & 0 deletions tests/haskell_serial_test/haskell_serial_test.ino
@@ -0,0 +1,166 @@
static int test_running;
static uint8_t expected_char;

static const uint8_t lowest_expected_char = 0x00;
static const uint8_t highest_expected_char = 0xff;

// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
Serial.begin(9600);
pinMode(13, OUTPUT);
}



void ConsumeAllSerial()
{
while( Serial.available() )
Serial.read();
}



// the loop routine runs over and over again forever:
void loop()
{
if( Serial.available() )
{
int i = Serial.read();
switch(i)
{
case 'a':
Serial.write(i);
Serial1.end();
Serial1.begin(1200);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'b':
Serial.write(i);
Serial1.end();
Serial1.begin(2400);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'c':
Serial.write(i);
Serial1.end();
Serial1.begin(4800);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'd':
Serial.write(i);
Serial1.end();
Serial1.begin(9600);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'e':
Serial.write(i);
Serial1.end();
Serial1.begin(19200);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'f':
Serial.write(i);
Serial1.end();
Serial1.begin(57600);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'g':
Serial.write(i);
Serial1.end();
Serial1.begin(115200);
ConsumeAllSerial();
test_running = 1;
expected_char = lowest_expected_char;
break;
case 'h':
Serial.write(i);
Serial1.end();
Serial1.begin(9600);
ConsumeAllSerial();
test_running = 2;
break;
}
}

if( test_running == 1 )
{
while( Serial1.available() )
{
uint8_t i = Serial1.read() & 0xff;
if( i == expected_char)
{
Serial1.write(expected_char);
if( expected_char == highest_expected_char )
{
// test successfull
Serial.println("ok");
Serial1.flush();
Serial1.end();
test_running = 0;
}
else
expected_char++;
}
else
{
Serial.print("expected char('");
Serial.print(expected_char, HEX);
Serial.print("') got ('");
Serial.print(i, HEX);
Serial.println("') test failed");
}
}
}
else if( test_running == 2 )
{
while( Serial1.available() )
{
uint8_t i = Serial1.read() & 0xff;
switch( i )
{
case 'a':
Serial1.write('a');
Serial1.write('A');
break;
case 'b':
Serial1.write('b');
delay(50);
Serial1.write('B');
break;
case 'c':
Serial1.write('c');
delay(150);
Serial1.write('C');
break;
case 'd':
Serial1.write('d');
delay(200);
Serial1.write('D');
break;
case 'e':
// 50 ms delay
delay(500);
Serial1.write('E');
break;
default:
test_running = 0;
break;
}
}
}
}

0 comments on commit d1f9892

Please sign in to comment.