Skip to content

Commit

Permalink
Merge 49e673b into 7a72ab7
Browse files Browse the repository at this point in the history
  • Loading branch information
nomis52 committed Dec 8, 2014
2 parents 7a72ab7 + 49e673b commit 81c4952
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
64 changes: 57 additions & 7 deletions plugins/gpio/GPIODriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,32 @@

#include "ola/io/IOUtils.h"
#include "ola/Logging.h"
#include "ola/thread/Mutex.h"

namespace ola {
namespace plugin {
namespace gpio {

const char GPIODriver::GPIO_BASE_DIR[] = "/sys/class/gpio/gpio";

using std::vector;

using ola::thread::MutexLocker;
using std::string;
using std::vector;

GPIODriver::GPIODriver(const Options &options)
: m_options(options) {
: m_options(options),
m_term(false),
m_dmx_changed(false) {
}

GPIODriver::~GPIODriver() {
{
MutexLocker locker(&m_mutex);
m_term = true;
}
m_cond.Signal();
Join();

CloseGPIOFDs();
}

Expand All @@ -59,11 +68,51 @@ bool GPIODriver::Init() {
return false;
}

return true;
return Start();
}

bool GPIODriver::SendDmx(const DmxBuffer &dmx) {
return UpdateGPIOPins(dmx);
{
MutexLocker locker(&m_mutex);
// avoid the reference counting
m_buffer.Set(dmx);
m_dmx_changed = true;
}
m_cond.Signal();
return true;
}

void *GPIODriver::Run() {
Clock clock;
DmxBuffer output;

while (true) {
bool update_pins = false;

TimeStamp wake_up;
clock.CurrentTime(&wake_up);
wake_up += TimeInterval(1, 0);

// Wait for one of: i) termination ii) DMX changed iii) timeout
m_mutex.Lock();
if (!m_term && !m_dmx_changed) {
m_cond.TimedWait(&m_mutex, wake_up);
}

if (m_term) {
m_mutex.Unlock();
break;
} else if (m_dmx_changed) {
output.Set(m_buffer);
m_dmx_changed = false;
update_pins = true;
}
m_mutex.Unlock();
if (update_pins) {
UpdateGPIOPins(output);
}
}
return NULL;
}

bool GPIODriver::SetupGPIO() {
Expand All @@ -79,11 +128,13 @@ bool GPIODriver::SetupGPIO() {
std::ostringstream str;
str << GPIO_BASE_DIR << static_cast<int>(*iter) << "/value";
int pin_fd;
if (ola::io::Open(str.str(), O_RDWR, &pin_fd)) {
if (!ola::io::Open(str.str(), O_RDWR, &pin_fd)) {
failed = true;
break;
}

GPIOPin pin = {pin_fd, UNDEFINED, false};

// Set dir
str.str("");
str << GPIO_BASE_DIR << static_cast<int>(*iter) << "/direction";
Expand All @@ -99,7 +150,6 @@ bool GPIODriver::SetupGPIO() {
}
close(fd);

GPIOPin pin = {fd, UNDEFINED, false};
m_gpio_pins.push_back(pin);
}

Expand Down
11 changes: 10 additions & 1 deletion plugins/gpio/GPIODriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdint.h>
#include <ola/DmxBuffer.h>
#include <ola/base/Macro.h>
#include <ola/thread/Thread.h>

#include <vector>

Expand All @@ -34,7 +35,7 @@ namespace gpio {
/**
* @brief Uses data in a DMXBuffer to drive GPIO pins.
*/
class GPIODriver {
class GPIODriver : private ola::thread::Thread {
public:
/**
* @brief The Options.
Expand Down Expand Up @@ -94,6 +95,8 @@ class GPIODriver {
*/
bool SendDmx(const DmxBuffer &dmx);

void *Run();

private:
enum GPIOState {
ON,
Expand All @@ -112,6 +115,12 @@ class GPIODriver {
const Options m_options;
GPIOPins m_gpio_pins;

DmxBuffer m_buffer;
bool m_term; // GUARDED_BY(m_mutex);
bool m_dmx_changed; // GUARDED_BY(m_mutex);
ola::thread::Mutex m_mutex;
ola::thread::ConditionVariable m_cond;

bool SetupGPIO();
bool UpdateGPIOPins(const DmxBuffer &dmx);
void CloseGPIOFDs();
Expand Down
2 changes: 1 addition & 1 deletion plugins/gpio/GPIOPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool GPIOPlugin::StartHook() {
bool GPIOPlugin::StopHook() {
if (m_device) {
m_plugin_adaptor->UnregisterDevice(m_device);
return m_device->Stop();
m_device->Stop();
delete m_device;
m_device = NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/gpio/GPIOPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class GPIOPlugin: public ola::Plugin {
* @param plugin_adaptor the PluginAdaptor to use
*/
explicit GPIOPlugin(class ola::PluginAdaptor *plugin_adaptor)
: Plugin(plugin_adaptor) {}
: Plugin(plugin_adaptor),
m_device(NULL) {}

std::string Name() const { return PLUGIN_NAME; }
std::string Description() const;
Expand Down
1 change: 0 additions & 1 deletion plugins/opendmx/OpenDmxThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ void *OpenDmxThread::Run() {
m_term_cond.TimedWait(&m_term_mutex, wake_up);
m_term_mutex.Unlock();


ola::io::Open(m_path, O_WRONLY, &m_fd);

} else {
Expand Down

0 comments on commit 81c4952

Please sign in to comment.