-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathGodmode.h
185 lines (153 loc) · 4.54 KB
/
Godmode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include "ArduinoDefines.h"
#include <avr/io.h>
#include "WString.h"
#include "PinHistory.h"
// random
void randomSeed(unsigned long seed);
long random(long vmax);
long random(long vmin, long vmax);
// Time
void delay(unsigned long millis);
void delayMicroseconds(unsigned long micros);
unsigned long millis();
unsigned long micros();
#define MOCK_PINS_COUNT 256
#if defined(UBRR3H)
#define NUM_SERIAL_PORTS 4
#elif defined(UBRR2H)
#define NUM_SERIAL_PORTS 3
#elif defined(UBRR1H)
#define NUM_SERIAL_PORTS 2
#elif defined(UBRRH) || defined(UBRR0H)
#define NUM_SERIAL_PORTS 1
#else
#define NUM_SERIAL_PORTS 0
#endif
// different EEPROM implementations have different macros that leak out
#if !defined(EEPROM_SIZE) && defined(E2END) && (E2END)
// public value indicates that feature is available
#define EEPROM_SIZE (E2END + 1)
// local array size
#define _EEPROM_SIZE EEPROM_SIZE
#else
// feature is not available but we want to have the array so other code compiles
#define _EEPROM_SIZE (0)
#endif
class GodmodeState {
private:
struct PortDef {
String dataIn;
String dataOut;
unsigned long readDelayMicros;
};
struct InterruptDef {
bool attached;
uint8_t mode;
};
uint8_t mmapPorts[MOCK_PINS_COUNT];
static GodmodeState* instance;
public:
unsigned long micros;
unsigned long seed;
// not going to put pinmode here unless its really needed. can't think of why it would be
PinHistory<bool> digitalPin[MOCK_PINS_COUNT];
PinHistory<int> analogPin[MOCK_PINS_COUNT];
struct PortDef serialPort[NUM_SERIAL_PORTS];
struct InterruptDef interrupt[MOCK_PINS_COUNT]; // not sure how to get actual number
struct PortDef spi;
uint8_t eeprom[_EEPROM_SIZE];
void resetPins() {
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
digitalPin[i].reset(LOW);
analogPin[i].reset(0);
}
}
void resetClock() {
micros = 0;
}
void resetInterrupts() {
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
interrupt[i].attached = false;
}
}
void resetPorts() {
for (int i = 0; i < serialPorts(); ++i)
{
serialPort[i].dataIn = "";
serialPort[i].dataOut = "";
serialPort[i].readDelayMicros = 0;
}
}
void resetSPI() {
spi.dataIn = "";
spi.dataOut = "";
spi.readDelayMicros = 0;
}
void resetMmapPorts() {
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
mmapPorts[i] = 1;
}
}
void resetEEPROM() {
for(int i = 0; i < EEPROM_SIZE; ++i) {
eeprom[i] = 255;
}
}
void reset() {
resetClock();
resetPins();
resetInterrupts();
resetPorts();
resetSPI();
resetMmapPorts();
resetEEPROM();
seed = 1;
}
int serialPorts() {
return NUM_SERIAL_PORTS;
}
// Using this for anything other than unit testing arduino_ci itself
// is unsupported at the moment
void overrideClockTruth(unsigned long (*getMicros)(void)) {
}
// singleton pattern
static GodmodeState* getInstance();
static unsigned long getMicros() {
return instance->micros;
}
uint8_t* pMmapPort(uint8_t port) { return &mmapPorts[port]; }
uint8_t mmapPortValue(uint8_t port) { return mmapPorts[port]; }
// C++ 11, declare as public for better compiler error messages
GodmodeState(GodmodeState const&) = delete;
void operator=(GodmodeState const&) = delete;
private:
GodmodeState() {
reset();
}
};
// io pins
#define pinMode(...) _NOP()
#define analogReference(...) _NOP()
void digitalWrite(uint8_t, uint8_t);
int digitalRead(uint8_t);
int analogRead(uint8_t);
void analogWrite(uint8_t, int);
#define analogReadResolution(...) _NOP()
#define analogWriteResolution(...) _NOP()
void attachInterrupt(uint8_t interrupt, void ISR(void), uint8_t mode);
void detachInterrupt(uint8_t interrupt);
// TODO: issue #26 to track the commanded state here
inline void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0) {}
inline void noTone(uint8_t _pin) {}
// These definitions allow the following to compile (see issue #193):
// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341
// we allow one byte per port which "wastes" 224 bytes, but makes the code easier
#if defined(__AVR__)
#define digitalPinToBitMask(pin) (1)
#define digitalPinToPort(pin) (pin)
#define portOutputRegister(port) (GODMODE()->pMmapPort(port))
#else
// we don't (yet) support other boards
#endif
GodmodeState* GODMODE();