Skip to content

Commit

Permalink
Merge pull request #41 from sidoh/udp_presets
Browse files Browse the repository at this point in the history
Add support for UDP presets
  • Loading branch information
sidoh authored Apr 6, 2017
2 parents d95d035 + 6afbc47 commit 500d5dc
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 132 deletions.
34 changes: 19 additions & 15 deletions lib/Udp/V6CctCommandHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
#include <V6CctCommandHandler.h>

bool V6CctCommandHandler::handlePreset(
MiLightClient* client,
uint8_t commandLsb,
uint32_t commandArg)
{
return false;
}

bool V6CctCommandHandler::handleCommand(
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
MiLightClient* client,
uint32_t command,
uint32_t commandArg)
{
const uint8_t cmd = command & 0xFF;
const uint8_t arg = commandArg >> 24;

client->prepare(MilightCctConfig, deviceId, group);


if (cmd == V2_CCT_COMMAND_PREFIX) {
switch (arg) {
case V2_CCT_ON:
client->updateStatus(ON);
break;

case V2_CCT_OFF:
client->updateStatus(OFF);
break;

case V2_CCT_BRIGHTNESS_DOWN:
client->decreaseBrightness();
break;

case V2_CCT_BRIGHTNESS_UP:
client->increaseBrightness();
break;

case V2_CCT_TEMPERATURE_DOWN:
client->decreaseTemperature();
break;

case V2_CCT_TEMPERATURE_UP:
client->increaseTemperature();
break;

default:
return false;
}

return true;
}

return false;
}
}
20 changes: 12 additions & 8 deletions lib/Udp/V6CctCommandHandler.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <V6CommandHandler.h>

#ifndef _V6_CCT_COMMAND_HANDLER_H
#define _V6_CCT_COMMAND_HANDLER_H
#define _V6_CCT_COMMAND_HANDLER_H

enum CctCommandIds {
V2_CCT_COMMAND_PREFIX = 0x01,

V2_CCT_BRIGHTNESS_UP = 0x01,
V2_CCT_BRIGHTNESS_DOWN = 0x02,
V2_CCT_TEMPERATURE_UP = 0x03,
Expand All @@ -20,15 +20,19 @@ class V6CctCommandHandler : public V6CommandHandler {
V6CctCommandHandler()
: V6CommandHandler(0x0100, MilightCctConfig)
{ }

virtual bool handleCommand(
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
MiLightClient* client,
uint32_t command,
uint32_t commandArg
);


virtual bool handlePreset(
MiLightClient* client,
uint8_t commandLsb,
uint32_t commandArg
);

};

#endif
#endif
33 changes: 20 additions & 13 deletions lib/Udp/V6ComamndHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ V6CommandHandler* V6CommandHandler::ALL_HANDLERS[] = {
};

const size_t V6CommandHandler::NUM_HANDLERS = size(ALL_HANDLERS);
bool V6CommandHandler::handleCommand(MiLightClient* client,

bool V6CommandHandler::handleCommand(MiLightClient* client,
uint16_t deviceId,
uint8_t group,
uint8_t commandType,
uint32_t command,
uint32_t commandArg)
uint32_t commandArg)
{
client->prepare(radioConfig, deviceId, group);

if (commandType == V6_PAIR) {
client->pair();
} else if (commandType == V6_UNPAIR) {
client->unpair();
} else if (commandType == V6_PRESET) {
return this->handlePreset(client, command, commandArg);
} else if (commandType == V6_COMMAND) {
return this->handleCommand(client, deviceId, group, command, commandArg);
return this->handleCommand(client, command, commandArg);
} else {
return false;
}

return true;
}

bool V6CommandDemuxer::handleCommand(MiLightClient* client,
bool V6CommandDemuxer::handleCommand(MiLightClient* client,
uint16_t deviceId,
uint8_t group,
uint8_t commandType,
Expand All @@ -49,15 +51,20 @@ bool V6CommandDemuxer::handleCommand(MiLightClient* client,
return true;
}
}

return false;
}

bool V6CommandDemuxer::handleCommand(MiLightClient* client,
uint16_t deviceId,
uint8_t group,
uint32_t command,
bool V6CommandDemuxer::handleCommand(MiLightClient* client,
uint32_t commandLsb,
uint32_t commandArg)
{
return false;
}
}

bool V6CommandDemuxer::handlePreset(MiLightClient* client,
uint8_t commandLsb,
uint32_t commandArg)
{
return false;
}
47 changes: 28 additions & 19 deletions lib/Udp/V6CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,84 @@
#include <MiLightRadioConfig.h>

#ifndef _V6_COMMAND_HANDLER_H
#define _V6_COMMAND_HANDLER_H
#define _V6_COMMAND_HANDLER_H

enum V6CommandTypes {
V6_PAIR = 0x3D,
V6_UNPAIR = 0x3E,
V6_PRESET = 0x3F,
V6_COMMAND = 0x31
};

class V6CommandHandler {
public:
static V6CommandHandler* ALL_HANDLERS[] PROGMEM;
static const size_t NUM_HANDLERS;

V6CommandHandler(uint16_t commandId, MiLightRadioConfig& radioConfig)
: commandId(commandId),
radioConfig(radioConfig)
{ }

virtual bool handleCommand(
MiLightClient* client,
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
uint8_t commandType,
uint32_t command,
uint32_t commandArg
);

const uint16_t commandId;
MiLightRadioConfig& radioConfig;

protected:

virtual bool handleCommand(
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
MiLightClient* client,
uint32_t command,
uint32_t commandArg
) = 0;

virtual bool handlePreset(
MiLightClient* client,
uint8_t commandLsb,
uint32_t commandArg
) = 0;
};

class V6CommandDemuxer : public V6CommandHandler {
public:
V6CommandDemuxer(V6CommandHandler* handlers[], size_t numHandlers)
: V6CommandHandler(0, MilightRgbwConfig),
: V6CommandHandler(0, MilightRgbwConfig),
handlers(handlers),
numHandlers(numHandlers)
{ }

virtual bool handleCommand(
MiLightClient* client,
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
uint8_t commandType,
uint32_t command,
uint32_t commandArg
);

protected:
V6CommandHandler** handlers;
size_t numHandlers;

virtual bool handleCommand(
MiLightClient* client,
uint16_t deviceId,
uint8_t group,
MiLightClient* client,
uint32_t command,
uint32_t commandArg
);

virtual bool handlePreset(
MiLightClient* client,
uint8_t commandLsb,
uint32_t commandArg
);
};

#endif
#endif
Loading

0 comments on commit 500d5dc

Please sign in to comment.