Skip to content

Commit 64c8ca7

Browse files
committed
Fixes #3
Moved detachInterrupt() from ~GpioPin() to close(). atomic<int> replaced by promise/future for stopping irqThread.
1 parent 101e228 commit 64c8ca7

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

examples/interrupt/interrupt.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#ifdef __unix__
3232
#include <Piduino.h> // All the magic is here ;-)
3333
#endif
34-
using namespace std;
3534

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

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

6263
void setup() {
64+
65+
Console.begin (115200);
6366
// initialize digital pin ledPin as an output.
6467
pinMode (ledPin, OUTPUT);
6568
// initialize digital pin irqPin as an input with pull-up (for button ?)
@@ -69,7 +72,7 @@ void setup() {
6972
}
7073

7174
void loop () {
72-
75+
7376
// Press Ctrl+C to abort ...
7477
delay (-1); // nothing to do, we sleep ...
7578
}

include/piduino/gpiopin.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <vector>
2323
#include <map>
2424
#include <memory>
25-
#include <atomic>
25+
#include <future>
2626
#include <thread>
2727
#include <mutex>
2828
#include <piduino/converter.h>
@@ -630,7 +630,7 @@ namespace Piduino {
630630
Mode _mode;
631631
Pull _pull;
632632

633-
std::atomic<int> _run; // indique au thread de continuer
633+
std::promise<void> _stopRead;
634634
std::thread _thread;
635635

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

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

649649
void holdMode();
650650
void holdPull();

libpiduino.project

-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@
204204
<File Name="include/piduino/string.h"/>
205205
<File Name="include/piduino/filedevice.h"/>
206206
<File Name="include/piduino/fileno.h"/>
207-
<File Name="include/piduino/readnotifier.h"/>
208207
<File Name="include/piduino/threadsafebuffer.h"/>
209208
<File Name="include/piduino/filestream.h"/>
210209
<File Name="include/piduino/terminalnotifier.h"/>

src/gpio/gpiopin.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <exception>
2323
#include <fstream>
2424
#include <sstream>
25+
#include <chrono>
2526
//
2627
#include <csignal>
2728
#include <sys/types.h>
@@ -103,7 +104,7 @@ namespace Piduino {
103104
_isopen (false), _parent (parent), _descriptor (desc), _holdMode (ModeUnknown),
104105
_holdPull (PullUnknown), _holdState (false), _useSysFs (false),
105106
_valueFd (-1), _firstPolling (true), _edge (EdgeUnknown), _mode (ModeUnknown),
106-
_pull (PullUnknown), _run (false), _dac (0) {
107+
_pull (PullUnknown), _dac (0) {
107108

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

@@ -154,7 +155,6 @@ namespace Piduino {
154155
// ---------------------------------------------------------------------------
155156
Pin::~Pin() {
156157

157-
detachInterrupt();
158158
close();
159159
}
160160

@@ -629,8 +629,10 @@ namespace Piduino {
629629

630630
sysFsRead (_valueFd);
631631
}
632-
_run = true;
633-
_thread = std::thread (irqThread, std::ref (_run), _valueFd, isr);
632+
633+
// Fetch std::future object associated with promise
634+
std::future<void> running = _stopRead.get_future();
635+
_thread = std::thread (irqThread, std::move (running), _valueFd, isr);
634636
}
635637
}
636638

@@ -640,7 +642,8 @@ namespace Piduino {
640642

641643
if (_thread.joinable()) {
642644

643-
_run = false;
645+
// Set the value in promise
646+
_stopRead.set_value();
644647
_thread.join();
645648
}
646649
}
@@ -715,6 +718,7 @@ namespace Piduino {
715718

716719
if (type() == TypeGpio) {
717720

721+
detachInterrupt();
718722
forceUseSysFs (false); // close & unexport
719723
if (gpio()->releaseOnClose()) {
720724

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

816820
Scheduler::setRtPriority (50);
817821

818822
try {
819-
while (run) {
823+
while (run.wait_for (std::chrono::milliseconds (1)) == std::future_status::timeout) {
820824

821-
ret = sysFsPoll (fd, 100);
825+
ret = sysFsPoll (fd, 10);
822826
if (ret > 0) {
823827

824828
isr();

0 commit comments

Comments
 (0)