Skip to content

Commit ceb4681

Browse files
committed
Add libadafruit-bbio with C++ wrappers for PWM/GPIO
1 parent 6c489fc commit ceb4681

File tree

18 files changed

+738
-17
lines changed

18 files changed

+738
-17
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ nosetests.xml
3232
# Annoying MacOSX files.
3333
.DS_Store
3434
._.DS_Store
35+
36+
# autotools
37+
Makefile.in
38+
aclocal.m4
39+
autom4te.cache/
40+
compile
41+
configure
42+
config.guess
43+
config.h.in
44+
config.sub
45+
depcomp
46+
install-sh
47+
ltmain.sh
48+
m4/libtool.m4
49+
m4/ltoptions.m4
50+
m4/ltsugar.m4
51+
m4/ltversion.m4
52+
m4/lt~obsolete.m4
53+
missing
54+
source/Makefile.in
55+

Makefile

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ time:
77
publish: clean
88
python setup.py sdist upload
99

10-
clean:
10+
clean: cpp-clean
1111
rm -rf Adafruit_BBIO.* build dist
1212
rm -f *.pyo
1313
rm -f *.egg
@@ -34,8 +34,56 @@ build3:
3434
install3: build3
3535
python3 setup.py install --force
3636

37+
################################################################################
38+
# c++ library
39+
#
40+
# sudo apt-get install automake
41+
# make cpp
42+
# cd build && sudo make install
43+
################################################################################
44+
configure: configure.ac
45+
rm -f configure && \
46+
autoreconf --install -I m4
47+
48+
build/Makefile: configure
49+
mkdir -p build && \
50+
cd build && \
51+
../configure
52+
53+
cpp: build/Makefile
54+
cd build && \
55+
$(MAKE)
56+
57+
cpp-install: cpp
58+
cd build && \
59+
$(MAKE) install
60+
61+
cpp-clean:
62+
rm -rf \
63+
Makefile.in \
64+
aclocal.m4 \
65+
autom4te.cache/ \
66+
compile \
67+
configure \
68+
config.guess \
69+
config.h.in \
70+
config.sub \
71+
depcomp \
72+
install-sh \
73+
ltmain.sh \
74+
m4/libtool.m4 \
75+
m4/ltoptions.m4 \
76+
m4/ltsugar.m4 \
77+
m4/ltversion.m4 \
78+
m4/lt~obsolete.m4 \
79+
missing \
80+
source/Makefile.in
81+
82+
################################################################################
83+
3784
.PHONY: all clean
3885
.PHONY: tests
3986
.PHONY: build install
4087
.PHONY: build2 install2
4188
.PHONY: build3 install3
89+
.PHONY: cpp cpp-install cpp-clean

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ACLOCAL_AMFLAGS = -I m4
2+
SUBDIRS = source

configure.ac

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
AC_INIT([Adafruit BeagleBone IO Library],
2+
[1.0.0],
3+
[support@adafruit.com],
4+
[adafruit-bbio])
5+
6+
AC_PREREQ([2.59])
7+
#AC_CONFIG_SRCDIR([./LICENSE])
8+
AC_CONFIG_MACRO_DIR([m4])
9+
AC_CONFIG_HEADERS([config.h])
10+
AC_CONFIG_FILES([Makefile source/Makefile])
11+
12+
AM_INIT_AUTOMAKE([1.9 dist-bzip2 dist-zip foreign subdir-objects])
13+
14+
AC_PROG_CC
15+
AC_PROG_CXX
16+
AC_LANG([C])
17+
AC_LANG([C++])
18+
AC_PROG_LIBTOOL
19+
20+
AC_OUTPUT

m4/.gitkeep

Whitespace-only changes.

source/Makefile.am

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
################################################################################
2+
# Copyright (c) 2017 Adafruit
3+
# Copyright (c) 2017 Nikolay Semenov
4+
################################################################################
5+
6+
lib_LTLIBRARIES =
7+
noinst_PROGRAMS =
8+
9+
################################################################################
10+
# Global flags
11+
################################################################################
12+
AM_CPPFLAGS =
13+
14+
AM_CPPFLAGS += \
15+
-DNDEBUG \
16+
-g -O2 \
17+
-fstack-protector-strong \
18+
-fPIC
19+
20+
AM_CPPFLAGS += \
21+
-Wall \
22+
-Wextra \
23+
-Werror
24+
25+
AM_CPPFLAGS += \
26+
-DNO_PYTHON
27+
28+
AM_CPPFLAGS += \
29+
-I$(srcdir)/include/
30+
31+
AM_CXXFLAGS = \
32+
-std=c++11
33+
34+
################################################################################
35+
# AdafruitBBIO library
36+
################################################################################
37+
lib_LTLIBRARIES += libadafruit-bbio.la
38+
39+
libadafruit_bbio_la_SOURCES = \
40+
c_adc.c \
41+
c_pinmux.c \
42+
c_pwm.c \
43+
c_uart.c \
44+
common.c \
45+
event_gpio.c \
46+
library/bbio.cpp \
47+
library/gpio.cpp \
48+
library/pwm.cpp
49+
50+
libadafruit_bbio_la_LDFLAGS = $(AM_LDFLAGS) -shared
51+
52+
################################################################################
53+
# Examples
54+
################################################################################
55+
noinst_PROGRAMS += examples/cpp/pwm
56+
57+
examples_cpp_pwm_SOURCES = examples/cpp/pwm.cpp
58+
examples_cpp_pwm_LDADD = libadafruit-bbio.la
59+
examples_cpp_pwm_LDFLAGS = $(AM_LDFLAGS) -lpthread -static
60+
61+
noinst_PROGRAMS += examples/cpp/gpio
62+
63+
examples_cpp_gpio_SOURCES = examples/cpp/gpio.cpp
64+
examples_cpp_gpio_LDADD = libadafruit-bbio.la
65+
examples_cpp_gpio_LDFLAGS = $(AM_LDFLAGS) -lpthread -static

source/common.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3030
SOFTWARE.
3131
*/
3232

33-
#include "Python.h"
33+
#ifndef NO_PYTHON
34+
# include "Python.h"
35+
#endif // !NO_PYTHON
36+
3437
#include <dirent.h>
38+
#include <errno.h>
39+
#include <glob.h>
40+
#include <stdio.h>
41+
#include <stdlib.h>
42+
#include <string.h>
3543
#include <syslog.h>
3644
#include <time.h>
37-
#include <string.h>
38-
#include <glob.h>
45+
3946
#include "common.h"
4047

4148
#include <linux/version.h>
@@ -165,7 +172,7 @@ pins_t table[] = {
165172
{ "DGND", "P9_44", 0, -1, -1},
166173
{ "DGND", "P9_45", 0, -1, -1},
167174
{ "DGND", "P9_46", 0, -1, -1},
168-
{ NULL, NULL, 0 }
175+
{ NULL, NULL, 0, 0, 0 }
169176
};
170177

171178
typedef struct uart_t {
@@ -182,7 +189,7 @@ uart_t uart_table[] = {
182189
{ "UART3", "/dev/ttyO3", "ADAFRUIT-UART3", "P9_42", ""},
183190
{ "UART4", "/dev/ttyO4", "ADAFRUIT-UART4", "P9_11", "P9_13"},
184191
{ "UART5", "/dev/ttyO5", "ADAFRUIT-UART5", "P8_38", "P8_37"},
185-
{ NULL, NULL, 0 }
192+
{ NULL, NULL, 0, 0, 0 }
186193
};
187194

188195
// Copied from https://github.com/jadonk/bonescript/blob/master/src/bone.js
@@ -453,7 +460,9 @@ BBIO_err load_device_tree(const char *name)
453460

454461
file = fopen(slots, "r+");
455462
if (!file) {
463+
#ifndef NO_PYTHON
456464
PyErr_SetFromErrnoWithFilename(PyExc_IOError, slots);
465+
#endif // !NO_PYTHON
457466
return BBIO_CAPE;
458467
}
459468

@@ -494,7 +503,9 @@ int device_tree_loaded(const char *name)
494503

495504
file = fopen(slots, "r+");
496505
if (!file) {
506+
#ifndef NO_PYTHON
497507
PyErr_SetFromErrnoWithFilename(PyExc_IOError, slots);
508+
#endif // !NO_PYTHON
498509
return -1;
499510
}
500511

@@ -528,7 +539,9 @@ BBIO_err unload_device_tree(const char *name)
528539
snprintf(slots, sizeof(slots), "%s/slots", ctrl_dir);
529540
file = fopen(slots, "r+");
530541
if (!file) {
542+
#ifndef NO_PYTHON
531543
PyErr_SetFromErrnoWithFilename(PyExc_IOError, slots);
544+
#endif // !NO_PYTHON
532545
return BBIO_SYSFS;
533546
}
534547

source/common.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ SOFTWARE.
3434
#include <stddef.h>
3535

3636
#include "adafruit/bbio/error.h"
37+
#ifdef __cplusplus
38+
using adafruit::bbio::BBIO_err;
39+
#endif
3740

3841
#define MODE_UNKNOWN -1
3942
#define BOARD 10
@@ -58,17 +61,17 @@ typedef struct pwm_t {
5861
const char *key; // Pin name eg P9_21
5962
} pwm_t;
6063

61-
int gpio_mode;
62-
int gpio_direction[120];
64+
extern int gpio_mode;
65+
extern int gpio_direction[120];
6366

6467
#ifdef BBBVERSION41
65-
char ctrl_dir[43];
66-
char ocp_dir[33];
68+
extern char ctrl_dir[43];
69+
extern char ocp_dir[33];
6770
#else
68-
char ctrl_dir[35];
69-
char ocp_dir[25];
71+
extern char ctrl_dir[35];
72+
extern char ocp_dir[25];
7073
#endif
71-
74+
7275
BBIO_err get_gpio_number(const char *key, unsigned int *gpio);
7376
BBIO_err get_pwm_key(const char *input, char *key);
7477
BBIO_err get_adc_ain(const char *key, int *ain);
@@ -83,7 +86,7 @@ BBIO_err get_pwm_by_key(const char *key, pwm_t **pwm);
8386
#define BBIO_LOG_OPTION LOG_CONS | LOG_PID | LOG_NDELAY
8487
void initlog(int level, const char* ident, int option);
8588

86-
int setup_error;
87-
int module_setup;
89+
extern int setup_error;
90+
extern int module_setup;
8891

8992
#endif

source/examples/cpp/gpio.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright (c) 2017 Adafruit
3+
Copyright (c) 2017 Nikolay Semenov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
#include "adafruit/bbio.h"
25+
26+
#include <chrono>
27+
#include <thread>
28+
29+
int main(int /*argc*/, char** /*argv*/)
30+
{
31+
using adafruit::bbio::lib_options;
32+
using adafruit::bbio::Gpio;
33+
34+
adafruit::bbio::init(lib_options(LOG_DEBUG, nullptr, LOG_PERROR));
35+
36+
Gpio gpio("P8_10", Gpio::Direction::Output);
37+
38+
for (int i = 0; i < 100; ++i) {
39+
gpio.set_value((i % 2 == 1) ? Gpio::Value::High : Gpio::Value::Low);
40+
std::this_thread::sleep_for(std::chrono::seconds(1));
41+
}
42+
43+
return 0;
44+
}

source/examples/cpp/pwm.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright (c) 2017 Adafruit
3+
Copyright (c) 2017 Nikolay Semenov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
#include "adafruit/bbio.h"
25+
26+
#include <chrono>
27+
#include <thread>
28+
29+
int main(int /*argc*/, char** /*argv*/)
30+
{
31+
using adafruit::bbio::Pwm;
32+
33+
adafruit::bbio::init({LOG_DEBUG, nullptr, LOG_PERROR});
34+
35+
Pwm pwm("P8_19");
36+
pwm.start(0., 1e9 / (1 * 1000 * 1000), Pwm::Polarity::Normal);
37+
38+
for (int i = 0; i < 1000; ++i) {
39+
pwm.set_duty_cycle(i / 10.0);
40+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
41+
}
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)