Skip to content

Commit

Permalink
polish up lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
dadleyy committed Nov 6, 2023
1 parent 244625b commit 8845f1e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/beetle-pio/lib/redis-events/redis-event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace redisevents {

struct PayloadReceived {
struct PayloadReceived final {
uint32_t size;
};

struct IdentificationReceived {};
struct IdentificationReceived final {};

struct Authorized {};
struct Authorized final {};

struct FailedConnection {};
struct FailedConnection final {};

typedef std::variant<PayloadReceived, Authorized, FailedConnection,
IdentificationReceived>
Expand Down
14 changes: 10 additions & 4 deletions src/beetle-pio/lib/redis-events/redis-events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ class Events final {
*/
std::pair<std::variant<Disconnected, Connected>, std::optional<RedisEvent>>
read_ok(Connected connected) {
bool pending_burnin_auth = connected.authorization_stage ==
AuthorizationStage::AuthorizationRequested;

while (context->client.available()) {
auto token = (char)context->client.read();
auto event = reader->fill(token, buffer);
Expand All @@ -201,14 +204,17 @@ class Events final {
read_len.size);

connected.authorization_stage =
connected.authorization_stage ==
AuthorizationStage::AuthorizationRequested
? AuthorizationStage::AuthorizationReceived
: AuthorizationStage::FullyAuthorized;
pending_burnin_auth ? AuthorizationStage::AuthorizationReceived
: AuthorizationStage::FullyAuthorized;
}
}
}

if (connected.authorization_stage ==
AuthorizationStage::FullyAuthorized) {
return std::make_pair(connected, Authorized{});
}

return std::make_pair(connected, std::nullopt);
}

Expand Down
27 changes: 23 additions & 4 deletions src/beetle-pio/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,46 @@ Engine::Engine(std::tuple<const char*, const char*> ap_config,
_redis(redis_config) {}

// Prepares the wifi server and memory locations for our persistent data like
// SSID,
// password and redis credentials.
// SSID, password and redis credentials.
void Engine::begin(void) {
_wifi.begin();
_redis.begin();
}

// Given a reference to a state, this method will poll events on both the wifi
// an redis
// channels, attempting to update the state.
// and redis channels, attempting to update the state.
states::State Engine::update(states::State&& current, uint32_t current_time) {
states::State next(std::move(current));

auto wifi_update = _wifi.update(current_time);

if (wifi_update != std::nullopt) {
switch (*wifi_update) {
case wifievents::Events::EMessage::Connecting:
next = states::Connecting{};
break;
case wifievents::Events::EMessage::ConnectionResumed:
case wifievents::Events::EMessage::Connected:
next = states::Connected{};
break;
case wifievents::Events::EMessage::FailedConnection:
case wifievents::Events::EMessage::ConnectionInterruption:
case wifievents::Events::EMessage::Disconnected:
next = states::Unknown{};
break;
}
}

auto redis_update = _redis.update(wifi_update, _buffer, current_time);

if (redis_update != std::nullopt &&
std::holds_alternative<redisevents::PayloadReceived>(*redis_update)) {
auto payload_info = std::get<redisevents::PayloadReceived>(*redis_update);
log_i("we have a payload of %d bytes from redis", payload_info.size);
next = states::HoldingUpdate{_buffer, payload_info.size};
} else if (redis_update != std::nullopt &&
std::holds_alternative<redisevents::Authorized>(*redis_update)) {
next = states::Working{};
}

return next;
Expand Down
2 changes: 2 additions & 0 deletions src/beetle-pio/src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Engine final {
~Engine() = default;

Engine() = delete;
Engine(Engine &) = delete;
Engine &operator=(Engine &) = delete;
Engine(const Engine &) = delete;
Engine &operator=(const Engine &) = delete;

Expand Down
2 changes: 1 addition & 1 deletion src/beetle-pio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void loop(void) {
display_render_unknown(last_frame);
}

state = states::Unknown{};
state = states::Idle{};

last_frame = now;
#ifndef RELEASE
Expand Down
7 changes: 6 additions & 1 deletion src/beetle-pio/src/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ constexpr static const uint32_t BUFFER_SIZE = 1024 * 80;

struct Unknown final {};

struct Idle final {};

struct Connecting final {};

struct Connected final {};

struct Working final {};

struct Configuring final {};

struct HoldingUpdate final {
std::shared_ptr<std::array<uint8_t, BUFFER_SIZE>> buffer;
uint32_t size;
};

typedef std::variant<Unknown, HoldingUpdate, Connected, Connecting, Configuring>
typedef std::variant<Idle, Working, Unknown, HoldingUpdate, Connected,
Connecting, Configuring>
State;
}

Expand Down
56 changes: 29 additions & 27 deletions src/beetle-pio/src/xiao-lighting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Lighting final {

Lighting(const Lighting&& other)
: _override(other._override), _pixels(std::move(other._pixels)) {}

Lighting& operator=(const Lighting&& other) {
this->_override = other._override;
this->_pixels = std::move(other._pixels);
Expand All @@ -34,48 +35,45 @@ class Lighting final {
uint32_t color = _pixels.Color(0, 0, 0);

if (std::holds_alternative<states::Unknown>(state)) {
log_e("unknown lighting state");
color = _pixels.Color(200, 0, 0);
}
if (std::holds_alternative<states::Connecting>(state)) {
} else if (std::holds_alternative<states::Connecting>(state)) {
log_e("connecting lighting state");
color = _pixels.Color(20, 0, 100);
}
if (std::holds_alternative<states::Connected>(state)) {
} else if (std::holds_alternative<states::Connected>(state)) {
log_e("connected lighting state");
color = _pixels.Color(0, 100, 100);
}
if (std::holds_alternative<states::Configuring>(state)) {
} else if (std::holds_alternative<states::Working>(state)) {
log_e("has working state");
color = _pixels.Color(0, 200, 0);
} else if (std::holds_alternative<states::Configuring>(state)) {
log_e("configuring lighting state");
color = _pixels.Color(100, 100, 0);
}
if (std::holds_alternative<states::HoldingUpdate>(state)) {
color = _pixels.Color(0, 100, 0);

} else if (std::holds_alternative<states::Idle>(state)) {
return *this;
} else if (std::holds_alternative<states::HoldingUpdate>(state)) {
color = _pixels.Color(0, 200, 0);
states::HoldingUpdate* working_state =
std::get_if<states::HoldingUpdate>(&state);

/*
bool sent = false;
char* prefix_match =
strstr((char*)working_state->buffer->data(), LIGHTING_PREFIX);

for (auto message = working_state->begin();
message != working_state->end(); message++) {
if (message->size == 0 || sent) {
continue;
}
char* prefix_match = strstr(message->content, LIGHTING_PREFIX);
if (prefix_match == nullptr) {
log_i("skipping non-lighting related message of size '%d'",
message->size);
continue;
}
sent = true;
if (prefix_match == nullptr) {
log_i("skipping non-lighting related message of size '%d'",
working_state->size);
} else {
char* lighting_command =
(char*)working_state->buffer->data() + LIGHTING_PREFIX_LEN;

if (strcmp(message->content + LIGHTING_PREFIX_LEN, "off") == 0) {
if (strcmp(lighting_command, "off") == 0) {
log_i("turning lights off");
_override = true;
} else if (strcmp(message->content + LIGHTING_PREFIX_LEN, "on") == 0) {
} else if (strcmp(lighting_command, "on") == 0) {
log_i("turning lights on");
_override = false;
}
}
*/
}

setAll(color);
Expand All @@ -102,14 +100,18 @@ class Lighting final {

private:
void setAll(uint32_t color) {
log_i("doing lighting (override: %d)", _override);
_pixels.clear();

if (_override) {
_pixels.show();
return;
}

for (uint8_t i = 0; i < XIAO_NEOPIXEL_COUNT; i++) {
_pixels.setPixelColor(i, color);
}

_pixels.show();
}

Expand Down

0 comments on commit 8845f1e

Please sign in to comment.