Skip to content

Commit

Permalink
[simpleio] Improve pwm
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Aug 15, 2024
1 parent ba1c996 commit 46fca33
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include <wobjectimpl.h>
W_OBJECT_IMPL(Protocols::SimpleIODevice)

namespace ossia::net
{

Expand Down Expand Up @@ -112,9 +111,12 @@ struct simpleio_protocol : public ossia::net::protocol_base
auto& port = *reinterpret_cast<const sio::Port*>(ptr);
auto param
= ossia::create_parameter(root, "/pwm/" + port.name.toStdString(), "float");

param->push_value(0.5f);
// FIXME add a child parameter to set the period.
PWM_impl impl{};
int32_t error;

PWM_configure(ptr->chip, ptr->channel, 1'000'000, 500'000, ptr->polarity, &error);
PWM_open(ptr->chip, ptr->channel, &impl.fd, &error);
m_pwm.emplace(param, impl);
}
Expand Down Expand Up @@ -171,7 +173,9 @@ struct simpleio_protocol : public ossia::net::protocol_base
else if(auto it = m_pwm.find(&p); it != m_pwm.end())
{
int32_t error;
PWM_write(it->second.fd, ossia::convert<int>(v), &error);
// Input is between [0; 1], we map that to the duty cycle range [0, period]
int val = ossia::convert<float>(v) * 1'000'000;
PWM_write(it->second.fd, val, &error);
return true;
}
else if(auto it = m_gpio_out.find(&p); it != m_gpio_out.end())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ class AddPortDialog : public QDialog
, m_gpioOut{"Out"}
, m_gpioPullUp{"Pull-up"}
, m_gpioPullDown{"Pull-down"}
, m_pwmChannel{}
, m_pwmPolarity{}
{
this->setLayout(&m_layout);
m_layout.addWidget(&m_availablePorts);
Expand All @@ -356,6 +358,7 @@ class AddPortDialog : public QDialog

updateParameters(newFixt);
};
m_pwmChannel.setRange(0, 32);

m_setupLayoutContainer.addLayout(&m_setupLayout);
m_setupLayout.addRow(tr("Name"), &m_name);
Expand All @@ -366,8 +369,10 @@ class AddPortDialog : public QDialog

m_custom.addWidget(&m_defaultWidget);
m_custom.addWidget(&m_gpioWidget);
m_custom.addWidget(&m_pwmWidget);

m_gpioWidget.setLayout(&m_gpioLayout);
m_pwmWidget.setLayout(&m_pwmLayout);

m_gpioInOutLayout.addWidget(&m_gpioIn);
m_gpioInOutLayout.addWidget(&m_gpioOut);
Expand All @@ -377,6 +382,9 @@ class AddPortDialog : public QDialog
m_gpioPullLayout.addWidget(&m_gpioPullDown);
m_gpioLayout.addRow(&m_gpioPullLayout);

m_pwmLayout.addRow("Channel", &m_pwmChannel);
m_pwmLayout.addRow("Polarity", &m_pwmPolarity);

m_custom.setCurrentIndex(0);

connect(&m_buttons, &QDialogButtonBox::accepted, this, &AddPortDialog::accept);
Expand Down Expand Up @@ -420,6 +428,10 @@ class AddPortDialog : public QDialog
m_gpioIn.setChecked(true);
m_gpioPullUp.setChecked(true);
break;
case 1: // PWM
m_custom.setCurrentIndex(2);
m_pwmChannel.setValue(0);
break;
default:
m_custom.setCurrentIndex(0);
break;
Expand Down Expand Up @@ -462,7 +474,12 @@ class AddPortDialog : public QDialog
}
}

void operator()(SimpleIO::PWM& gpio) const noexcept { }
void operator()(SimpleIO::PWM& pwm) const noexcept
{
pwm.channel = self.m_pwmChannel.value();
pwm.polarity = self.m_pwmPolarity.isChecked();
}

void operator()(SimpleIO::ADC& gpio) const noexcept { }
void operator()(SimpleIO::DAC& gpio) const noexcept { }
void operator()(SimpleIO::HID& gpio) const noexcept { }
Expand All @@ -488,12 +505,17 @@ class AddPortDialog : public QDialog

QWidget m_defaultWidget;
QWidget m_gpioWidget;
QWidget m_pwmWidget;
QFormLayout m_gpioLayout;
QHBoxLayout m_gpioInOutLayout;
QHBoxLayout m_gpioPullLayout;
QRadioButton m_gpioIn, m_gpioOut;
QCheckBox m_gpioPullUp, m_gpioPullDown;

QFormLayout m_pwmLayout;
QSpinBox m_pwmChannel;
QCheckBox m_pwmPolarity;

const SimpleIOData* m_currentNode{};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct PWM
{
int32_t chip{};
int32_t channel{};
int32_t polarity{};
};
struct ADC
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ JSON_METADATA(Protocols::SimpleIO::Custom, "Custom")
template <>
void DataStreamReader::read(const Protocols::SimpleIO::GPIO& n)
{
m_stream << n.chip << n.direction << n.events << n.flags << n.line << n.state;
insertDelimiter();
}

template <>
void DataStreamWriter::write(Protocols::SimpleIO::GPIO& n)
{
m_stream >> n.chip >> n.direction >> n.events >> n.flags >> n.line >> n.state;
checkDelimiter();
}

Expand Down Expand Up @@ -103,25 +105,35 @@ void JSONWriter::write(Protocols::SimpleIO::DAC& n)
template <>
void DataStreamReader::read(const Protocols::SimpleIO::PWM& n)
{
m_stream << n.chip << n.channel << n.polarity;
insertDelimiter();
}

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

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

template <>
void JSONWriter::write(Protocols::SimpleIO::PWM& n)
{
if(!obj.tryGet("Chip"))
return;
n.chip = obj["Chip"].toInt();
n.channel = obj["Channel"].toInt();
n.polarity = obj["Polarity"].toInt();
}

template <>
Expand Down

0 comments on commit 46fca33

Please sign in to comment.