libioctrl
Bring together the GPIO, ADC and PWM libraries you have developed into one reusable C library. Build static and shared versions, install them system-wide, and migrate your existing applications to use libioctrl.
Practical Checklist
1. Consolidate the source code
Rather than maintaining three independent libraries, we will centralise their implementation files in src/, their public headers in include/, and provide a single umbrella header, libioctrl.h.
-
Create the new project directory and its
srcandincludesubdirectories.$ mkdir -p ~/libioctrl/{src, include} -
Copy the existing ADC source and header into their respective directories.
$ cp bbb_adc/bbb_adc.c ~/libioctrl/src && cp bbb_adc/bbb_adc.h ~/libioctrl/includeRepeat the same process for
bbb_gpio.{c,h}andbbb_pwm.{c,h}. -
Inspect the resulting project structure.
$ ls -l ~/libioctrl/* libioctrl/include: total 12 -rw-r--r-- 1 debian debian 685 Apr 14 19:25 bbb_adc.h -rw-r--r-- 1 debian debian 2557 Apr 14 20:35 bbb_gpio.h -rw-r--r-- 1 debian debian 1636 Apr 14 20:35 bbb_pwm.h libioctrl/src: total 12 -rw-r--r-- 1 debian debian 1288 Apr 14 19:24 bbb_adc.c -rw-r--r-- 1 debian debian 3559 Apr 14 19:46 bbb_gpio.c -rw-r--r-- 1 debian debian 2490 Apr 14 19:24 bbb_pwm.c
-
Create
include/libioctrl.hand reproduce the following umbrella header.#ifndef LIBIOCTRL_H #define LIBIOCTRL_H #include "bbb_gpio.h" #include "bbb_pwm.h" #include "bbb_adc.h" #endif // LIBIOCTRL_H -
Update the header inclusion in each of the three implementation files. The supplied example for
src/bbb_adc.cis:# include "../include/bbb_adc.h"Repeat the same pattern for the other
*.cfiles.
2. Build and install with Make
The Makefile compiles each C source into an object file, combines those objects into static and shared libraries, and installs the resulting public interface.
-
Before installing
libioctrl, uninstall and clean the three previously installed libraries using their existing Makefiles.Repeat the original commands in each old library directory:
make uninstall make clean
-
Create a
Makefilein~/libioctrland reproduce the supplied build rules.CC = gcc CFLAGS = -Wall -Werror -fPIC -I./include # Include headers AR = ar ARFLAGS = rcs PREFIX = /usr INCLUDEDIR = $(PREFIX)/include LIBDIR = $(PREFIX)/lib # Directories SRCDIR = ./src OBJDIR = ./obj # Library names TARGET_STATIC = libioctrl.a TARGET_SHARED = libioctrl.so # Source files for each component SRC = $(SRCDIR)/bbb_gpio.c $(SRCDIR)/bbb_pwm.c $(SRCDIR)/bbb_adc.c OBJ = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRC)) # Default target: Build both libraries all: $(TARGET_STATIC) $(TARGET_SHARED) # Compile source files into object files $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir -p $(OBJDIR) $(CC) $(CFLAGS) -c $< -o $@ # Create the static library $(TARGET_STATIC): $(OBJ) $(AR) $(ARFLAGS) $@ $(OBJ) # Create the shared library $(TARGET_SHARED): $(OBJ) $(CC) -shared -o $@ $(OBJ) # Clean up build artifacts clean: rm -rf $(OBJDIR) $(TARGET_STATIC) $(TARGET_SHARED) # Install libraries and headers install: $(TARGET_STATIC) $(TARGET_SHARED) sudo cp include/bbb_gpio.h include/bbb_pwm.h include/bbb_adc.h include/bbb_libioctrl.h $(INCLUDEDIR)/ sudo cp $(TARGET_STATIC) $(TARGET_SHARED) $(LIBDIR)/ sudo ldconfig # Uninstall libraries and headers uninstall: sudo rm -f $(INCLUDEDIR)/bbb_gpio.h $(INCLUDEDIR)/bbb_pwm.h $(INCLUDEDIR)/bbb_adc.h $(INCLUDEDIR)/libioctrl.h sudo rm -f $(LIBDIR)/$(TARGET_STATIC) $(LIBDIR)/$(TARGET_SHARED) $(OBJDIR) sudo ldconfig # Phony targets .PHONY: all clean install uninstall
-
Run the original installation command from the
libioctrldirectory.make install
-
Inspect what
make installis doing at each stage.
3. Use the unified library
Once installed, applications can use the combined public header and link against libioctrl rather than the individual libraries.
-
Choose one of your earlier C examples:
adc2pwm.c,adc_continous_read,adc_read,blinkorpwm_test.For example, update the header include in
adc_read.c:#include <libioctrl.h> # originally bbb_adc.h #include <stdio.h> #include <unistd.h> ... -
Change the corresponding
Makefilelibrary link option.... # Library to link against LIBS = -lioctrl # originally, lbbb_adc ...Repeat for other applications as appropriate, and remember to rebuild each executable with
make cleanfollowed bymake. -
Compile and run your updated example. Confirm its behaviour is unchanged now that it links against the unified library.
4. Alternative: Fork and clone libioctrl
The repository provides an alternative copy of the library and completed C examples, which can be transferred to the BeagleBone over the USB network connection.
-
Fork the repository from GitHub on your computer:
-
Clone your fork on the computer:
git clone git@github.com:YOURUSERNAME/bb.black_pin_library -
From the parent directory containing the clone, transfer it to the BeagleBone with the supplied
scpcommand:scp -r /path/to/bb.black_pin_library/ debian@192.168.7.2:libioctrl
-
In the transferred repository, run its installation script:
bash install.sh
-
Explore the supplied
examples_Cdirectory and compare the implementations with your own programmes.