Difference between revisions of "Matrix Starter Kit with NanoPi"
(→温室报警系统) |
(→下载Matrix源码) |
||
(19 intermediate revisions by one other user not shown) | |||
Line 2: | Line 2: | ||
==NanoPi门铃系统== | ==NanoPi门铃系统== | ||
− | + | ===NanoPi门铃系统介绍=== | |
− | [[File:门铃系统.png| | + | [[File:门铃系统.png|frameless|600px|门铃系统]] |
*使用Matrix Starter Kit套件的蜂鸣器、LED、声音传感器做一个门铃系统,当按键按下时,蜂鸣器会响起来,以及LED会闪烁;当声音传感器识别到声音时,蜂鸣器也会响起来。 | *使用Matrix Starter Kit套件的蜂鸣器、LED、声音传感器做一个门铃系统,当按键按下时,蜂鸣器会响起来,以及LED会闪烁;当声音传感器识别到声音时,蜂鸣器也会响起来。 | ||
*如果需要更好的效果,可以发挥自己的想象力,按键在按下来的时候,蜂鸣器随机播放一首音乐。 | *如果需要更好的效果,可以发挥自己的想象力,按键在按下来的时候,蜂鸣器随机播放一首音乐。 | ||
<br> | <br> | ||
− | + | ||
+ | ===Matrix配件=== | ||
*这里需要使用到Matrix的配件为LED、声音传感器、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用: | *这里需要使用到Matrix的配件为LED、声音传感器、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用: | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 13: | Line 14: | ||
|NanoPi门铃系统 || NanoPi | |NanoPi门铃系统 || NanoPi | ||
|- | |- | ||
− | |LED || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_LED] | + | |Matrix-LED || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_LED] |
|- | |- | ||
− | | | + | |Matrix-Sound Sensor || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Sound_Sensor] |
|- | |- | ||
− | | | + | |Matrix-Buzzer || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Buzzer] |
|- | |- | ||
− | | | + | |Matrix-Button || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Button] |
|} | |} | ||
<br> | <br> | ||
− | + | ||
− | + | ===下载Matrix源码=== | |
+ | Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:https://github.com/friendlyarm/matrix.git <br> | ||
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | 该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | ||
* nanopi分支包含了Matrix对NanoPi的支持; | * nanopi分支包含了Matrix对NanoPi的支持; | ||
Line 29: | Line 31: | ||
* raspberrypi分支包含了Matrix对RaspberryPi的支持; | * raspberrypi分支包含了Matrix对RaspberryPi的支持; | ||
<br> | <br> | ||
− | + | ||
+ | ===连接使用=== | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
|NanoPi门铃系统 || NanoPi | |NanoPi门铃系统 || NanoPi | ||
|- | |- | ||
− | |LED || PIN12 | + | |Matrix-LED || PIN12 |
|- | |- | ||
− | | | + | |Matrix - Sound Sensor || PIN7 |
|- | |- | ||
− | | | + | |Matrix - Buzzer || PIN22 |
|- | |- | ||
− | | | + | |Matrix - Button || PIN11 |
|} | |} | ||
<br> | <br> | ||
− | + | ||
+ | ===编译程序及使用=== | ||
进入Matrix代码仓库,切换到nanopi分支 | 进入Matrix代码仓库,切换到nanopi分支 | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 180: | Line 184: | ||
==温室报警系统== | ==温室报警系统== | ||
− | + | ===温室报警系统介绍=== | |
− | [[File:温室报警系统.png| | + | [[File:温室报警系统.png|frameless|600px|温室报警系统]] |
*使用Matrix Starter Kit套件的无源蜂鸣器、LED、温湿度传感器、lcd1602做一个温室报警系统。 | *使用Matrix Starter Kit套件的无源蜂鸣器、LED、温湿度传感器、lcd1602做一个温室报警系统。 | ||
*温室报警系统,使用NanoPi作为中控,接上温湿度传感器检测温室的温湿度变化,这里介绍的温室报警系统每隔两秒读取一次温湿度传感器的温湿度并显示在LCD1602上,如果温度超过预定设置的值,蜂鸣器会响和LED会亮一秒。 | *温室报警系统,使用NanoPi作为中控,接上温湿度传感器检测温室的温湿度变化,这里介绍的温室报警系统每隔两秒读取一次温湿度传感器的温湿度并显示在LCD1602上,如果温度超过预定设置的值,蜂鸣器会响和LED会亮一秒。 | ||
<br> | <br> | ||
− | + | ||
− | * | + | ===Matrix配件=== |
+ | *这里需要使用到Matrix的配件为LED、温湿度传感器、无源蜂鸣器,I2C LCD1602,均可参考以下链接了解Matrix配件的使用: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | | | + | |NanoPi温室报警系统 || NanoPi |
|- | |- | ||
− | |LED || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_LED] | + | |Matrix-LED || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_LED] |
|- | |- | ||
− | | | + | |Matrix - Temperature and Humidity Sensor || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Temperature_and_Humidity_Sensor] |
|- | |- | ||
− | | | + | |Matrix - Buzzer || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Buzzer] |
|- | |- | ||
− | | | + | |Matrix - I2C LCD1602 || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_I2C_LCD1602] |
|} | |} | ||
<br> | <br> | ||
− | + | ||
+ | ===下载Matrix源码=== | ||
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br> | Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br> | ||
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | 该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | ||
Line 207: | Line 213: | ||
* raspberrypi分支包含了Matrix对RaspberryPi的支持; | * raspberrypi分支包含了Matrix对RaspberryPi的支持; | ||
<br> | <br> | ||
− | + | ===连接使用=== | |
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | | | + | |NanoPi报警系统 || NanoPi |
|- | |- | ||
− | |LED || | + | |Matrix-LED || PIN11 |
|- | |- | ||
− | | | + | |Matrix-I2C LCD1602-SDA0 || PIN3 |
+ | |- | ||
+ | |Matrix-I2C LCD1602-SCL0 || PIN5 | ||
|- | |- | ||
− | | | + | |Matrix-Buzzer || PIN22 |
|- | |- | ||
− | | | + | |Matrix - Temperature and Humidity Sensor || PIN7 |
|} | |} | ||
<br> | <br> | ||
− | + | ||
+ | ===编译程序及使用=== | ||
进入Matrix代码仓库,切换到nanopi分支 | 进入Matrix代码仓库,切换到nanopi分支 | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 235: | Line 244: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。<br> | 注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。<br> | ||
− | 编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix- | + | 编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-temperature_and_humidity_monitoring对应的测试程序为matrix-temperature_and_humidity_monitoring。<br> |
拷贝库文件和测试程序到NanoPi的文件系统上 | 拷贝库文件和测试程序到NanoPi的文件系统上 | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 242: | Line 251: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-temperature_and_humidity_monitoring的测试程序 <br> | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ matrix- | + | $ matrix-temperature_and_humidity_monitoring |
</syntaxhighlight> | </syntaxhighlight> | ||
代码展示: | 代码展示: | ||
Line 252: | Line 261: | ||
#include <stdlib.h> | #include <stdlib.h> | ||
#include <signal.h> | #include <signal.h> | ||
+ | #include "common.h" | ||
#include "libfahw.h" | #include "libfahw.h" | ||
− | #define | + | #define LCD_LENGTH 16 |
− | #define | + | #define SET_TEMP 37000 |
− | + | ||
− | + | int DHT11pin = GPIO_PIN1; | |
− | + | int LEDpin = GPIO_PIN2; | |
− | + | int BUZZERpin = PWM_PIN1; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | int | + | int devFD_LCD; |
+ | int devFD_DHT11; | ||
int pwm_HZ = 1000; | int pwm_HZ = 1000; | ||
int pwm_duty = 500; | int pwm_duty = 500; | ||
− | |||
− | |||
− | |||
int stop = 0; | int stop = 0; | ||
Line 280: | Line 282: | ||
} | } | ||
− | int | + | int THmonitoring_init() { |
− | + | //LED | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | //LED | + | |
if ((exportGPIOPin(LEDpin)) == -1) { | if ((exportGPIOPin(LEDpin)) == -1) { | ||
printf("exportGPIOPin(%d) failed\n", LEDpin); | printf("exportGPIOPin(%d) failed\n", LEDpin); | ||
return -1; | return -1; | ||
} | } | ||
− | + | if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { | |
printf("setGPIODirection(%d) failed\n", LEDpin); | printf("setGPIODirection(%d) failed\n", LEDpin); | ||
+ | return -1; | ||
+ | } | ||
+ | //LCD | ||
+ | if ((devFD_LCD = LCD1602Init()) == -1) { | ||
+ | printf("Fail to init LCD1602\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | if (LCD1602Clear(devFD_LCD) == -1) { | ||
+ | printf("Fail to Clear\n"); | ||
return -1; | return -1; | ||
} | } | ||
+ | //dht11 | ||
+ | if ((devFD_DHT11 = dht11Init(DHT11pin)) == -1) { | ||
+ | printf("Fail to init dht11\n"); | ||
+ | return -1; | ||
+ | } | ||
return 0; | return 0; | ||
} | } | ||
− | void | + | void THmonitoring_deinit() { |
− | + | ||
PWMStop(BUZZERpin); | PWMStop(BUZZERpin); | ||
− | unexportGPIOPin(LEDpin); | + | unexportGPIOPin(LEDpin); |
+ | LCD1602DeInit(devFD_LCD); | ||
+ | dht11DeInit(devFD_DHT11); | ||
} | } | ||
− | int | + | int THmonitoring_alart() { |
if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { | if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { | ||
printf("setGPIOValue(%d) failed\n", LEDpin); | printf("setGPIOValue(%d) failed\n", LEDpin); | ||
return -1; | return -1; | ||
} | } | ||
+ | if(PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { | ||
+ | printf("Fail to output PWM\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
− | if ( | + | int THmonitoring_noalart() { |
− | printf("Fail to | + | if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { |
− | + | printf("setGPIOValue(%d) failed\n", LEDpin); | |
+ | return -1; | ||
+ | } | ||
+ | if(PWMStop(BUZZERpin) == -1) { | ||
+ | printf("Fail to stop PWM\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int main(int argc, char ** argv) { | ||
+ | int dhtTemp = 0; | ||
+ | int dhtHdty = 0; | ||
+ | int INT_BUF = 0; | ||
+ | char* buf = (char *) malloc(LCD_LENGTH); | ||
+ | memset(buf, 0, LCD_LENGTH); | ||
+ | char* line1 = " hum: "; | ||
+ | char* line2 = " temp: "; | ||
+ | THmonitoring_init(); | ||
+ | if (LCD1602DispLines(devFD_LCD, line1, line2) == -1) { | ||
+ | printf("Fail to Display String\n"); | ||
+ | } | ||
+ | signal(SIGINT, Stop); | ||
+ | printf("you can press ctrl+c to stop\n"); | ||
+ | while(!stop) { | ||
+ | if ((dht11Read(DHT_HUMIDITY, &dhtHdty)) == -1) { | ||
+ | dhtHdty = 0; | ||
+ | } | ||
+ | if ((dht11Read(DHT_TEMP, &dhtTemp)) == -1) { | ||
+ | dhtTemp = 0; | ||
+ | } | ||
+ | |||
+ | if (dhtHdty == 0) { | ||
+ | sprintf(buf, "%s", "Er"); | ||
+ | } else { | ||
+ | INT_BUF = dhtHdty / 1000; | ||
+ | sprintf(buf, "%d", INT_BUF); | ||
+ | } | ||
+ | LCD1602DispStr(devFD_LCD, 6, 0, buf); | ||
+ | |||
+ | if (dhtTemp == 0) { | ||
+ | sprintf(buf, "%s", "Er"); | ||
+ | } else { | ||
+ | INT_BUF = dhtTemp / 1000; | ||
+ | sprintf(buf, "%d", INT_BUF); | ||
+ | } | ||
+ | LCD1602DispStr(devFD_LCD, 7, 1, buf); | ||
+ | |||
+ | if (dhtTemp >= SET_TEMP) { | ||
+ | if (THmonitoring_alart() != 0) { | ||
+ | printf("fail to alart!\n"); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | sleep(1); | ||
+ | THmonitoring_noalart(); | ||
+ | sleep(1); | ||
} | } | ||
+ | free(buf); | ||
+ | THmonitoring_deinit(); | ||
return 0; | return 0; | ||
} | } | ||
+ | </syntaxhighlight> | ||
− | int | + | ==NanoPi音乐盒== |
− | if( | + | ===NanoPi音乐盒介绍=== |
− | + | [[File:音乐盒.png|frameless|600px|音乐盒]] | |
+ | *使用Matrix Starter Kit套件的无源蜂鸣器、LED音乐盒播放系统。 | ||
+ | *音乐盒播放系统,使用NanoPi作为中控,接上无源蜂鸣器来播放音乐,这里介绍的是播放一首《欢乐颂》,并且在播放的过程中,LED会随着音乐播放而闪烁。蜂鸣器播放音乐需要把相应的音阶转化成相对应的频率,按照每个音阶的音长给蜂鸣器适当的发音时间,因此要把乐谱转化一个音阶对应一个【频率,时间】数组。 | ||
+ | <br> | ||
+ | ===Matrix配件=== | ||
+ | *这里需要使用到Matrix的配件为LED、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |NanoPi音乐盒 || NanoPi | ||
+ | |- | ||
+ | |Matrix-LED || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_LED] | ||
+ | |- | ||
+ | |Matrix - Buzzer || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Buzzer] | ||
+ | |} | ||
+ | <br> | ||
+ | |||
+ | ===下载Matrix源码=== | ||
+ | Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br> | ||
+ | 该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | ||
+ | * nanopi分支包含了Matrix对NanoPi的支持; | ||
+ | * tiny4412分支包含了Matrix对Tiny4412的支持; | ||
+ | * raspberrypi分支包含了Matrix对RaspberryPi的支持; | ||
+ | <br> | ||
+ | ===连接使用=== | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |NanoPi音乐盒 || NanoPi | ||
+ | |- | ||
+ | |Matrix-LED01 || PIN7 | ||
+ | |- | ||
+ | |Matrix-LED02 || PIN11 | ||
+ | |- | ||
+ | |Matrix-LED03 || PIN12 | ||
+ | |- | ||
+ | |Matrix-Buzzer || PIN22 | ||
+ | |} | ||
+ | <br> | ||
+ | |||
+ | ===编译程序及使用=== | ||
+ | 进入Matrix代码仓库,切换到nanopi分支 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ 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-MusicBuzzer对应的测试程序为matrix-MusicBuzzer。<br> | ||
+ | 拷贝库文件和测试程序到NanoPi的文件系统上 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ cp install/usr/bin/* nanopi_rootfs/usr/bin/ | ||
+ | $ cp install/lib/* nanopi_rootfs/lib/ -d | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-MusicBuzzer的测试程序 <br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ matrix-MusicBuzzer | ||
+ | </syntaxhighlight> | ||
+ | 代码展示: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | #include <unistd.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <signal.h> | ||
+ | #include "common.h" | ||
+ | #include "libfahw.h" | ||
+ | |||
+ | unsigned int Music_code[]={659, 250, 659, 250, 698, 250, 784, 250, | ||
+ | 784, 250, 698, 250, 659, 250, 587, 250, | ||
+ | 523, 250, 523, 250, 587, 250, 659, 250, | ||
+ | 659, 375, 587, 125, 587, 250, 1, 500, | ||
+ | 659, 250, 659, 250, 698, 250, 784, 250, | ||
+ | 784, 250, 698, 250, 659, 250, 587, 250, | ||
+ | 523, 250, 523, 250, 587, 250, 659, 250, | ||
+ | 587, 375, 523, 125, 523, 250, 1, 500, | ||
+ | 587, 250, 587, 250, 659, 250, 523, 250, | ||
+ | 587, 250, 659, 125, 698, 125, 659, 250, 523, 250, | ||
+ | 523, 250, 587, 250, 392, 250, 1, 500, | ||
+ | 659, 250, 659, 250, 698, 250, 784, 250, | ||
+ | 784, 250, 698, 250, 659, 250, 587, 250, | ||
+ | 523, 250, 523, 250, 587, 250, 659, 250, | ||
+ | 587, 375, 523, 250, 523, 250, 1, 500}; | ||
+ | |||
+ | int BUZZERpin = PWM_PIN1; | ||
+ | int REDpin = GPIO_PIN1; | ||
+ | int GREENpin = GPIO_PIN2; | ||
+ | int BLUEpin = GPIO_PIN3; | ||
+ | |||
+ | int pwm_HZ; | ||
+ | |||
+ | int stop = 0; | ||
+ | void Stop(int signo) { | ||
+ | stop = 1; | ||
+ | } | ||
+ | |||
+ | int MusicBuzzer_init() { | ||
+ | if ((exportGPIOPin(REDpin)) == -1) { | ||
+ | printf("exportGPIOPin(%d) failed\n", REDpin); | ||
return -1; | return -1; | ||
} | } | ||
− | if( | + | if ((setGPIODirection(REDpin, GPIO_OUT)) == -1) { |
− | printf(" | + | printf("setGPIODirection(%d) failed\n", REDpin); |
+ | return -1; | ||
+ | } | ||
+ | if ((exportGPIOPin(GREENpin)) == -1) { | ||
+ | printf("exportGPIOPin(%d) failed\n", GREENpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if ((setGPIODirection(GREENpin, GPIO_OUT)) == -1) { | ||
+ | printf("setGPIODirection(%d) failed\n", GREENpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if ((exportGPIOPin(BLUEpin)) == -1) { | ||
+ | printf("exportGPIOPin(%d) failed\n", BLUEpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if ((setGPIODirection(BLUEpin, GPIO_OUT)) == -1) { | ||
+ | printf("setGPIODirection(%d) failed\n", BLUEpin); | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void MusicBuzzer_deinit() { | ||
+ | unexportGPIOPin(REDpin); | ||
+ | unexportGPIOPin(GREENpin); | ||
+ | unexportGPIOPin(BLUEpin); | ||
+ | } | ||
+ | |||
+ | int LEDon() { | ||
+ | if(setGPIOValue(REDpin, GPIO_HIGH) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", REDpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if(setGPIOValue(GREENpin, GPIO_HIGH) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", GREENpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if(setGPIOValue(BLUEpin, GPIO_HIGH) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", BLUEpin); | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | int LEDoff() { | ||
+ | if(setGPIOValue(REDpin, GPIO_LOW) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", REDpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if(setGPIOValue(GREENpin, GPIO_LOW) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", GREENpin); | ||
+ | return -1; | ||
+ | } | ||
+ | if(setGPIOValue(BLUEpin, GPIO_LOW) == -1) { | ||
+ | printf("setGPIOValue(%d) failed\n", BLUEpin); | ||
return -1; | return -1; | ||
} | } | ||
Line 331: | Line 564: | ||
int main(int argc, char ** argv) { | int main(int argc, char ** argv) { | ||
− | int | + | int state = 0; |
− | + | int len = ARRAY_SIZE(Music_code); | |
− | + | ||
signal(SIGINT, Stop); | signal(SIGINT, Stop); | ||
printf("you can press ctrl+c to stop\n"); | printf("you can press ctrl+c to stop\n"); | ||
+ | |||
+ | MusicBuzzer_init(); | ||
+ | |||
while(!stop) { | while(!stop) { | ||
− | + | for(state = 0;(state <= len-1) && (!stop);) { | |
− | + | pwm_HZ = Music_code[state++]; | |
− | + | PWMPlay(BUZZERpin, pwm_HZ, pwm_HZ / 2); | |
− | + | LEDon(); | |
− | + | usleep(Music_code[state] * 500); | |
− | + | LEDoff(); | |
− | + | usleep(Music_code[state++] * 500); | |
− | + | PWMStop(BUZZERpin); | |
− | usleep( | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | sleep(1); | ||
} | } | ||
− | + | ||
+ | MusicBuzzer_deinit(); | ||
+ | return 0; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==NanoPi超声波测距== | ||
+ | ===NanoPi超声波测距介绍=== | ||
+ | [[File:超声波测距.png|frameless|600px|超声波测距]] | ||
+ | *使用Matrix Starter Kit套件的超声波、IIC LCD1602配件。 | ||
+ | *超声波测距系统,使用NanoPi作为中控,接上超声波进行测距,IIC LCD1602用来显示检测到的距离每运行一次程序,超声波会检测一次距离并显示在LCD上,如果检测不到LCD上会提示失败并提醒重试。利用超声波的特性,系统可以用在智能小车等好玩的智能硬件上。 | ||
+ | <br> | ||
+ | |||
+ | ===Matrix配件=== | ||
+ | *这里需要使用到Matrix的配件为超声波、IIC LCD1602,均可参考以下链接了解Matrix配件的使用: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |NanoPi超声波测距 || NanoPi | ||
+ | |- | ||
+ | |Matrix-IIC LCD1602 || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_I2C_LCD1602] | ||
+ | |- | ||
+ | |Matrix-Ultrasonic_Ranger || [http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Ultrasonic_Ranger] | ||
+ | |} | ||
+ | <br> | ||
+ | |||
+ | ===下载Matrix源码=== | ||
+ | Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br> | ||
+ | 该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br> | ||
+ | * nanopi分支包含了Matrix对NanoPi的支持; | ||
+ | * tiny4412分支包含了Matrix对Tiny4412的支持; | ||
+ | * raspberrypi分支包含了Matrix对RaspberryPi的支持; | ||
+ | <br> | ||
+ | ===连接使用=== | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |NanoPi音乐盒 || NanoPi | ||
+ | |- | ||
+ | |Matrix-IIC LCD1602 SCL0 || PIN5 | ||
+ | |- | ||
+ | |Matrix-IIC LCD1602 SDA0 || PIN3 | ||
+ | |- | ||
+ | |Matrix-Ultrasonic Ranger || PIN7 | ||
+ | |} | ||
+ | <br> | ||
+ | |||
+ | ===编译程序及使用=== | ||
+ | 进入Matrix代码仓库,切换到nanopi分支 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ 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-HCSRshow对应的测试程序为matrix-HCSRshow。<br> | ||
+ | 拷贝库文件和测试程序到NanoPi的文件系统上 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ cp install/usr/bin/* nanopi_rootfs/usr/bin/ | ||
+ | $ cp install/lib/* nanopi_rootfs/lib/ -d | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-HCSRshow的测试程序 <br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ matrix-HCSRshow | ||
+ | </syntaxhighlight> | ||
+ | 代码展示: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | #include <unistd.h> | ||
+ | #include <stdlib.h> | ||
+ | #include "common.h" | ||
+ | #include "libfahw.h" | ||
+ | |||
+ | #define LCD_LENGTH 16 | ||
+ | |||
+ | int HCSRpin = GPIO_PIN1; | ||
+ | int devFD_LCD = -1; | ||
+ | |||
+ | int HcsrShow_init() { | ||
+ | if (Hcsr04Init(HCSRpin) == -1) { | ||
+ | printf("Fail to init hcsr04\n"); | ||
+ | } | ||
+ | if ((devFD_LCD = LCD1602Init()) == -1) { | ||
+ | printf("Fail to init LCD1602\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | if (LCD1602Clear(devFD_LCD) == -1) { | ||
+ | printf("Fail to Clear\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void HcsrShow_deinit() { | ||
+ | Hcsr04DeInit(); | ||
+ | LCD1602DeInit(devFD_LCD); | ||
+ | } | ||
+ | |||
+ | int main(int argc, char ** argv) { | ||
+ | int distance = -1; | ||
+ | char *STR = "distance is :"; | ||
+ | char *ERR1 = " Faid to get"; | ||
+ | char *ERR2 = "Please try again"; | ||
+ | char* BUF = (char *) malloc(LCD_LENGTH); | ||
+ | memset(BUF, 0, LCD_LENGTH); | ||
+ | |||
+ | HcsrShow_init(); | ||
+ | |||
+ | if ((Hcsr04Read(&distance)==0 && distance == -1) || distance == 0) { | ||
+ | LCD1602DispLines(devFD_LCD, ERR1, ERR2); | ||
+ | } else { | ||
+ | LCD1602DispStr(devFD_LCD, 0, 0, STR); | ||
+ | sprintf(BUF, "%d cm", distance); | ||
+ | LCD1602DispStr(devFD_LCD, 6, 1, BUF); | ||
+ | } | ||
+ | |||
+ | free(BUF); | ||
+ | HcsrShow_deinit(); | ||
return 0; | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 03:10, 15 October 2018
Contents
1 NanoPi门铃系统
1.1 NanoPi门铃系统介绍
- 使用Matrix Starter Kit套件的蜂鸣器、LED、声音传感器做一个门铃系统,当按键按下时,蜂鸣器会响起来,以及LED会闪烁;当声音传感器识别到声音时,蜂鸣器也会响起来。
- 如果需要更好的效果,可以发挥自己的想象力,按键在按下来的时候,蜂鸣器随机播放一首音乐。
1.2 Matrix配件
- 这里需要使用到Matrix的配件为LED、声音传感器、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用:
NanoPi门铃系统 | NanoPi |
Matrix-LED | [1] |
Matrix-Sound Sensor | [2] |
Matrix-Buzzer | [3] |
Matrix-Button | [4] |
1.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:https://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
1.4 连接使用
NanoPi门铃系统 | NanoPi |
Matrix-LED | PIN12 |
Matrix - Sound Sensor | PIN7 |
Matrix - Buzzer | PIN22 |
Matrix - Button | PIN11 |
1.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-doorbell对应的测试程序为matrix-doorbell。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrixdoorbell的测试程序
$ matrix-doorbell
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include "libfahw.h" #define SOUND 0 #define BUTTON 1 static struct sensor sensor_dev[2] = { { GPIO_PIN1, IRQ_TYPE_EDGE_BOTH, }, { GPIO_PIN2, IRQ_TYPE_EDGE_FALLING, } }; int devFD = -1; int pwm_HZ = 1000; int pwm_duty = 500; int BUZZERpin = PWM_PIN1; int LEDpin = GPIO_PIN3; int stop = 0; void Stop(int signo) { stop = 1; } int doorbell_init() { //sensor init if ((devFD = sensorInit(sensor_dev, ARRAY_SIZE(sensor_dev))) == -1) { printf("Fail to init sensor sound\n"); return -1; } //LED init if ((exportGPIOPin(LEDpin)) == -1) { printf("exportGPIOPin(%d) failed\n", LEDpin); return -1; } if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", LEDpin); return -1; } return 0; } void doorbell_deinit() { sensorDeinit(devFD); PWMStop(BUZZERpin); unexportGPIOPin(LEDpin); } int doorbell_open() { if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if (PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { printf("Fail to output PWM\n"); return -1; } return 0; } int doorbell_close() { if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMStop(BUZZERpin) == -1) { printf("Fail to stop PWM\n"); return -1; } return 0; } int main(int argc, char ** argv) { int sensor_state; char value[ARRAY_SIZE(sensor_dev)]; doorbell_init(); signal(SIGINT, Stop); printf("you can press ctrl+c to stop\n"); while(!stop) { sensor_state = sensorRead(devFD, value, ARRAY_SIZE(sensor_dev)); if(value[SOUND] || value[BUTTON]) { if(doorbell_open() == -1) { printf("fail to open doorbell\n"); return -1; } value[SOUND] = 0; value[BUTTON] = 0; usleep(1000*500); } if(doorbell_close() == -1) { printf("fail to close doorbell\n"); return -1; } } doorbell_deinit(); return 0; }
2 温室报警系统
2.1 温室报警系统介绍
- 使用Matrix Starter Kit套件的无源蜂鸣器、LED、温湿度传感器、lcd1602做一个温室报警系统。
- 温室报警系统,使用NanoPi作为中控,接上温湿度传感器检测温室的温湿度变化,这里介绍的温室报警系统每隔两秒读取一次温湿度传感器的温湿度并显示在LCD1602上,如果温度超过预定设置的值,蜂鸣器会响和LED会亮一秒。
2.2 Matrix配件
- 这里需要使用到Matrix的配件为LED、温湿度传感器、无源蜂鸣器,I2C LCD1602,均可参考以下链接了解Matrix配件的使用:
NanoPi温室报警系统 | NanoPi |
Matrix-LED | [5] |
Matrix - Temperature and Humidity Sensor | [6] |
Matrix - Buzzer | [7] |
Matrix - I2C LCD1602 | [8] |
2.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
2.4 连接使用
NanoPi报警系统 | NanoPi |
Matrix-LED | PIN11 |
Matrix-I2C LCD1602-SDA0 | PIN3 |
Matrix-I2C LCD1602-SCL0 | PIN5 |
Matrix-Buzzer | PIN22 |
Matrix - Temperature and Humidity Sensor | PIN7 |
2.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-temperature_and_humidity_monitoring对应的测试程序为matrix-temperature_and_humidity_monitoring。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-temperature_and_humidity_monitoring的测试程序
$ matrix-temperature_and_humidity_monitoring
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include "common.h" #include "libfahw.h" #define LCD_LENGTH 16 #define SET_TEMP 37000 int DHT11pin = GPIO_PIN1; int LEDpin = GPIO_PIN2; int BUZZERpin = PWM_PIN1; int devFD_LCD; int devFD_DHT11; int pwm_HZ = 1000; int pwm_duty = 500; int stop = 0; void Stop(int signo) { stop = 1; } int THmonitoring_init() { //LED if ((exportGPIOPin(LEDpin)) == -1) { printf("exportGPIOPin(%d) failed\n", LEDpin); return -1; } if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", LEDpin); return -1; } //LCD if ((devFD_LCD = LCD1602Init()) == -1) { printf("Fail to init LCD1602\n"); return -1; } if (LCD1602Clear(devFD_LCD) == -1) { printf("Fail to Clear\n"); return -1; } //dht11 if ((devFD_DHT11 = dht11Init(DHT11pin)) == -1) { printf("Fail to init dht11\n"); return -1; } return 0; } void THmonitoring_deinit() { PWMStop(BUZZERpin); unexportGPIOPin(LEDpin); LCD1602DeInit(devFD_LCD); dht11DeInit(devFD_DHT11); } int THmonitoring_alart() { if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { printf("Fail to output PWM\n"); return -1; } return 0; } int THmonitoring_noalart() { if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMStop(BUZZERpin) == -1) { printf("Fail to stop PWM\n"); return -1; } return 0; } int main(int argc, char ** argv) { int dhtTemp = 0; int dhtHdty = 0; int INT_BUF = 0; char* buf = (char *) malloc(LCD_LENGTH); memset(buf, 0, LCD_LENGTH); char* line1 = " hum: "; char* line2 = " temp: "; THmonitoring_init(); if (LCD1602DispLines(devFD_LCD, line1, line2) == -1) { printf("Fail to Display String\n"); } signal(SIGINT, Stop); printf("you can press ctrl+c to stop\n"); while(!stop) { if ((dht11Read(DHT_HUMIDITY, &dhtHdty)) == -1) { dhtHdty = 0; } if ((dht11Read(DHT_TEMP, &dhtTemp)) == -1) { dhtTemp = 0; } if (dhtHdty == 0) { sprintf(buf, "%s", "Er"); } else { INT_BUF = dhtHdty / 1000; sprintf(buf, "%d", INT_BUF); } LCD1602DispStr(devFD_LCD, 6, 0, buf); if (dhtTemp == 0) { sprintf(buf, "%s", "Er"); } else { INT_BUF = dhtTemp / 1000; sprintf(buf, "%d", INT_BUF); } LCD1602DispStr(devFD_LCD, 7, 1, buf); if (dhtTemp >= SET_TEMP) { if (THmonitoring_alart() != 0) { printf("fail to alart!\n"); break; } } sleep(1); THmonitoring_noalart(); sleep(1); } free(buf); THmonitoring_deinit(); return 0; }
3 NanoPi音乐盒
3.1 NanoPi音乐盒介绍
- 使用Matrix Starter Kit套件的无源蜂鸣器、LED音乐盒播放系统。
- 音乐盒播放系统,使用NanoPi作为中控,接上无源蜂鸣器来播放音乐,这里介绍的是播放一首《欢乐颂》,并且在播放的过程中,LED会随着音乐播放而闪烁。蜂鸣器播放音乐需要把相应的音阶转化成相对应的频率,按照每个音阶的音长给蜂鸣器适当的发音时间,因此要把乐谱转化一个音阶对应一个【频率,时间】数组。
3.2 Matrix配件
- 这里需要使用到Matrix的配件为LED、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用:
NanoPi音乐盒 | NanoPi |
Matrix-LED | [9] |
Matrix - Buzzer | [10] |
3.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
3.4 连接使用
NanoPi音乐盒 | NanoPi |
Matrix-LED01 | PIN7 |
Matrix-LED02 | PIN11 |
Matrix-LED03 | PIN12 |
Matrix-Buzzer | PIN22 |
3.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-MusicBuzzer对应的测试程序为matrix-MusicBuzzer。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-MusicBuzzer的测试程序
$ matrix-MusicBuzzer
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include "common.h" #include "libfahw.h" unsigned int Music_code[]={659, 250, 659, 250, 698, 250, 784, 250, 784, 250, 698, 250, 659, 250, 587, 250, 523, 250, 523, 250, 587, 250, 659, 250, 659, 375, 587, 125, 587, 250, 1, 500, 659, 250, 659, 250, 698, 250, 784, 250, 784, 250, 698, 250, 659, 250, 587, 250, 523, 250, 523, 250, 587, 250, 659, 250, 587, 375, 523, 125, 523, 250, 1, 500, 587, 250, 587, 250, 659, 250, 523, 250, 587, 250, 659, 125, 698, 125, 659, 250, 523, 250, 523, 250, 587, 250, 392, 250, 1, 500, 659, 250, 659, 250, 698, 250, 784, 250, 784, 250, 698, 250, 659, 250, 587, 250, 523, 250, 523, 250, 587, 250, 659, 250, 587, 375, 523, 250, 523, 250, 1, 500}; int BUZZERpin = PWM_PIN1; int REDpin = GPIO_PIN1; int GREENpin = GPIO_PIN2; int BLUEpin = GPIO_PIN3; int pwm_HZ; int stop = 0; void Stop(int signo) { stop = 1; } int MusicBuzzer_init() { if ((exportGPIOPin(REDpin)) == -1) { printf("exportGPIOPin(%d) failed\n", REDpin); return -1; } if ((setGPIODirection(REDpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", REDpin); return -1; } if ((exportGPIOPin(GREENpin)) == -1) { printf("exportGPIOPin(%d) failed\n", GREENpin); return -1; } if ((setGPIODirection(GREENpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", GREENpin); return -1; } if ((exportGPIOPin(BLUEpin)) == -1) { printf("exportGPIOPin(%d) failed\n", BLUEpin); return -1; } if ((setGPIODirection(BLUEpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", BLUEpin); return -1; } return 0; } void MusicBuzzer_deinit() { unexportGPIOPin(REDpin); unexportGPIOPin(GREENpin); unexportGPIOPin(BLUEpin); } int LEDon() { if(setGPIOValue(REDpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", REDpin); return -1; } if(setGPIOValue(GREENpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", GREENpin); return -1; } if(setGPIOValue(BLUEpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", BLUEpin); return -1; } return 0; } int LEDoff() { if(setGPIOValue(REDpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", REDpin); return -1; } if(setGPIOValue(GREENpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", GREENpin); return -1; } if(setGPIOValue(BLUEpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", BLUEpin); return -1; } return 0; } int main(int argc, char ** argv) { int state = 0; int len = ARRAY_SIZE(Music_code); signal(SIGINT, Stop); printf("you can press ctrl+c to stop\n"); MusicBuzzer_init(); while(!stop) { for(state = 0;(state <= len-1) && (!stop);) { pwm_HZ = Music_code[state++]; PWMPlay(BUZZERpin, pwm_HZ, pwm_HZ / 2); LEDon(); usleep(Music_code[state] * 500); LEDoff(); usleep(Music_code[state++] * 500); PWMStop(BUZZERpin); } sleep(1); } MusicBuzzer_deinit(); return 0; }
4 NanoPi超声波测距
4.1 NanoPi超声波测距介绍
- 使用Matrix Starter Kit套件的超声波、IIC LCD1602配件。
- 超声波测距系统,使用NanoPi作为中控,接上超声波进行测距,IIC LCD1602用来显示检测到的距离每运行一次程序,超声波会检测一次距离并显示在LCD上,如果检测不到LCD上会提示失败并提醒重试。利用超声波的特性,系统可以用在智能小车等好玩的智能硬件上。
4.2 Matrix配件
- 这里需要使用到Matrix的配件为超声波、IIC LCD1602,均可参考以下链接了解Matrix配件的使用:
NanoPi超声波测距 | NanoPi |
Matrix-IIC LCD1602 | [11] |
Matrix-Ultrasonic_Ranger | [12] |
4.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
4.4 连接使用
NanoPi音乐盒 | NanoPi |
Matrix-IIC LCD1602 SCL0 | PIN5 |
Matrix-IIC LCD1602 SDA0 | PIN3 |
Matrix-Ultrasonic Ranger | PIN7 |
4.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-HCSRshow对应的测试程序为matrix-HCSRshow。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-HCSRshow的测试程序
$ matrix-HCSRshow
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include "common.h" #include "libfahw.h" #define LCD_LENGTH 16 int HCSRpin = GPIO_PIN1; int devFD_LCD = -1; int HcsrShow_init() { if (Hcsr04Init(HCSRpin) == -1) { printf("Fail to init hcsr04\n"); } if ((devFD_LCD = LCD1602Init()) == -1) { printf("Fail to init LCD1602\n"); return -1; } if (LCD1602Clear(devFD_LCD) == -1) { printf("Fail to Clear\n"); return -1; } return 0; } void HcsrShow_deinit() { Hcsr04DeInit(); LCD1602DeInit(devFD_LCD); } int main(int argc, char ** argv) { int distance = -1; char *STR = "distance is :"; char *ERR1 = " Faid to get"; char *ERR2 = "Please try again"; char* BUF = (char *) malloc(LCD_LENGTH); memset(BUF, 0, LCD_LENGTH); HcsrShow_init(); if ((Hcsr04Read(&distance)==0 && distance == -1) || distance == 0) { LCD1602DispLines(devFD_LCD, ERR1, ERR2); } else { LCD1602DispStr(devFD_LCD, 0, 0, STR); sprintf(BUF, "%d cm", distance); LCD1602DispStr(devFD_LCD, 6, 1, BUF); } free(BUF); HcsrShow_deinit(); return 0; }