Skip to content

Commit

Permalink
Implement more messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabas5532 committed Nov 27, 2023
1 parent ef957e5 commit 6688224
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "test/support/protobuf-c/protobuf-c"]
path = test/support/protobuf-c/protobuf-c
url = https://github.com/protobuf-c/protobuf-c.git
[submodule "thirdparty/esp-idf-components/cppcodec"]
path = thirdparty/esp-idf-components/cppcodec
url = https://github.com/tplgy/cppcodec.git
3 changes: 2 additions & 1 deletion firmware/components/presets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ target_sources(presets
src/selected_preset_json_builder.cpp)
target_include_directories(presets PUBLIC proto/generated include)

target_link_libraries(presets PUBLIC idf::protobuf-c shrapnel::rapidjson shrapnel::etl shrapnel::uuid)
target_link_libraries(presets PUBLIC idf::log idf::protobuf-c shrapnel::rapidjson shrapnel::etl shrapnel::uuid cppcodec)

if(DEFINED TESTING)
add_executable(presets_test
test/presets_json_builder.cpp
test/selected_preset_json_builder.cpp)

target_include_directories(presets_test PRIVATE include)
Expand Down
30 changes: 15 additions & 15 deletions firmware/components/presets/include/presets_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ namespace shrapnel::presets {

struct ParametersData
{
float ampGain;
float ampChannel;
float amp_gain;
float amp_channel;
float bass;
float middle;
float treble;
float contour;
float volume;
float noiseGateThreshold;
float noiseGateHysteresis;
float noiseGateAttack;
float noiseGateHold;
float noiseGateRelease;
float noiseGateBypass;
float chorusRate;
float chorusDepth;
float chorusMix;
float chorusBypass;
float wahPosition;
float wahVocal;
float wahBypass;
float noise_gate_threshold;
float noise_gate_hysteresis;
float noise_gate_attack;
float noise_gate_hold;
float noise_gate_release;
float noise_gate_bypass;
float chorus_rate;
float chorus_depth;
float chorus_mix;
float chorus_bypass;
float wah_position;
float wah_vocal;
float wah_bypass;

std::strong_ordering
operator<=>(const ParametersData &other) const = default;
Expand Down
101 changes: 100 additions & 1 deletion firmware/components/presets/src/presets_json_builder.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,100 @@
#include "presets_json_builder.h"
#include "presets_json_builder.h"
#include "presets.pb-c.h"
#include "uuid.h"
#include <cppcodec/base64_default_rfc4648.hpp>
#include <esp_log.h>

namespace shrapnel::presets {

template <>
rapidjson::Value to_json(rapidjson::Document &document,
const ParametersData &object)
{
struct PresetParameters preset = PRESET_PARAMETERS__INIT;
preset_parameters__init(&preset);

preset.amp_gain = object.amp_gain * 1000;
preset.amp_channel = object.amp_channel * 1000;
preset.bass = object.bass * 1000;
preset.middle = object.middle * 1000;
preset.treble = object.treble * 1000;
preset.contour = object.contour * 1000;
preset.volume = object.volume * 1000;
preset.noise_gate_threshold = object.noise_gate_threshold * 1000;
preset.noise_gate_hysteresis = object.noise_gate_hysteresis * 1000;
preset.noise_gate_attack = object.noise_gate_attack * 1000;
preset.noise_gate_hold = object.noise_gate_hold * 1000;
preset.noise_gate_release = object.noise_gate_release * 1000;
preset.noise_gate_bypass = object.noise_gate_bypass * 1000;
preset.chorus_rate = object.chorus_rate * 1000;
preset.chorus_depth = object.chorus_depth * 1000;
preset.chorus_mix = object.chorus_mix * 1000;
preset.chorus_bypass = object.chorus_bypass * 1000;
preset.wah_position = object.wah_position * 1000;
preset.wah_vocal = object.wah_vocal * 1000;
preset.wah_bypass = object.wah_bypass * 1000;

uint8_t buffer[100] = {};
auto packed_size = preset_parameters__get_packed_size(&preset);
if(packed_size > 100)
{
return {/* null */};
}

preset_parameters__pack(&preset, buffer);

ESP_LOG_BUFFER_HEX("presets", buffer, packed_size);
ESP_LOGI("presets", "Packed size: %zu", packed_size);

using base64_codec = cppcodec::base64_rfc4648;

std::array<char, 200> base64_buffer;
auto encoded_size = base64_codec::encoded_size(packed_size);
if(encoded_size > base64_buffer.size())
{
return {/* null */};
}

base64_codec::encode(
base64_buffer.data(), base64_buffer.size(), buffer, packed_size);

auto json = rapidjson::Value{};
json.SetString(base64_buffer.data(), encoded_size, document.GetAllocator());
return json;
}

template <>
rapidjson::Value to_json(rapidjson::Document &document,
const PresetData &object)
{
rapidjson::Value json;
json.SetObject();

json.AddMember(
"id", uuid::to_json(document, object.id), document.GetAllocator());
rapidjson::Value name{object.name.c_str(), document.GetAllocator()};
json.AddMember("name", name, document.GetAllocator());
json.AddMember("parameters",
to_json(document, object.parameters),
document.GetAllocator());

return json;
}

template <>
rapidjson::Value to_json(rapidjson::Document &document, const Notify &object)
{
rapidjson::Value json;
json.SetObject();

json.AddMember("messageType",
rapidjson::StringRef("Presets::notify"),
document.GetAllocator());

json.AddMember(
"preset", to_json(document, object.preset), document.GetAllocator());

return json;
}

} // namespace shrapnel::presets
91 changes: 91 additions & 0 deletions firmware/components/presets/test/presets_json_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "presets_json_builder.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

// Disable warning inside rapidjson
// https://github.com/Tencent/rapidjson/issues/1700
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wclass-memaccess"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wswitch-enum"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#pragma GCC diagnostic pop

namespace {

using namespace shrapnel;
using namespace shrapnel::presets;

template <typename T>
std::string write_json(const T &object)
{
rapidjson::Document document;
auto result = to_json(document, object);

result.Swap(document);

rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter writer(buffer);
document.Accept(writer);

return buffer.GetString();
}

std::string normalise_json(const std::string &json)
{
rapidjson::Document document;
document.Parse(json.c_str());

rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter writer(buffer);
document.Accept(writer);

return buffer.GetString();
}

TEST(PresetsJsonBuilder, Notify)
{
Notify input{.preset{.id{1, 2, 3},
.name{"test"},
.parameters{
.amp_gain{0.001},
.amp_channel{0.002},
.bass{0.003},
.middle{0.004},
.treble{0.005},
.contour{0.006},
.volume{0.007},
.noise_gate_threshold{0.008},
.noise_gate_hysteresis{0.009},
.noise_gate_attack{0.010},
.noise_gate_hold{0.011},
.noise_gate_release{0.012},
.noise_gate_bypass{0.013},
.chorus_rate{0.014},
.chorus_depth{0.015},
.chorus_mix{0.016},
.chorus_bypass{0.017},
.wah_position{0.018},
.wah_vocal{0.019},
.wah_bypass{0.020},
}}};

rapidjson::Document document;

auto reference = normalise_json(R"({
"messageType": "Presets::notify",
"preset": {
"id": "01020300-0000-0000-0000-000000000000",
"name": "test",
"parameters": "CAEQAhgDIAQoBTAGOAdACEgJUApYC2AMaA1wDngPgAEQiAERkAESmAEToAEU"
}
})");

EXPECT_THAT(write_json(input), reference);
}

} // namespace
1 change: 1 addition & 0 deletions thirdparty/esp-idf-components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(cppcodec)
add_subdirectory(esp-dsp)
add_subdirectory(etl)
add_subdirectory(rapidjson)
1 change: 1 addition & 0 deletions thirdparty/esp-idf-components/cppcodec
Submodule cppcodec added at 8019b8

0 comments on commit 6688224

Please sign in to comment.