【STM32MP135F-DK】10-完整小项目--基于QT的智能家居
<div class='showpostmsg'> 本帖最后由 qiao--- 于 2024-1-11 23:15 编辑<p>经过漫长的学习,测评也来到了最后冲刺阶段,于是我打算把之前的测评整合起来,做一个简易的智能家居。因为测评马上结束了,时间比较急,可能有的功能不是很完善,希望大家不要介意,后面我会把这个智能家居慢慢完善分享给大家。</p>
<p>先来一张界面图。</p>
<p> </p>
<p>功能演示(吊灯按钮控制的是红色led灯):</p>
<p>a7e57eab541f3cf8d139c621d1c737f9</p>
<p> </p>
<p>接下来我从三个方面来讲解这个智能家居的测评。</p>
<p><strong><span style="font-size:22px;">1.界面</span></strong></p>
<p>界面我是用qt现有的组件搭建了一个框架如下图</p>
<p> </p>
<p>然后我再代码中用一些图片美化了一下界面,界面美化代码如下:</p>
<pre>
<code>#include "home.h"
#include "ui_home.h"
home::home(QWidget *parent)
: QWidget(parent)
, ui(new Ui::home)
{
ui->setupUi(this);
my_timer = new QTimer(this);
connect(my_timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
my_timer->start(1000);
/*led灯窗口*/
led = new device_led(this);
led->move(160,100);
/*舵机窗窗口*/
motor = new device_motor(this);
motor->move(310,100);
/* 设置样式*/
QPalette pal = this->palette();
pal.setBrush(QPalette::Background,QBrush(QPixmap(":/png/背景.png")));
setPalette(pal);
ui->label_pic->setStyleSheet("border-image:url(:/png/头像.png)");
ui->label_pic->setText("");
ui->frame_backgrand->setStyleSheet("#frame_backgrand{background-color:rgba(0,0,0,15%);}");
ui->label_time1->setStyleSheet("color:#FFFFFF");
ui->label_time2->setStyleSheet("color:#FFFFFF");
ui->label_line->setFixedWidth(3);
ui->label_line->setText("");
ui->label_line->setStyleSheet("background-color:#FFFFFF");
ui->label_eeword->setStyleSheet("color:#FFFFFF");
ui->label_title->setStyleSheet("color:rgba(255,255,255,200)");
ui->label_time1->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd ddd"));
ui->label_time2->setText(QTime::currentTime().toString("hh:mm"));
}</code></pre>
<p>led灯的窗口和舵机的窗口我是从新设计的一个界面框架,如下图</p>
<p> </p>
<p> </p>
<p>后面也是用的代码美化了他们,他们可以用CSS样式来美化。</p>
<pre>
<code>//led界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)");
ui->label_pic->setText("");
//舵机界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/百叶窗-叶子-开.png)");
ui->label_pic->setText("");</code></pre>
<p> </p>
<p><strong><span style="font-size:22px;">2.led灯控制</span></strong></p>
<p>对于led灯的控制我是封装了一个led的类,其中实现了灯打开和关闭的操作,并且为按钮设置了槽函数,用于控制led灯,在槽函数里面我用了替换图片的办法来使吊灯的图片发生变化。代码如下:</p>
<pre>
<code>#include "device/device_led.h"
#include "ui_device_led.h"
device_led::device_led(QWidget *parent) :
QWidget(parent),
ui(new Ui::device_led)
{
ui->setupUi(this);
//滑动按钮
button = new SliderButton(this);
button->set_button_color(QColor(36,110,202),QColor(188,188,188),QColor(255,255,255));
button->set_button_size(12,20);
button->move(85,30);
connect(button,SIGNAL(signal_button_off()),this,SLOT(slot_slider_OFF()));
connect(button,SIGNAL(signal_button_on()),this,SLOT(slot_slider_ON()));
//界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)");
ui->label_pic->setText("");
//硬件初始化
fd =::open("/dev/led_red",O_RDWR);
if(fd<0){
printf("file open failed!\r\n");
return ;
}
::printf("打开成功");
}
device_led::~device_led()
{
delete ui;
}
void device_led::open_led(void){
databuf = 1;
retvalue = ::write(fd,databuf,1);
if(retvalue < 0){
::close(fd);
return ;
}
}
void device_led::close_led(void){
databuf = 0;
retvalue = ::write(fd,databuf,1);
if(retvalue < 0){
::close(fd);
return;
}
}
void device_led::slot_slider_OFF(){
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-关.png)");//替换灯图片
ui->label_onoff->setText("OFF");
close_led();
}
void device_led::slot_slider_ON(){
ui->label_pic->setStyleSheet("border-image:url(:/png/吊灯-开.png)");//替换灯图片
ui->label_onoff->setText("ON");
open_led();
}
</code></pre>
<p><strong><span style="font-size:22px;">3.舵机控制</span></strong></p>
<p>看过我之前的测评的贴友都知道我前面实现了舵机0到180度的随意转动,于是我这里就可以用之前写好的可执行文件streering。</p>
<p>c语言有一个执行文件的函数为system();我这里就是用了这个来实现舵机的控制。控制部分关键代码如下。</p>
<pre>
<code>void device_motor::motor_change(unsigned int angle){
char s ;
::sprintf(s,"/home/root/streering %d",angle);
system(s);
}</code></pre>
<p>最后再绑定滑动组件的槽函数就可以实现,舵机控制完整代码如下:</p>
<pre>
<code>#include "device_motor.h"
#include "ui_device_motor.h"
device_motor::device_motor(QWidget *parent) :
QWidget(parent),
ui(new Ui::device_motor)
{
ui->setupUi(this);
//界面初始化
this->setStyleSheet("#frame_background{background-color:#f4fcff;border-radius:15px}");
ui->label_pic->setStyleSheet("border-image:url(:/png/百叶窗-叶子-开.png)");
ui->label_pic->setText("");
}
device_motor::~device_motor()
{
delete ui;
}
void device_motor::motor_change(unsigned int angle){
char s ;
::sprintf(s,"/home/root/streering %d",angle);
system(s);
}
void device_motor::on_Slider_sliderReleased()
{
motor_change(ui->Slider->value());
}
</code></pre>
<p> </p>
<p>总结:通过本次对之前的测评进行的小整合,完成了一个小的智能家居的项目,感谢eeworld和st公司给我的测评机会。后期我会把这个项目更加完善进而分享出来。</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]