Skip to content

Commit 598834a

Browse files
committed
add pin mocks
1 parent 9c83ea0 commit 598834a

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- Support for all builtin Bits and Bytes functions https://www.arduino.cc/reference/en/
1111
- Support for GODMODE and time functions
1212
- Support for Character functions https://www.arduino.cc/reference/en/
13-
- Support for `random` functions with seed control
13+
- Mocks for `random` functions with seed control
1414
- Many original Arduino `#define`s
15+
- Mocks for pinMode, analog/digital read/write
1516

1617
### Changed
1718
- Made `wget` have quieter output

SampleProjects/TestSomething/test/good-godmode.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,42 @@ unittest(random)
2727
assertEqual(74, x);
2828
}
2929

30+
void myInterruptHandler() {
31+
}
32+
33+
unittest(interrupts)
34+
{
35+
// these are meaningless for testing; just call the routine directly.
36+
// make sure our mocks work though
37+
attachInterrupt(2, myInterruptHandler, CHANGE);
38+
detachInterrupt(2);
39+
}
40+
41+
unittest(pins)
42+
{
43+
GodmodeState* state = GODMODE();
44+
state->reset();
45+
pinMode(1, OUTPUT); // this is a no-op in unit tests. it's just here to prove compilation
46+
digitalWrite(1, HIGH);
47+
assertEqual(HIGH, state->digitalPin[1]);
48+
digitalWrite(1, LOW);
49+
assertEqual(LOW, state->digitalPin[1]);
50+
51+
pinMode(1, INPUT);
52+
state->digitalPin[1] = HIGH;
53+
assertEqual(HIGH, digitalRead(1));
54+
state->digitalPin[1] = LOW;
55+
assertEqual(LOW, digitalRead(1));
56+
57+
analogWrite(1, 37);
58+
assertEqual(37, state->analogPin[1]);
59+
analogWrite(1, 22);
60+
assertEqual(22, state->analogPin[1]);
61+
62+
state->analogPin[1] = 99;
63+
assertEqual(99, analogRead(1));
64+
state->analogPin[1] = 56;
65+
assertEqual(56, analogRead(1));
66+
}
67+
3068
unittest_main()

cpp/arduino/Arduino.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ long random(long vmin, long vmax)
3838
{
3939
return vmin < vmax ? (random(vmax - vmin) + vmin) : vmin;
4040
}
41+
42+
void digitalWrite(uint8_t pin, uint8_t val) {
43+
godmode.digitalPin[pin] = val;
44+
}
45+
46+
int digitalRead(uint8_t pin) {
47+
return godmode.digitalPin[pin];
48+
}
49+
50+
int analogRead(uint8_t pin) {
51+
return godmode.analogPin[pin];
52+
}
53+
54+
void analogWrite(uint8_t pin, int val) {
55+
godmode.analogPin[pin] = val;
56+
}

cpp/arduino/Arduino.h

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,51 @@ Mock Arduino.h library.
55
Where possible, variable names from the Arduino library are used to avoid conflicts
66
77
*/
8+
#ifndef __did_sized_types
9+
typedef unsigned char uint8_t;
10+
// typedef __uint16_t uint16_t;
11+
// typedef __uint32_t uint32_t;
12+
// typedef __uint64_t uint64_t;
13+
#define __did_sized_types
14+
#endif
15+
16+
17+
18+
#include "ArduinoDefines.h"
19+
#include "binary.h"
20+
21+
// Math and Trig
22+
#include "AvrMath.h"
23+
24+
typedef bool boolean;
25+
typedef uint8_t byte;
26+
27+
28+
29+
#define MOCK_PINS_COUNT 256
830

931
class GodmodeState {
1032
public:
1133
unsigned long micros;
1234
unsigned long seed;
35+
// not going to put pinmode here unless its really needed. can't think of why it would be
36+
uint8_t digitalPin[MOCK_PINS_COUNT];
37+
int analogPin[MOCK_PINS_COUNT];
38+
39+
void resetPins() {
40+
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
41+
digitalPin[i] = LOW;
42+
analogPin[i] = 0;
43+
}
44+
}
1345

1446
void resetClock() {
1547
micros = 0;
1648
}
1749

1850
void reset() {
1951
resetClock();
52+
resetPins();
2053
seed = 1;
2154
}
2255

@@ -28,12 +61,6 @@ class GodmodeState {
2861

2962
GodmodeState* GODMODE();
3063

31-
#include "ArduinoDefines.h"
32-
33-
#include "binary.h"
34-
35-
// Math and Trig
36-
#include "AvrMath.h"
3764

3865
// Bits and Bytes
3966
#define bit(b) (1UL << (b))
@@ -48,9 +75,14 @@ GodmodeState* GODMODE();
4875
#define _NOP() do { 0; } while (0)
4976

5077
// might as well use that NO-op macro for these, while unit testing
78+
// you need interrupts? interrupt yourself
5179
#define yield() _NOP()
5280
#define interrupts() _NOP()
5381
#define noInterrupts() _NOP()
82+
#define attachInterrupt(...) _NOP()
83+
#define detachInterrupt(...) _NOP()
84+
85+
5486

5587
// Character stuff
5688
#include "WCharacter.h"
@@ -65,22 +97,23 @@ typedef unsigned int word;
6597

6698
#define bit(b) (1UL << (b))
6799

68-
#ifndef __did_sized_types
69-
typedef unsigned char uint8_t;
70-
// typedef __uint16_t uint16_t;
71-
// typedef __uint32_t uint32_t;
72-
// typedef __uint64_t uint64_t;
73-
#define __did_sized_types
74-
#endif
100+
// io pins
101+
#define pinMode(...) _NOP()
102+
#define analogReference(...) _NOP()
75103

76-
typedef bool boolean;
77-
typedef uint8_t byte;
104+
void digitalWrite(uint8_t, uint8_t);
105+
int digitalRead(uint8_t);
106+
int analogRead(uint8_t);
107+
void analogWrite(uint8_t, int);
108+
#define analogReadResolution(...) _NOP()
109+
#define analogWriteResolution(...) _NOP()
78110

79111

80112
// Get the bit location within the hardware port of the given virtual pin.
81113
// This comes from the pins_*.c file for the active board configuration.
82114

83115
#define analogInPinToBit(P) (P)
116+
#define digitalPinToInterrupt(P) (P)
84117

85118
// uint16_t makeWord(uint16_t w);
86119
// uint16_t makeWord(byte h, byte l);

0 commit comments

Comments
 (0)