Git Product home page Git Product logo

zig-gpio's Introduction

zig-gpio

zig-gpio is a Zig library for controlling GPIO lines on Linux systems

This library can be used to access GPIO on devices such as Raspberry Pis or the Milk-V Duo (which is the board I created it for and tested it with).

This is my first Zig project, so I'm open to any suggestions!

There's a companion article available on my website: https://www.elara.ws/articles/milkv-duo.

Compatibility

zig-gpio uses the v2 character device API, which means it will work on any Linux system running kernel 5.10 or above. All you need to do is find out which gpiochip device controls which pin and what the offsets are, which you can do by either finding documentation online, or using the gpiodetect and gpioinfo tools from this repo or from libgpiod.

Commands

zig-gpio provides replacements for some of the libgpiod tools, such as gpiodetect and gpioinfo. You can build all of them using zig build commands or specific ones using zig build <command> (for example: zig build gpiodetect).

Try it yourself!

Here's an example of a really simple program that requests pin 22 from gpiochip2 and makes it blink at a 1 second interval. That pin offset is the LED of a Milk-V Duo board, so if you're using a different board, make sure to change it.

const std = @import("std");
const gpio = @import("gpio");

pub fn main() !void {
    var chip = try gpio.getChip("/dev/gpiochip2");
    defer chip.close();
    std.debug.print("Chip Name: {s}\n", .{chip.name});

    var line = try chip.requestLine(22, .{ .output = true });
    defer line.close();
    while (true) {
        try line.setHigh();
        std.time.sleep(std.time.ns_per_s);
        try line.setLow();
        std.time.sleep(std.time.ns_per_s);
    }
}

For more examples, see the _examples directory. You can build all the examples using the zig build examples command.

Using zig-gpio in your project

If you don't have a zig project already, you can create one by running zig init-exe in a new folder.

To add zig-gpio as a dependency, there are two steps:

  1. Add zig-gpio to your build.zig.zon file
  2. Add zig-gpio to your build.zig file

If you don't have a build.zig.zon file, create one. If you do, just add zig-gpio as a dependency. Here's what it should look like:

.{
    .name = "my_project",
    .version = "0.0.1",

    .dependencies = .{
        .gpio = .{
            .url = "https://gitea.elara.ws/Elara6331/zig-gpio/archive/v0.0.2.tar.gz",
            .hash = "1220e3af3194d1154217423d60124ae3a46537c2253dbfb8057e9b550526d2885df1",
        }
    }
}

Then, in your build.zig file, add the following before b.installArtifact(exe):

const gpio = b.dependency("gpio", .{
    .target = target,
    .optimize = optimize,
});
exe.addModule("gpio", gpio.module("gpio"));

And that's it! You should now be able to use zig-gpio via @import("gpio");

zig-gpio's People

Contributors

elara6331 avatar vesim987 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

zig-gpio's Issues

Building on dev zig

Hey, there are some compilation errors on 0.12.0-dev.2711+f995c1b08, which are introduced in ziglang/zig commit 142471fcc460:

diff --git a/build.zig b/build.zig
index c1fa71188d9d..9f57029281be 100644
--- a/build.zig
+++ b/build.zig
@@ -24,7 +24,7 @@ pub fn build(b: *std.Build) !void {
     const optimize = b.standardOptimizeOption(.{});
 
     // Add the gpio module so it can be used by the package manager
-    var gpio_module = b.createModule(.{ .source_file = .{ .path = "src/index.zig" } });
+    const gpio_module = b.createModule(.{ .root_source_file = .{ .path = "src/index.zig" } });
     try b.modules.put(b.dupe("gpio"), gpio_module);
 
     // Create a step to build all the examples
@@ -41,7 +41,7 @@ pub fn build(b: *std.Build) !void {
             .target = target,
             .optimize = optimize,
         });
-        exe.addModule("gpio", gpio_module);
+        exe.root_module.addImport("gpio", gpio_module);
 
         const build_step = b.addInstallArtifact(exe, .{});
         step.dependOn(&build_step.step);
@@ -62,7 +62,7 @@ pub fn build(b: *std.Build) !void {
             .target = target,
             .optimize = optimize,
         });
-        exe.addModule("gpio", gpio_module);
+        exe.root_module.addImport("gpio", gpio_module);
 
         const build_step = b.addInstallArtifact(exe, .{});
         step.dependOn(&build_step.step);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.