Skip to content

Commit

Permalink
Update flatbuffers code
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 29, 2025
1 parent 8474cbf commit c8452f6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ active():boolean {
return offset ? !!this.bb!.readInt8(this.bb_pos + offset) : false;
}

/**
* Set When EmergencyStop is a latching switch
*/
latching():boolean {
const offset = this.bb!.__offset(this.bb_pos, 10);
return offset ? !!this.bb!.readInt8(this.bb_pos + offset) : false;
}

static startEStopConfig(builder:flatbuffers.Builder) {
builder.startObject(3);
builder.startObject(4);
}

static addEnabled(builder:flatbuffers.Builder, enabled:boolean) {
Expand All @@ -59,16 +67,21 @@ static addActive(builder:flatbuffers.Builder, active:boolean) {
builder.addFieldInt8(2, +active, +false);
}

static addLatching(builder:flatbuffers.Builder, latching:boolean) {
builder.addFieldInt8(3, +latching, +false);
}

static endEStopConfig(builder:flatbuffers.Builder):flatbuffers.Offset {
const offset = builder.endObject();
return offset;
}

static createEStopConfig(builder:flatbuffers.Builder, enabled:boolean, gpioPin:number, active:boolean):flatbuffers.Offset {
static createEStopConfig(builder:flatbuffers.Builder, enabled:boolean, gpioPin:number, active:boolean, latching:boolean):flatbuffers.Offset {
EStopConfig.startEStopConfig(builder);
EStopConfig.addEnabled(builder, enabled);
EStopConfig.addGpioPin(builder, gpioPin);
EStopConfig.addActive(builder, active);
EStopConfig.addLatching(builder, latching);
return EStopConfig.endEStopConfig(builder);
}
}
4 changes: 4 additions & 0 deletions include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#define OPENSHOCK_ESTOP_PIN OPENSHOCK_GPIO_INVALID
#endif

#ifndef OPENSHOCK_ESTOP_LATCHING
#define OPENSHOCK_ESTOP_LATCHING 0
#endif

// Check if OPENSHOCK_FW_USERAGENT is overridden trough compiler flags, if not, generate a default useragent.
#ifndef OPENSHOCK_FW_USERAGENT
#define OPENSHOCK_FW_USERAGENT OPENSHOCK_FW_HOSTNAME "/" OPENSHOCK_FW_VERSION " (arduino-esp32; " OPENSHOCK_FW_BOARD "; " OPENSHOCK_FW_CHIP "; Espressif)"
Expand Down
4 changes: 3 additions & 1 deletion include/config/EStopConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
namespace OpenShock::Config {
struct EStopConfig : public ConfigBase<Serialization::Configuration::EStopConfig> {
EStopConfig();
EStopConfig(bool enabled, gpio_num_t gpioPin);
EStopConfig(bool enabled, gpio_num_t gpioPin, bool latching, bool active);

bool enabled;
gpio_num_t gpioPin;
bool latching;
bool active;

void ToDefault() override;

Expand Down
15 changes: 13 additions & 2 deletions include/serialization/_fbs/HubConfig_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ struct EStopConfig FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_ENABLED = 4,
VT_GPIO_PIN = 6,
VT_ACTIVE = 8
VT_ACTIVE = 8,
VT_LATCHING = 10
};
bool enabled() const {
return GetField<uint8_t>(VT_ENABLED, 0) != 0;
Expand All @@ -203,11 +204,16 @@ struct EStopConfig FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
bool active() const {
return GetField<uint8_t>(VT_ACTIVE, 0) != 0;
}
/// Set When EmergencyStop is a latching switch
bool latching() const {
return GetField<uint8_t>(VT_LATCHING, 0) != 0;
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<uint8_t>(verifier, VT_ENABLED, 1) &&
VerifyField<int8_t>(verifier, VT_GPIO_PIN, 1) &&
VerifyField<uint8_t>(verifier, VT_ACTIVE, 1) &&
VerifyField<uint8_t>(verifier, VT_LATCHING, 1) &&
verifier.EndTable();
}
};
Expand All @@ -225,6 +231,9 @@ struct EStopConfigBuilder {
void add_active(bool active) {
fbb_.AddElement<uint8_t>(EStopConfig::VT_ACTIVE, static_cast<uint8_t>(active), 0);
}
void add_latching(bool latching) {
fbb_.AddElement<uint8_t>(EStopConfig::VT_LATCHING, static_cast<uint8_t>(latching), 0);
}
explicit EStopConfigBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
Expand All @@ -240,8 +249,10 @@ inline ::flatbuffers::Offset<EStopConfig> CreateEStopConfig(
::flatbuffers::FlatBufferBuilder &_fbb,
bool enabled = false,
int8_t gpio_pin = 0,
bool active = false) {
bool active = false,
bool latching = false) {
EStopConfigBuilder builder_(_fbb);
builder_.add_latching(latching);
builder_.add_active(active);
builder_.add_gpio_pin(gpio_pin);
builder_.add_enabled(enabled);
Expand Down
30 changes: 26 additions & 4 deletions src/config/EStopConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ using namespace OpenShock::Config;
EStopConfig::EStopConfig()
: enabled(OpenShock::IsValidInputPin(OPENSHOCK_ESTOP_PIN))
, gpioPin(static_cast<gpio_num_t>(OPENSHOCK_ESTOP_PIN))
, latching(OPENSHOCK_ESTOP_LATCHING)
, active(false)
{
}

EStopConfig::EStopConfig(bool enabled, gpio_num_t gpioPin)
EStopConfig::EStopConfig(bool enabled, gpio_num_t gpioPin, bool latching, bool active)
: enabled(enabled)
, gpioPin(gpioPin)
, latching(latching)
, active(active)
{
}

void EStopConfig::ToDefault()
{
enabled = OpenShock::IsValidInputPin(OPENSHOCK_ESTOP_PIN);
gpioPin = static_cast<gpio_num_t>(OPENSHOCK_ESTOP_PIN);
enabled = OpenShock::IsValidInputPin(OPENSHOCK_ESTOP_PIN);
gpioPin = static_cast<gpio_num_t>(OPENSHOCK_ESTOP_PIN);
latching = OPENSHOCK_ESTOP_LATCHING;
active = false;
}

bool EStopConfig::FromFlatbuffers(const Serialization::Configuration::EStopConfig* config)
Expand All @@ -42,12 +48,16 @@ bool EStopConfig::FromFlatbuffers(const Serialization::Configuration::EStopConfi
enabled = false;
}

latching = config->latching();

active = config->active();

return true;
}

flatbuffers::Offset<OpenShock::Serialization::Configuration::EStopConfig> EStopConfig::ToFlatbuffers(flatbuffers::FlatBufferBuilder& builder, bool withSensitiveData) const
{
return Serialization::Configuration::CreateEStopConfig(builder, enabled, gpioPin);
return Serialization::Configuration::CreateEStopConfig(builder, enabled, gpioPin, active, latching);
}

bool EStopConfig::FromJSON(const cJSON* json)
Expand All @@ -69,6 +79,16 @@ bool EStopConfig::FromJSON(const cJSON* json)
return false;
}

if (!Internal::Utils::FromJsonBool(latching, json, "latching", OPENSHOCK_ESTOP_LATCHING)) {
OS_LOGE(TAG, "Failed to parse latching");
return false;
}

if (!Internal::Utils::FromJsonBool(active, json, "active", false)) {
OS_LOGE(TAG, "Failed to parse active");
return false;
}

return true;
}

Expand All @@ -78,6 +98,8 @@ cJSON* EStopConfig::ToJSON(bool withSensitiveData) const

cJSON_AddBoolToObject(root, "enabled", enabled);
cJSON_AddNumberToObject(root, "gpioPin", gpioPin);
cJSON_AddBoolToObject(root, "latching", latching);
cJSON_AddBoolToObject(root, "active", active);

return root;
}

1 comment on commit c8452f6

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.3) reports: 1 file(s) not formatted
  • include/serialization/_fbs/HubConfig_generated.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.