diff --git a/Makefile b/Makefile index 144d77b..9d8b1b0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ SRC_DIR := src -SRC := $(SRC_DIR)/*.c +SRC := $(wildcard $(SRC_DIR)/*.c) BUILD_DIR := build @@ -50,7 +50,7 @@ SSH_AUTH_KEYS := $(ALPINE)/authorized_keys all: $(TARGET) $(TARGET): $(SRC) $(BUILD_DIR) $(LIB) $(LIBGPIOD) - $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LIB) + $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LIB) $(LIBGPIOD) get: $(ASSETS) $(ALPINE_TAR) $(ALPINE_HEADLESS) rm -rf $(ALPINE)/* @@ -75,14 +75,19 @@ bootable: $(ALPINE) $(WPA_CONFIG) get $(ALPINE_HEADLESS): wget https://github.com/macmpi/alpine-linux-headless-bootstrap/raw/98728731299f262eab270941c522138d6c5cea73/headless.apkovl.tar.gz -O $(ALPINE_HEADLESS) -$(LIBGPIOD): +$(LIBGPIOD): $(TOOLCHAIN) + rm -f $(LIBGPIOD) git clone https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git && \ cd libgpiod && \ + export ac_cv_func_malloc_0_nonnull=yes && \ + export ac_cv_func_realloc_0_nonnull=yes && \ ./autogen.sh && \ ./configure --host=aarch64-linux-musl \ - --prefix=/home/victor/.local/toolchains/aarch64-linux-musl-cross \ + --prefix=$(TOOLCHAIN) \ --enable-tools=no \ - --enable-bindings-cxx=no && \ + --enable-bindings-cxx=no \ + --disable-shared \ + --enable-static && \ make && make install && \ cd .. && \ rm -rf libgpiod @@ -114,3 +119,5 @@ $(ASSETS): clean: rm -rf $(BUILD_DIR) $(ALPINE) $(ASSETS) make fclean -C $(LIBFT_DIR) + +.PHONY: sync diff --git a/image/raspi.apkovl.tar.gz b/image/raspi.apkovl.tar.gz index 9f000f7..f1fb956 100755 Binary files a/image/raspi.apkovl.tar.gz and b/image/raspi.apkovl.tar.gz differ diff --git a/src/main.c b/src/main.c index 0ca2bdc..3b79057 100644 --- a/src/main.c +++ b/src/main.c @@ -1,28 +1,85 @@ #include "ctools/libft/printf/ft_printf.h" #include "ctools/printf/ft_printf.h" -#include -#include -#include -#include -#include +#include +#include #include +#include +#include #define GPIO_CHIP_NAME "gpiochip0" -#define GPIO_CHIP_PATH "/dev/gpiochip0" +#define GPIO_CHIP_PATH "/dev/"GPIO_CHIP_NAME #define FAIL(code, msg, ...) \ do { \ - ft_fprintf(STDERR_FILENO, msg, ...); \ + ft_fprintf(STDERR_FILENO, msg, ##__VA_ARGS__); \ exit(code); \ } while (0); +#define DIGIT_ONE 17 +#define DIGIT_TWO 22 +#define DIGIT_THREE 26 +#define DIGIT_FOUR 24 + +#define SER 7 +#define RCLK 5 +#define SRCLK 6 + int main() { - struct gpiod_chip* chip = gpiod_chip_open(GPIO_CHIP_PATH); + struct gpiod_chip *chip; + struct gpiod_line_request *request; + struct gpiod_line_settings *settings; + struct gpiod_line_config *line_cfg; + struct gpiod_request_config *req_cfg; + uint32_t lines[] = {DIGIT_ONE, DIGIT_TWO, DIGIT_THREE, DIGIT_FOUR, SER, SRCLK, RCLK}; - FAIL(1, "Failed to load chip %s", "chippy"); + uint32_t digits[] = { DIGIT_ONE, DIGIT_TWO, DIGIT_THREE, DIGIT_FOUR }; - if (!chip) { + // Open chip + chip = gpiod_chip_open("/dev/gpiochip0"); + // Create line settings for output + settings = gpiod_line_settings_new(); + gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT); + gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_INACTIVE); + + // Create line config and add our line + line_cfg = gpiod_line_config_new(); + + gpiod_line_config_add_line_settings(line_cfg, lines, 2, settings); + + // Create request config + req_cfg = gpiod_request_config_new(); + gpiod_request_config_set_consumer(req_cfg, "my_program"); + + // Request the line + request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); + + gpiod_line_request_set_value(request, SRCLK, GPIOD_LINE_VALUE_INACTIVE); + gpiod_line_request_set_value(request, RCLK, GPIOD_LINE_VALUE_INACTIVE); + while (1) { + for (uint32_t i = 0; i < 4; i++) { + gpiod_line_request_set_value(request, digits[i], GPIOD_LINE_VALUE_ACTIVE); + for (uint32_t j = 0; j < 7; j++) { + gpiod_line_request_set_value(request, SRCLK, GPIOD_LINE_VALUE_INACTIVE); + gpiod_line_request_set_value(request, SER, GPIOD_LINE_VALUE_INACTIVE); + gpiod_line_request_set_value(request, SRCLK, GPIOD_LINE_VALUE_ACTIVE); + usleep(50); + } + usleep(100); + gpiod_line_request_set_value(request, RCLK, GPIOD_LINE_VALUE_ACTIVE); + usleep(100); + gpiod_line_request_set_value(request, RCLK, GPIOD_LINE_VALUE_INACTIVE); + gpiod_line_request_set_value(request, digits[i], GPIOD_LINE_VALUE_INACTIVE); + usleep(1); // 500ms + } } - ft_printf("Hello world\n"); + + // Cleanup + gpiod_line_request_release(request); + gpiod_line_config_free(line_cfg); + gpiod_line_settings_free(settings); + gpiod_request_config_free(req_cfg); + gpiod_chip_close(chip); + + return 0; }