首先新建一个Qt工程文件(可参考前几篇帖子)。
首先新建一个led.c和led.h文件,添加到工程中去。
view plaincopy to clipboardprint?
- //led.h文件
- #ifndef LED_H
- #define LED_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- extern int led_open(const char *devname);
- extern int led_ioctl(const int no,const int sw);
- extern int led_close(void);
- extern int led_fd;
- #ifdef __cplusplus
- }
- #endif
- #endif // LED_H
view plaincopy to clipboardprint?
- //led.c文件
- #include "led.h"
- #include
- #include
- #include
- #include
- #include
-
- #define IOCTL_LED_ON 0
- #define IOCTL_LED_OFF 1
-
- int led_fd=0;
- int led_open(const char*devname){
- led_fd=open(devname,O_RDWR);
- printf("LED driver is ok\n");
- if(led_fd<0){
- printf("open device %s failed.\n",devname);
- return -1;
- }
- return 0;
- }
- int led_ioctl(const int no,const int sw){
- //led_fd=open("/dev/led",O_RDWR);
- int num=no;
- ioctl(led_fd,sw,num);
- printf("LED%dis open\n",num);
- return 0;
- }
- int led_close(void){
- if(led_fd)
- close(led_fd);
- }
添加一个pushbutton,如下图所示:
将鼠标放在pushbutton上面,点击右键,选择转到槽( goto slot。。。)。选择pressed()和 release()按键函数。
修改mainwindow.cpp代码如下:
view plaincopy to clipboardprint?
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include "led.h"
-
- #define LED1 0
- #define LED2 1
- #define LED3 2
- #define LED4 3
-
- #define LED_ON 0
- #define LED_OFF 1
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- led_open("/dev/led");
- printf("C++ /dev/led\n");
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- led_close();
- }
-
- void MainWindow::on_pushButton_pressed()
- {
- led_ioctl(LED1,LED_ON);
- }
-
- void MainWindow::on_pushButton_released()
- {
- led_ioctl(LED1,LED_OFF);
- }
发布运行。。。
点击pushbutton之后,LED1点亮。松开pushbutton之后LED1熄灭。