Scons的使用和使用线程循环点亮用户灯
时间:10-02
整理:3721RD
点击:
该体验主要使用C语言,在一个线程里循环点亮LED,话不多说上代码以下是主程序代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <pthread.h>
- #include "led_hw.h"
- #include "error_util.h"
- int abort_program = 0;
- pthread_t blink_led_thread;
- void exit_program(int sig)
- {
- abort_program = 1;
- }
- void *blink_led(void *arg)
- {
- printf("Executing thread\n");
- while(1)
- {
- if (write_led(LED_ON) < 0)
- {
- printf("Write to LED failed. Aborting\n");
- abort_program = 1;
- }
- sleep(1);
- if (write_led(LED_OFF) < 0)
- {
- printf("Write to LED failed. Aborting\n");
- abort_program = 1;
- }
- sleep(1);
- }
- }
- int create_led_thread(void)
- {
- int error;
- error = pthread_create(&blink_led_thread, NULL, blink_led, NULL);
- CHECK_ERROR;
- printf("thread created\n");
- return 0;
- }
- void terminate_led_thread(void)
- {
- void *ret_value;
- pthread_cancel(blink_led_thread);
- pthread_join(blink_led_thread, &ret_value);
- printf("thread terminated\n");
- }
- int main(int argc, char *argv[])
- {
- /*初始化中断信号*/
- signal(SIGINT, exit_program);
- /*打开LED文件*/
- if ( init_led() < 0 )
- {
- printf("LED init failed\n");
- abort_program = 1;
- }
- /*开始线程使用户灯闪烁*/
- if (create_led_thread() == 1 )
- {
- printf("Cannot create thread. Aborting\n");
- abort_program = 1;
- }
- /*循环*/
- while (!abort_program)
- {
- }
- /*清楚内存退出线程*/
- terminate_led_thread();
- close_led();
- printf("\nProgram exiting\n");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "led_hw.h"
- #include "error_util.h"
- /* LED 2文件操作*/
- #define LED2 "/sys/class/leds/apq8016-sbc\:green\:user2/brightness"
- static int fd_led2 = -1;
- /* LED 2初始化*/
- int init_led(void)
- {
- fd_led2 = open(LED2, O_WRONLY);
- if (fd_led2 < 0)
- {
- ERR_FATAL;
- return -1;
- }
- return 0;
- }
- /* LED 2写操作*/
- int write_led(int data)
- {
- char buf[1];
- if (fd_led2 < 0)
- {
- printf("file not open to write\n");
- return -1;
- }
- if (data < LED_OFF || data > LED_ON)
- {
- printf("Invalid data %d for led\n", data);
- return -1;
- }
- sprintf(buf, "%d", data);
- if ( write(fd_led2, buf , 1) < 0)
- {
- ERR_FATAL;
- return -1;
- }
- return 0;
- }
- int close_led(void)
- {
- if (fd_led2 < 0)
- {
- printf("file not open to write\n");
- return -1;
- }
- /*清除内存退出*/
- write(fd_led2, "0", 1);
- close (fd_led2);
- return 0;
- }
使用了scons工具进行编译,SCons 是一个用 Python 语言编写的类似于 make 工具的程序。与 make 工具相比较,SCons 的配置文件更加简单清晰明了,除此之外,它还有许多的优点。
首先得安装scons工具,到scons的官网下载安装包
官网下载地址:http://scons.org/pages/download.html
我下载的是 scons-2.5.0,可以下载tar.gz文件在linux中解压,也可以下载zip在windos解压后传到DB410c中,在liunx中用
- tar -zxf scons-2.0.1.tar.gz
然后进入解压的目录进行安装,执行命令如下
- cd scons-2.0.1
- sudo python setup.py install
- scons
输入ls查询该文件夹下的文件,会看到出现了一个可执行文件
输入命令
- sudo ./led_thread
[img]file:///C:\Users\Administrator\AppData\Roaming\Tencent\Users\370319314\QQ\WinTemp\RichOle\XGX$[ZM$9(N8@3X5)QWHOCC.png[/img]
附上完整代码:led_threads.zip(2.31 KB, 下载次数: 0)
2016-9-26 20:41 上传
点击文件名下载附件