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.3C++appmanager.cpp

AppManager.cpp

SDK Source File: AppManager.cpp

Source / SDK 6.3 / C++ / Core

AppManager.cpp

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

Related nodes

AppManager.h
Source / SDK 6.3 / C++ / Core
Gfx.h
Source / SDK 6.3 / C++ / Core
native access.h
Source / SDK 6.3 / C++ / Core
native defines.h
Source / SDK 6.3 / C++ / Core
Previous Node
Splashscreen.h
Source / SDK 6.2 / C++ / Core
Next Node
AppManager.h
Source / SDK 6.3 / C++ / Core