This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
das.cpp
128 lines (113 loc) · 2.8 KB
/
das.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
121
122
123
124
125
126
127
128
#include "das.h"
std::atomic<bool> m_bPins[MAX_UC32_BASIC_IO_PIN + 1];
std::atomic<PIN_MODE> m_PinModes[MAX_UC32_BASIC_IO_PIN + 1];
mutex m_muxPin;
chrono::steady_clock::time_point m_tpSimulationStarted;
int swIndex(int i) {
Clip(i, 1, NUM_SW);
switch (i) {
case 1:
return (int)SW1;
case 2:
return (int)SW2;
case 3:
return (int)SW3;
case 4:
return (int)SW4;
default:
return -1; // not found
}
}
int btnIndex(int i) {
Clip(i, 1, NUM_BTN);
switch (i) {
case 1:
return (int)BTN1;
case 2:
return (int)BTN2;
case 3:
return (int)BTN3;
case 4:
return (int)BTN4;
default:
return -1; // not found
}
}
void executeSoftReset(uint32_t options)
{
if (options == RUN_SKETCH_ON_BOOT)
{
for (int i = 0; i < MAX_UC32_BASIC_IO_PIN; ++i) {
m_bPins[i].store(false);
}
m_tpSimulationStarted = chrono::steady_clock::now();
}
else if (options == ENTER_BOOTLOADER_ON_BOOT) {
}
}
void pinMode(int pin, PIN_MODE mode) {
pinMode((PIN)pin, mode);
}
void pinMode(PIN pin, PIN_MODE mode) {
m_PinModes[(int)pin].store(mode);
}
void digitalWrite(int pin, int value) {
digitalWrite((PIN)pin, value);
}
void digitalWrite(PIN pin, int value)
{
m_bPins[(int)pin].store(value > 0);
}
int digitalRead(int pin) {
if (pin < 0 || pin > MAX_UC32_BASIC_IO_PIN) {
wchar_t errBuff[100];
wsprintf(errBuff, L" \"int digitalRead(%d)\" : argument \"%d\" is invalid!", pin, pin);
exit(EXIT_FAILURE);
}
else {
return m_bPins[pin].load() == true ? HIGH : LOW;
}
}
int digitalRead(PIN pin) {
return m_bPins[(int)pin].load() == true ? HIGH : LOW;
}
void delay(unsigned int ms) {
this_thread::sleep_for(chrono::milliseconds(ms));
}
long random(long howbig)
{
if (howbig > 0) {
return random(0, howbig - 1);
}
return 0;
}
long random(long howsmall, long howbig)
{
if (howsmall > howbig) {
std::swap(howsmall, howbig);
}
static default_random_engine generator((unsigned int)chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<long> distribution(howsmall, howbig);
return distribution(generator);
}
long long millis()
{
auto timePassed = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - m_tpSimulationStarted);
return timePassed.count();
}
static void Swap(int& x, int& y) {
x ^= y;
y ^= x;
x ^= y;
}
static void Clip(int& value, int lowerBoundary, int higherBoundary) {
if (lowerBoundary > higherBoundary) {
Swap(lowerBoundary, higherBoundary);
}
if (value < lowerBoundary) {
value = lowerBoundary;
}
else if (value > higherBoundary) {
value = higherBoundary;
}
}