1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Pi 3 でlibgpiodライブラリ(v2)でLチカ

1
Posted at

プロローグ

ネットで圧倒的に見るのはWiringPiなんだけれどすでに非推奨。
公式が推奨しているのはlibgpiodらしいけど、ブームが落ち着いたせいで情報が少ない。
さらにv1とv2があって互換性もないらしい。

libgpiodライブラリのインストール

PI側

ドキュメントは入れない。

# インストール
$ sudo apt install libgpiod-dev

$ sudo apt info libgpiod-dev
Package: libgpiod-dev
Version: 2.2.1-2+rpi1+deb13u1

WSL2側

VS Codeでエラー表示されないようにヘッダファイルを入れる。

# インストール
$ sudo apt install libgpiod-dev

$ sudo apt info libgpiod-dev
Package: libgpiod-dev
Version: 1.6.3-1.1build1

v1のライブラリが入った。
v1に存在しない定義を指定するとエラーの赤波線が表示される。

VSCode_error.png

Pi上のヘッダファイルをWSL2にコピー

$ mkdir -p ~/gpiod_headers
$ scp pi@raspberrypi.local:/usr/include/gpiod.h ~/gpiod_headers/

c_cpp_properties.jsonを編集

コマンドパレット(Ctrl+Shift+P) → C/C++: 構成の編集(JSON)
includePathに作成したヘッダファイルのディレクトリを追加

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${env:HOME}/gpiod_headers"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++98",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

赤波線は消えたしv2の関数の補完もされている。

VSCode_normal.png

Lチカソース作成

blink.c

#include <stdio.h>
#include <unistd.h>
#include <gpiod.h>

#define CHIP_PATH   "/dev/gpiochip0"
#define LINE_NUM    17
#define CONSUMER    "blink"
#define BLINK_COUNT 10

int main(void) {
    struct gpiod_chip *chip;
    struct gpiod_line_config *line_cfg;
    struct gpiod_line_settings *line_settings;
    struct gpiod_request_config *req_cfg;
    struct gpiod_line_request *request;
    unsigned int offsets[] = { LINE_NUM };
    int ret;

    // GPIOチップのオープン
    chip = gpiod_chip_open(CHIP_PATH);
    if (!chip) {
        perror("chip open failed.");
        return 1;
    }

    // GPIOの入出力設定
    line_settings = gpiod_line_settings_new();
    gpiod_line_settings_set_direction(line_settings, GPIOD_LINE_DIRECTION_OUTPUT);
    gpiod_line_settings_set_output_value(line_settings, GPIOD_LINE_VALUE_INACTIVE);

    // 入出力設定をGPIO番号に適用
    line_cfg = gpiod_line_config_new();
    ret = gpiod_line_config_add_line_settings(line_cfg, offsets, 1, line_settings);
    if (ret < 0) {
        perror("add line settings failed");
        goto cleanup;
    }

    // 使用者を設定
    req_cfg = gpiod_request_config_new();
    gpiod_request_config_set_consumer(req_cfg, CONSUMER);

    // GPIO番号の使用権をリクエスト
    request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
    if (!request) {
        perror("request lines failed");
        goto cleanup;
    }

    // Lチカ
    for (int i = 0; i < BLINK_COUNT; i++) {
        printf("%d times...\n", i+1);
        // 点灯 (HIGHE)
        gpiod_line_request_set_value(request, LINE_NUM, GPIOD_LINE_VALUE_ACTIVE);
        sleep(1);

        // 消灯 (LOW)
        gpiod_line_request_set_value(request, LINE_NUM, GPIOD_LINE_VALUE_INACTIVE);
        sleep(1);
    }

    // リソースの解放
    gpiod_line_request_release(request);

cleanup:
    gpiod_request_config_free(req_cfg);
    gpiod_line_config_free(line_cfg);
    gpiod_line_settings_free(line_settings);
    gpiod_chip_close(chip);
    
    return 0;
}

Lチカ実行

VS Codeのターミナルで

# WSL2→pi3へscp
$ scp ./blink.c pi@raspberrypi.local:/home/pi/

# pi3へssh
$ ssh pi@raspberrypi.local

# コンパイル
$ gcc blink.c  -o blink -lgpiod

# 実行
$ ./blink

静止画像なので分からないけどチカチカしている。
blink.png

エピローグ

LEDを光らせるだけでこの長さよ。
この複雑なコードがこの先デファクトスタンダードになるのかは疑問だな。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?