首先新建一个Qt工程文件(可参考前几篇帖子)。
首先新建一个led.c和led.h文件,添加到工程中去。
//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
//led.c文件
#include "led.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#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代码如下:
#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熄灭。
好吧,今天为止Qt也配置出来了,Qt调用驱动也完成了,剩下的就是搬转了,2月份就到此为止了,这个月由于要过年了,就不做程序猿,不做攻城狮。。。开学继续。。。。
珍爱生命,远离编程。。。
最后,非常感谢sinlinx公司提供的SIN210开发板。
我要赚赏金
