esphome-ratgdo/components/ratgdo/common.h

94 lines
2.3 KiB
C
Raw Normal View History

#pragma once
#include <cstdint>
#include <functional>
#include "observable.h"
#define ESP_LOG1 ESP_LOGD
#define ESP_LOG2 ESP_LOGD
namespace esphome {
namespace ratgdo {
2024-01-09 16:42:11 +00:00
namespace protocol {
struct SetRollingCodeCounter { uint32_t counter; };
struct GetRollingCodeCounter {};
struct SetClientID { uint64_t client_id; };
2024-01-09 16:42:11 +00:00
struct QueryStatus{};
struct QueryOpenings{};
struct ActivateLearn {};
struct InactivateLearn {};
// a poor man's sum-type, because C++
2024-01-09 16:42:11 +00:00
class Args {
public:
union {
SetRollingCodeCounter set_rolling_code_counter;
GetRollingCodeCounter get_rolling_code_counter;
SetClientID set_client_id;
2024-01-09 16:42:11 +00:00
QueryStatus query_status;
QueryOpenings query_openings;
ActivateLearn activate_learn;
InactivateLearn inactivate_learn;
} value;
enum class Tag {
set_rolling_code_counter,
get_rolling_code_counter,
set_client_id,
2024-01-09 16:42:11 +00:00
query_status,
query_openings,
activate_learn,
inactivate_learn,
} tag;
2024-01-09 16:42:11 +00:00
Args(GetRollingCodeCounter&& arg): tag(Tag::get_rolling_code_counter) {
value.get_rolling_code_counter = std::move(arg);
}
2024-01-09 16:42:11 +00:00
Args(SetRollingCodeCounter&& arg): tag(Tag::set_rolling_code_counter) {
value.set_rolling_code_counter = std::move(arg);
}
2024-01-09 16:42:11 +00:00
Args(SetClientID&& arg): tag(Tag::set_client_id) {
value.set_client_id = std::move(arg);
}
2024-01-09 16:42:11 +00:00
Args(QueryStatus&& arg): tag(Tag::query_status) {
value.query_status = std::move(arg);
}
Args(QueryOpenings&& arg): tag(Tag::query_openings) {
value.query_openings = std::move(arg);
}
Args(ActivateLearn&& arg): tag(Tag::activate_learn) {
value.activate_learn = std::move(arg);
}
2024-01-09 16:42:11 +00:00
Args(InactivateLearn&& arg): tag(Tag::inactivate_learn) {
value.inactivate_learn = std::move(arg);
}
};
2024-01-09 16:42:11 +00:00
struct RollingCodeCounter { observable<uint32_t>* value; };
class Result {
public:
union {
RollingCodeCounter rolling_code_counter;
} value;
enum class Tag {
rolling_code_counter,
void_,
} tag;
Result(): tag(Tag::void_) {
}
Result(RollingCodeCounter&& arg): tag(Tag::rolling_code_counter) {
value.rolling_code_counter = std::move(arg);
}
};
} // namespace protocol
} // namespace ratgdo
} // namespace esphome