Difference between revisions of "Matrix - Rotary Encoder"

From FriendlyELEC WiKi
Jump to: navigation, search
(Created page with "查看中文")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Matrix - Rotary Encoder/zh|查看中文]]
 
[[Matrix - Rotary Encoder/zh|查看中文]]
 +
 +
==Introduction==
 +
[[File:Matrix-Rotary_Encoder.png|thumb|]]
 +
*
 +
==Features==
 +
*
 +
[[File:Matrix-Rotary_Encoder_PCB.png|frameless|400px|]]
 +
 +
* Pin Description:
 +
{| class="wikitable"
 +
|-
 +
|Pin  || Description
 +
|-
 +
|}
 +
 +
==Basic Device Operation==
 +
 +
==Applications==
 +
===Connect to NanoPi M1===
 +
Refer to the following connection diagram to connect the module to the NanoPi M1:<br>
 +
[[File:Matrix-Rotary_Encoder_nanopi_m1.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m1]]
 +
 +
Connection Details:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-Rotary_Encoder || NanoPi M1
 +
|-
 +
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 +
|-
 +
|V    || Pin4
 +
|-
 +
|G    || Pin6
 +
|}
 +
 +
===Connect to NanoPi 2===
 +
Refer to the following connection diagram to connect the module to the NanoPi 2:<br>
 +
[[File:Matrix-Rotary_Encoder_nanopi_2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_2]]
 +
 +
Connection Details:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-Rotary_Encoder || NanoPi 2
 +
|-
 +
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 +
|-
 +
|V    || Pin4
 +
|-
 +
|G    || Pin6
 +
|}
 +
 +
===Connect to NanoPi M2 / NanoPi 2 Fire===
 +
Refer to the following connection diagram to connect the module to the NanoPi M2/ NanoPi 2 Fire:<br>
 +
[[File:Matrix-Rotary_Encoder_nanopi_m2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m2]]
 +
 +
Connection Details:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-Rotary_Encoder || NanoPi M2
 +
|-
 +
|R    || Pin7
 +
|-
 +
|G    || Pin8
 +
|-
 +
|B    || Pin10
 +
|-
 +
|V    || Pin4
 +
|-
 +
|G    || Pin6
 +
|}
 +
 +
===Connect to NanoPC-T2===
 +
Refer to the following connection diagram to connect the module to the NanoPC-T2:<br>
 +
[[File:Matrix-Rotary_Encoder_NanoPC-T2.jpg|frameless|600px|Matrix-Rotary_Encoder_NanoPC-T2]]
 +
 +
Connection Details:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-Rotary_Encoder || NanoPC-T2
 +
|-
 +
|R    || Pin15
 +
|-
 +
|G    || Pin16
 +
|-
 +
|B    || Pin17
 +
|-
 +
|V    || Pin29
 +
|-
 +
|G    || Pin30
 +
|}
 +
 +
==Compile & Run Test Program==
 +
Boot your ARM board with Debian and copy the matrix code:
 +
<syntaxhighlight lang="bash">
 +
$ apt-get update && apt-get install git
 +
$ git clone https://github.com/friendlyarm/matrix.git
 +
</syntaxhighlight>
 +
If your cloning is done successfully a "matrix" directory will be generated.
 +
 +
Compile and install Matrix:
 +
<syntaxhighlight lang="bash">
 +
$ cd matrix
 +
$ make && make install
 +
</syntaxhighlight>
 +
 +
Run test program:
 +
<syntaxhighlight lang="bash">
 +
$ matrix-rotary_encoder
 +
</syntaxhighlight>
 +
Note: this module is not plug and play therefore before running the module please make sure it is connected to an ARM board.<br>
 +
Here is what you should observe:<br>
 +
<syntaxhighlight lang="bash">
 +
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>
 +
Rotating this module will trigger events and generate values.
 +
 +
==Code Sample==
 +
This Matrix code sample can work with all the ARM boards mentioned in this module's wiki. The name of this code sample is "matrix-rotary_encoder". Here is its source code:
 +
<syntaxhighlight lang="c">
 +
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;
 +
}
 +
</syntaxhighlight>
 +
For more details about this APIs called in this code sample refer to [[Matrix API reference manual]] <br>
 +
 +
==Resources==
 +
 +
==Update Log==
 +
===June-24-2016===
 +
* Created English wiki

Latest revision as of 00:00, 24 June 2016

查看中文

1 Introduction

2 Features

File:Matrix-Rotary Encoder PCB.png

  • Pin Description:
Pin Description

3 Basic Device Operation

4 Applications

4.1 Connect to NanoPi M1

Refer to the following connection diagram to connect the module to the NanoPi M1:
Matrix-Rotary_Encoder_nanopi_m1

Connection Details:

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

4.2 Connect to NanoPi 2

Refer to the following connection diagram to connect the module to the NanoPi 2:
Matrix-Rotary_Encoder_nanopi_2

Connection Details:

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

4.3 Connect to NanoPi M2 / NanoPi 2 Fire

Refer to the following connection diagram to connect the module to the NanoPi M2/ NanoPi 2 Fire:
Matrix-Rotary_Encoder_nanopi_m2

Connection Details:

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

4.4 Connect to NanoPC-T2

Refer to the following connection diagram to connect the module to the NanoPC-T2:
Matrix-Rotary_Encoder_NanoPC-T2

Connection Details:

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

5 Compile & Run Test Program

Boot your ARM board with Debian and copy the matrix code:

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

If your cloning is done successfully a "matrix" directory will be generated.

Compile and install Matrix:

$ cd matrix
$ make && make install

Run test program:

$ matrix-rotary_encoder

Note: this module is not plug and play therefore before running the module please make sure it is connected to an ARM board.
Here is what you should observe:

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

Rotating this module will trigger events and generate values.

6 Code Sample

This Matrix code sample can work with all the ARM boards mentioned in this module's wiki. The name of this code sample is "matrix-rotary_encoder". Here is its source code:

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;
}

For more details about this APIs called in this code sample refer to Matrix API reference manual

7 Resources

8 Update Log

8.1 June-24-2016

  • Created English wiki