forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGodmode.h
166 lines (139 loc) · 3.7 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
#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
class GodmodeState {
struct PortDef {
String dataIn;
String dataOut;
unsigned long readDelayMicros;
};
struct InterruptDef {
bool attached;
uint8_t mode;
};
struct SleepDef {
bool sleep_enable = false;
unsigned int sleep_enable_count = 0;
unsigned int sleep_disable_count = 0;
unsigned char sleep_mode = 0;
unsigned int sleep_cpu_count = 0;
unsigned int sleep_mode_count = 0;
unsigned int sleep_bod_disable_count = 0;
};
struct WdtDef {
bool wdt_enable = false;
unsigned char timeout = 0;
unsigned int wdt_enable_count = 0;
unsigned int wdt_disable_count = 0;
unsigned int wdt_reset_count = 0;
};
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;
struct SleepDef sleep;
struct WdtDef wdt;
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 resetSleep() {
sleep.sleep_enable = false;
sleep.sleep_enable_count = 0;
sleep.sleep_disable_count = 0;
sleep.sleep_mode = 0;
sleep.sleep_cpu_count = 0;
sleep.sleep_mode_count = 0;
sleep.sleep_bod_disable_count = 0;
}
void resetWdt() {
wdt.wdt_enable = false;
wdt.timeout = 0;
wdt.wdt_enable_count = 0;
wdt.wdt_disable_count = 0;
wdt.wdt_reset_count = 0;
}
void reset() {
resetClock();
resetPins();
resetInterrupts();
resetPorts();
resetSPI();
resetSleep();
resetWdt();
seed = 1;
}
int serialPorts() {
return NUM_SERIAL_PORTS;
}
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) {}
GodmodeState* GODMODE();