1// This file is generated by WOWCube SDK project wizard
2
3#include <stdint.h>
4#include <string>
5#include <vector>
6#include <string>
7#include <memory>
8
9#include "AppManager.h"
10#include "Messaging.h"
11
12using namespace Cubios::Gfx;
13
14//Applicaton initialization callback. Called once when CUB application starts
15void OnInit(Cubios::AppManager& appMgr)
16{
17 Messaging* theApp = new Messaging();
18
19 appMgr.SetApplication(theApp,"AAbbCCddEE");
20
21 theApp->InitializeResources();
22
23}
24
25
26Messaging::Messaging()
27{
28}
29
30Messaging::~Messaging()
31{
32}
33
34void Messaging::InitializeResources()
35{
36 LOG_i("\n");
37 LOG_i("WOWCube SDK Example \"Network Messaging\" ***\n");
38
39 LOG_i("Cubeapp is initializing...");
40 LOG_i("Cubeapp is started on module %d",this->Module);
41
42 this->received = false;
43 this->counter = 0;
44 this->currentColor = Colors::black;
45
46 LOG_i("Done.");
47}
48
49//This callback is called every "tick" of cubeapp application loop
50void Messaging::on_Tick(uint32_t currentTime, uint32_t deltaTime)
51{
52}
53
54//This callback is called every time device is about to render visuals. Use it for calling your rendering code.
55void Messaging::on_Render(std::array<Cubios::Screen, 3>& screens)
56{
57 for(auto it = screens.begin(); it != screens.end(); ++it)
58 {
59 Cubios::GFX_setRenderTarget(it->ID());
60
61 if(this->Module == 0)
62 {
63 Cubios::GFX_clear(Colors::red);
64 Cubios::GFX_drawText(120,80, 14, 0, Cubios::text_align_t::TEXT_ALIGN_CENTER, Colors::white, "PAT");
65 Cubios::GFX_drawText(120,160, 14, 0, Cubios::text_align_t::TEXT_ALIGN_CENTER, Colors::white, "ME");
66 }
67 else
68 {
69 Cubios::GFX_clear(this->currentColor);
70 }
71
72 Cubios::GFX_render();
73 }
74}
75
76//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.
77void Messaging::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)
78{
79}
80
81//This callback is called on programmable timer (if any)
82void Messaging::on_Timer(uint8_t timerID)
83{
84}
85
86//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed
87void Messaging::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)
88{
89}
90
91//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module
92void Messaging::on_Message(uint32_t type, uint8_t* pkt, u32_t size)
93{
94 if(this->Module!=0)
95 {
96 switch(type)
97 {
98 case Commands::cmd_paintBlue: this->currentColor = Colors::blue; break;
99 case Commands::cmd_paintGreen: this->currentColor = Colors::green; break;
100 case Commands::cmd_paintRed: this->currentColor = Colors::red; break;
101 default: break;
102 }
103 }
104}
105
106//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source
107void Messaging::on_ExternalMessage(uint8_t* pkt, u32_t size)
108{
109}
110
111//Screen pat callback
112void Messaging::on_Pat(uint32_t count)
113{
114 if (this->Module == 0)
115 {
116 //When device is patted, module 0 will broadcast commands to all neighbouring modules
117 this->sendDataToNeighbours();
118 }
119 else
120 {
121 }
122}
123
124void Messaging::sendDataToNeighbours()
125{
126 Cubios::NetworkMessage message;
127
128 //we will send empty network messages with different commands to all neighbours
129 switch(this->counter)
130 {
131 case 0: this->SendNetworkMessage(Commands::cmd_paintBlue, &message); break;
132 case 1: this->SendNetworkMessage(Commands::cmd_paintGreen, &message); break;
133 case 2: this->SendNetworkMessage(Commands::cmd_paintRed, &message); break;
134 default:
135 break;
136 }
137
138 if(this->counter<2) this->counter++; else this->counter = 0;
139}