Skip to content

Commit

Permalink
Fixes #3
Browse files Browse the repository at this point in the history
Moved detachInterrupt() from ~GpioPin() to close().
atomic<int>  replaced by promise/future for stopping irqThread.
  • Loading branch information
epsilonrt committed Oct 9, 2018
1 parent 101e228 commit 64c8ca7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
9 changes: 6 additions & 3 deletions examples/interrupt/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#ifdef __unix__
#include <Piduino.h> // All the magic is here ;-)
#endif
using namespace std;

// <DANGER> Be careful !!! Before launching this program :
// -> Check that the pin below is well connected to an LED ! <-
Expand All @@ -55,11 +54,15 @@ void isr() {
digitalWrite (ledPin, value);

// prints the time difference between edges and the state of the irq pin.
cout << t2 - t1 << ":\t" << value << endl;
Console.print (t2 - t1);
Console.print (":\t");
Console.println (value);
t1 = t2; // the new time becomes the first for the next irq
}

void setup() {

Console.begin (115200);
// initialize digital pin ledPin as an output.
pinMode (ledPin, OUTPUT);
// initialize digital pin irqPin as an input with pull-up (for button ?)
Expand All @@ -69,7 +72,7 @@ void setup() {
}

void loop () {

// Press Ctrl+C to abort ...
delay (-1); // nothing to do, we sleep ...
}
Expand Down
6 changes: 3 additions & 3 deletions include/piduino/gpiopin.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <vector>
#include <map>
#include <memory>
#include <atomic>
#include <future>
#include <thread>
#include <mutex>
#include <piduino/converter.h>
Expand Down Expand Up @@ -630,7 +630,7 @@ namespace Piduino {
Mode _mode;
Pull _pull;

std::atomic<int> _run; // indique au thread de continuer
std::promise<void> _stopRead;
std::thread _thread;

std::shared_ptr<Converter> _dac;
Expand All @@ -644,7 +644,7 @@ namespace Piduino {
static const std::map<std::string, Mode> _str2mode;
static std::string _syspath;

static void * irqThread (std::atomic<int> & run, int fd, Isr isr);
static void * irqThread (std::future<void> run, int fd, Isr isr);

void holdMode();
void holdPull();
Expand Down
1 change: 0 additions & 1 deletion libpiduino.project
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
<File Name="include/piduino/string.h"/>
<File Name="include/piduino/filedevice.h"/>
<File Name="include/piduino/fileno.h"/>
<File Name="include/piduino/readnotifier.h"/>
<File Name="include/piduino/threadsafebuffer.h"/>
<File Name="include/piduino/filestream.h"/>
<File Name="include/piduino/terminalnotifier.h"/>
Expand Down
20 changes: 12 additions & 8 deletions src/gpio/gpiopin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <exception>
#include <fstream>
#include <sstream>
#include <chrono>
//
#include <csignal>
#include <sys/types.h>
Expand Down Expand Up @@ -103,7 +104,7 @@ namespace Piduino {
_isopen (false), _parent (parent), _descriptor (desc), _holdMode (ModeUnknown),
_holdPull (PullUnknown), _holdState (false), _useSysFs (false),
_valueFd (-1), _firstPolling (true), _edge (EdgeUnknown), _mode (ModeUnknown),
_pull (PullUnknown), _run (false), _dac (0) {
_pull (PullUnknown), _dac (0) {

if ( (parent->gpio()->accessLayer() & AccessLayerIoMap) != AccessLayerIoMap) {

Expand Down Expand Up @@ -154,7 +155,6 @@ namespace Piduino {
// ---------------------------------------------------------------------------
Pin::~Pin() {

detachInterrupt();
close();
}

Expand Down Expand Up @@ -629,8 +629,10 @@ namespace Piduino {

sysFsRead (_valueFd);
}
_run = true;
_thread = std::thread (irqThread, std::ref (_run), _valueFd, isr);

// Fetch std::future object associated with promise
std::future<void> running = _stopRead.get_future();
_thread = std::thread (irqThread, std::move (running), _valueFd, isr);
}
}

Expand All @@ -640,7 +642,8 @@ namespace Piduino {

if (_thread.joinable()) {

_run = false;
// Set the value in promise
_stopRead.set_value();
_thread.join();
}
}
Expand Down Expand Up @@ -715,6 +718,7 @@ namespace Piduino {

if (type() == TypeGpio) {

detachInterrupt();
forceUseSysFs (false); // close & unexport
if (gpio()->releaseOnClose()) {

Expand Down Expand Up @@ -810,15 +814,15 @@ namespace Piduino {
// ---------------------------------------------------------------------------
// Thread de surveillance des entrées du port
void *
Pin::irqThread (std::atomic<int> & run, int fd, Isr isr) {
Pin::irqThread (std::future<void> run, int fd, Isr isr) {
int ret;

Scheduler::setRtPriority (50);

try {
while (run) {
while (run.wait_for (std::chrono::milliseconds (1)) == std::future_status::timeout) {

ret = sysFsPoll (fd, 100);
ret = sysFsPoll (fd, 10);
if (ret > 0) {

isr();
Expand Down

0 comments on commit 64c8ca7

Please sign in to comment.