Skip to content

Commit

Permalink
Merge pull request #50 from NoHomey/close-opened-fd
Browse files Browse the repository at this point in the history
Fixes #43 How to release a wiringPiI2CSetup() file descriptor
  • Loading branch information
nekuz0r authored Sep 29, 2016
2 parents 6931958 + 4e19523 commit bf14d46
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/wiringPiI2C.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "wiringPiI2C.h"
#include <wiringPiI2C.h>
#include <unistd.h>

DECLARE(wiringPiI2CRead);
DECLARE(wiringPiI2CReadReg8);
Expand All @@ -9,6 +10,7 @@ DECLARE(wiringPiI2CWriteReg8);
DECLARE(wiringPiI2CWriteReg16);
DECLARE(wiringPiI2CSetupInterface);
DECLARE(wiringPiI2CSetup);
DECLARE(wiringPiI2CClose);

// Func : int wiringPiI2CRead (int fd);
// Simple device read. Some devices present data when you read them without having to do any register transactions.
Expand Down Expand Up @@ -188,6 +190,26 @@ IMPLEMENT(wiringPiI2CSetup) {
SCOPE_CLOSE(INT32(res));
}

// Func : void wiringPiI2CClose(const int fd)
// Description : This closes opened I2C file descriptor
// fd is file descriptor returned either from wiringPiI2CSetup or wiringPiI2CSetupInterface

IMPLEMENT(wiringPiI2CClose) {
SCOPE_OPEN();

SET_ARGUMENT_NAME(0, fd);

CHECK_ARGUMENTS_LENGTH_EQUAL(1);

CHECK_ARGUMENT_TYPE_INT32(0);

int fd = GET_ARGUMENT_AS_INT32(0);

::close(fd);

SCOPE_CLOSE(UNDEFINED());
}

IMPLEMENT_EXPORT_INIT(wiringPiI2C) {
EXPORT_FUNCTION(wiringPiI2CRead);
EXPORT_FUNCTION(wiringPiI2CReadReg8);
Expand All @@ -197,4 +219,5 @@ IMPLEMENT_EXPORT_INIT(wiringPiI2C) {
EXPORT_FUNCTION(wiringPiI2CWriteReg16);
EXPORT_FUNCTION(wiringPiI2CSetupInterface);
EXPORT_FUNCTION(wiringPiI2CSetup);
}
EXPORT_FUNCTION(wiringPiI2CClose);
}
23 changes: 23 additions & 0 deletions src/wiringPiSPI.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "wiringPiSPI.h"
#include <wiringPiSPI.h>
#include <unistd.h>

DECLARE(wiringPiSPIGetFd);
DECLARE(wiringPiSPIDataRW);
DECLARE(wiringPiSPISetup);
DECLARE(wiringPiSPISetupMode);
DECLARE(wiringPiSPIClose);

// Func : int wiringPiSPIGetFd(int channel)

Expand Down Expand Up @@ -98,9 +100,30 @@ IMPLEMENT(wiringPiSPISetupMode) {
SCOPE_CLOSE(INT32(res));
}

// Func : void wiringPiSPIClose(const int fd)
// Description : This closes opened SPI file descriptor
// fd is file descriptor returned either from wiringPiSPISetup or wiringPiSPISetupMode

IMPLEMENT(wiringPiSPIClose) {
SCOPE_OPEN();

SET_ARGUMENT_NAME(0, fd);

CHECK_ARGUMENTS_LENGTH_EQUAL(1);

CHECK_ARGUMENT_TYPE_INT32(0);

int fd = GET_ARGUMENT_AS_INT32(0);

::close(fd);

SCOPE_CLOSE(UNDEFINED());
}

IMPLEMENT_EXPORT_INIT(wiringPiSPI) {
EXPORT_FUNCTION(wiringPiSPIGetFd);
EXPORT_FUNCTION(wiringPiSPIDataRW);
EXPORT_FUNCTION(wiringPiSPISetup);
EXPORT_FUNCTION(wiringPiSPISetupMode);
EXPORT_FUNCTION(wiringPiSPIClose);
}

0 comments on commit bf14d46

Please sign in to comment.