6
7

More than 3 years have passed since last update.

Raspberry Pi C#でLチカ

Last updated at Posted at 2020-06-01

マイクロソフト製のGPIOライブラリを使う

呼び出し方

using System.Device.Gpio;

GitにC#で書かれたLチカサンプルソースがあるので利用します。

サンプルソース

RaspberryPiにNetCore 3.1をインストール

LinuxのArmのところをクリックします
スクリーンショット 2020-06-01 17.27.58.png

.net core sdk DownLoadPage

クリックした次に画面でパスをコピペできるのでパスを取得します
スクリーンショット 2020-06-01 17.13.58.png

$ wget パス

.NET Core SDK をインストールする

$ mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-3.1.100-linux-x64.tar.gz -C $HOME/dotnet

$ vim ~/.bashrc
#パスを追加する
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
#保存する
$ source ~/.bashrc

dotnetをパスを通します

VisualStudioMacでコンソールアプリの開発

スクリーンショット 2020-06-01 17.02.26.png
.net Coreを選択
スクリーンショット 2020-06-01 17.02.35.png

NugetからSystem.Device.Gpioを取得

MicroSoft製のGpioライブラリをダウンロードします
スクリーンショット 2020-06-01 17.01.24.png

using System;
using System.Device.Gpio;
using System.Threading;

namespace led {
    class Program {
        static void Main(string[] args) {
       //GPIOを番号を指定すること
            int pin = 7;
            GpioController controller = new GpioController();
            controller.OpenPin(pin, PinMode.Output);

            int lightTimeInMilliseconds = 1000;
            int dimTimeInMilliseconds = 200;

            while(true) {
                Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
                controller.Write(pin, PinValue.High);
                Thread.Sleep(lightTimeInMilliseconds);
                Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                controller.Write(pin, PinValue.Low);
                Thread.Sleep(dimTimeInMilliseconds);
            }
           }
      }
}

ビルド

dllを指定すると実行できます。exeに出力するやり方もあります。

$ dotnet led.dll

ポイント

controller.OpenPin(pin, PinMode.Output) はGPIOを番号すること
スクリーンショット 2020-06-01 17.05.29.png

課題

リモートGPIOについて調べる

VisualStudioMacで作成したプロジェクトファイルをMonoDevelopで開きデバッグする

MoNoDevelopにSDKのパスを設定する
スクリーンショット 2020-06-01 17.04.46.png

C#でのLチカ動作確認とれました。

PythonでLチカ

6
7
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
6
7