This repository was archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
135 lines (116 loc) · 2.44 KB
/
Engine.cpp
File metadata and controls
135 lines (116 loc) · 2.44 KB
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
#include "Engine.h"
Engine::Engine()
{
mLedController.shutdown(0, false);
mLedController.shutdown(1, false);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_BUZZER, OUTPUT);
ClearDisplay();
time = 0;
}
void Engine::Update(float deltaTime_)
{
deltaTime = deltaTime_;
time += deltaTime;
// Getting button input:
int oldButtonState = mButtonState;
mButtonState = digitalRead(PIN_BUTTON);
isButtonDown = mButtonState == 1;
isButtonDownThisFrame = isButtonDown && mButtonState != oldButtonState;
isButtonUpThisFrame = mButtonState == false && oldButtonState == true;
if (isButtonDownThisFrame)
{
buttonDownDuration = 0;
}
if (isButtonDown)
{
buttonDownDuration += deltaTime;
}
inputX = Remap(analogRead(PIN_X_AXIS), 0, 1023, -1, 1);
inputY = Remap(analogRead(PIN_Y_AXIS), 0, 1023, -1, 1);
if (abs(inputX) < JOYSTICK_TRESHOLD)
{
inputX = 0;
}
if (abs(inputY) < JOYSTICK_TRESHOLD)
{
inputY = 0;
}
}
void Engine::DrawToDisplay()
{
for (int row = 0; row < 8; row++)
{
mLedController.setRow(0, row, mDisplay0[row]);
mLedController.setRow(1, row, mDisplay1[row]);
}
}
void Engine::ClearDisplay()
{
for (int row = 0; row < 8; row++)
{
mDisplay0[row] = 0;
mDisplay1[row] = 0;
}
}
void Engine::SetPixel(int x, int y, boolean value)
{
if ((x < 0 || x >= DISPLAY_WIDTH) || (y < 0 || y >= DISPLAY_HEIGHT))
{
/*
Serial.print("ERROR : ");
if (x < 0 || x >= DISPLAY_WIDTH)
{
Serial.print("[X] = ");
Serial.print(x);
Serial.print(" is out of bounds. ");
}
if (y < 0 || y >= DISPLAY_HEIGHT)
{
Serial.print("[Y] = ");
Serial.print(y);
Serial.print(" is out of bounds. ");
}
Serial.print("\n");
*/
return;
}
if (x < 8) // mDisplay0[]
{
x = 7 - x;
y = 7 - y;
if (value)
{
mDisplay0[x] |= 1 << y;
} else
{
mDisplay0[x] &= ~(1 << y);
}
} else // mDisplay1[]
{
if (value)
{
mDisplay1[x - 8] |= 1 << y;
} else
{
mDisplay1[x - 8] &= ~(1 << y);
}
}
}
void Engine::SetPixel(Vector2 position, boolean value)
{
SetPixel(position.x, position.y, value);
}
void Engine::PlaySound(int frequency, int duration)
{
tone(PIN_BUZZER, frequency, duration);
}
void Engine::SetDisplayBrightness(int brightness)
{
mLedController.setIntensity(0, brightness);
mLedController.setIntensity(1, brightness);
}
float Engine::Remap(float currentValue, float oldMin, float oldMax, float newMin, float newMax) const
{
return newMin + (currentValue - oldMin) / (oldMax - oldMin) * (newMax - newMin);
}