-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMain.cpp
More file actions
245 lines (190 loc) · 6.41 KB
/
Copy pathMain.cpp
File metadata and controls
245 lines (190 loc) · 6.41 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <algorithm>
#include <chrono>
#include <ctime>
#include <functional>
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
#include <thread>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <memory>
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <GL/gl3w.h>
#include <GL/gl.h>
#include "HelpersGL.h"
#include "VirtualUI.h"
#include "UI/MenuSystem.h"
#include "UI/Painter/Painter.h"
// Debug
#define DEBUG_DRAW
#ifdef DEBUG_DRAW
#include "UI/Painter/DebugPainter.h"
#endif
// Periphery
#include "USBCDCTerminalDevice.h"
//#include "CentralDB.h"
#define FRAMEBUFFER_WIDTH 320
#define FRAMEBUFFER_HEIGHT 240
enum class GLTextureFilter
{
Nearest,
Linear
};
void Shutdown(SDL_Window* win)
{
if (win != nullptr)
{
SDL_DestroyWindow(win);
}
SDL_Quit();
}
void Initialization(SDL_Window** window)
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
*window = SDL_CreateWindow("AXIOM Remote Visualizer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 480 * 2,
SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI);
if (*window == nullptr)
{
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
exit(EXIT_FAILURE);
}
}
void SetupGL(SDL_Window* window, SDL_GLContext& glContext)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
glContext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glContext);
gl3wInit();
// Set black background
glClearColor(0, 0, 0, 1);
int majorVersionGL = 0;
int minorVersionGL = 0;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &majorVersionGL);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minorVersionGL);
std::cout << "GL Version: " << majorVersionGL << "." << minorVersionGL << std::endl;
}
void SetupImGui(SDL_Window* window, SDL_GLContext glContext)
{
ImGui::CreateContext();
ImGui_ImplSDL2_InitForOpenGL(window, glContext);
ImGui_ImplOpenGL3_Init("#version 130");
}
void OpenSerialPort(std::string port)
{
int serial_port = open(port.c_str(), O_RDWR);
// Check for errors
if (serial_port < 0)
{
std::cout << "Error " << errno << " from open: " << strerror(errno) << std::endl;
}
std::string testMessage = "Vis serial test\n";
write(serial_port, testMessage.c_str(), testMessage.length());
}
bool GetCmdOption(char** begin, uint8_t count, const std::string& option, std::string& value)
{
char** end = begin + count;
char** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
value = *itr;
return true;
}
return false;
}
void ProcessCommandLine(int argc, char* argv[])
{
std::string value = "";
if (GetCmdOption(argv, argc, "-p", value))
{
// TODO: Add serial port handling
OpenSerialPort(value);
}
}
void ScreenshotHandler(uint16_t* frameBuffer, int width, int height)
{
SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(frameBuffer, width, height, 8 * 2, width * 2, 0, 0, 0, 0);
const auto now = std::chrono::system_clock::now();
const auto inTimeT = std::chrono::system_clock::to_time_t(now);
const auto localTime = std::localtime(&inTimeT);
std::string filePath = "../screenshots/";
constexpr auto dateBufferSize = 50;
char buffer[dateBufferSize];
std::strftime(buffer, sizeof buffer, "%F_%T.png", localTime);
filePath.append(buffer);
IMG_SavePNG(surf, filePath.c_str());
}
int main(int argc, char* argv[])
{
std::cout << "AXIOM Remote Visualizer" << std::endl;
ProcessCommandLine(argc, argv);
auto frameBuffer = new uint16_t[FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT];
SDL_Window* window;
SDL_GLContext glContext;
Initialization(&window);
SetupGL(window, glContext);
SetupImGui(window, glContext);
uint32_t displayTextureID = CreateGLTexture(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, nullptr, GL_RGB, GL_NEAREST);
SDL_Rect texture_rect;
texture_rect.x = 400; // the x coordinate
texture_rect.y = 120; // the y coordinate
texture_rect.w = FRAMEBUFFER_WIDTH * 4; // the width of the texture
texture_rect.h = FRAMEBUFFER_HEIGHT * 4; // the height of the texture
Painter painter(frameBuffer, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
#ifdef DEBUG_DRAW
DebugPainter debugPainter;
painter.SetDebugOverlay(&debugPainter);
#endif
USBCDCTerminalDevice cdcDevice;
CentralDB centralDB;
MenuSystem menuSystem(&cdcDevice, ¢ralDB);
Button button = Button::BUTTON_NONE;
int8_t knobValue = 0;
bool debugOverlayEnabled = false;
auto partialScreenshotHandler = std::bind(ScreenshotHandler, frameBuffer, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
std::shared_ptr<VirtualUI> virtualUI =
std::make_shared<VirtualUI>(window, displayTextureID, ¢ralDB, partialScreenshotHandler);
centralDB.SetUint32(Attribute::ID::REMOTE_LCD_BRIGHTNESS, 75);
bool appIsRunning = true;
const int frames = 60;
SDL_Event events;
while (appIsRunning)
{
while (SDL_PollEvent(&events))
{
if ((events.type == SDL_WINDOWEVENT && events.window.event == SDL_WINDOWEVENT_CLOSE) ||
(events.type == SDL_KEYDOWN && events.key.keysym.sym == SDLK_ESCAPE))
{
appIsRunning = false;
}
}
glClear(GL_COLOR_BUFFER_BIT);
#ifdef DEBUG_DRAW
debugPainter.SetEnable(debugOverlayEnabled);
#endif
menuSystem.Draw(&painter);
glBindTexture(GL_TEXTURE_2D, displayTextureID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
frameBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
button = Button::BUTTON_NONE;
virtualUI->RenderUI(button, knobValue, debugOverlayEnabled);
menuSystem.Update(button, knobValue);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / frames));
}
Shutdown(window);
return 0;
}