WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. AppManager.cpp
Mission NodeSDK 6.1C++appmanager.cpp

AppManager.cpp

SDK Source File: AppManager.cpp

Source / SDK 6.1 / C++ / Core

AppManager.cpp

AppManager.cpp
CPP
1/* Copyright Statement:
2 *
3 * (C) 2021-2024 Cubios Inc. All rights reserved.
4 */
5
6#include "AppManager.h"
7#include "Screen.h"
8
9namespace Cubios
10{
11
12 void AppManager::handleNetPacket()
13 {
14 uint8_t pkt[m_sMaxRxPacketSize];
15 uint32_t pktType;
16 int32_t rxSize;
17
18 rxSize = recvPacket(m_sMaxRxPacketSize, &pktType, pkt);
19
20 while (rxSize > 0)
21 {
22 if (application)
23 {
24 application->on_Message(pktType, pkt, rxSize);
25 }
26
27 rxSize = recvPacket(m_sMaxRxPacketSize, &pktType, pkt);
28 }
29 }
30
31 void AppManager::handleExternalData()
32 {
33 uint8_t pkt[m_sMaxRxPacketSize];
34 int32_t rxSize;
35
36 rxSize = recvBleData(pkt, m_sMaxRxPacketSize);
37
38 while (rxSize > 0)
39 {
40 if (application)
41 {
42 if (rxSize >= 10)
43 {
44 if (pkt[0] == application->appUUID[0] && pkt[1] == application->appUUID[1] && pkt[2] == application->appUUID[2] && pkt[3] == application->appUUID[3] && pkt[4] == application->appUUID[4] &&
45 pkt[5] == application->appUUID[5] && pkt[6] == application->appUUID[6] && pkt[7] == application->appUUID[7] && pkt[8] == application->appUUID[8] && pkt[9] == application->appUUID[9])
46 {
47 application->on_ExternalMessage((uint8_t *)(pkt + 10), rxSize - 10);
48 }
49 }
50 else
51 {
52 LOG_e("BLE message doesn't contain app UUID, size is %d", rxSize);
53 }
54 }
55 rxSize = recvBleData(pkt, m_sMaxRxPacketSize);
56 }
57 }
58
59 void AppManager::handleTwist()
60 {
61 TOPOLOGY_twistInfo_t data;
62 int count = TOPOLOGY_getTwist(&data);
63 if (application)
64 {
65 application->on_Twist(data);
66 }
67 }
68
69 void AppManager::handleTap()
70 {
71 uint32_t tapCount = getTap();
72
73 if (application)
74 {
75 application->on_Tap(tapCount);
76 }
77 }
78
79 int AppManager::Main()
80 {
81 std::array<Screen, 3> scr = {Screen(0), Screen(1), Screen(2)};
82
83 u32_t eventList = EVENT_getList();
84
85 if (eventList & WASM_EVENT_ON_CLOSE)
86 {
87 if (application)
88 {
89 application->on_Close();
90 }
91 return 1;
92 }
93
94 if (eventList & WASM_EVENT_ON_UART_RX)
95 {
96 handleNetPacket();
97 }
98
99 if (eventList & WASM_EVENT_ON_CFG_DATA)
100 {
101 handleExternalData();
102 }
103
104 if (eventList & WASM_EVENT_ON_PHYSICAL_TICK)
105 {
106 if (application)
107 {
108 application->on_PhysicsTick(scr);
109 }
110 }
111 if (eventList & WASM_EVENT_ON_TWIST)
112 {
113 handleTwist();
114 }
115
116 if (eventList & WASM_EVENT_ON_TAP)
117 {
118 handleTap();
119 }
120
121 if (application)
122 {
123 application->curTime = getTime();
124
125 if (prevTime == 0)
126 {
127 prevTime = application->curTime;
128 }
129
130 application->on_Tick(application->curTime, application->curTime - prevTime);
131 application->handleTimers();
132
133 application->on_Render(scr);
134
135 prevTime = application->curTime;
136 }
137
138 return 0;
139 }
140
141 void AppManager::SendNetworkMessage(uint32_t type, uint8_t *data, size_t size) const
142 {
143 if(data==nullptr || size==0)
144 {
145 LOG_e("Failed to send packet %d, data buffer or data size is null!", type);
146 }
147
148 if (size > m_sMaxRxPacketSize)
149 {
150 LOG_e("Network packet size exceeds %d b,skipping...", m_sMaxRxPacketSize);
151 return;
152 }
153
154 if (type > m_sMaxPacketType)
155 {
156 LOG_e("Network packet type should be in [0,31] range,skipping...");
157 return;
158 }
159
160 if (sendPacket(type, data, size) < 0)
161 {
162 LOG_e("Failed to send packet %d of size %d", type, size);
163 }
164 }
165
166 void AppManager::SendExternalMessage(uint32_t type, uint8_t *data, size_t size)
167 {
168 if (size > (m_sMaxRxPacketSize - 10))
169 {
170 LOG_e("External message data size exceeds %d b,skipping...", (m_sMaxRxPacketSize - 10));
171 return;
172 }
173
174 if (type > m_sMaxPacketType)
175 {
176 LOG_e("External message type should be in [0,31] range,skipping...");
177 return;
178 }
179
180 if (sendBuffer == nullptr)
181 {
182 sendBuffer = (uint8_t *)malloc(m_sMaxRxPacketSize);
183 if (sendBuffer == nullptr)
184 {
185 LOG_e("Failed to send external message, can't allocate memory buffer");
186 return;
187 }
188 else
189 {
190 memcpy(sendBuffer, application->appUUID, 10);
191 }
192 }
193
194 memcpy((uint8_t *)(sendBuffer + 10), data, size);
195 int32_t r = sendBleData(type, sendBuffer, size + 10);
196
197 if (r < 0)
198 {
199 LOG_e("Failed to send external message %d of size %d", type, size);
200 }
201 }
202
203 void AppManager::SetApplication(Application *app, const std::string &appIdentifier)
204 {
205 if (appIdentifier.length() != 10)
206 {
207 memset(app->appUUID, 0x00, 10);
208 LOG_w("Application UUID is not defined!");
209 }
210 else
211 {
212 memcpy(app->appUUID, appIdentifier.data(), appIdentifier.length());
213 }
214
215 app->appMgr = this;
216 this->application.reset(app);
217 }
218
219 void Application::SendNetworkMessage(uint32_t type, NetworkMessage *msg) const
220 {
221 int size = 0;
222 uint8_t *data = msg->GetData(size);
223
224 appMgr->SendNetworkMessage(type, data, size);
225 }
226
227 void Application::SendExternalMessage(uint32_t type, uint8_t *data, size_t size) const
228 {
229 appMgr->SendExternalMessage(type, data, size);
230 }
231
232 void Application::SaveState(void *data, size_t size) const
233 {
234 if(data==nullptr || size==0)
235 {
236 LOG_e("Unable to save application state, invalid source buffer or its size!");
237 return;
238 }
239
240 if (saveState(data, size) < 0)
241 {
242 LOG_e("Unable to save application state!");
243 }
244 }
245
246 i32_t Application::LoadState(u32_t *id, void *data, size_t size) const
247 {
248 if(data==nullptr || size==0)
249 {
250 LOG_e("Unable to load application state, invalid destination buffer or its size!");
251 return -1;
252 }
253
254 i32_t result = loadState(id, data, size);
255 if (result < 0)
256 {
257 LOG_e("Unable to load application state!");
258 }
259 return result;
260 }
261
262 void Application::SaveState(SaveMessage *msg) const
263 {
264 int size = 0;
265 if(msg==nullptr)
266 {
267 LOG_e("Unable to save application state, invalid source buffer!");
268 return;
269 }
270
271 uint8_t *data = msg->GetData(size);
272
273 SaveState(data, size);
274 }
275
276 i32_t Application::LoadState(u32_t *id, SaveMessage *msg) const
277 {
278 uint8_t data[GAME_SAVE_SIZE];
279 if(msg==nullptr)
280 {
281 LOG_e("Unable to load application state, invalid destination buffer!");
282 return -1;
283 }
284
285 i32_t result = loadState(id, data, GAME_SAVE_SIZE);
286 if (result < 0)
287 {
288 LOG_e("Unable to load application state!");
289 }
290 msg->SetData(data, GAME_SAVE_SIZE);
291 return result;
292 }
293
294 void Application::handleTimers()
295 {
296 for (size_t i = 0; i < this->timers.size(); i++)
297 {
298 if (this->timers[i].suspended)
299 continue;
300
301 if (this->curTime - this->timers[i].time >= this->timers[i].delay)
302 {
303 this->timers[i].time = this->curTime;
304 this->on_Timer(this->timers[i].id);
305 }
306 }
307 }
308
309 bool Application::SetTimer(uint8_t id, uint32_t delay, bool suspended)
310 {
311 for (Cubios::Timer_t t : this->timers)
312 {
313 if (t.id == id)
314 {
315 LOG_e("Timer with ID=%d already set!", id);
316 return false;
317 }
318 }
319
320 Timer_t newTimer;
321 newTimer.id = id;
322 newTimer.delay = delay;
323 newTimer.time = this->curTime;
324 newTimer.suspended = suspended;
325
326 this->timers.push_back(newTimer);
327
328 return true;
329 }
330
331 bool Application::StartTimer(uint8_t id)
332 {
333 for (size_t i = 0; i < this->timers.size(); i++)
334 {
335 if (this->timers[i].id == id)
336 {
337 this->timers[i].suspended = false;
338 return true;
339 }
340 }
341
342 LOG_e("Timer with ID=%d is either already started or not set!", id);
343 return false;
344 }
345 bool Application::StopTimer(uint8_t id)
346 {
347 for (size_t i = 0; i < this->timers.size(); i++)
348 {
349 if (this->timers[i].id == id)
350 {
351 this->timers[i].suspended = true;
352 return true;
353 }
354 }
355
356 LOG_e("Timer with ID=%d is either already stopped or not set!", id);
357 return false;
358 }
359
360 void Application::KillTimer(uint8_t id)
361 {
362 for (size_t i = 0; i < this->timers.size(); i++)
363 {
364 if (this->timers[i].id == id)
365 {
366 this->timers.erase(this->timers.begin() + i);
367 return;
368 }
369 }
370 }
371
372 void Application::ShowFPSCounter(bool b)
373 {
374 if (b)
375 Cubios::GFX_setFpsWindow(1);
376 else
377 Cubios::GFX_setFpsWindow(0);
378 }
379
380}
381
382
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

AppManager.h
Source / SDK 6.1 / C++ / Core
Gfx.h
Source / SDK 6.1 / C++ / Core
native access.h
Source / SDK 6.1 / C++ / Core
native defines.h
Source / SDK 6.1 / C++ / Core
Previous Node
wowcubeapp-build.json
Examples / SDK 6.3 / Rust / basics / Working with Emulator / project
Next Node
AppManager.h
Source / SDK 6.1 / C++ / Core