Skip to content

Commit 4978aec

Browse files
committed
Add STLTest
Test abs/min/max/round called from C or C++ Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 8b2bdc6 commit 4978aec

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

examples/NonReg/STLTest/STLTest.ino

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
STLTest
3+
4+
Test abs/min/max/round called from C or C++
5+
6+
Creation 10 Oct 2017
7+
by Frederic Pillon <[email protected]>
8+
9+
This example code is in the public domain.
10+
*/
11+
12+
#include "test_stl.h"
13+
static uint32_t count_pressed = 0;
14+
static uint32_t random_val = 0;
15+
16+
int pushButton = USER_BTN;
17+
18+
// the setup routine runs once when you press reset:
19+
void setup() {
20+
// initialize serial communication at 115200 bits per second:
21+
Serial.begin(115200);
22+
// make the pushbutton's pin an input:
23+
pinMode(pushButton, INPUT);
24+
// if analog input pin 0 is unconnected, random analog
25+
// noise will cause the call to randomSeed() to generate
26+
// different seed numbers each time the sketch runs.
27+
// randomSeed() will then shuffle the random function.
28+
randomSeed(analogRead(0));
29+
}
30+
31+
// the loop routine runs over and over again forever:
32+
void loop() {
33+
// read the input pin:
34+
int buttonState = digitalRead(pushButton);
35+
random_val = random(300);
36+
if (buttonState == LOW)
37+
{
38+
count_pressed++;
39+
}
40+
41+
Serial.print("count pressed: ");
42+
Serial.print(count_pressed);
43+
Serial.print("\trandom: ");
44+
Serial.println(random_val);
45+
46+
Serial.print("C++\tmin: ");
47+
Serial.print(min(random_val,count_pressed));
48+
Serial.print("\tmax: ");
49+
Serial.print(max(random_val,count_pressed));
50+
Serial.print("\tabs: ");
51+
Serial.print(abs(count_pressed));
52+
Serial.print("\tround (count_pressed/random_val): ");
53+
Serial.println(round(count_pressed/random_val));
54+
55+
Serial.print("C\tmin: ");
56+
Serial.print(test_c(random_val,count_pressed, MIN));
57+
Serial.print("\tmax: ");
58+
Serial.print(test_c(random_val,count_pressed, MAX));
59+
Serial.print("\tabs: ");
60+
Serial.print(test_c(count_pressed, 0, ABS));
61+
Serial.print("\tround (count_pressed/random_val) is ");
62+
Serial.println(test_c(count_pressed, random_val, ROUND));
63+
64+
Serial.println();
65+
delay(100); // delay in between reads for stability
66+
}

examples/NonReg/STLTest/test_stl.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "test_stl.h"
2+
#include "Arduino.h"
3+
4+
int test_c(int a, int b, test t)
5+
{
6+
switch(t) {
7+
default:
8+
case ABS:
9+
return abs(a);
10+
break;
11+
case MIN:
12+
return min(a,b);
13+
break;
14+
case MAX:
15+
return max(a,b);
16+
break;
17+
case ROUND:
18+
return round(a/b);
19+
break;
20+
}
21+
22+
}
23+

examples/NonReg/STLTest/test_stl.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _STLTEST_H_
2+
#define _STLTEST_H_
3+
4+
typedef enum _test {
5+
ABS,
6+
MIN,
7+
MAX,
8+
ROUND
9+
} test;
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
int test_c(int a, int b, test t);
15+
#ifdef __cplusplus
16+
}
17+
#endif
18+
19+
20+
21+
#endif /*_STLTEST_H_*/

0 commit comments

Comments
 (0)