Difference between revisions of "Matrix - Rotary Encoder/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(硬件连接)
(连接NanoPC-T2)
 
(26 intermediate revisions by 2 users not shown)
Line 17: Line 17:
 
==工作原理==
 
==工作原理==
  
==下载Matrix源码==
+
==硬件连接==
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br>
+
===连接NanoPi NEO/NanoPi NEO Air===
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br>
+
NanoPi M1和NanoPi NEO以及NanoPi NEO Air的前24Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,并且使用同一份代码。<br>
* nanopi分支用于支持NanoPi;
+
* nanopi2分支用于支持NanoPi 2;
+
* tiny4412分支用于支持Tiny4412;
+
* raspberrypi分支用于支持RaspberryPi;
+
  
在主机PC上安装git,以Ubuntu14.04为例
+
参考下图连接模块:<br>
<syntaxhighlight lang="bash">
+
[[File:Matrix-Rotary_Encoder_nanopi_NEO.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_NEO]]
$ sudo apt-get install git
+
</syntaxhighlight>
+
 
+
克隆Matrix配件代码仓库
+
<syntaxhighlight lang="bash">
+
$ git clone git://github.com/friendlyarm/matrix.git
+
</syntaxhighlight>
+
克隆完成后会得到一个名为matrix的目录,里面存放着所有Matrix配件的代码
+
 
+
==与NanoPi 2连接使用==
+
===准备工作===
+
在NanoPi 2上运行Debian系统,然后在主机PC上安装并使用相应的编译器,参考wiki: [[NanoPi_2/zh|NanoPi_2]] & [[How_to_build_the_Compiling_Environment/zh|How to Build the Compiling Environment]]。<br>
+
注意: 只有使用s5p4418-nanopi2-matrix分支编译出来的内核才能配合Matrix配件正常工作。<br>
+
下载NanoPi 2内核源代码并编译:<br>
+
<syntaxhighlight lang="bash">
+
$ git clone https://github.com/friendlyarm/linux-3.4.y.git
+
$ cd linux-3.4.y
+
$ git checkout s5p4418-nanopi2-matrix
+
$ make nanopi2_linux_defconfig
+
$ touch .scmversion
+
$ make
+
</syntaxhighlight>
+
编译好后的uImage位于内核源码arch/arm/boot/目录下,把该uImage替换掉SD卡boot分区上的uImage即可。<br>
+
 
+
===硬件连接===
+
参考下图连接模块Matrix-Rotary_Encoder和NanoPi 2:<br>
+
[[File:Matrix-Rotary_Encoder_nanopi_2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_2]]
+
  
 
连接说明:
 
连接说明:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-Rotary_Encoder || NanoPi 2
+
|Matrix-Rotary_Encoder || NanoPi NEO
 
|-
 
|-
|SIB      || Pin12
+
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 
|-
 
|-
|SIA      || Pin11
+
|V    || Pin4
 
|-  
 
|-  
|SW      || Pin7
+
|G    || Pin6
 +
|}
 +
 
 +
===连接NanoPi M1===
 +
参考下图连接模块:<br>
 +
[[File:Matrix-Rotary_Encoder_nanopi_m1.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m1]]
 +
 
 +
连接说明:
 +
{| class="wikitable"
 
|-
 
|-
|5V        || Pin2
+
|Matrix-Rotary_Encoder || NanoPi M1
 
|-
 
|-
|GND    || Pin6
+
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 
|-
 
|-
 +
|V    || Pin4
 +
|-
 +
|G    || Pin6
 
|}
 
|}
  
===编译测试程序===
+
===连接NanoPi 2===
进入Matrix代码仓库,切换到nanopi2分支
+
参考下图连接模块:<br>
<syntaxhighlight lang="bash">
+
[[File:Matrix-Rotary_Encoder_nanopi_2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_2]]
$ cd matrix
+
$ git checkout nanopi2
+
</syntaxhighlight>
+
 
+
编译Matrix配件代码
+
<syntaxhighlight lang="bash">
+
$ make CROSS_COMPILE=arm-linux- clean
+
$ make CROSS_COMPILE=arm-linux-
+
$ make CROSS_COMPILE=arm-linux- install
+
</syntaxhighlight>
+
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi 2配套的arm-linux-gcc.4.8.5。<br>
+
编译成功后库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-Rotary_Encoder对应的测试程序为matrix-rotary_encoder。<br>
+
 
+
===运行测试程序===
+
将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。<br>
+
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令可将Matrix的所有库文件和测试程序拷贝到NanoPi 2的文件系统上。<br>
+
<syntaxhighlight lang="bash">
+
$ cp install/usr/bin/* /media/rootfs/usr/bin/
+
$ cp install/lib/* /media/rootfs/lib/ -d
+
</syntaxhighlight>
+
 
+
将SD卡重新插入NanoPi 2,上电启动,在Debian的shell终端中执行以下命令运行模块Matrix-Rotary_Encoder的测试程序。<br>
+
<syntaxhighlight lang="bash">
+
$ matrix-rotary_encoder
+
</syntaxhighlight>
+
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
+
 
+
===代码展示===
+
<syntaxhighlight lang="c">
+
int main(int argc, char ** argv)
+
{
+
    int ret = -1;
+
    int encoderSw = 0;
+
    int encoderValue = 0;
+
   
+
    if ((ret = rotaryEncoderRead(ENCODER_SW, &encoderSw)) != -1) {
+
        printf("Get sw : %d\n", encoderSw);
+
    } else {
+
        printf("Faided to get sw\n");
+
    }
+
 
+
    if ((ret = rotaryEncoderRead(ENCODER_VALUE, &encoderValue)) != -1) {
+
        printf("Get value : %d\n", encoderValue);
+
    } else {
+
        printf("Faided to get value\n");
+
    }
+
 
+
    return 0;
+
}
+
</syntaxhighlight>
+
 
+
==与NanoPi连接使用==
+
===准备工作===
+
在NanoPi上运行Debian系统,然后在主机PC上安装并使用相应的编译器。参考wiki:[[NanoPi/zh|NanoPi]] <br>
+
注意:必须使用nanopi-v4.1.y-matrix分支编译出来的内核。<br>
+
下载NanoPi内核源代码并编译
+
<syntaxhighlight lang="bash">
+
$ git clone https://github.com/friendlyarm/linux-4.x.y.git
+
$ cd linux-4.x.y
+
$ git checkout nanopi-v4.1.y-matrix
+
$ make nanopi_defconfig
+
$ touch .scmversion
+
$ make
+
</syntaxhighlight>
+
 
+
===硬件连接===
+
参考下图连接模块Matrix-Rotary_Encoder和NanoPi:<br>
+
[[File:Matrix-Rotary_Encoder_nanopi.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi]]
+
  
 
连接说明:
 
连接说明:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-Rotary_Encoder || NanoPi
+
|Matrix-Rotary_Encoder || NanoPi 2
 
|-
 
|-
|SIB      || Pin12
+
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 
|-
 
|-
|SIA      || Pin11
+
|V    || Pin4
 
|-  
 
|-  
|SW      || Pin7
+
|G    || Pin6
 +
|}
 +
 
 +
===连接NanoPi M2 / NanoPi 2 Fire===
 +
NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。<br>
 +
参考下图连接模块:<br>
 +
[[File:Matrix-Rotary_Encoder_nanopi_m2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m2]]
 +
 
 +
连接说明:
 +
{| class="wikitable"
 
|-
 
|-
|5V        || Pin2
+
|Matrix-Rotary_Encoder || NanoPi M2
 
|-
 
|-
|GND    || Pin6
+
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 
|-
 
|-
 +
|V    || Pin4
 +
|-
 +
|G    || Pin6
 
|}
 
|}
  
===编译测试程序===
+
===连接NanoPC-T2/NanoPC-T3===
进入Matrix代码仓库,切换到nanopi分支
+
由于NanoPC-T2跟NanoPC-T3的引脚是一样的,所以连接方式是一样的,这里仅以T2为例,参考下图连接模块:<br>
<syntaxhighlight lang="bash">
+
[[File:Matrix-Rotary_Encoder_NanoPC-T2.jpg|frameless|600px|Matrix-Rotary_Encoder_NanoPC-T2]]
$ cd matrix
+
$ git checkout nanopi
+
</syntaxhighlight>
+
 
+
编译Matrix配件代码
+
<syntaxhighlight lang="bash">
+
$ make CROSS_COMPILE=arm-linux- clean
+
$ make CROSS_COMPILE=arm-linux-
+
$ make CROSS_COMPILE=arm-linux- install
+
</syntaxhighlight>
+
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。<br>
+
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-Rotary_Encoder对应的测试程序为matrix-rotary_encoder。<br>
+
 
+
===运行测试程序===
+
将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。<br>
+
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令可将Matrix的所有库文件和测试程序拷贝到NanoPi的文件系统上。<br>
+
<syntaxhighlight lang="bash">
+
$ cp install/usr/bin/* /media/rootfs/usr/bin/
+
$ cp install/lib/* /media/rootfs/lib/ -d
+
</syntaxhighlight>
+
 
+
将SD卡重新插入NanoPi,上电启动,在Debian的shell终端中执行以下命令运行模块Matrix-Rotary_Encoder的测试程序。<br>
+
<syntaxhighlight lang="bash">
+
$ matrix-rotary_encoder
+
</syntaxhighlight>
+
注意:此模块并不支持热插拔,启动系统前需要确保硬件正常连接。
+
 
+
===代码展示===
+
<syntaxhighlight lang="c">
+
int main(int argc, char ** argv)
+
{
+
    int ret = -1;
+
    int encoderSw = 0;
+
    int encoderValue = 0;
+
   
+
    if ((ret = rotaryEncoderRead(ENCODER_SW, &encoderSw)) != -1) {
+
        printf("Get sw : %d\n", encoderSw);
+
    } else {
+
        printf("Faided to get sw\n");
+
    }
+
 
+
    if ((ret = rotaryEncoderRead(ENCODER_VALUE, &encoderValue)) != -1) {
+
        printf("Get value : %d\n", encoderValue);
+
    } else {
+
        printf("Faided to get value\n");
+
    }
+
 
+
    return 0;
+
}
+
</syntaxhighlight>
+
 
+
==与Tiny4412连接使用==
+
===准备工作===
+
参考Tiny4412光盘里的《友善之臂Ubuntu使用手册》,在Tiny4412上运行UbuntuCore系统,然后在主机PC上安装并使用相应的编译器。<br>
+
注意:只能使用Tiny4412SDK-1506的底板。
+
 
+
===硬件连接===
+
参考下图连接模块Matrix-Rotary_Encoder和Tiny4412:<br>
+
[[File:Matrix-Rotary_Encoder_tiny4412.jpg|frameless|600px|Matrix-Rotary_Encoder_tiny4412]]
+
  
 
连接说明:
 
连接说明:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-Rotary_Encoder || Tiny4412
+
|Matrix-Rotary_Encoder || NanoPC-T2
 
|-
 
|-
 +
|R    || Pin15
 +
|-
 +
|G    || Pin16
 +
|-
 +
|B    || Pin17
 +
|-
 +
|V    || Pin29
 +
|-
 +
|G    || Pin30
 
|}
 
|}
  
===编译测试程序===
+
==编译运行测试程序==
进入Matrix代码仓库,切换到tiny4412分支
+
启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ cd matrix
+
$ apt-get update && apt-get install git
$ git checkout tiny4412
+
$ git clone https://github.com/friendlyarm/matrix.git
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
克隆完成后会得到一个名为matrix的目录。
  
编译Matrix配件代码
+
编译并安装Matrix:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ make CROSS_COMPILE=arm-linux-gnueabihf- clean
+
$ cd matrix
$ make CROSS_COMPILE=arm-linux-gnueabihf-
+
$ make && make install
$ make CROSS_COMPILE=arm-linux-gnueabihf- install
+
 
</syntaxhighlight>
 
</syntaxhighlight>
注意:请确保你的主机PC当前使用的交叉编译器为Tiny4412-UbuntuCore配套的arm-linux-gnueabihf-gcc-4.7.3。<br>
 
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-Rotary_Encoder对应的测试程序为matrix-rotary_encoder。
 
  
===运行测试程序===
+
运行测试程序:
将带有UbuntuCore系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。<br>
+
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令可将Matrix的所有库文件和测试程序拷贝到Tiny4412的文件系统上。<br>
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ cp install/usr/bin/* /media/rootfs/usr/bin/
+
$ matrix-rotary_encoder
$ cp install/lib/* /media/rootfs/lib/ -d
+
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。<br>
将SD卡重新插入Tiny4412,上电启动,在UbuntuCore的shell终端中执行以下命令运行模块Matrix-Rotary_Encoder的测试程序。<br>
+
运行效果如下:<br>
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ matrix-rotary_encoder
+
Get sw=0 value=1
 +
Get sw=0 value=2
 +
Get sw=0 value=3
 +
Get sw=0 value=4
 +
Get sw=0 value=5
 
</syntaxhighlight>
 
</syntaxhighlight>
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
+
旋转模块时,能读到对应的值。
  
===代码展示===
+
==代码说明==
 +
所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rotary_encoder,内容如下:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int main(int argc, char ** argv)  
 
int main(int argc, char ** argv)  
 
{  
 
{  
     int ret = -1;
+
     int i = 0;
 
     int encoderSw = 0;
 
     int encoderSw = 0;
 
     int encoderValue = 0;
 
     int encoderValue = 0;
      
+
     int board;
     if ((ret = rotaryEncoderRead(ENCODER_SW, &encoderSw)) != -1) {
+
     int swPin = GPIO_PIN(7);
        printf("Get sw : %d\n", encoderSw);
+
    int siaPin = GPIO_PIN(8);
     } else {
+
     int sibPin = GPIO_PIN(10);
        printf("Faided to get sw\n");
+
    }
+
  
     if ((ret = rotaryEncoderRead(ENCODER_VALUE, &encoderValue)) != -1) {
+
     if ((board = boardInit()) < 0) {
         printf("Get value : %d\n", encoderValue);
+
         printf("Fail to init board\n");
    } else {
+
         return -1;
         printf("Faided to get value\n");
+
 
     }
 
     }
 
+
    if (board == BOARD_NANOPI_T2) {
 +
        swPin = GPIO_PIN(15);
 +
        siaPin = GPIO_PIN(16);
 +
        sibPin = GPIO_PIN(17);
 +
    }
 +
    system("modprobe "DRIVER_MODULE);
 +
    if (rotaryEncoderInit(swPin, siaPin, sibPin)) {
 +
        printf("Fail to init rotary encoder\n");
 +
        goto err;
 +
    }
 +
    signal(SIGINT, encoderHandler);
 +
    for (i=0; i<ENCODER_READ_TIMES; i++) {
 +
        rotaryEncoderRead(ENCODER_SW, &encoderSw);
 +
        rotaryEncoderRead(ENCODER_VALUE, &encoderValue);
 +
        printf("Get sw=%d value=%d\n", encoderSw, encoderValue);
 +
        sleep(1);
 +
    }
 +
    rotaryEncoderDeInit();
 +
err:
 +
    system("rmmod "DRIVER_MODULE);
 
     return 0;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
API说明参考维基:[[Matrix API reference manual/zh|Matrix API reference manual]] <br>
==与RaspberryPi连接使用==
+
 
+
==与Arduino连接使用==
+
  
 
==相关资料==
 
==相关资料==

Latest revision as of 08:56, 10 November 2016

English

1 介绍

2 特性

File:Matrix-Rotary Encoder PCB.png

  • 引脚说明:
名称 描述

3 工作原理

4 硬件连接

4.1 连接NanoPi NEO/NanoPi NEO Air

NanoPi M1和NanoPi NEO以及NanoPi NEO Air的前24Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,并且使用同一份代码。

参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_NEO

连接说明:

Matrix-Rotary_Encoder NanoPi NEO
R Pin7
G Pin8
B Pin10
V Pin4
G Pin6

4.2 连接NanoPi M1

参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_m1

连接说明:

Matrix-Rotary_Encoder NanoPi M1
R Pin7
G Pin8
B Pin10
V Pin4
G Pin6

4.3 连接NanoPi 2

参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_2

连接说明:

Matrix-Rotary_Encoder NanoPi 2
R Pin7
G Pin8
B Pin10
V Pin4
G Pin6

4.4 连接NanoPi M2 / NanoPi 2 Fire

NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。
参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_m2

连接说明:

Matrix-Rotary_Encoder NanoPi M2
R Pin7
G Pin8
B Pin10
V Pin4
G Pin6

4.5 连接NanoPC-T2/NanoPC-T3

由于NanoPC-T2跟NanoPC-T3的引脚是一样的,所以连接方式是一样的,这里仅以T2为例,参考下图连接模块:
Matrix-Rotary_Encoder_NanoPC-T2

连接说明:

Matrix-Rotary_Encoder NanoPC-T2
R Pin15
G Pin16
B Pin17
V Pin29
G Pin30

5 编译运行测试程序

启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库:

$ apt-get update && apt-get install git
$ git clone https://github.com/friendlyarm/matrix.git

克隆完成后会得到一个名为matrix的目录。

编译并安装Matrix:

$ cd matrix
$ make && make install

运行测试程序:

$ matrix-rotary_encoder

注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
运行效果如下:

Get sw=0 value=1
Get sw=0 value=2
Get sw=0 value=3
Get sw=0 value=4
Get sw=0 value=5

旋转模块时,能读到对应的值。

6 代码说明

所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rotary_encoder,内容如下:

int main(int argc, char ** argv) 
{ 
    int i = 0;
    int encoderSw = 0;
    int encoderValue = 0;
    int board;
    int swPin = GPIO_PIN(7);
    int siaPin = GPIO_PIN(8);
    int sibPin = GPIO_PIN(10);
 
    if ((board = boardInit()) < 0) {
        printf("Fail to init board\n");
        return -1;
    }
    if (board == BOARD_NANOPI_T2) {
        swPin = GPIO_PIN(15);
        siaPin = GPIO_PIN(16);
        sibPin = GPIO_PIN(17);
    }
    system("modprobe "DRIVER_MODULE);
    if (rotaryEncoderInit(swPin, siaPin, sibPin)) {
        printf("Fail to init rotary encoder\n");
        goto err;
    }
    signal(SIGINT, encoderHandler);
    for (i=0; i<ENCODER_READ_TIMES; i++) {
        rotaryEncoderRead(ENCODER_SW, &encoderSw);
        rotaryEncoderRead(ENCODER_VALUE, &encoderValue);
        printf("Get sw=%d value=%d\n", encoderSw, encoderValue);
        sleep(1);
    }
    rotaryEncoderDeInit();
err:
    system("rmmod "DRIVER_MODULE);
    return 0;
}

API说明参考维基:Matrix API reference manual

7 相关资料