-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] Raw I/O support with libsimpleio
- Loading branch information
Showing
43 changed files
with
7,577 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN) | ||
add_library(simpleio STATIC | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/cplusplus.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/errmsg.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/errmsg.inc" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libadc.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libadc.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libdac.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libdac.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libevent.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libevent.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libgpio.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libgpio.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libhidraw.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libhidraw.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libi2c.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libi2c.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libipv4.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libipv4.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/liblinux.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/liblinux.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/liblinx.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/liblinx.h" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libpwm.c" | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libpwm.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libserial.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libserial.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libspi.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libspi.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libstream.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libstream.h" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libwatchdog.c" | ||
# "${CMAKE_CURRENT_LIST_DIR}/libsimpleio/libsimpleio/libwatchdog.h" | ||
) | ||
|
||
target_include_directories( | ||
simpleio | ||
PUBLIC | ||
"${CMAKE_CURRENT_LIST_DIR}/libsimpleio/" | ||
) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// C++ linkage goop: Define _BEGIN_STD_C and _END_STD_C, if necessary | ||
|
||
// Copyright (C)2013-2023, Philip Munts dba Munts Technologies. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#ifndef CPLUSPLUS_H | ||
#define CPLUSPLUS_H | ||
|
||
#if !(defined(_BEGIN_STD_C) && defined(_END_STD_C)) | ||
#ifdef __cplusplus | ||
#ifdef _HAVE_STD_CXX | ||
#define _BEGIN_STD_C namespace std { extern "C" { | ||
#define _END_STD_C } } | ||
#else | ||
#define _BEGIN_STD_C extern "C" { | ||
#define _END_STD_C } | ||
#endif | ||
#else | ||
#define _BEGIN_STD_C | ||
#define _END_STD_C | ||
#endif | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Define macros for display error messages | ||
|
||
// Copyright (C)2016-2023, Philip Munts dba Munts Technologies. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <syslog.h> | ||
|
||
void PrintErrorMessage(const char *func, const char *file, int line, const char *msg, int err) | ||
{ | ||
char *slevel = getenv("DEBUGLEVEL"); | ||
if (slevel == NULL) return; | ||
|
||
int ilevel = atoi(slevel); | ||
|
||
switch (ilevel) | ||
{ | ||
case 1 : | ||
fprintf(stderr, "ERROR in %s(), at %s line %d: %s, %s\n", func, file, line, msg, strerror(err)); | ||
break; | ||
|
||
case 2 : | ||
syslog(LOG_ERR, "ERROR in %s(), at %s line %d: %s, %s\n", func, file, line, msg, strerror(err)); | ||
break; | ||
|
||
case 3 : | ||
fprintf(stderr, "ERROR in %s(), at %s line %d: %s, %s\n", func, file, line, msg, strerror(err)); | ||
syslog(LOG_ERR, "ERROR in %s(), at %s line %d: %s, %s\n", func, file, line, msg, strerror(err)); | ||
break; | ||
|
||
default : | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Define macros for display error messages | ||
|
||
// Copyright (C)2016-2023, Philip Munts dba Munts Technologies. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#include <string.h> | ||
|
||
void PrintErrorMessage(const char *func, const char *file, int line, const char *msg, int err); | ||
|
||
#define ERRORMSG(msg, err, line) PrintErrorMessage(__func__, __FILE__, line, msg, err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* Industrial I/O A/D Converter services for Linux */ | ||
|
||
// Copyright (C)2017-2023, Philip Munts dba Munts Technologies. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <sys/param.h> | ||
|
||
#include "errmsg.inc" | ||
#include "libadc.h" | ||
|
||
#define NAME_FILE "/sys/bus/iio/devices/iio:device%d/name" | ||
#define DATA_FILE "/sys/bus/iio/devices/iio:device%d/in_voltage%d_raw" | ||
|
||
void ADC_get_name(int32_t chip, char *name, int32_t namesize, int32_t *error) | ||
{ | ||
assert(error != NULL); | ||
|
||
char filename[MAXPATHLEN]; | ||
int fd; | ||
ssize_t len; | ||
|
||
// Validate parameters | ||
|
||
if (chip < 0) | ||
{ | ||
*error = EINVAL; | ||
ERRORMSG("chip argument is invalid", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
if (name == NULL) | ||
{ | ||
*error = EINVAL; | ||
ERRORMSG("name argument is NULL", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
if (namesize < 16) | ||
{ | ||
*error = EINVAL; | ||
ERRORMSG("namesize argument is too small", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
memset(filename, 0, sizeof(filename)); | ||
snprintf(filename, sizeof(filename), NAME_FILE, chip); | ||
|
||
fd = open(filename, O_RDONLY); | ||
if (fd < 0) | ||
{ | ||
*error = errno; | ||
ERRORMSG("open() failed", *error, __LINE__ - 4); | ||
return; | ||
} | ||
|
||
memset(name, 0, namesize); | ||
len = read(fd, name, namesize - 1); | ||
|
||
if (len >= 0) | ||
*error = 0; | ||
else | ||
{ | ||
*error = errno; | ||
ERRORMSG("read() failed", *error, __LINE__ - 7); | ||
} | ||
|
||
while ((len > 0) && isspace(name[len-1])) | ||
name[--len] = 0; | ||
|
||
close(fd); | ||
} | ||
|
||
void ADC_open(int32_t chip, int32_t channel, int32_t *fd, int32_t *error) | ||
{ | ||
assert(error != NULL); | ||
|
||
// Validate parameters | ||
|
||
if (fd == NULL) | ||
{ | ||
*error = EINVAL; | ||
ERRORMSG("fd argument is NULL", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
if (chip < 0) | ||
{ | ||
*fd = -1; | ||
*error = EINVAL; | ||
ERRORMSG("chip argument is invalid", *error, __LINE__ - 4); | ||
return; | ||
} | ||
|
||
// Validate parameters | ||
|
||
if (channel < 0) | ||
{ | ||
*fd = -1; | ||
*error = EINVAL; | ||
ERRORMSG("channel argument is invalid", *error, __LINE__ - 4); | ||
return; | ||
} | ||
|
||
char filename[MAXPATHLEN]; | ||
snprintf(filename, sizeof(filename), DATA_FILE, chip, channel); | ||
|
||
*fd = open(filename, O_RDONLY); | ||
if (*fd < 0) | ||
{ | ||
*fd = -1; | ||
*error = errno; | ||
ERRORMSG("open() failed", *error, __LINE__ - 5); | ||
return; | ||
} | ||
|
||
*error = 0; | ||
} | ||
|
||
void ADC_read(int32_t fd, int32_t *sample, int32_t *error) | ||
{ | ||
assert(error != NULL); | ||
|
||
char buf[32]; | ||
ssize_t len; | ||
|
||
// Validate parameters | ||
|
||
if (fd < 0) | ||
{ | ||
*sample = 0; | ||
*error = EINVAL; | ||
ERRORMSG("fd argument is invalid", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
if (sample == NULL) | ||
{ | ||
*error = EINVAL; | ||
ERRORMSG("sample argument is NULL", *error, __LINE__ - 3); | ||
return; | ||
} | ||
|
||
// Rewind the raw data file | ||
|
||
if (lseek(fd, SEEK_SET, 0) < 0) | ||
{ | ||
*sample = 0; | ||
*error = errno; | ||
ERRORMSG("lseek() failed", *error, __LINE__ - 4); | ||
return; | ||
} | ||
|
||
// Read the raw data file | ||
|
||
len = read(fd, buf, sizeof(buf) - 1); | ||
|
||
if (len < 0) | ||
{ | ||
*sample = 0; | ||
*error = errno; | ||
ERRORMSG("read() failed", *error, __LINE__ - 4); | ||
return; | ||
} | ||
|
||
buf[len] = 0; | ||
*sample = atoi(buf); | ||
*error = 0; | ||
} |
Oops, something went wrong.