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. Examples
  4. Messaging Part II
Mission NodeSDK 6.2C++networkProject Included

Messaging Part II

Network messaging Part II So, in the previous example we figured out how to send data from one module to another. And in principle we could have finished her...

Examples / SDK 6.2 / C++ / network

Network messaging - Part II

So, in the previous example we figured out how to send data from one module to another. And in principle we could have finished here, if not for one nuance - our example will not work if the WOWCube is half-rotated and only half of the modules are connected to each other. But this happens quite often - the player spins the cube, the contacts between the modules are constantly disconnect and connect back again... and this means that there is a fairly high chance of losing data, because if the contact between the modules is broken, the data simply will not reach the recipient!

So it's safe to say the method we used in the previous example, although it works, is not optimal in the case where data delivery to modules is required regardless of the current state of the cube.

What should we do?

It's all quite simple!

Instead of making a network message to be sent on a specific event (such as a pat), the application should send data periodically. Because in this case, even if the actual event that all modules should "know" about happened at the moment the connection was broken, the information will not be lost, it will simply be delayed for a short time. As soon as the connection between the modules is restored, the data will be received.

Aggregating the application state on one of the modules (the master module) and then periodically sending this state to the other modules is a good practice. The frequency of sending depends on the application logic and the expected user actions.

Please note that sending data over the network too frequently may result in increased battery drain.

The example

So, let's move on to an example. As was said above, if we want to ensure reliable data exchange between modules, we need to send this data periodically, and not just once. But what exactly are we going to send?

Ok, let's imagine that one module has a heart, and it beats like a human's. And the other modules also want to know whether this module is alive, whether the heart is beating?

It turns out that all we need to do is send the heartbeat to all modules over the network periodically!

In fact, we will be sending out the information needed to animate the heartbeat graph

So...

First of all, let's define some enum values for the graphic resources and for the timer, which will be used to send messages periodically:

Messaging Part II
CPP
1enum GfxObjects
2{
3 idle=0,
4 beat,
5 background,
6 cachette,
7 dot
8};
9
10enum Timers
11{
12 mainTimer=1
13};
14
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

Next we initialize the graphics and the timer

Messaging Part II
CPP
1void Messaging2::InitializeResources()
2{
3 //Create a Cubios::Gfx::Sprite object
4 this->Scene.CreateObjectWithID(GfxObjects::idle,new Sprite("idle.png",Transform(120,120,0)));
5 this->Scene[GfxObjects::idle]->Color.Set(0xFFFFFFFF);
6 this->Scene[GfxObjects::idle]->Color.SetA(255);
7
8 this->Scene.CreateObjectWithID(GfxObjects::beat,new Sprite("beat.png",Transform(120,120,0)));
9 this->Scene[GfxObjects::beat]->Color.Set(0xFFFFFFFF);
10 this->Scene[GfxObjects::beat]->Color.SetA(255);
11
12 this->Scene.CreateObjectWithID(GfxObjects::dot,new Sprite("dot.png",Transform(120,120,0)));
13 this->Scene[GfxObjects::dot]->Color.Set(0xFFFFFFFF);
14 this->Scene[GfxObjects::dot]->Color.SetA(255);
15
16 this->Scene.CreateObjectWithID(GfxObjects::background,new Sprite("background.png",Transform(120,120,0)));
17 this->Scene[GfxObjects::background]->Color.Set(0xFFFFFFFF);
18 this->Scene[GfxObjects::background]->Color.SetA(255);
19
20 this->Scene.CreateObjectWithID(GfxObjects::cachette,new Sprite("cachette.png",Transform(120,120,0)));
21 this->Scene[GfxObjects::cachette]->Color.Set(0xFFFFFFFF);
22 this->Scene[GfxObjects::cachette]->Color.SetA(255);
23
24 this->SetTimer(Timers::mainTimer,30);
25}
26
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

and create a function called by the timer:

Messaging Part II
CPP
1void Messaging2::on_Timer(uint8_t timerID)
2{
3 if(timerID==Timers::mainTimer)
4 {
5 this->dot_x+=10;
6 if(this->dot_x>240) this->dot_x = 0;
7
8 if(this->Module==0)
9 {
10 if(this->beat == 0)
11 {
12 if(this->dot_x==140)
13 {
14 this->beat = 255;
15
16 //Send the position to other modules
17 this->timerMessage.Reset(true);
18 this->timerMessage.WriteByte(255);
19 this->timerMessage.WriteByte(this->dot_x);
20
21 this->SendNetworkMessage(0, &this->timerMessage);
22 }
23 }
24 else
25 {
26 this->beat-=30;
27 if(this->beat<0) this->beat = 0;
28 }
29 }
30 else
31 {
32 if(this->beat>0)
33 {
34 this->beat-=30;
35 if(this->beat<0) this->beat = 0;
36 }
37 }
38 }
39}
40
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

As you can see, this function performs two tasks at once: it calculates coordinates for animation and sends data to other modules if a certain condition is met.

Note that sending data, although tied to the logic of this application, is performed periodically, regardless of the state of neighboring modules.

Now let's write the code that receives the data. It's quite simple: if cubepp is running on a non-zero module, then we just have to read out the network message data when it received it and assign the values ​​to variables.

Messaging Part II
CPP
1void Messaging2::on_Message(uint32_t type, uint8_t* pkt, u32_t size)
2{
3 NetworkMessage nm(pkt,size);
4
5 if(this->Module!=0)
6 {
7 this->beat = (int)nm.ReadByte();
8 this->dot_x = (int)nm.ReadByte();
9 }
10}
11
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

And the last thing: now that all modules "know" about the heartbeat state of the sending module, the only thing left is to draw a nice graph.

I'm sure there shouldn't be any problems with this part.

It's all very simple - the background is drawn, then the graph line is drawn on top, and finally - an animated dot. While the coordinates of this dot are transmitted via a message over the network.

Messaging Part II
CPP
1void Messaging2::on_Render(std::array<Cubios::Screen, 3>& screens)
2{
3 for(auto it = screens.begin(); it != screens.end(); ++it)
4 {
5 //Start adding objects to a screen
6 it->Begin(TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true);
7
8 it->Add(this->Scene[GfxObjects::background]);
9
10 if(beat==0)
11 {
12 it->Add(this->Scene[GfxObjects::idle]);
13 it->Add(this->Scene[GfxObjects::dot])->SetPosition(this->dot_x,122);
14 }
15 else
16 {
17 it->Add(this->Scene[GfxObjects::beat])->SetA(this->beat);
18 it->Add(this->Scene[GfxObjects::idle])->SetA(255-this->beat);
19 it->Add(this->Scene[GfxObjects::dot])->SetPosition(this->dot_x,122)->SetA(255-this->beat);
20 }
21
22 it->Add(this->Scene[GfxObjects::cachette]);
23
24 //Finish adding objects
25 it->End();
26 }
27}
28
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.

What did we get in the end?

One module sends out a heartbeat, the other modules listen to it and show it synchronously. But only if the cube is fully assembled. If the connection is broken, only one source module will show the heartbeat.

Context Rail

Project files

Messaging2.cpp
project/src/Messaging2.cpp
Messaging2.h
project/src/Messaging2.h
wowcubeapp-build.json
project/wowcubeapp-build.json
Jump Grid

On This Page

Network messaging Part IIThe example
Context Rail

Related nodes

info.json
Examples / SDK 6.2 / C++ / network / Messaging Part II
Messaging2.cpp
Examples / SDK 6.2 / C++ / network / Messaging Part II / project / src
Messaging2.h
Examples / SDK 6.2 / C++ / network / Messaging Part II / project / src
wowcubeapp-build.json
Examples / SDK 6.2 / C++ / network / Messaging Part II / project
Previous Node
wowcubeapp-build.json
Examples / SDK 6.2 / C++ / network / Messaging / project
Next Node
info.json
Examples / SDK 6.2 / C++ / network / Messaging Part II