RasPi2 でCの王道? WiringPi

とりあえず、RasPi でC を検索したら一番最初にヒットしたので、これで。

オフィシャル
http://wiringpi.com/

GITHUB

https://github.com/WiringPi

Ruby や PHP やPython などのラッパーもあるようです。とりあえず、c で。

 

インストールは、INSTALL か、オフィシャルサイトのドキュメントのように。build スクリプトがあります。

[root@pi wiringPi 12/04 05:34:23]# ./build 
wiringPi Build script
=====================


WiringPi Library
[UnInstall]
[Compile] wiringPi.c
[Compile] wiringSerial.c
[Compile] piHiPri.c
[Compile] wiringShift.c
[Compile] piThread.c
[Compile] wiringPiSPI.c
[Compile] softPwm.c
[Compile] wiringPiI2C.c
[Compile] softTone.c
[Compile] mcp23008.c
[Compile] mcp23016.c
[Compile] mcp23017.c
[Compile] mcp23s08.c
[Compile] mcp23s17.c
[Compile] pcf8574.c
[Compile] sr595.c
[Compile] mcp3002.c
[Compile] pcf8591.c
[Compile] mcp3004.c
[Compile] mcp4802.c
[Compile] mcp3422.c
[Compile] max31855.c
[Compile] max5322.c
[Compile] sn3218.c
[Compile] drcSerial.c
[Compile] wpiExtensions.c
[Link (Dynamic)]
[Install Headers]
[Install Dynamic Lib]

WiringPi Devices Library
[UnInstall]
[Compile] ds1302.c
[Compile] maxdetect.c
[Compile] piNes.c
[Compile] gertboard.c
[Compile] piFace.c
[Compile] lcd128x64.c
[Compile] lcd.c
[Compile] piGlow.c
[Link (Dynamic)]
[Install Headers]
[Install Dynamic Lib]

GPIO Utility
[Compile] gpio.c
[Compile] readall.c
[Compile] pins.c
[Link]
[Install]

All Done.

NOTE: To compile programs with wiringPi, you need to add:
    -lwiringPi
  to your compile line(s) To use the Gertboard, MaxDetect, etc.
  code (the devLib), you need to also add:
    -lwiringPiDev
  to your compile line(s).

[root@pi wiringPi 12/04 05:38:22]# gpio -v
gpio version: 2.31
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 2, Revision: 01, Memory: 1024MB, Maker: Embest 
  Device tree is enabled.
  * Root or sudo required for GPIO access.
[root@pi wiringPi 12/04 05:38:41]# 

こんな感じになりました。

[root@pi wiringPi 12/04 05:38:45]# gpio -v
gpio version: 2.31
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 2, Revision: 01, Memory: 1024MB, Maker: Embest 
  Device tree is enabled.
  * Root or sudo required for GPIO access.
[root@pi wiringPi 12/04 05:38:46]# 

というバージョンを使っています。Gordon Hendersonさんが作られてたようです。感謝。

[root@pi work 12/04 06:17:08]# gpio -warranty
gpio version: 2.31
Copyright (c) 2012-2015 Gordon Henderson

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Leser General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.

[root@pi work 12/04 06:17:14]# 

とりあえず、使ってみます。

a

いつもPIN番号を忘れて検索します。貼り付けておくことに。ラズパイのピン。

Raspberry-Pi-GPIO-compressed

コードは以下。

// Include header file
#include <wiringPi.h>

// Define GPIO21 number
#define LED 21

// Main function
int main(void) {
        int i;

        // Initialize WiringPi
        if(wiringPiSetupGpio() == -1) return 1;

        // Set GPIO pin to output mode
        pinMode(LED, OUTPUT);

        // Repeat LED blinking 10 times
        for(i=0; i<100; i++){
                digitalWrite(LED, 0);
                delay(100);
                digitalWrite(LED, 1);
                delay(100);
        }

        // Turn off LED
        digitalWrite(LED, 0);

        return 0;
}

コンパイル、実行。

[pi@pi work]$ gcc -o blink blink.c -lwiringPi
[pi@pi work]$ sudo ./blink

終了すると、プロンプトに戻ります。RasPi のGPIOはたくさんあるので、同時にいくつ使えるんでしょうね。

 

シフトレジスターを経由しなくても、今回、15本のGPIOを使えればよさそうですが、

[root@pi work 12/04 06:26:32]# gpio readall
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | OUT  | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | ALT0 | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | OUT  | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
[root@pi work 12/04 06:26:33]# 

このテーブルのwPi というのがアサインされている番号ですかね。

マニュアルだと、以下のようです。1 でON(点灯) 0でOFF(消灯)

[root@pi work 12/04 06:32:40]# gpio -g write 21 1 
[root@pi work 12/04 06:33:56]# gpio -g write 21 0

とりあえず、使えそうですね。あとは、プログラムの問題となります。さぁて、本日金曜日。お仕事終わればまた開発タイムです。

examples ディレクトリに参考コードがたくさんあります。

[root@pi examples 12/04 06:41:26]# pwd
/home/pi/wiringPi/examples
[root@pi examples 12/04 06:41:28]# 
[root@pi examples 12/04 06:41:29]# tree
.
├── blink12.c
├── blink12drcs.c
├── blink6drcs.c
├── blink8.c
├── blink.c
├── blink.rtb
├── blink.sh
├── clock.c
├── COPYING.LESSER
├── delayTest.c
├── ds1302.c
├── Gertboard
│   ├── 7segments.c
│   ├── buttons.c
│   ├── gertboard.c
│   ├── Makefile
│   ├── record.c
│   ├── temperature.c
│   ├── voltmeter.c
│   └── vumeter.c
├── header.h
├── isr.c
├── isr-osc.c
├── lcd-adafruit.c
├── lcd.c
├── lowPower.c
├── Makefile
├── max31855.c
├── nes.c
├── okLed.c
├── PiFace
│   ├── blink.c
│   ├── buttons.c
│   ├── ladder.c
│   ├── Makefile
│   ├── metro.c
│   ├── motor.c
│   └── reaction.c
├── PiGlow
│   ├── Makefile
│   ├── piGlow0.c
│   ├── piGlow1.c
│   └── piglow.c
├── pwm.c
├── q2w
│   ├── binary.c
│   ├── blink.c
│   ├── blink-io.c
│   ├── blink.sh
│   ├── bright.c
│   ├── button.c
│   ├── Makefile
│   └── volts.c
├── README.TXT
├── rht03.c
├── serialRead.c
├── serialTest.c
├── servo.c
├── softPwm.c
├── softTone.c
├── speed.c
├── spiSpeed.c
└── wfi.c

4 directories, 59 files
[root@pi examples 12/04 06:41:32]# 

 

コハクラフトさんからも発送されたようで、土日にはなんらか楽しめそうです。

RasPi2 でCの王道? WiringPi」への1件のフィードバック

  1. ピンバック: OPIでLチカ GPIOをC で | junkhack

コメントは受け付けていません。