[client] Create PixelMessage class with initial constructor.

This commit is contained in:
Starbeamrainbowlabs 2017-03-02 13:07:58 +00:00
parent 078d9eb75a
commit e16ff31b92
4 changed files with 53 additions and 6 deletions

View File

@ -38,6 +38,12 @@ public:
PixelBot();
~PixelBot();
/// <summary>
/// The current protocol version that this this PixelBot implementation understands.
/// Very important to avoid strange behavious when a PixelBot hasn't has it's code updated.
/// </summary>
static const unsigned short PROTOCOL_VERSION = 1;
/// <summary>
/// Listens for beacon pings that reveal the location of the PixelHub server.
/// </summary>

View File

@ -1 +0,0 @@

View File

@ -0,0 +1,29 @@
#include "PixelMessage.h"
#include <Arduino.h>
PixelMessage::PixelMessage()
{
}
PixelMessage::PixelMessage(byte* rawMessage) : PixelMessage()
{
// Casting solution from http://stackoverflow.com/a/300837/1460422
// Basically, it casts a byte* pointer reference to the start of the bit we want to extract
// into a void* (which is just a point that points to a specific point in memory without a
// specific type associated with it), which we then cast into a pointer of the type we
// actually want. This extra step is needed to get it to cast multiple consecutive bytes
// in the raw message into the type we want.
// With the casts done, we simply dereference the pointer to shove the value into the
// instance of this class that we're building.
ProtocolVersion = *(static_cast<unsigned short*>(static_cast<void*>(rawMessage)));
MessageType = *(static_cast<unsigned short*>(static_cast<void*>(rawMessage + 2)));
MessageId = *(static_cast<unsigned int*>(static_cast<void*>(rawMessage + 4)));
MessageLength = *(static_cast<unsigned int*>(static_cast<void*>(rawMessage + 8)));
}
PixelMessage::~PixelMessage()
{
}

View File

@ -1,17 +1,30 @@
#pragma once
#include <Arduino.h>
struct PixelMessage
{
public:
PixelMessage();
PixelMessage(byte* rawMessage);
~PixelMessage();
const ushort ProtocolVersion;
const ushort MessageType;
const uint MessageId;
const uint MessageLength;
/// <summary>
/// The length of the message header, in bytes.
/// The message header contains things like the protocol version, the payload length, and the message type.
/// </summary>
static const unsigned int MESSAGE_HEADER_LENGTH = 12;
// The protocol version associated with this message.
unsigned short ProtocolVersion = 1;
unsigned short MessageType = 1;
unsigned int MessageId = 0;
unsigned int MessageLength = 0;
//const byte* Payload;
private:
}
};