This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino.cpp
108 lines (91 loc) · 2.02 KB
/
Arduino.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
#include "Arduino.h"
#include <chrono>
#include <thread>
SerialPort Serial;
bool bitRead(byte b, byte bit)
{
// TODO typ b
return b & (1 << bit);
}
void bitWrite(byte &b, byte bit, bool on)
{
// TODO typ b
b = (b & ~(1 << bit)) | (on << bit);
}
// TODO const static pole
int pinToScanCode(int pin)
{
// see gamelib/input.h Pins enum
switch (pin) {
case 3: return SDL_SCANCODE_S;
case 2: return SDL_SCANCODE_A;
case 12: return SDL_SCANCODE_LEFT;
case 11: return SDL_SCANCODE_UP;
case 10: return SDL_SCANCODE_RIGHT;
case 8: return SDL_SCANCODE_DOWN;
case 9: return SDL_SCANCODE_KP_0;
default: return -1;
}
}
// all pins are now PULLUP
bool isPinPullUp(int pin)
{
UNUSED(pin);
return true;
}
byte readPin(int pin)
{
int scanCode = pinToScanCode(pin);
if (!pressedKeys || scanCode == -1)
return LOW;
return pressedKeys[scanCode] ? HIGH : LOW;
}
// TODO pinToScanCode2 - for alternate controls? wsad? numpad?
byte analogRead(int pin)
{
UNUSED(pin);
// TODO impl
// at the moment only used as RNG seed
return LOW;
}
byte digitalRead(int pin)
{
byte value = readPin(pin);
if (isPinPullUp(pin))
return value == LOW ? HIGH : LOW;
return value;
}
void pinMode(int pin, int mode)
{
UNUSED(pin); UNUSED(mode);
// at the moment hardcoded in isPinPullUp
// TODO read from file (together with key mapping)?
}
void delay(int ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
uint32_t millis()
{
static auto start = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
return static_cast<uint32_t>(diff.count());
}
void randomSeed(unsigned int seed)
{
srand(seed);
}
long int random(long int max)
{
return rand() % max;
}
long int random(long int min, long int max)
{
return min + rand() % (max - min);
}
void tone(int pin, int pitch, int ms)
{
UNUSED(pin); UNUSED(pitch); UNUSED(ms);
// TODO impl
}