-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
124 lines (97 loc) · 2.78 KB
/
main.ino
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
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
// ----------------------------------------------------------
// 3D
#include "mesh.h"
#include "transform.h"
#include "renderer.h"
#include "cube.h"
// ----------------------------------------------------------
mesh *meshes[] = {
&cube,
};
// Buffers
screen g_screen;
screen g_screen2; // for double buffering to prevent flickering
// NOTE: some libraries will allow you to get the screenbuffer directly
uint16_t *screenbuffer; // for faster writes to screen (only write if pixel has changed)
#define WHITE 0xFFFF
#define BLACK 0x0000
#define TFT_CS RX
#define TFT_RST A3
#define TFT_DC A2
Adafruit_ST7789 *lcd;
// Stats
int renderFPS = 0;
int videoFPS = 0;
// ----------------------------------------------------------
void videoTask(void *stuff)
{
while (true)
{
long millisStart = millis();
lcd->startWrite();
for (int screenPos = 0; screenPos < 240 * 135; screenPos++)
{
if (screenbuffer[screenPos] != g_screen2.buffer[screenPos])
{
lcd->writePixel(screenPos % 240, screenPos / 240, g_screen2.buffer[screenPos]);
screenbuffer[screenPos] = g_screen2.buffer[screenPos];
}
}
lcd->endWrite();
videoFPS = 1000 / (millis() - millisStart);
// Log stats
lcd->setCursor(0, 0);
lcd->setTextColor(WHITE, BLACK);
lcd->print(videoFPS);
lcd->println(" Video FPS");
lcd->print(renderFPS);
lcd->println(" Render FPS");
}
}
void setup()
{
Serial.begin(115200); // Init Display
// Init Display
lcd = new Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
lcd->setSPISpeed(80000000);
lcd->init(135, 240);
lcd->setSPISpeed(80000000);
lcd->setRotation(1);
lcd->fillScreen(BLACK);
screenbuffer = new uint16_t[240 * 135];
for (int i = 0; i < 240 * 135; i++)
{
screenbuffer[i] = 0;
}
// 3d
g_screen.width = 240;
g_screen.height = 135;
g_screen.buffer = new uint16_t[240 * 135];
g_screen2.width = 240;
g_screen2.height = 135;
g_screen2.buffer = new uint16_t[240 * 135];
memset(g_screen.buffer, 0, 240 * 135 * 2);
memset(g_screen2.buffer, 0, 240 * 135 * 2);
xTaskCreatePinnedToCore(
videoTask,
"videoTask",
10000,
NULL,
0,
NULL,
0);
}
void loop()
{
long millisStart = millis();
cube.rotation->y += 7;
render(meshes, 1, &g_screen);
// Move the rendered screen to the second buffer to prevent flickering
memcpy(g_screen2.buffer, g_screen.buffer, 240 * 135 * 2);
// Clear the first buffer
memset(g_screen.buffer, 0, 240 * 135 * 2);
// This will be logged in the videoTask
renderFPS = 1000 / (millis() - millisStart);
}