Skip to content

Commit

Permalink
simpleio: add a timer to regularly poll inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Feb 18, 2025
1 parent 0a22dc5 commit fa1ac55
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include <ossia/detail/config.hpp>

#include <ossia/detail/timer.hpp>

#if defined(OSSIA_PROTOCOL_SIMPLEIO)
#include "SimpleIODevice.hpp"
#include "SimpleIOSpecificSettings.hpp"
Expand All @@ -14,6 +16,7 @@
#include <ossia/detail/variant.hpp>
#include <ossia/network/base/protocol.hpp>
#include <ossia/network/common/complex_type.hpp>
#include <ossia/network/context.hpp>
#include <ossia/network/generic/generic_device.hpp>

#include <QDebug>
Expand All @@ -32,17 +35,19 @@ namespace ossia::net

struct simpleio_protocol : public ossia::net::protocol_base
{

// protocol_base interface
public:
simpleio_protocol(ossia::net::network_context_ptr ctx)
: protocol_base{flags{}}
, m_context{ctx}
, m_timer{ctx->context}
{
m_timer.set_delay(std::chrono::milliseconds{4});
}

~simpleio_protocol()
{
stop_processing();

int error;
for(auto& adc : m_adc)
ADC_close(adc.second.fd, &error);
Expand All @@ -55,6 +60,9 @@ struct simpleio_protocol : public ossia::net::protocol_base
for(auto& gpio : m_gpio_out)
GPIO_close(gpio.second.fd, &error);
}

void stop_processing() { m_timer.stop(); }

void set_device(ossia::net::device_base& dev) override { m_device = &dev; }
void init(const Protocols::SimpleIOSpecificSettings& conf)
{
Expand Down Expand Up @@ -147,6 +155,27 @@ struct simpleio_protocol : public ossia::net::protocol_base
else
m_gpio_in.emplace(param, impl);
}

if(!m_gpio_in.empty() || !m_adc.empty())
{
m_timer.start([this] { this->update_function(); });
}
}

void update_function()
{
for(auto& [param, obj] : m_adc)
{
int32_t sample, error;
ADC_read(obj.fd, &sample, &error);
param->set_value(sample);
}
for(auto& [param, obj] : m_gpio_in)
{
int32_t sample, error;
GPIO_line_read(obj.fd, &sample, &error);
param->set_value(bool(sample));
}
}

bool pull(parameter_base& v) override
Expand All @@ -163,7 +192,7 @@ struct simpleio_protocol : public ossia::net::protocol_base
{
int32_t sample, error;
GPIO_line_read(it->second.fd, &sample, &error);
v.set_value(sample);
v.set_value(bool(sample));
return true;
}
return false;
Expand Down Expand Up @@ -198,6 +227,7 @@ struct simpleio_protocol : public ossia::net::protocol_base
bool update(node_base& node_base) override { return false; }

ossia::net::network_context_ptr m_context;
ossia::timer m_timer;
ossia::net::device_base* m_device{};

struct ADC_impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,49 +59,67 @@ void JSONWriter::write(Protocols::SimpleIO::GPIO& n)
template <>
void DataStreamReader::read(const Protocols::SimpleIO::ADC& n)
{
m_stream << n.chip << n.channel;
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::SimpleIO::ADC& n)
{
m_stream >> n.chip >> n.channel;
checkDelimiter();
}

template <>
void JSONReader::read(const Protocols::SimpleIO::ADC& n)
{
stream.StartObject();
obj["Chip"] = n.chip;
obj["Channel"] = n.channel;
stream.EndObject();
}

template <>
void JSONWriter::write(Protocols::SimpleIO::ADC& n)
{
if(!obj.tryGet("Chip"))
return;

n.chip = obj["Chip"].toInt();
n.channel = obj["Channel"].toInt();
}

template <>
void DataStreamReader::read(const Protocols::SimpleIO::DAC& n)
{
m_stream << n.chip << n.channel;
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::SimpleIO::DAC& n)
{
m_stream >> n.chip >> n.channel;
checkDelimiter();
}

template <>
void JSONReader::read(const Protocols::SimpleIO::DAC& n)
{
stream.StartObject();
obj["Chip"] = n.chip;
obj["Channel"] = n.channel;
stream.EndObject();
}

template <>
void JSONWriter::write(Protocols::SimpleIO::DAC& n)
{
if(!obj.tryGet("Chip"))
return;

n.chip = obj["Chip"].toInt();
n.channel = obj["Channel"].toInt();
}

template <>
Expand Down Expand Up @@ -189,12 +207,14 @@ void JSONWriter::write(Protocols::SimpleIO::Custom& n)
template <>
void DataStreamReader::read(const Protocols::SimpleIO::Port& n)
{
m_stream << n.name << n.path << n.control;
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::SimpleIO::Port& n)
{
m_stream >> n.name >> n.path >> n.control;
checkDelimiter();
}

Expand Down

0 comments on commit fa1ac55

Please sign in to comment.