Browse Source

[client] Create PixelMessage class with initial constructor.

master
Starbeamrainbowlabs 4 years ago
parent
commit
e16ff31b92
  1. 6
      PixelHub-Client/PixelBot.h
  2. 1
      PixelHub-Client/PixelMessaage.cpp
  3. 29
      PixelHub-Client/PixelMessage.cpp
  4. 23
      PixelHub-Client/PixelMessage.h

6
PixelHub-Client/PixelBot.h

@ -37,6 +37,12 @@ class PixelBot
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.

1
PixelHub-Client/PixelMessaage.cpp

@ -1 +0,0 @@

29
PixelHub-Client/PixelMessage.cpp

@ -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()
{
}

23
PixelHub-Client/PixelMessage.h

@ -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:
}
};