Skip to content

Commit

Permalink
add more templating
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth10001 committed Sep 14, 2023
1 parent a1c8b62 commit f154023
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
51 changes: 51 additions & 0 deletions FrontLights.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <FrontLights.hpp>
#include <DataModuleInfo.hpp>

namespace SolarGators {
namespace DataModules {
namespace {
static constexpr uint32_t SIZE = 3;
}

FrontLights::FrontLights():
DataModule(SolarGators::DataModuleInfo::FRONT_LIGHTS_ID, 0, SIZE),

throttle(0)

{ }

FrontLights::~FrontLights()
{ }


uint_16_t FrontLights::GetthrottleVal() const
{
return throttle;
}


void FrontLights::ToByteArray(uint8_t* buff) const
{


buff[0] = static_cast<uint8_t>(throttle << 0);

buff[1] = static_cast<uint8_t>(throttle << 1);


}
void FrontLights::FromByteArray(uint8_t* buff)
{

throttle_ = static_cast<uint16_t>(buff[1]) << 8 | buff[0];

}

#ifdef IS_TELEMETRY
void FrontLights::PostTelemetry(PythonScripts* scripts) {

}
#endif

} /* namespace DataModules */
} /* namespace SolarGators */
Empty file.
22 changes: 13 additions & 9 deletions datamodule/cpp/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ namespace {
{ }

{% for attribute in attributes %}
{{ attribute["type"] }} {{ moduleName }}::Get{{ attribute["name"] }}Val() const
{
return {{ attribute["name"] }};
}
{{ attribute["type"] }} {{ moduleName }}::Get{{ attribute["name"] }}Val() const
{
return {{ attribute["name"] }};
}
{% endfor %}

void {{ moduleName }}::ToByteArray(uint8_t* buff) const
{
buff[0] = static_cast<uint8_t>(throttle_);
buff[1] = static_cast<uint8_t>(throttle_ >> 8);
buff[2] = static_cast<uint8_t>(breaks_);
{% for attribute in attributes %}
{%- for byte in range(attribute["bytes"]) %}
buff[{{ byte }}] = static_cast<uint8_t>({{ attribute["name"] }} >> {{ byte }});
{% endfor %}
{% endfor %}
}
void {{ moduleName }}::FromByteArray(uint8_t* buff)
{
throttle_ = static_cast<uint16_t>(buff[1]) << 8 | buff[0];
breaks_ = static_cast<bool>(buff[2] & 0x1);
{% for attribute in attributes %}
// TODO: This one is much harder and prone to error
throttle_ = static_cast<uint16_t>(buff[1]) << 8 | buff[0];
{% endfor %}
}

#ifdef IS_TELEMETRY
Expand Down
12 changes: 9 additions & 3 deletions datamodule/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ def main():

for can_message in config["can_messages"]:

cpp_template.render(
moduleName=can_message["name"]
)
for attribute in can_message["schema"]:
attribute["bytes"] = 2

with open(f"{can_message['name']}.cpp", "w") as f:
file = cpp_template.render(
moduleName=can_message["name"],
attributes=can_message["schema"],
)
f.write(file)


if __name__ == "__main__":
Expand Down

0 comments on commit f154023

Please sign in to comment.