Example: Messaging2.cpp
1// This file is generated by WOWCube SDK project wizard2//3// Application UUID: 9WEBpryloR4//56#include <stdint.h>7#include <string>8#include <vector>9#include <string>10#include <memory>1112#include "AppManager.h"13#include "Messaging2.h"1415using namespace Cubios;16using namespace Cubios::Math;17using namespace Cubios::Gfx;1819//Applicaton initialization callback. Called once when CUB application starts20void OnInit(Cubios::AppManager& appMgr)21{22 Messaging2* theApp = new Messaging2();2324 appMgr.SetApplication(theApp,"9WEBpryloR");2526 theApp->InitializeResources();27}282930Messaging2::Messaging2():dot_x(0),beat(0)31{32}3334Messaging2::~Messaging2()35{36 this->Scene.DisposeAllObjects();37}3839void Messaging2::InitializeResources()40{41 //Create a Cubios::Gfx::Sprite object42 this->Scene.CreateObjectWithID(GfxObjects::idle,new Sprite("idle.png",Transform(120,120,0)));43 this->Scene[GfxObjects::idle]->Color.Set(0xFFFFFFFF);44 this->Scene[GfxObjects::idle]->Color.SetA(255);4546 this->Scene.CreateObjectWithID(GfxObjects::beat,new Sprite("beat.png",Transform(120,120,0)));47 this->Scene[GfxObjects::beat]->Color.Set(0xFFFFFFFF);48 this->Scene[GfxObjects::beat]->Color.SetA(255);4950 this->Scene.CreateObjectWithID(GfxObjects::dot,new Sprite("dot.png",Transform(120,120,0)));51 this->Scene[GfxObjects::dot]->Color.Set(0xFFFFFFFF);52 this->Scene[GfxObjects::dot]->Color.SetA(255);5354 this->Scene.CreateObjectWithID(GfxObjects::background,new Sprite("background.png",Transform(120,120,0)));55 this->Scene[GfxObjects::background]->Color.Set(0xFFFFFFFF);56 this->Scene[GfxObjects::background]->Color.SetA(255);5758 this->Scene.CreateObjectWithID(GfxObjects::cachette,new Sprite("cachette.png",Transform(120,120,0)));59 this->Scene[GfxObjects::cachette]->Color.Set(0xFFFFFFFF);60 this->Scene[GfxObjects::cachette]->Color.SetA(255);6162 this->SetTimer(Timers::mainTimer,30);63}6465//This callback is called every "tick" of cubeapp application loop66void Messaging2::on_Tick(uint32_t currentTime, uint32_t deltaTime)67{68 if(this->Module!=0)69 {70 if(!Cubios::TOPOLOGY_isAssembled()) this->beat = 0;71 }72}7374//This callback is called every time device is about to render visuals. Use it for calling your rendering code.75void Messaging2::on_Render(std::array<Cubios::Screen, 3>& screens)76{77 for(auto it = screens.begin(); it != screens.end(); ++it)78 {79 //Start adding objects to a screen80 it->Begin(TOPOLOGY_orientation_mode_t::ORIENTATION_MODE_GRAVITY,true);8182 it->Add(this->Scene[GfxObjects::background]);8384 if(beat==0)85 {86 it->Add(this->Scene[GfxObjects::idle]);87 it->Add(this->Scene[GfxObjects::dot])->SetPosition(this->dot_x,122);88 }89 else90 {91 it->Add(this->Scene[GfxObjects::beat])->SetA(this->beat);92 it->Add(this->Scene[GfxObjects::idle])->SetA(255-this->beat);93 it->Add(this->Scene[GfxObjects::dot])->SetPosition(this->dot_x,122)->SetA(255-this->beat);94 }9596 it->Add(this->Scene[GfxObjects::cachette]);9798 //Finish adding objects99 it->End();100 }101}102103//The "physics" callback. Gets called recurrently with at least 33ms resolution or less depending on the load.104void Messaging2::on_PhysicsTick(const std::array<Cubios::Screen, 3>& screens)105{106}107108//This callback is called on programmable timer (if any)109void Messaging2::on_Timer(uint8_t timerID)110{111 if(timerID==Timers::mainTimer)112 {113 this->dot_x+=10;114 if(this->dot_x>240) this->dot_x = 0;115116 if(this->Module==0)117 {118 if(this->beat == 0)119 {120 if(this->dot_x==140)121 {122 this->beat = 255;123124 //Send the position to other modules125 this->timerMessage.Reset(true);126 this->timerMessage.WriteByte(255);127 this->timerMessage.WriteByte(this->dot_x);128129 this->SendNetworkMessage(0, &this->timerMessage);130 }131 }132 else133 {134 this->beat-=30;135 if(this->beat<0) this->beat = 0;136 }137 }138 else139 {140 if(this->beat>0)141 {142 this->beat-=30;143 if(this->beat<0) this->beat = 0;144 }145 }146 }147}148149//The cube topology change callback. Gets called when cube is twisted and its topological desctiption has been changed150void Messaging2::on_Twist(const Cubios::TOPOLOGY_twistInfo_t& twist)151{152}153154//The "inner network" callback. Gets called when WOWCube module receives a data packet from other module155void Messaging2::on_Message(uint32_t type, uint8_t* pkt, u32_t size)156{157 NetworkMessage nm(pkt,size);158159 if(this->Module!=0)160 {161 this->beat = (int)nm.ReadByte();162 this->dot_x = (int)nm.ReadByte();163 }164}165166//The external data transfer callback. Gets called when WOWCube module receives a data over BLE from an external source167void Messaging2::on_ExternalMessage(uint8_t* pkt, u32_t size)168{169}170171//Screen pat callback172void Messaging2::on_Pat(uint32_t count)173{174}175176//This callback is called before the application quits177void Messaging2::on_Close()178{179}