-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathGodmode.cpp
120 lines (98 loc) · 3.09 KB
/
Godmode.cpp
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
#include "Godmode.h"
#include "HardwareSerial.h"
#include "SPI.h"
#include "Wire.h"
GodmodeState* GODMODE() {
return GodmodeState::getInstance();
}
GodmodeState* GodmodeState::instance = nullptr;
GodmodeState* GodmodeState::getInstance()
{
if (instance == nullptr)
{
instance = new GodmodeState();
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
instance->digitalPin[i].setMicrosRetriever(&GodmodeState::getMicros);
instance->analogPin[i].setMicrosRetriever(&GodmodeState::getMicros);
}
}
return instance;
}
unsigned long millis() {
return GODMODE()->micros / 1000;
}
unsigned long micros() {
return GODMODE()->micros;
}
void delay(unsigned long millis) {
GODMODE()->micros += millis * 1000;
}
void delayMicroseconds(unsigned long micros) {
GODMODE()->micros += micros;
}
void randomSeed(unsigned long seed)
{
GODMODE()->seed = seed;
}
long random(long vmax)
{
GodmodeState* godmode = GODMODE();
godmode->seed += 4294967291; // it's a prime that fits in 32 bits
godmode->seed = godmode->seed % 4294967296; // explicitly wrap in case we're on a 64-bit impl
return godmode->seed % vmax;
}
long random(long vmin, long vmax)
{
return vmin < vmax ? (random(vmax - vmin) + vmin) : vmin;
}
void digitalWrite(unsigned char pin, unsigned char val) {
GodmodeState* godmode = GODMODE();
godmode->digitalPin[pin] = val;
}
int digitalRead(unsigned char pin) {
GodmodeState* godmode = GODMODE();
return godmode->digitalPin[pin].retrieve();
}
void analogWrite(unsigned char pin, int val) {
GodmodeState* godmode = GODMODE();
godmode->analogPin[pin] = val;
}
int analogRead(unsigned char pin) {
GodmodeState* godmode = GODMODE();
return godmode->analogPin[pin].retrieve();
}
void attachInterrupt(uint8_t interrupt, void ISR(void), uint8_t mode) {
GodmodeState* godmode = GODMODE();
godmode->interrupt[interrupt].attached = true;
godmode->interrupt[interrupt].mode = mode;
}
void detachInterrupt(uint8_t interrupt) {
GodmodeState* godmode = GODMODE();
godmode->interrupt[interrupt].attached = false;
}
// Serial ports
#if defined(HAVE_HWSERIAL0)
HardwareSerial Serial(&GODMODE()->serialPort[0].dataIn, &GODMODE()->serialPort[0].dataOut, &GODMODE()->serialPort[0].readDelayMicros);
#endif
#if defined(HAVE_HWSERIAL1)
HardwareSerial Serial1(&GODMODE()->serialPort[1].dataIn, &GODMODE()->serialPort[1].dataOut, &GODMODE()->serialPort[1].readDelayMicros);
#endif
#if defined(HAVE_HWSERIAL2)
HardwareSerial Serial2(&GODMODE()->serialPort[2].dataIn, &GODMODE()->serialPort[2].dataOut, &GODMODE()->serialPort[2].readDelayMicros);
#endif
#if defined(HAVE_HWSERIAL3)
HardwareSerial Serial3(&GODMODE()->serialPort[3].dataIn, &GODMODE()->serialPort[3].dataOut, &GODMODE()->serialPort[3].readDelayMicros);
#endif
template <typename T>
inline std::ostream& operator << ( std::ostream& out, const PinHistory<T>& ph ) {
out << ph;
return out;
}
// defined in SPI.h
SPIClass SPI = SPIClass(&GODMODE()->spi.dataIn, &GODMODE()->spi.dataOut);
// defined in Wire.h
TwoWire Wire = TwoWire();
#if defined(EEPROM_SIZE)
#include <EEPROM.h>
EEPROMClass EEPROM;
#endif