Skip to content

Commit fb1bbe7

Browse files
Merge pull request FabioBatSilva#4 from vvvlc/master
support for interrupt related functions
2 parents fbd4df8 + 05b6e42 commit fb1bbe7

File tree

8 files changed

+177
-2
lines changed

8 files changed

+177
-2
lines changed

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
.pioenvs
22
.piolibdeps
33
/external/unity/*-repo/
4-
/build/
4+
/build/
5+
/.cproject
6+
/.project
7+
**/CMakeFiles/*
8+
**/CMakeCache.txt
9+
**/*.cmake
10+
**/Makefile
11+
!/Makefile
12+
/Testing/*

CONTRIBUTING.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Contribution Guidelines
2+
This is a step-by-step guide for contributors.
3+
4+
## Adding missing function
5+
I was missing `sei()`, `cli()` and `attachInterrupt()` in `ArduinoFake`, here is list of steps I did.
6+
7+
8+
1. add definitions of new functions in [src/arduino/Arduino.h](/src/arduino/Arduino.h), check if your function is in [Arduino.h](/src/arduino/Arduino.h). There are two situations:
9+
* `attachInterrupt()` was already in [Arduino.h](/src/arduino/Arduino.h) so we are done.
10+
* `sei()` was not defined in [Arduino.h](/src/arduino/Arduino.h) so
11+
* create a new header file [avr/interrupt.h](/src/arduino/avr/interrupt.h) to cover interrupt related definitions with a content
12+
```c++
13+
/**
14+
* Fake version of avr/interrupt.h
15+
*/
16+
void cli(void);
17+
void sei(void);
18+
* add `#include "avr/interrupt.h"` in [Arduino.h](/src/arduino/Arduino.h)
19+
1. Find approriate place for your functions, in my case I extended [src/FunctionFake.h](/src/FunctionFake.h) for new functions
20+
```c++
21+
struct FunctionFake
22+
{
23+
...
24+
virtual void attachInterrupt(uint8_t, void (*)(void), int mode) = 0;
25+
virtual void cli() = 0;
26+
virtual void sei() = 0;
27+
...
28+
}
29+
```
30+
1. add default implementations into corresponding cpp file, in my case [src/FunctionFake.cpp](/src/FunctionFake.cpp).
31+
```c++
32+
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
33+
ArduinoFakeInstance(Function)->attachInterrupt(interruptNum, userFunc, mode);
34+
}
35+
36+
void cli(void) {
37+
ArduinoFakeInstance(Function)->cli();
38+
}
39+
40+
void sei(void) {
41+
ArduinoFakeInstance(Function)->sei();
42+
}
43+
```
44+
1. **don't forget to add TESTs** for new functionality, at least test if a function can be executed, in my case [test/test_function.h](/test/test_function.h)
45+
```c++
46+
void test_attach(void)
47+
{
48+
When(Method(ArduinoFake(), attachInterrupt)).AlwaysReturn();
49+
50+
attachInterrupt(1, (void (*)(void))NULL, FALLING);
51+
attachInterrupt(2, (void (*)(void))NULL, CHANGE);
52+
attachInterrupt(3, (void (*)(void))NULL, RISING);
53+
54+
Verify(Method(ArduinoFake(), attachInterrupt)).Exactly(3);
55+
}
56+
57+
void test_cli(void)
58+
{
59+
When(Method(ArduinoFake(), cli)).AlwaysReturn();
60+
61+
cli();
62+
63+
Verify(Method(ArduinoFake(), cli)).Once();
64+
}
65+
66+
void test_sei(void)
67+
{
68+
When(Method(ArduinoFake(), sei)).AlwaysReturn();
69+
70+
sei();
71+
72+
Verify(Method(ArduinoFake(), sei)).Once();
73+
}
74+
```
75+
and add tests to test list
76+
```c
77+
void run_tests(void)
78+
{
79+
...
80+
RUN_TEST(FunctionTest::test_attach);
81+
RUN_TEST(FunctionTest::test_cli);
82+
RUN_TEST(FunctionTest::test_sei);
83+
...
84+
}
85+
1. excersice tests from command line, there are two ways based on your Makefile
86+
* default project [Makefile](/Makefile),
87+
* execute `make`
88+
* verify
89+
```
90+
Running tests...
91+
Test project /home/vlcvi01/Dropbox/git/ArduinoFake/build
92+
Start 1: main
93+
1/1 Test #1: main ............................. Passed 0.01 sec
94+
95+
100% tests passed, 0 tests failed out of 1
96+
```
97+
* [eclipse based Makefile](https://www.mantidproject.org/Setting_up_Eclipse_projects_with_CMake) generated via `cmake -G "Eclipse CDT4 - Unix Makefiles"`.
98+
* execute `make clean all && test/main`
99+
* verify PASS of all tests
100+
```
101+
...
102+
.../ArduinoFake/test/main.cpp:184:FunctionTest::test_attach:PASS
103+
.../ArduinoFake/test/main.cpp:185:FunctionTest::test_sei:PASS
104+
.../ArduinoFake/test/main.cpp:186:FunctionTest::test_cli:PASS
105+
...
106+
107+
-----------------------
108+
39 Tests 0 Failures 0 Ignored
109+
OK
110+
```

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ void test_loop(void)
5656
5757
Checkout the [examples](./examples) for many more examples!
5858
Or take a look at the [tests](./test)
59+
60+
# Contributing
61+
If you want to extend `ArduinoFake` library to add missing functions (for example `attachInterrupt`) see [contribution guidelines](CONTRIBUTING.md).

src/FunctionFake.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ void detachInterrupt(uint8_t interruptNum) {
7474
ArduinoFakeInstance(Function)->detachInterrupt(interruptNum);
7575
}
7676

77+
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
78+
ArduinoFakeInstance(Function)->attachInterrupt(interruptNum, userFunc, mode);
79+
}
80+
81+
void cli(void) {
82+
ArduinoFakeInstance(Function)->cli();
83+
}
84+
85+
void sei(void) {
86+
ArduinoFakeInstance(Function)->sei();
87+
}
88+
7789
void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
7890
{
7991
ArduinoFakeInstance(Function)->tone(pin, frequency, duration);

src/FunctionFake.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ struct FunctionFake
2929
virtual uint8_t shiftIn(uint8_t, uint8_t, uint8_t) = 0;
3030

3131
virtual void detachInterrupt(uint8_t) = 0;
32+
virtual void attachInterrupt(uint8_t, void (*)(void), int mode) = 0;
33+
virtual void cli() = 0;
34+
virtual void sei() = 0;
3235

3336
virtual void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) = 0;
3437
virtual void noTone(uint8_t _pin) = 0;

src/arduino/Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <stdbool.h>
2626
#include <string.h>
2727
#include <math.h>
28+
#include "avr/interrupt.h"
29+
2830

2931
#include "binary.h"
3032

src/arduino/avr/interrupt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Fake version of avr/interrupt.h
3+
*/
4+
void cli(void);
5+
void sei(void);

test/test_function.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ namespace FunctionTest
9191
Verify(Method(ArduinoFake(), detachInterrupt).Using(1)).Once();
9292
}
9393

94+
void test_attach(void)
95+
{
96+
When(Method(ArduinoFake(), attachInterrupt)).AlwaysReturn();
97+
98+
attachInterrupt(1, (void (*)(void))NULL, FALLING);
99+
attachInterrupt(2, (void (*)(void))NULL, CHANGE);
100+
attachInterrupt(3, (void (*)(void))NULL, RISING);
101+
102+
Verify(Method(ArduinoFake(), attachInterrupt)).Exactly(3);
103+
}
104+
105+
void test_cli(void)
106+
{
107+
When(Method(ArduinoFake(), cli)).AlwaysReturn();
108+
109+
cli();
110+
111+
Verify(Method(ArduinoFake(), cli)).Once();
112+
}
113+
114+
void test_sei(void)
115+
{
116+
When(Method(ArduinoFake(), sei)).AlwaysReturn();
117+
118+
sei();
119+
120+
Verify(Method(ArduinoFake(), sei)).Once();
121+
}
122+
94123
void test_random(void)
95124
{
96125
When(Method(ArduinoFake(), randomSeed)).AlwaysReturn();
@@ -152,6 +181,9 @@ namespace FunctionTest
152181
RUN_TEST(FunctionTest::test_analog_pin);
153182
RUN_TEST(FunctionTest::test_delay);
154183
RUN_TEST(FunctionTest::test_detach);
184+
RUN_TEST(FunctionTest::test_attach);
185+
RUN_TEST(FunctionTest::test_cli);
186+
RUN_TEST(FunctionTest::test_sei);
155187
RUN_TEST(FunctionTest::test_pulsein);
156188
RUN_TEST(FunctionTest::test_shift);
157189
RUN_TEST(FunctionTest::test_random);
@@ -160,4 +192,4 @@ namespace FunctionTest
160192
}
161193
}
162194

163-
#endif
195+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy