Difference between revisions of "Matrix - Joystick"
From FriendlyELEC WiKi
Line 14: | Line 14: | ||
===Linux下的C示例=== | ===Linux下的C示例=== | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | #include <unistd.h> | ||
+ | #include <signal.h> | ||
+ | #include <stdlib.h> | ||
+ | #include "libfahw.h" | ||
+ | |||
+ | #define SW_TRIGGER (5) | ||
+ | #define PS2_READ_TIMES (10) | ||
+ | |||
+ | static int devFD; | ||
+ | void PS2Handler(int signNum) | ||
+ | { | ||
+ | if (signNum == SIGINT) { | ||
+ | printf("Quit reading PS2 rocker\n"); | ||
+ | pcf8591DeInit(devFD); | ||
+ | } | ||
+ | exit(0); | ||
+ | } | ||
+ | |||
+ | int main(int argc, char ** argv) | ||
+ | { | ||
+ | int mode = 0x0; | ||
+ | |||
+ | if ((devFD = pcf8591Init()) == -1) { | ||
+ | printf("Fail to init pcf8591 AD\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | if (pcf8591SetCtrl(devFD, PCF8591_INIT_AD_CONTROL) == -1) { | ||
+ | printf("Fail to Set pcf8591 control AD\n"); | ||
+ | pcf8591DeInit(devFD); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | int i = 0; | ||
+ | int x, y, z; | ||
+ | signal(SIGINT, PS2Handler); | ||
+ | for (i=0; i<PS2_READ_TIMES; i++) { | ||
+ | x = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL0); | ||
+ | y = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL1); | ||
+ | z = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL2); | ||
+ | if (z > SW_TRIGGER) { | ||
+ | z = 0; | ||
+ | } else { | ||
+ | "PS2rocker.c" 51L, 1082C 1,1 顶端 | ||
+ | z = 0; | ||
+ | } else { | ||
+ | z = 1; | ||
+ | } | ||
+ | printf("X=%3d Y=%3d Z=%d\n", x, y, z); | ||
+ | sleep(1); | ||
+ | } | ||
+ | pcf8591DeInit(devFD); | ||
+ | return 0; | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 06:14, 27 July 2015
Contents
1 介绍
我们把游戏手柄上用的摇杆拿出来,做成单独的配件。摇杆是由两个滑动变阻器和一个按键组成。当拨动摇杆时,滑动变阻器的阻值就发生变化,对应的X/Y电压值也随之变化,而用力按下摇杆就会触发按键按下,对应的SW信号变为低电平。
2 特性
- X和Y轴,一个按键
- 2.54mm排针接口,接线方便,通用性强
3 使用方法
3.1 连接
- 连接到Tiny4412 SDK (1506)
- 首先,你需要去掉Tiny4412 SDK 1506底板上面的BP1_EN的跳线帽
- 然后,将配件S针脚连接到GPIO PIN1, V接5V,G接地
3.2 Linux下的C示例
#include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> #include "libfahw.h" #define SW_TRIGGER (5) #define PS2_READ_TIMES (10) static int devFD; void PS2Handler(int signNum) { if (signNum == SIGINT) { printf("Quit reading PS2 rocker\n"); pcf8591DeInit(devFD); } exit(0); } int main(int argc, char ** argv) { int mode = 0x0; if ((devFD = pcf8591Init()) == -1) { printf("Fail to init pcf8591 AD\n"); return -1; } if (pcf8591SetCtrl(devFD, PCF8591_INIT_AD_CONTROL) == -1) { printf("Fail to Set pcf8591 control AD\n"); pcf8591DeInit(devFD); return -1; } int i = 0; int x, y, z; signal(SIGINT, PS2Handler); for (i=0; i<PS2_READ_TIMES; i++) { x = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL0); y = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL1); z = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL2); if (z > SW_TRIGGER) { z = 0; } else { "PS2rocker.c" 51L, 1082C 1,1 顶端 z = 0; } else { z = 1; } printf("X=%3d Y=%3d Z=%d\n", x, y, z); sleep(1); } pcf8591DeInit(devFD); return 0; }
3.3 编译并运行示例
git clone http://github.com/friendlyarm/fa-hardware.git cd fa-hardware cd demo cd buzzer make
将编译生成的buzzer通过ftp上传到开发板上运行即可测试。