Network messaging
As we know, WOWCube consists of eight modules, and each module is a separate independent micro-computer with its own processor, memory and storage space. Obviously, the modules must be able to interact and transfer information to each other, how else can we possibly create really high-quality games?
Such a connection between modules certainly exists.
In much the same way as in the case of a conventional network, computers are connected with network cables, the WOWCube modules are connected by UART channels.
Without much digging into technical details, UART, or universal asynchronous receiver-transmitter, is a device-to-device communication protocol, one of the most used ones too.
By definition,
UARTis a hardware communication protocol that uses asynchronous serial communication with configurable speed. Asynchronous means there is no clock signal to synchronize the output bits from the transmitting device going to the receiving end.
The purpose of a transmitter and receiver channel for each device is to transmit and receive serial data intended for serial communication.
So as you can see, the analogy with ordinary network is pretty close. The modules are connected via UART channels, these connections form an internal "network" inside WOWCube device. The only difference is, each module has three adjoined modules, therefore each module must have three UART channels to be able to talk to all its neighbours.
The example
In this example we actually want to do rather simple, but very important thing which takes an essential part in the architecture of many WOWCube games - we want to send a message from one module to the other. I mean, once you know how to do that, you know how to send many more messages wherever and whenever you need to, right?
So let's begin.
Firstly, we define commands we want to be able to send from one module to the other:
This is more for convenience really. Nothing stops us from sending of some raw data, some decimal number or floats. But the code would look tidier if you just give a bit of a human readable description to these values.
So yeah, we've defined three commands.
But how to use them? What exactly to command?
The answer is, we will command one module from the other one. One module asks to do something - the other does it. One module sends commands - the other one receives them.
Simple.
How the commanding module knows it should send the command? Even simple, it will do that on tap. You tap it - and it sends the command to use another color.
In order to handle screen taps, on_Tap(uint32_t count) callback function is used:
and the sendDataToNeighbours() function is called to broadcast current command to the adjoined modules.
Now, to receive the message, void Messaging::on_Message(uint32_t type, uint8_t* pkt, u32_t size) callback is used. It gets called every time when WOWCube module receives data over UART channels.
And finally, in order for us to know that the message has been sent and received successfully, let's give some visual feedback: