Skip to content

Commit 7b975d9

Browse files
committed
factor out Godmode to its own file
1 parent 79a6d1a commit 7b975d9

File tree

4 files changed

+75
-63
lines changed

4 files changed

+75
-63
lines changed

cpp/arduino/Arduino.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
#include "Arduino.h"
2-
3-
GodmodeState godmode = GodmodeState();
4-
5-
GodmodeState* GODMODE() {
6-
return &godmode;
7-
}
2+
#include "Godmode.h"
83

94
unsigned long millis() {
10-
return godmode.micros / 1000;
5+
GodmodeState* godmode = GODMODE();
6+
return godmode->micros / 1000;
117
}
128

139
unsigned long micros() {
14-
return godmode.micros;
10+
GodmodeState* godmode = GODMODE();
11+
return godmode->micros;
1512
}
1613

1714
void delay(unsigned long millis) {
18-
godmode.micros += millis * 1000;
15+
GodmodeState* godmode = GODMODE();
16+
godmode->micros += millis * 1000;
1917
}
2018

2119
void delayMicroseconds(unsigned long micros) {
22-
godmode.micros += micros;
20+
GodmodeState* godmode = GODMODE();
21+
godmode->micros += micros;
2322
}
2423

2524

2625
void randomSeed(unsigned long seed)
2726
{
28-
godmode.seed = seed;
27+
GodmodeState* godmode = GODMODE();
28+
godmode->seed = seed;
2929
}
3030

3131
long random(long vmax)
3232
{
33-
godmode.seed += 4294967291; // it's a prime that fits in 32 bits
34-
return godmode.seed % vmax;
33+
GodmodeState* godmode = GODMODE();
34+
godmode->seed += 4294967291; // it's a prime that fits in 32 bits
35+
return godmode->seed % vmax;
3536
}
3637

3738
long random(long vmin, long vmax)
3839
{
3940
return vmin < vmax ? (random(vmax - vmin) + vmin) : vmin;
4041
}
4142

42-
void digitalWrite(uint8_t pin, uint8_t val) {
43-
godmode.digitalPin[pin] = val;
43+
void digitalWrite(unsigned char pin, unsigned char val) {
44+
GodmodeState* godmode = GODMODE();
45+
godmode->digitalPin[pin] = val;
4446
}
4547

46-
int digitalRead(uint8_t pin) {
47-
return godmode.digitalPin[pin];
48+
int digitalRead(unsigned char pin) {
49+
GodmodeState* godmode = GODMODE();
50+
return godmode->digitalPin[pin];
4851
}
4952

50-
int analogRead(uint8_t pin) {
51-
return godmode.analogPin[pin];
53+
int analogRead(unsigned char pin) {
54+
GodmodeState* godmode = GODMODE();
55+
return godmode->analogPin[pin];
5256
}
5357

54-
void analogWrite(uint8_t pin, int val) {
55-
godmode.analogPin[pin] = val;
58+
void analogWrite(unsigned char pin, int val) {
59+
GodmodeState* godmode = GODMODE();
60+
godmode->analogPin[pin] = val;
5661
}

cpp/arduino/Arduino.h

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,21 @@ Where possible, variable names from the Arduino library are used to avoid confli
66
77
*/
88
// Chars and strings
9+
10+
#include "ArduinoDefines.h"
11+
#include "Godmode.h"
12+
913
#include "WCharacter.h"
1014
#include "WString.h"
1115

1216
typedef bool boolean;
1317
typedef uint8_t byte;
1418

15-
#include "ArduinoDefines.h"
1619
#include "binary.h"
1720

1821
// Math and Trig
1922
#include "AvrMath.h"
2023

21-
22-
23-
24-
25-
26-
#define MOCK_PINS_COUNT 256
27-
28-
class GodmodeState {
29-
public:
30-
unsigned long micros;
31-
unsigned long seed;
32-
// not going to put pinmode here unless its really needed. can't think of why it would be
33-
uint8_t digitalPin[MOCK_PINS_COUNT];
34-
int analogPin[MOCK_PINS_COUNT];
35-
36-
void resetPins() {
37-
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
38-
digitalPin[i] = LOW;
39-
analogPin[i] = 0;
40-
}
41-
}
42-
43-
void resetClock() {
44-
micros = 0;
45-
}
46-
47-
void reset() {
48-
resetClock();
49-
resetPins();
50-
seed = 1;
51-
}
52-
53-
GodmodeState() {
54-
reset();
55-
}
56-
57-
};
58-
59-
GodmodeState* GODMODE();
60-
61-
6224
// Bits and Bytes
6325
#define bit(b) (1UL << (b))
6426
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))

cpp/arduino/Godmode.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Godmode.h"
2+
3+
GodmodeState godmode = GodmodeState();
4+
5+
GodmodeState* GODMODE() {
6+
return &godmode;
7+
}

cpp/arduino/Godmode.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
#include "ArduinoDefines.h"
3+
4+
#define MOCK_PINS_COUNT 256
5+
6+
class GodmodeState {
7+
public:
8+
unsigned long micros;
9+
unsigned long seed;
10+
// not going to put pinmode here unless its really needed. can't think of why it would be
11+
bool digitalPin[MOCK_PINS_COUNT];
12+
int analogPin[MOCK_PINS_COUNT];
13+
14+
void resetPins() {
15+
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
16+
digitalPin[i] = LOW;
17+
analogPin[i] = 0;
18+
}
19+
}
20+
21+
void resetClock() {
22+
micros = 0;
23+
}
24+
25+
void reset() {
26+
resetClock();
27+
resetPins();
28+
seed = 1;
29+
}
30+
31+
GodmodeState() {
32+
reset();
33+
}
34+
35+
};
36+
37+
GodmodeState* GODMODE();
38+

0 commit comments

Comments
 (0)