Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
Benchmark.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "benchmark.h"
3 
6 #define BUTTON_PIN 12
7 #define LED_PIN 13
8 
9 void setup()
10 {
11  Serial.begin(57600);
12  while (!Serial);
13 
14  // Set pin mode; Arduino style
15  pinMode(LED_PIN, OUTPUT);
16  pinMode(BUTTON_PIN, INPUT_PULLUP);
17 
18  // Set pin mode; GPIO style
19  led.output();
20  button.input().pullup();
21 
22  // Calculate baseline
23  BENCHMARK_BASELINE(1000);
24 }
25 
26 void loop()
27 {
28  BENCHMARK("1.1 Arduino core digitalRead", 1000) {
29  bool state = digitalRead(BUTTON_PIN);
30  (void) state;
31  }
32 
33  BENCHMARK("1.2 Arduino core digitalWrite(HIGH)", 1000) {
34  digitalWrite(LED_PIN, HIGH);
35  }
36 
37  BENCHMARK("1.3 Arduino core digitalWrite(LOW)", 1000) {
38  digitalWrite(LED_PIN, LOW);
39  }
40 
41  BENCHMARK("1.4 Arduino core toggle; digitalRead-digitalWrite", 1000) {
42  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
43  }
44 
45  BENCHMARK("1.5 Arduino core toggle; digitalWrite", 1000) {
46  digitalWrite(LED_PIN, HIGH);
47  digitalWrite(LED_PIN, LOW);
48  }
49 
50  BENCHMARK("2.1 GPIO pin value operator", 1000) {
51  bool state = button;
52  (void) state;
53  }
54 
55  BENCHMARK("2.2 GPIO high member function", 1000) {
56  led.high();
57  }
58 
59  BENCHMARK("2.3 GPIO low member function", 1000) {
60  led.low();
61  }
62 
63  BENCHMARK("2.4 GPIO assignment HIGH", 1000) {
64  led = HIGH;
65  }
66 
67  BENCHMARK("2.5 GPIO assignment LOW", 1000) {
68  led = LOW;
69  }
70 
71  BENCHMARK("2.6 GPIO toggle; value and assignment operator", 1000) {
72  led = !led;
73  }
74 
75  BENCHMARK("2.7 GPIO toggle; high and low member functions", 1000) {
76  led.high();
77  led.low();
78  }
79 
80  BENCHMARK("2.8 GPIO toggle; assignment operator, HIGH/LOW", 1000) {
81  led = HIGH;
82  led = LOW;
83  }
84 
85  BENCHMARK("2.9 GPIO toggle member function", 1000) {
86  led.toggle();
87  }
88 
89 #ifdef PORTH
90 
91  // GPIO atomic access of io ports with higher address. These
92  // benchmarks are for Arduino Mega pins that use ports above address
93  // 0x40 (PORTH, PORTJ, PINK and PINL). See Hardware/AVR/Board.h.
94 
95 #define DIN_PIN 6
96 #define DOUT_PIN 7
97  GPIO<BOARD::D6> din;
98  GPIO<BOARD::D7> dout;
99  din.input();
100  dout.input();
101 
102  BENCHMARK("3.1 Arduino core digitalRead", 1000) {
103  bool state = digitalRead(DIN_PIN);
104  (void) state;
105  }
106 
107  BENCHMARK("3.2 Arduino core digitalWrite(HIGH)", 1000) {
108  digitalWrite(DOUT_PIN, HIGH);
109  }
110 
111  BENCHMARK("3.3 Arduino core digitalWrite(LOW)", 1000) {
112  digitalWrite(DOUT_PIN, LOW);
113  }
114 
115  BENCHMARK("3.4 Arduino core toggle; digitalRead-digitalWrite", 1000) {
116  digitalWrite(DOUT_PIN, !digitalRead(DOUT_PIN));
117  }
118 
119  BENCHMARK("3.5 Arduino core toggle; digitalWrite", 1000) {
120  digitalWrite(DOUT_PIN, HIGH);
121  digitalWrite(DOUT_PIN, LOW);
122  }
123 
124  BENCHMARK("4.1 GPIO pin value operator", 1000) {
125  bool state = din;
126  (void) state;
127  }
128 
129  BENCHMARK("4.2 GPIO high member function", 1000) {
130  dout.high();
131  }
132 
133  BENCHMARK("4.3 GPIO low member function", 1000) {
134  dout.low();
135  }
136 
137  BENCHMARK("4.4 GPIO assignment HIGH", 1000) {
138  dout = HIGH;
139  }
140 
141  BENCHMARK("4.5 GPIO assignment LOW", 1000) {
142  dout = LOW;
143  }
144 
145  BENCHMARK("4.6 GPIO toggle; value and assignment operator", 1000) {
146  dout = !dout;
147  }
148 
149  BENCHMARK("4.7 GPIO toggle; high and low member functions", 1000) {
150  dout.high();
151  dout.low();
152  }
153 
154  BENCHMARK("4.8 GPIO toggle; assignment operator", 1000) {
155  dout = HIGH;
156  dout = LOW;
157  }
158 
159  BENCHMARK("4.9 GPIO toggle member function", 1000) {
160  dout.toggle();
161  }
162 #endif
163 
164  Serial.println();
165  delay(2000);
166 }
#define BUTTON_PIN
Definition: Benchmark.ino:6
#define LED_PIN
Definition: Benchmark.ino:7
void setup()
Definition: Benchmark.ino:11
GPIO< BOARD::D2 > button
Definition: Benchmark.ino:7
void high()
Definition: GPIO.h:104
GPIO< PIN > & input()
Definition: GPIO.h:36
void toggle()
Definition: GPIO.h:113
#define BENCHMARK(msg, scale)
Definition: benchmark.h:52
#define BENCHMARK_BASELINE(scale)
Definition: benchmark.h:34
Software::Serial< BOARD::D0 > Serial
Definition: Benchmark.ino:5
void loop()
Definition: Benchmark.ino:28
void output()
Definition: GPIO.h:56
Definition: GPIO.h:31
GPIO< BOARD::D1 > led
Definition: Benchmark.ino:6
void low()
Definition: GPIO.h:95