Skip to content

Commit

Permalink
[wip] More work on HW devices
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 26, 2024
1 parent 272e708 commit dd12a24
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/vst3/public.sdk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <ossia/network/base/protocol.hpp>
#include <ossia/network/generic/generic_device.hpp>

#include <QDebug>

#include <avnd/binding/ossia/from_value.hpp>
#include <avnd/binding/ossia/to_value.hpp>
#include <avnd/binding/ossia/uuid.hpp>
Expand All @@ -23,7 +25,6 @@
#include <libsimpleio/libdac.h>
#include <libsimpleio/libgpio.h>
#include <libsimpleio/libpwm.h>

namespace Protocols::SimpleIO
{
template <typename T>
Expand Down Expand Up @@ -119,10 +120,11 @@ class GenericHardwareDevice : public HardwareDevice
}

HardwareDevice::used_parameters setupDevice(
ossia::net::simpleio_protocol& proto, ossia::net::node_base& root,
ossia::net::simpleio_protocol& proto, ossia::net::node_base& rroot,
const QString& name, const QString& path) override
{
used_parameters p;
auto& root = ossia::net::create_node(rroot, path.toStdString());

// Create ossia parameters for all the inputs
// Create SIO devices for all the outputs
Expand Down Expand Up @@ -288,9 +290,15 @@ class GenericHardwareDeviceFactory : public HardwareDeviceFactory
{
return oscr::fromStringView(avnd::get_name<T>());
}
std::unique_ptr<HardwareDevice> make() override
HardwareDevice* make(QObject* parent) override
{
return std::make_unique<GenericHardwareDevice<T>>(nullptr);
return new GenericHardwareDevice<T>(parent);
}
HardwareDevice* load(const VisitorVariant& vis, QObject* parent) override
{
return score::deserialize_dyn(vis, [&](auto&& deserializer) {
return new GenericHardwareDevice<T>{deserializer, parent};
});
}
};
}
Expand Down Expand Up @@ -324,22 +332,15 @@ struct TSerializer<JSONObject, Protocols::SimpleIO::GenericHardwareDevice<T>>
static void writeTo(JSONObject::Deserializer& s, model_type& obj) { }
};

#include <halp/controls.hpp>
struct Motor2PWM
{
static constexpr auto name() { return "Motor (2 PWM)"; }
static constexpr auto uuid() { return "1b5a3764-5e22-11ef-b97c-5ce91ee31bcd"; }
struct
{
struct
{
static constexpr auto name() { return "Speed"; }
struct range
{
float min = -1.f, max = 1.f, init = 0.f;
};

float value;
} control;
halp::hslider_f32<"speed"> speed;
halp::toggle<"direction"> direction;
} inputs;

struct
Expand Down Expand Up @@ -373,23 +374,20 @@ struct Motor2PWM
// Hardware : put in separate bank ? As it can be used for I & O. e.g. Serial UART, etc.
void operator()()
{
if(!inputs.control.value)
{
return;
}

auto& p1 = hardware.pwm1.duty_cycle;
auto& p2 = hardware.pwm2.duty_cycle;
float v = inputs.control.value;
if(v > 0.)
float v = 1. - inputs.speed;
if(inputs.direction)
{
p1 = 1. - v;
p2 = 0.;
p1 = 1;
p2 = v;
}
else
{
p1 = 0.;
p2 = 1. - v;
p1 = 1;
p2 = v;
}

qDebug() << *p1 << *p2;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ class HardwareDeviceFactory : public score::InterfaceBase
virtual ~HardwareDeviceFactory();

virtual QString prettyName() const noexcept = 0;
virtual std::unique_ptr<HardwareDevice> make() = 0;
virtual HardwareDevice* make(QObject* parent) = 0;
virtual HardwareDevice* load(const VisitorVariant& data, QObject* parent) = 0;
};

class HardwareDeviceFactoryList final
: public score::InterfaceList<HardwareDeviceFactory>
{
public:
using object_type = HardwareDevice;
object_type* loadMissing(const VisitorVariant& vis, QObject* parent) const
{
SCORE_TODO;
return nullptr;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SimpleIODatabase : public TreeNodeBasedItemModel<SimpleIONode>
{
n.emplace_back(
SimpleIOData{
{.control = SimpleIO::Custom{factory.make()},
{.control = SimpleIO::Custom{factory.make(nullptr)},
.name = factory.prettyName(),
.path = "/foo"},
factory.prettyName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ struct Custom
}
Custom& operator=(Custom&& other) noexcept = default;

explicit Custom(HardwareDevice* other)
: device{other}
{
}
explicit Custom(std::unique_ptr<HardwareDevice> other)
: device{std::move(other)}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if defined(OSSIA_PROTOCOL_SIMPLEIO)
#include "SimpleIOSpecificSettings.hpp"

#include <score/plugins/SerializableHelpers.hpp>
#include <score/serialization/DataStreamVisitor.hpp>
#include <score/serialization/JSONVisitor.hpp>
#include <score/serialization/StdVariantSerialization.hpp>
Expand Down Expand Up @@ -181,25 +182,43 @@ void JSONWriter::write(Protocols::SimpleIO::HID& n)
template <>
void DataStreamReader::read(const Protocols::SimpleIO::Custom& n)
{
SCORE_ASSERT(n.device);
readFrom(*n.device);
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::SimpleIO::Custom& n)
{
auto& csl = components.interfaces<Protocols::SimpleIO::HardwareDeviceFactoryList>();
auto hw = deserialize_interface(csl, *this, nullptr);
if(hw)
n.device.reset(hw);
else
SCORE_ABORT;
checkDelimiter();
}

template <>
void JSONReader::read(const Protocols::SimpleIO::Custom& n)
{
stream.StartObject();
SCORE_ASSERT(n.device);
obj["Custom"] = *n.device.get();
stream.EndObject();
}

template <>
void JSONWriter::write(Protocols::SimpleIO::Custom& n)
{
auto& csl = components.interfaces<Protocols::SimpleIO::HardwareDeviceFactoryList>();

JSONObject::Deserializer hw_deser{obj["Custom"].toObject()};
auto hw = deserialize_interface(csl, hw_deser, nullptr);
if(hw)
n.device.reset(hw);
else
SCORE_ABORT;
}

template <>
Expand Down

0 comments on commit dd12a24

Please sign in to comment.