Difference between revisions of "Matrix - Ball Rolling Switch"
(→Run Test Program) |
(→Download Matrix Source Code) |
||
Line 30: | Line 30: | ||
==Download Matrix Source Code== | ==Download Matrix Source Code== | ||
− | All the matrix modules' code samples are open source. They are maintained on GitHub: | + | All the matrix modules' code samples are open source. They are maintained on GitHub: https://github.com/friendlyarm/matrix.git <br> |
Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with<br> | Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with<br> | ||
* The matrix-nanopi branch contains the matrix modules' code samples for the NanoPi | * The matrix-nanopi branch contains the matrix modules' code samples for the NanoPi | ||
Line 45: | Line 45: | ||
Clone the matrix code from GitHub | Clone the matrix code from GitHub | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ git clone | + | $ git clone https://github.com/friendlyarm/matrix.git |
</syntaxhighlight> | </syntaxhighlight> | ||
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples. | If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples. |
Revision as of 08:57, 28 December 2015
Contents
1 Introduction
- The Matrix-Ball_Rolling_Switch module is a ball switch. Its electrical characteristics are very similar to a mercury switch's however a mercury switch is easily broken, oxidized, leaking and not environment-friendly. This ball switch prevents all these issues.
- The switch is free to move any angle and if that angle is between 15 degrees and 45 degrees a signal will be generated and used as an input to a Schmitt trigger.
2 Features
- GPIO
- Small
- 2.54 mm spacing pin header
- PCB Dimension(mm):16 x 16
- Pin Description:
Pin | Description |
S | Digital GPIO |
V | Supply Voltage 5V |
G | Ground |
3 Basic Device Operation
Please connect Pin V to a power supply, Pin G grounded and Pin S to a digital output. When you move the switch pushing the internal metal ball to connect the trigger point a high level signal will be generated and output to a Schmitt trigger which reverses this signal to output a low level signal. When you move the switch pushing the internal metal ball away from the trigger point a low level signal will be generated and output to a Schmitt trigger which reverses this signal to output a high level signal.
4 Download Matrix Source Code
All the matrix modules' code samples are open source. They are maintained on GitHub: https://github.com/friendlyarm/matrix.git
Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with
- The matrix-nanopi branch contains the matrix modules' code samples for the NanoPi
- The matrix-nanopi2 branch contains the matrix modules' code samples for the NanoPi 2
- The matrix-tiny4412 branch contains the matrix modules' code samples for the Tiny4412;
- The matrix-raspberrypi branch contains the matrix modules' code samples for the RaspberryPi;
Please follow the steps below to get the source code:
Install the git utility on a PC running Ubuntu14.04
$ sudo apt-get install git
Clone the matrix code from GitHub
$ git clone https://github.com/friendlyarm/matrix.git
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.
5 Connect to NanoPi 2
5.1 Hardware Connection
Please refer to the following connection diagram to connect the Matrix-Ball_Rolling_Switch to the NanoPi 2:
Connection Details:
Matrix-Ball_Rolling_Switch | NanoPi2 |
S | Pin7 |
V | Pin4 |
G | Pin6 |
5.2 Compile Test Program
Please login the matrix hub and enter the nanopi2 branch
$ cd matrix $ git checkout nanopi2
Compile the Matrix code
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
Note: please make sure to install the cross compiler "arm-linux-gcc-4.9.3" on your PC, which is used to compile files for the NanoPi 2.
Generated library files are under the "install/lib" directory. The test program is under the "install/usr/bin" directory. The modules are under the "modules" directory.
5.3 Run Test Program
Please insert a TF card which is flashed with Debian to a Linux host and mount its boot and rootfs sections.
We assume the rootfs is mounted to /media/rootfs then please run the following commands to copy the module, library and test program to the card.
$ cp modules /media/rootfs/ -r $ cp install/lib/* /media/rootfs/lib/ -d $ cp install/usr/bin/* /media/rootfs/usr/bin/
Insert this TF card to your NanoPi 2, power on and run the following commands to start the matrix-ball_switch program.
$ matrix-ball_switch
Here is what you should expect:
After you get the prompt "Lean the switch" please tilt the module to roll the ball making it touch the trigger point you will see "Switch[0]:1".
5.4 Code Sample
static struct sensor brSwitch[] = { { GPIO_PIN(7), IRQ_TYPE_EDGE_FALLING, } }; int main(int argc, char ** argv) { int i; int retSize = -1; char value[ARRAY_SIZE(brSwitch)]; int devFD = -1; if (argc == 2) { brSwitch[0].pin = atoi(argv[1]); } printf("Using GPIO_PIN(%d)\n", brSwitch[0].pin); if ((devFD =sensorInit(brSwitch, ARRAY_SIZE(brSwitch))) == -1) { printf("Fail to init sensor\n"); return -1; } printf("Lean the switch...\n"); if ((retSize = sensorRead(devFD, value, ARRAY_SIZE(brSwitch))) == -1) { printf("Fail to read sensors\n"); } if (retSize > 0) { i = 0; for (i=0; i<retSize; i++) { printf("Switch[%d]:%d\n", i, value[i]); } } sensorDeinit(devFD); return 0; }