Skip to content

Commit c9e39c7

Browse files
committed
Add Basics tests
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 3931a8b commit c9e39c7

File tree

8 files changed

+246
-1
lines changed

8 files changed

+246
-1
lines changed

examples/NonReg/STLTest/test_stl.c renamed to examples/NonReg/Basics/STLTest/test_stl.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ int test_c(int a, int b, test t)
1818
return round(a/b);
1919
break;
2020
}
21-
2221
}
2322

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Test some advanced I/O functions
3+
*/
4+
5+
/*
6+
* Variable for advanced I/O test
7+
*/
8+
int modeWR = 0;
9+
volatile int clknb = 0;
10+
volatile int data = 0;
11+
int clkOut = 2;
12+
int dataOut = 3;
13+
int clkIn = 4;
14+
int dataIn = 5;
15+
16+
void test_IO(void)
17+
{
18+
bool validate = true;
19+
int tmp = 0;
20+
int freq_test = 1000;
21+
22+
Serial.println();
23+
Serial.println("----------------------------------------");
24+
Serial.println("TEST ADVANCED I/O");
25+
Serial.println("Connect D2 with D4 and D3 with D5. Then press ENTER.");
26+
while (message((char*)"\n") == false) {yield();}
27+
28+
pinMode(clkOut, OUTPUT);
29+
pinMode(dataOut, OUTPUT);
30+
pinMode(clkIn, INPUT);
31+
pinMode(dataIn, INPUT);
32+
33+
tone(clkOut,freq_test);
34+
while(digitalRead(clkIn) == HIGH){yield();}
35+
if(digitalRead(clkIn) == LOW) {
36+
tmp = pulseIn(clkIn, HIGH);
37+
}
38+
noTone(clkOut);
39+
pinMode(clkOut, OUTPUT);
40+
41+
float freq_period_us = 1.0/(float)freq_test;
42+
tmp -= freq_period_us*1000000/2;
43+
tmp = abs(tmp);
44+
45+
//error = +/-1%
46+
if(tmp > 10) {validate = false;}
47+
48+
digitalWrite(clkOut, LOW);
49+
digitalWrite(dataOut, LOW);
50+
51+
attachInterrupt(clkIn, test_IO_IT, RISING);
52+
53+
clknb = 0;
54+
data = 0;
55+
modeWR = 0;
56+
shiftOut(dataOut, clkOut, LSBFIRST, 0xE2);
57+
if(data != 0xE2) {validate = false;}
58+
digitalWrite(clkOut, LOW);
59+
digitalWrite(dataOut, LOW);
60+
pinMode(dataIn, OUTPUT);
61+
pinMode(dataOut, INPUT);
62+
data = 0xD7;
63+
clknb = 0;
64+
65+
modeWR = 1;
66+
data = shiftIn(dataOut, clkOut, LSBFIRST);
67+
if(data != 0xD7) {validate = false;}
68+
69+
detachInterrupt(clkIn);
70+
71+
printResult(validate);
72+
}
73+
74+
void test_IO_IT(void)
75+
{
76+
if(modeWR == 0) {
77+
data |= digitalRead(dataIn) << clknb;
78+
clknb++;
79+
} else {
80+
int tmp = bitRead(data, clknb);
81+
digitalWrite(dataIn, tmp);
82+
clknb++;
83+
}
84+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Test some bytes functions
3+
*/
4+
void test_bytes(void)
5+
{
6+
bool validate = true;
7+
8+
Serial.println();
9+
Serial.println("----------------------------------------");
10+
Serial.println("TEST BIT & BYTES FUNCTIONS");
11+
12+
int val = 0x2563;
13+
14+
if(lowByte(val) != 0x63) {validate = false;}
15+
if(highByte(val) != 0x25) {validate = false;}
16+
if(bitRead(val, 5) != 1) {validate = false;}
17+
bitWrite(val, 3, 1);
18+
if(val != 0x256B) {validate = false;}
19+
bitSet(val, 12);
20+
if(val != 0x356B) {validate = false;}
21+
bitClear(val, 8);
22+
if(val != 0x346B) {validate = false;}
23+
val = bit(2) + bit(4);
24+
if(val != 0x14) {validate = false;}
25+
26+
printResult(validate);
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Test some math and trigo functions
3+
*/
4+
void test_math(void)
5+
{
6+
bool validate = true;
7+
8+
Serial.println();
9+
Serial.println("----------------------------------------");
10+
Serial.println("TEST MATH & TRIGONOMETRY FUNCTIONS");
11+
12+
if(min(1512,25) != 25) {validate = false;}
13+
if(max(1512,25) != 1512) {validate = false;}
14+
if((abs(-12) != 12) && (abs(52) != 52)) {validate = false;}
15+
if((round(13.99) != 13) && (round(13.01) != 13)) {validate = false;}
16+
if(pow(2,3) != 8) {validate = false;}
17+
if(sqrt(4) != 2) {validate = false;}
18+
if((sin(PI/2) - 1.0) > 0.00001) {validate = false;}
19+
if((cos(0) - 1.0) > 0.00001) {validate = false;}
20+
if((tan(PI/4) - 1.0) > 0.00001) {validate = false;}
21+
22+
printResult(validate);
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Test some the string functions
3+
*/
4+
void test_string(void)
5+
{
6+
int validate = true;
7+
8+
Serial.println();
9+
Serial.println("----------------------------------------");
10+
Serial.println("TEST STRING FUNCTIONS");
11+
12+
String str = "hello world!";
13+
String strTmp = String("helln");
14+
15+
if(strTmp.compareTo(str) != 0) {
16+
strTmp.concat(" world!");
17+
} else {validate = false;}
18+
19+
strTmp.remove(0,6);
20+
if(strTmp.equals("world!") == false) {validate = false;}
21+
22+
char cbuf[13];
23+
byte bbuf[13];
24+
str.getBytes(bbuf,13);
25+
str.toCharArray(cbuf,13);
26+
if(strcmp((const char *)bbuf,str.c_str()) != 0) {validate = false;}
27+
if(strcmp((const char *)cbuf,str.c_str()) != 0) {validate = false;}
28+
29+
strTmp = "12.34";
30+
if(strTmp.toFloat()-12.34 >= 0.00001) {validate = false;}
31+
32+
char sout[256]="";
33+
if(strcmp(dtostrf(1.23, 7, 2, sout), " 1.23") != 0) {validate = false;}
34+
if(strcmp(dtostrf(1.899, 2, 2, sout), "1.90") != 0) {validate = false;}
35+
if(strcmp(dtostrf(-123456.78910, 127, 6, sout),
36+
" -123456.789100"
37+
) != 0) {validate = false;}
38+
if(strcmp(dtostrf(-123456.78910, 128, 6, sout),
39+
"-123456.789100 "
40+
) != 0) {validate = false;}
41+
42+
printResult(validate);
43+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This sketch test several of the Arduino standard functions which
3+
* are not tested in the example sketches.
4+
*
5+
* !!! The human action is sometimes required!
6+
*
7+
* Created by Wi6Labs www.wi6labs.com
8+
* Modified by Frederic Pillon <frederic.pillon@st.com>
9+
*/
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
Serial.begin(9600);
14+
15+
list_test();
16+
}
17+
18+
void list_test(void)
19+
{
20+
Serial.println();
21+
Serial.println("**Enter a number to select a test**");
22+
Serial.println("1-String");
23+
Serial.println("2-Math");
24+
Serial.println("3-Bytes");
25+
Serial.println("4-Advanced I/O");
26+
}
27+
28+
void loop() {
29+
int test = 0;
30+
31+
while (Serial.available() > 0) {
32+
test = Serial.parseInt();
33+
Serial.flush();
34+
35+
switch(test)
36+
{
37+
case 1: test_string(); break;
38+
case 2: test_math(); break;
39+
case 3: test_bytes(); break;
40+
case 4: test_IO(); break;
41+
default: break;
42+
}
43+
44+
list_test();
45+
}
46+
47+
test = 0;
48+
}
49+
50+
bool message(char *str)
51+
{
52+
if (Serial.available() > 0) {
53+
if (Serial.find(str) == true)
54+
return true;
55+
}
56+
57+
return false;
58+
}
59+
60+
void printResult(int validate)
61+
{
62+
if(validate == true) {
63+
Serial.println("> OK <");
64+
} else {
65+
Serial.println(">>> FAIL <<<");
66+
}
67+
}
68+
69+
/*----------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)