Skip to content

Commit

Permalink
C++ Message system
Browse files Browse the repository at this point in the history
Setting all colors differently
  • Loading branch information
McZonk committed Sep 15, 2012
1 parent b465f34 commit b12fcaa
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 111 deletions.
30 changes: 30 additions & 0 deletions ColorArrayMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "Message.h"

#include "HSBColor.h"

namespace RGBStrip {

class ColorArrayMessage : public Message {
public:
static const uint8_t Type = 0x02;

static uint16_t size(uint16_t count) {
return sizeof(ColorArrayMessage) + sizeof(HSBColor) * count;
}

public:
uint16_t offset;
uint16_t count;

HSBColor colors[0];

void fillHeader(uint16_t count) {
header.identifier = Identifier;
header.type = Type;
header.length = size(count);
}
};

}
30 changes: 30 additions & 0 deletions ColorMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "Message.h"

#include "HSBColor.h"

namespace RGBStrip {

class ColorMessage : public Message {
public:
static const uint8_t Type = 0x01;

static uint16_t size() {
return sizeof(ColorMessage);
}

public:
uint16_t offset;
uint16_t count;

HSBColor color;

void fillHeader() {
header.identifier = Identifier;
header.type = Type;
header.length = size();
}
};

}
57 changes: 57 additions & 0 deletions Message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

namespace RGBStrip {

class Message {
public:
static const uint16_t MaxLength = 520;

static const uint8_t Identifier = 0x42;

struct Header {
uint8_t identifier;
uint8_t type;

uint16_t checksum;
uint16_t length;
uint16_t code;
};

public:
Header header;
unsigned char data[0];

public:
uint16_t calcChecksum() const {
const uint16_t length = header.length - sizeof(header);

uint16_t checksum = header.type;
for(uint16_t index = 0; index < length; ++index)
{
checksum += data[index];
}

return checksum;
}

void fillChecksum()
{
header.checksum = calcChecksum();
}

bool validateChecksum() const
{
if(header.identifier != Identifier)
{
return false;
}

if(header.checksum != calcChecksum())
{
return false;
}

return true;
}
};
}
8 changes: 6 additions & 2 deletions REDLEDClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
50A27ADC15FB93EA00B0D39B /* RGBSCBonjourServiceCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RGBSCBonjourServiceCell.m; sourceTree = "<group>"; };
50A27ADE15FB9B2800B0D39B /* RGBSCViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RGBSCViewController.h; sourceTree = "<group>"; };
50A27ADF15FB9B2800B0D39B /* RGBSCViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RGBSCViewController.mm; sourceTree = "<group>"; };
50AD9ACF15F8DF98005D031A /* RGBStrip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RGBStrip.h; sourceTree = "<group>"; };
50AD9AD015F8DFA1005D031A /* MCUDPSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCUDPSocket.h; sourceTree = "<group>"; };
50AD9AD115F8DFA1005D031A /* MCUDPSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MCUDPSocket.m; sourceTree = "<group>"; };
50AF0CC015F8D39B00BB3C5F /* REDLEDClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = REDLEDClient.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -54,6 +53,9 @@
50CB647215FE4CBD001C5999 /* UIPColorSlider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIPColorSlider.m; sourceTree = "<group>"; };
50CB647415FE4DEC001C5999 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
50CB647615FE5051001C5999 /* Circle.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Circle.png; sourceTree = "<group>"; };
50D6C3C5160524A6005E7263 /* Message.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Message.h; sourceTree = "<group>"; };
50D6C3C916052803005E7263 /* ColorMessage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ColorMessage.h; sourceTree = "<group>"; };
50D6C3CA16052925005E7263 /* ColorArrayMessage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ColorArrayMessage.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -74,11 +76,13 @@
50288CB01602587E005F6F3A /* Shared */ = {
isa = PBXGroup;
children = (
50AD9ACF15F8DF98005D031A /* RGBStrip.h */,
50288CAC16025874005F6F3A /* RGBColor.h */,
50288CAB16025874005F6F3A /* RGBColor.cpp */,
50288CAA16025874005F6F3A /* HSBColor.h */,
50288CA916025874005F6F3A /* HSBColor.cpp */,
50D6C3C5160524A6005E7263 /* Message.h */,
50D6C3C916052803005E7263 /* ColorMessage.h */,
50D6C3CA16052925005E7263 /* ColorArrayMessage.h */,
);
name = Shared;
sourceTree = "<group>";
Expand Down
43 changes: 33 additions & 10 deletions REDLEDClient/RGBSCViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#import "MCUDPSocket.h"

#import "RGBStrip.h"
#import "HSBColor.h"
#import "ColorMessage.h"
#import "ColorArrayMessage.h"

@interface RGBSCViewController () <NSStreamDelegate>

Expand Down Expand Up @@ -65,6 +65,28 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (void)bindToAddress:(NSData*)address
{
self.socket = [[MCUDPSocket alloc] initWithAddress:address];

#if 0
// Test random colors

NSMutableData* data = [NSMutableData dataWithLength:RGBStrip::ColorArrayMessage::size(56)];

RGBStrip::ColorArrayMessage& message = *(RGBStrip::ColorArrayMessage*)data.mutableBytes;

message.fillHeader(56);

message.offset = 0;
message.count = 56;

for(NSUInteger index = 0; index < 56; ++index)
{
message.colors[index] = HSBColor((uint16_t)(rand() % 1536), (uint8_t)(rand() % 255), (uint8_t)(rand() % 256));
}

message.fillChecksum();

NSLog(@"%@ success:%d", data, [self.socket sendData:data]);
#endif
}

- (IBAction)sliderChanged:(id)sender
Expand Down Expand Up @@ -92,18 +114,19 @@ - (IBAction)sliderChanged:(id)sender
[UIColor colorWithHue:h saturation:s brightness:0.0f alpha:1.0f],
[UIColor colorWithHue:h saturation:s brightness:1.0f alpha:1.0f],
];

RGBStripMessageSetRange message;
message.color = HSBColor(h, s, b);

message.firstLED = self.firstStepper.value;
message.lastLED = self.lastStepper.value;
NSMutableData* data = [NSMutableData dataWithLength:RGBStrip::ColorMessage::size()];

RGBStrip::ColorMessage& message = *(RGBStrip::ColorMessage*)data.mutableBytes;

message.animation = 0;
message.fillHeader();

message.fillChecksum();
message.offset = self.firstStepper.value;
message.count = self.lastStepper.value - self.firstStepper.value;

message.color = HSBColor(h, s, b);

NSData* data = message;
message.fillChecksum();

NSLog(@"%@ success:%d", data, [self.socket sendData:data]);
}
Expand Down
81 changes: 0 additions & 81 deletions RGBStrip.h

This file was deleted.

Loading

0 comments on commit b12fcaa

Please sign in to comment.