-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
296 lines (259 loc) · 7.43 KB
/
main.cpp
File metadata and controls
296 lines (259 loc) · 7.43 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <appdef.hpp>
// main
#include "calc.hpp"
#include "lib/draw_functions.hpp"
#include "lib/core/exceptions.hpp"
#include "lib/core/event_handler.hpp"
#include "lib/core/touch_event_handler.hpp"
#include "lib/functions/random.hpp"
// shell
#include "src/internal.hpp" // definitions
#include "src/terminal.hpp"
#include "src/virtual_keyboard.hpp"
// Terminal pointer
Terminal* terminal;
// Virtual Keyboard pointer
VirtualKeyboard* keyboard;
#include "src/cpshell.cpp"
#ifndef PC
APP_NAME("CP Shell")
APP_DESCRIPTION("A calculator shell, with file handling, memory management, breakpoints, and a lot more.")
APP_AUTHOR("s3ansh33p")
APP_VERSION(CPS_VERSION)
#endif
// Ends the Shell and is called by the event handler
void endShell() {
shell_running = false;
}
// Updates the random number
void updateRNG() {
rng->Generate(50);
}
// Kayboard Pseudo functions
void kbShift() {
keyboard->ToggleShift();
}
void kbLeft() {
if (keyboard->xcursor > 0) {
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor, keyboard->keyColor, true);
keyboard->xcursor--;
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor);
}
}
void kbRight() {
if (keyboard->xcursor < 2 || (keyboard->xcursor < 21 && keyboard->ycursor != 3)) {
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor, keyboard->keyColor, true);
keyboard->xcursor++;
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor);
}
}
void kbUp() {
if (keyboard->ycursor > 0) {
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor, keyboard->keyColor, true);
if (keyboard->yfix > 4) {
// check if going from specials to normal
if (keyboard->ycursor == 3) {
if (keyboard->xcursor == 2) {
keyboard->xcursor = 15;
} else if (keyboard->xcursor == 1) {
keyboard->xcursor = 8;
} else {
keyboard->xcursor = 0;
}
}
keyboard->ycursor--;
keyboard->yfix = 0;
} else {
keyboard->yfix++;
}
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor);
}
}
void kbDown() {
if (keyboard->ycursor < 3) {
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor, keyboard->keyColor, true);
if (keyboard->yfix > 4) {
keyboard->ycursor++;
keyboard->yfix = 0;
// check if going to specials
if (keyboard->ycursor == 3) {
if (keyboard->xcursor < 8) {
keyboard->xcursor = 0;
} else if (keyboard->xcursor < 15) {
keyboard->xcursor = 1;
} else {
keyboard->xcursor = 2;
}
}
} else {
keyboard->yfix++;
}
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor);
}
}
void kbBackspace() {
terminal->RemoveLast();
}
void kbEnter() {
// write to the char buffer
char activeKey = keyboard->activeKey[0];
// checks for specials
if (keyboard->ycursor == 3) {
if (keyboard->xcursor == 2) {
// enter
// store buffer
if (terminal->bufferInPos != 0) {
terminal->HideCursor();
terminal->bufferCY++;
// copy bufferIn to callingArgs
char callingArgs[BUF_SIZE];
for (int i = 0; i < terminal->bufferInPos; i++) {
callingArgs[i] = terminal->bufferIn[i];
}
// add space to end
callingArgs[terminal->bufferInPos] = '\0';
int argc = 0;
// count number spaces in callingArgs
for (int i = 0; i < strlen(callingArgs); i++) {
if (callingArgs[i] == ' ') {
argc++;
}
}
// check if final space is in callingArgs
if (callingArgs[strlen(callingArgs) - 1] != ' ') {
argc++;
}
// create instance of argv
char** argv = new char*[argc];
// split callingArgs into argv
int argvIndex = 0;
char currentArg[ARGV_SIZE];
int currentArgIndex = 0;
for (int i = 0; i < strlen(callingArgs); i++) {
if (callingArgs[i] == ' ') {
argv[argvIndex] = new char[currentArgIndex + 1];
for (int j = 0; j < currentArgIndex; j++) {
argv[argvIndex][j] = currentArg[j];
}
argv[argvIndex][currentArgIndex] = '\0';
argvIndex++;
currentArgIndex = 0;
} else {
currentArg[currentArgIndex] = callingArgs[i];
currentArgIndex++;
}
}
// set last argv to last arg
argv[argvIndex] = new char[currentArgIndex + 1];
for (int j = 0; j < currentArgIndex; j++) {
argv[argvIndex][j] = currentArg[j];
}
argv[argvIndex][currentArgIndex] = '\0';
// call cpshell_main
// testfunc_main(argc, argv);
psuedo_main(argc, argv);
// display host after psuedo_main
display_host();
}
// else ignore as it is a blank line
} else if (keyboard->xcursor == 1) {
// back
kbBackspace();
} else {
// space
terminal->WriteBuffer(0x20);
}
} else {
terminal->WriteBuffer(activeKey);
}
}
void HandleTouchForKeyboard() {
keyboard->Highlight(keyboard->xcursor, keyboard->ycursor, keyboard->keyColor, true);
uint16_t xIn = event.data.touch_single.p1_x;
uint16_t yIn = event.data.touch_single.p1_y;
Debug_Printf(1,34,true,0,"%d | %d",xIn,yIn);
// get x and y to be top left of keyboard
xIn -= keyboard->x;
yIn -= keyboard->y;
// read column / row
uint16_t xOffset = xIn / keyboard->xmargin;
uint16_t yOffset = yIn / keyboard->ymargin;
// to investigate later
if (yOffset > 3) yOffset = 3;
// check if on bottom line
if (yOffset > 2) {
if (xIn < keyboard->xmargin * 7) {
xOffset = 0;
} else if (xIn < keyboard->xmargin * 14) {
xOffset = 1;
} else {
xOffset = 2;
}
}
keyboard->xcursor = xOffset;
keyboard->ycursor = yOffset;
keyboard->Highlight(xOffset, yOffset);
kbEnter();
}
void testTouch() {
Debug_Printf(10,32,true,0,"Working");
}
//The acutal main
void main2() {
// load the textures and fonts
LOAD_FONT_PTR("7x8", f_7x8);
fillScreen(0); // clear the screen to black (0,0,0)
RandomGenerator rngp;
rng = &rngp;
rng->SetSeed(1337);
VirtualKeyboard keyboardp;
keyboard = &keyboardp;
keyboard->SetFont(f_7x8);
keyboard->Render();
keyboard->Highlight(); // 0,0
Terminal terminalp;
terminal = &terminalp;
terminal->SetFont(f_7x8);
// Debug_Printf(10,29,true,0,"Max X: %i", terminal->xmax);
// Debug_Printf(10,30,true,0,"W x H: %i x %i", terminal->termWidth, terminal->termHeight);
// Add event listeners
addListener(KEY_CLEAR, endShell); // end the shell - cmd now
addListener(KEY_BACKSPACE, kbBackspace); // remove last character
addListener(KEY_SHIFT, kbShift); // toggle Shift
// Keyboard Listeners
addListener(KEY_LEFT, kbLeft); // left cursor
addListener(KEY_RIGHT, kbRight); // right cursor
addListener2(KEY_UP, kbUp); // up cursor
addListener2(KEY_DOWN, kbDown); // down cursor
addListener(KEY_EXE, kbEnter); // send the key
addTouchListener(0, 0, 300, 100, testTouch); // touch listener
addTouchListener(keyboard->x, keyboard->y, keyboard->x + keyboard->xmargin * 22 - 1, keyboard->y + keyboard->ymargin * 4 - 1, HandleTouchForKeyboard);
// Initialize the shell
cpshell_init();
// Display Host
display_host();
LCD_Refresh();
// frame counter for updating the blinky cursor
uint8_t frameCounter = 0;
bool isCursorShowing = false;
while (shell_running) {
// frame counter
frameCounter++;
// update the cursor
if (frameCounter > 8) {
frameCounter = 0;
isCursorShowing = !isCursorShowing;
if (isCursorShowing) {
terminal->ShowCursor();
} else {
terminal->HideCursor();
}
}
checkEvents();
checkTouchEvents();
// Debug_Printf(10,28,true,0,"T X: %i | Y: %i | PX: %i | PY: %i",terminal->bufferCX, terminal->bufferCY, terminal->bufferCX * terminal->xmargin + terminal->bufferOffsetX, terminal->bufferCY * terminal->ymargin + terminal->bufferOffsetY);
LCD_Refresh();
}
// free memory
// free(f_7x8);
}