您現在的位置是:首頁 > 運動
嵌入式系統開發與應用——交通燈系統
怎麼用c語言畫直線
《
嵌入式系統開發與應用
--限選》實訓
題目:交通燈系統
1 系統設計
1。1 設計要求
1。1。1 設計任務
利用Qt-Creator程式設計實現各個路口紅綠燈及時間顯示,設計一個交通燈控制系統。
1。1。2 效能指標要求
(1)按照題目要求使用Qt程式設計,獨立設計系統所需介面。實現交通燈計時顯示、按鈕控制交通燈啟動和停止。
(2)在十字交叉路口,東南西北各方向都設定紅、黃、綠色訊號燈,紅燈亮表示禁止通行,綠燈亮表示可以通行,紅燈滅之前5秒鐘黃燈開始閃爍直到綠燈亮起後黃燈熄滅。其中東西方向為主幹道,南北方向為次幹道,各個方向分別設定兩位數碼管,用來顯示紅燈和綠燈倒計時間,東西方向時間一致,南北方向時間一致。
(3)啟動時主幹道為60秒倒計時,次幹道為40秒倒計時。
(4)單獨設計人行道指示燈標誌,當禁止行走時為紅燈,當可以橫穿馬路時,綠燈亮起,在禁止通行之前10秒鐘綠燈開始閃爍(以警示行人),最終紅燈亮起綠燈熄滅。
(5)透過按鍵可隨意設定主幹道與次幹道時間。
(6)真實場景模擬展示等。
1。2 設計思路及設計框圖
1。2。1設計思路
交通燈倒計時設計的系統框圖。
1。2。2總體設計框圖
交通燈介面設計圖。
2 各個模組程式的設計
設定初試時間:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(“Traffic Light”);
m_nClickCnt = 0;
//設定預設時間
ui->lineEdit_mainTime->setText(“60”);
ui->lineEdit_secondTime->setText(“40”);
//獲取初始時間
m_nmainTime = ui->lineEdit_mainTime->text()。toInt();
m_nsecondTime = ui->lineEdit_secondTime->text()。toInt();
Init();
LightTime();
ui->car_up->setPixmap(QPixmap(“://new/prefix1/tu/up。png”));//新增資源
//ui->car_up->setPixmap(QPixmap(“://new/prefix1/picture/up。png”));//新增資源
ui->car_up->setScaledContents(true);//圖片自動適應label大小
}
介面規則設定:
MainWindow::~MainWindow()
{
delete ui;
}
// 該函式將label控制元件變成一個圓形指示燈,需要指定顏色color以及直徑size
// color 0:grey 1:red 2:green 3:yellow
// size 單位是畫素
void MainWindow::SetLed(QLabel* label, int color, int size,int type)
{
// 將label中的文字清空
label->setText(“”);
// 先設定矩形大小
// 如果ui介面設定的label大小比最小寬度和高度小,矩形將被設定為最小寬度和最小高度;
// 如果ui介面設定的label大小比最小寬度和高度大,矩形將被設定為最大寬度和最大高度;
QString min_width = QString(“min-width: %1px;”)。arg(size); // 最小寬度:size
QString min_height = QString(“min-height: %1px;”)。arg(size); // 最小高度:size
QString max_width = QString(“max-width: %1px;”)。arg(size); // 最小寬度:size
QString max_height = QString(“max-height: %1px;”)。arg(size); // 最小高度:size
// 再設定邊界形狀及邊框
QString border_radius = QString(“border-radius: %1px;”)。arg(size/2); // 邊框是圓角,半徑為size/2
QString border; // 邊框為1px黑色
// 最後設定背景顏色
QString background = “background-color:”;
switch (color) {
case 0:
// 灰色
if(type == 1)
border = QString(“border:2px solid rgb(255,0,0);”);
else if(type ==2)
border = QString(“border:2px solid rgb(0,255,0);”);
else if(type ==3)
border = QString(“border:2px solid rgb(255,255,0);”);
break;
case 1:
// 紅色
background += “rgb(255,0,0)”;
border = QString(“border:1px solid red;”);
break;
case 2:
// 綠色
background += “rgb(0,255,0)”;
border = QString(“border:1px solid green;”);
break;
case 3:
// 黃色
background += “rgb(255,255,0)”;
border = QString(“border:1px solid yellow;”);
break;
case 4:
// 黃色
background += “rgb(255,255,255)”;
break;
default:
break;
}
const QString SheetStyle = min_width + min_height + max_width + max_height + border_radius + border + background;
label->setStyleSheet(SheetStyle);
}
介面繪製:
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen;
pen。setColor(Qt::black);
pen。setWidth(3);
painter。setPen(pen);
painter。drawLine(370,50,370,190);//畫直線
painter。drawLine(230,190,370,190);//畫直線
int n = 390;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(n,50,n,80);//畫直線
n+=20;
}
n = 210;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(230,n,260,n);//畫直線
n+=20;
}
painter。drawLine(550,50,550,190);//畫直線
painter。drawLine(550,190,690,190);//畫直線
painter。drawLine(370,350,370,490);//畫直線
painter。drawLine(230,350,370,350);//畫直線
n = 390;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(n,460,n,490);//畫直線
n+=20;
}
painter。drawLine(550,350,550,490);//畫直線
painter。drawLine(550,350,690,350);//畫直線
n = 210;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(670,n,690,n);//畫直線
n+=20;
}
}
各方向紅綠黃燈:
void MainWindow::Init()
{
//南北等待
//North
SetLed(ui->red_n,1,16);
SetLed(ui->yellow_n,0,16,3);
SetLed(ui->green_n,0,16,2);
SetLed(ui->p_red_n_1,0,16,1);
SetLed(ui->p_grean_n_1,2,16);
SetLed(ui->p_red_n_2,0,16,1);
SetLed(ui->p_grean_n_2,2,16);
SetLed(ui->p_red_s_1,0,16,1);
SetLed(ui->p_grean_s_1,2,16);
SetLed(ui->p_red_s_2,0,16,1);
SetLed(ui->p_grean_s_2,2,16);
SetLed(ui->p_red_w_1,1,16);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_red_w_2,1,16);
SetLed(ui->p_grean_w_2,0,16,2);
SetLed(ui->p_red_e_1,1,16);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_red_e_2,1,16);
SetLed(ui->p_grean_e_2,0,16,2);
//South
SetLed(ui->red_s,1,16);
SetLed(ui->yellow_s,0,16,3);
SetLed(ui->green_s,0,16,2);
//東西通行
//East
SetLed(ui->red_e,0,16,1);
SetLed(ui->yellow_e,0,16,3);
SetLed(ui->green_e,2,16);
//West
SetLed(ui->red_w,0,16,1);
SetLed(ui->yellow_w,0,16,3);
SetLed(ui->green_w,2,16);
}
交通燈亮滅時間的判斷:
void MainWindow::LightTime()
{
//初始化變數
count_red1 = m_nmainTime;
count_green1 = -1;
count_yellow1 = 3;
count_red2 = -1;
count_green2 = m_nmainTime;
count_yellow2 = 3;
//顯示紅綠燈時間
ui->lcd_e->display(count_green2);
ui->lcd_n->display(count_red1);
myTimer = new QTimer();
//myTimer->start(1000); //啟動QTimer
connect(myTimer,SIGNAL(timeout()),
this,SLOT(doProcessTimeOut1()));//繫結槽
connect(myTimer,SIGNAL(timeout()),
this,SLOT(doProcessTimeOut2()));//繫結槽
}
//南北
void MainWindow::doProcessTimeOut1()
{
if(count_red1 < 6 && count_red1 != -1)
{
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->yellow_n,3,16); //green
SetLed(ui->yellow_s,3,16); //green
sleep(1000);
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->yellow_s,0,16,3); //green
}
//如果紅燈時間到,黃燈亮
if(count_red1 == 0)
{
count_yellow1 ——;
count_green1 = m_nsecondTime;
// if(m_nClickCnt == 1)
// count_green1 = 60;
// else
// count_green1 = 40;
count_yellow1 = 3;
count_red1 = -1;
ui->lcd_n->display(count_green1);
//重啟定時器
myTimer->start(1000);
}
//如果紅燈還有時間
else if(count_red1 > 0){
count_red1——;
ui->lcd_n->display(count_red1);
SetLed(ui->red_n,1,16); //
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->red_s,1,16); //
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->yellow_s,0,16,3); //green
SetLed(ui->p_red_e_1,1,16);
SetLed(ui->p_red_w_1,1,16);
SetLed(ui->p_red_e_2,1,16);
SetLed(ui->p_red_w_2,1,16);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_grean_e_2,0,16,2);
SetLed(ui->p_grean_w_2,0,16,2);
}
if(count_green1 < 11 && count_green1 != -1)
{
SetLed(ui->p_red_e_1,0,16,1);
SetLed(ui->p_red_w_1,0,16,1);
SetLed(ui->p_red_e_2,0,16,1);
SetLed(ui->p_red_w_2,0,16,1);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_grean_e_2,0,16,2);
SetLed(ui->p_grean_w_2,0,16,2);
sleep(1000);
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_2,2,16);
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_2,2,16);
}
//如果綠燈時間到,黃燈亮
if(count_green1 == 0){
count_yellow1——;
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->red_n,0,16,1); //red
SetLed(ui->red_s,0,16,1); //red
SetLed(ui->yellow_n,3,16); //green
SetLed(ui->yellow_s,3,16); //green
count_green1 = -1;
count_red1 = m_nmainTime;
// if(m_nClickCnt == 1)
// count_red1 = 40;
// else
// count_red1 = 60;
count_yellow1 = 3;
//重啟定時器
myTimer->start(1000);
}
//如果綠燈還有時間
else if(count_green1 > 0){
count_green1——;
ui->lcd_n->display(count_green1);
SetLed(ui->green_n,2,16); //green
SetLed(ui->green_s,2,16); //green
SetLed(ui->red_n,0,16,1); //green
SetLed(ui->red_s,0,16,1); //green
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->yellow_s,0,16,3); //green
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_1,2,16);
SetLed(ui->p_grean_e_2,2,16);
SetLed(ui->p_grean_w_2,2,16);
SetLed(ui->p_red_e_1,0,16,1);
SetLed(ui->p_red_w_1,0,16,1);
SetLed(ui->p_red_e_2,0,16,1);
SetLed(ui->p_red_w_2,0,16,1);
SetLed(ui->p_grean_n_1,0,16,2);
SetLed(ui->p_grean_s_1,0,16,2);
SetLed(ui->p_grean_n_2,0,16,2);
SetLed(ui->p_grean_s_2,0,16,2);
SetLed(ui->p_red_n_1,1,16);
SetLed(ui->p_red_s_1,1,16);
SetLed(ui->p_red_n_2,1,16);
SetLed(ui->p_red_s_2,1,16);
//一會的小車程式碼0
if(carup_state == STOP){
carup_state = RUN;
up_through();
}
}
}
//東西
小車執行:
void MainWindow::sleep(unsigned int msec)
{
QTime newTime = QTime::currentTime()。addMSecs(msec);
while(QTime::currentTime() < newTime)
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
void MainWindow::carup_run()
{
carup_state = RUN;
ui->car_up->setGeometry(ui->car_up->x(),ui->car_up->y(),
ui->car_up->width(),
ui->car_up->height());
//向上走
for(int i=ui->car_up->y();i+ui->car_up->height()>=ui->stopline_s->y()+120;——i)
{
ui->car_up->setGeometry(ui->car_up->x(),i,
ui->car_up->width(),
ui->car_up->height());
sleep(10);
}
if(count_red1>0) carup_stop();
if(count_green1>=0) up_through();
}
void MainWindow::carup_stop()
{
carup_state = STOP;
ui->car_up->setGeometry(ui->car_up->x(),ui->car_up->y(),
ui->car_up->width(),
ui->car_up->height());
}
void MainWindow::up_through()
{
for(int i=ui->car_up->y();i+ui->car_up->height()>0;——i)
{
ui->car_up->setGeometry(ui->car_up->x(),i,
ui->car_up->width(),
ui->car_up->height());
sleep(10);
}
}
void MainWindow::on_btn_start_clicked()
{
//啟動定時器
myTimer->start(1000);
}
void MainWindow::on_btn_stop_clicked()
{
//停止定時器
myTimer->stop();
}
void MainWindow::on_btn_change_clicked()
{
// if(m_nClickCnt >=1)
// {
// m_nClickCnt = 0;
// }else
// {
// m_nClickCnt +=1;
// }
m_nmainTime = ui->lineEdit_mainTime->text()。toInt();
m_nsecondTime = ui->lineEdit_secondTime->text()。toInt();
}
void MainWindow::on_btn_car_start_clicked()
{
carup_run();
}
3 除錯過程
透過平時所學知識、查詢資料,利用QT軟體畫圖、並且編寫好程式實現交通燈的功能,再把QT檔案放到虛擬機器裡執行。
4 功能測試
4。1 測試儀器與裝置
Qt Creator軟體,VMware WoekstationPro軟體。
4。2 效能指標測試
(1)在十字交叉路口,東南西北各方向都設定紅、黃、綠色訊號燈,紅燈亮表示禁止通行,綠燈亮表示可以通行,紅燈滅之前5秒鐘黃燈開始閃爍直到綠燈亮起後黃燈熄滅。其中東西方向為主幹道,南北方向為次幹道,各個方向分別設定兩位數碼管,用來顯示紅燈和綠燈倒計時間,東西方向時間一致,南北方向時間一致。
(2)啟動時主幹道為60秒倒計時,次幹道為40秒倒計時。
(3)單獨設計人行道指示燈標誌,當禁止行走時為紅燈,當可以橫穿馬路時,綠燈亮起,在禁止通行之前10秒鐘綠燈開始閃爍(以警示行人),最終紅燈亮起綠燈熄滅。
5 實訓心得體會
本次實訓做的是利用QT軟體設計交通燈系統,設計好介面並且編寫好程式,放入虛擬機器中執行。本次的實訓跟上次的實訓有些關係,都需要用到虛擬機器去執行。本次實訓還用到了新的軟體QT,程式設計的語言用的是C++,是我沒學過的一種新語言,程式設計起來十分的困難,有很多不懂的地方,實訓中需要用到的函式都需要一個一個的百度查詢才能弄懂函式的作用。
雖說跟C語言有些類似,但是我感覺到差別還是挺大的,這是第一次接觸C++。交通燈系統的介面利用標籤和數碼管以及一些控制元件來組成,並且編寫相對應的程式來實現。
這次的實訓讓自己再一次的感受到了自己在程式設計上的知識儲備不足,多掌握一門語言對自己以後的工作有很大的幫助。在今後的學習中,我要更加註重理論知識的學習,希望能懂得更多的知識,並且要理解透徹,能夠把知識為我所用。我會努力加油的!感謝老師和同學給予我的幫助!
6 參考文獻
[1] 嵌入式Linux系統開發與應用/康維新主編。-北京:機械工業出版社,2011。4版。
附錄
附錄1:本設計Qt介面
附錄2:程式清單
mainwindow。h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#define STOP 1
#define RUN 2
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
protected:
void paintEvent(QPaintEvent *event);
public:
QTimer *myTimer;
int m_nMainIndex; //用來計數
int m_nSecondIndex;
//兩組紅綠燈時間引數(1:南北 2:東西)
int count_red1;
int count_green1;
int count_yellow1;
int count_red2;
int count_green2;
int count_yellow2;
int carup_state;//向北行駛小車狀態
int carleft_state;//向西行駛小車狀態
int m_nClickCnt;
bool m_bclick;
int m_nmainTime; //主幹道時間
int m_nsecondTime; //次幹道時間
public:
void SetLed(QLabel* label, int color, int size,int type =0);
void Init();
void LightTime();//紅綠燈時間交替
private slots:
void on_btn_car_start_clicked();
void on_btn_change_clicked();
void on_btn_stop_clicked();
void on_btn_start_clicked();
void doProcessTimeOut1();//南北
void doProcessTimeOut2();//東西
void sleep(unsigned int msec);//延時函式,控制小車速度
void carup_run();//小車正常向北行駛
void carup_stop();//小車向北停止
void up_through();//遇到紅綠燈情況
};
#endif // MAINWINDOW_H
main。cpp:
#include
#include “mainwindow。h”
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w。show();
//將視窗居中顯示
w。move((QApplication::desktop()->width() -w。width())/2,
(QApplication::desktop()->height() -w。height()));
return a。exec();
}
mainwindow。cpp:
#include “mainwindow。h”
#include “ui_mainwindow。h”
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(“Traffic Light”);
m_nClickCnt = 0;
//設定預設時間
ui->lineEdit_mainTime->setText(“60”);
ui->lineEdit_secondTime->setText(“40”);
//獲取初始時間
m_nmainTime = ui->lineEdit_mainTime->text()。toInt();
m_nsecondTime = ui->lineEdit_secondTime->text()。toInt();
Init();
LightTime();
ui->car_up->setPixmap(QPixmap(“://new/prefix1/tu/up。png”));//新增資源
//ui->car_up->setPixmap(QPixmap(“://new/prefix1/picture/up。png”));//新增資源
ui->car_up->setScaledContents(true);//圖片自動適應label大小
}
MainWindow::~MainWindow()
{
delete ui;
}
// 該函式將label控制元件變成一個圓形指示燈,需要指定顏色color以及直徑size
// color 0:grey 1:red 2:green 3:yellow
// size 單位是畫素
void MainWindow::SetLed(QLabel* label, int color, int size,int type)
{
// 將label中的文字清空
label->setText(“”);
// 先設定矩形大小
// 如果ui介面設定的label大小比最小寬度和高度小,矩形將被設定為最小寬度和最小高度;
// 如果ui介面設定的label大小比最小寬度和高度大,矩形將被設定為最大寬度和最大高度;
QString min_width = QString(“min-width: %1px;”)。arg(size); // 最小寬度:size
QString min_height = QString(“min-height: %1px;”)。arg(size); // 最小高度:size
QString max_width = QString(“max-width: %1px;”)。arg(size); // 最小寬度:size
QString max_height = QString(“max-height: %1px;”)。arg(size); // 最小高度:size
// 再設定邊界形狀及邊框
QString border_radius = QString(“border-radius: %1px;”)。arg(size/2); // 邊框是圓角,半徑為size/2
QString border; // 邊框為1px黑色
// 最後設定背景顏色
QString background = “background-color:”;
switch (color) {
case 0:
// 灰色
if(type == 1)
border = QString(“border:2px solid rgb(255,0,0);”);
else if(type ==2)
border = QString(“border:2px solid rgb(0,255,0);”);
else if(type ==3)
border = QString(“border:2px solid rgb(255,255,0);”);
break;
case 1:
// 紅色
background += “rgb(255,0,0)”;
border = QString(“border:1px solid red;”);
break;
case 2:
// 綠色
background += “rgb(0,255,0)”;
border = QString(“border:1px solid green;”);
break;
case 3:
// 黃色
background += “rgb(255,255,0)”;
border = QString(“border:1px solid yellow;”);
break;
case 4:
// 黃色
background += “rgb(255,255,255)”;
break;
default:
break;
}
const QString SheetStyle = min_width + min_height + max_width + max_height + border_radius + border + background;
label->setStyleSheet(SheetStyle);
}
void MainWindow::Init()
{
//南北等待
//North
SetLed(ui->red_n,1,16);
SetLed(ui->yellow_n,0,16,3);
SetLed(ui->green_n,0,16,2);
SetLed(ui->p_red_n_1,0,16,1);
SetLed(ui->p_grean_n_1,2,16);
SetLed(ui->p_red_n_2,0,16,1);
SetLed(ui->p_grean_n_2,2,16);
SetLed(ui->p_red_s_1,0,16,1);
SetLed(ui->p_grean_s_1,2,16);
SetLed(ui->p_red_s_2,0,16,1);
SetLed(ui->p_grean_s_2,2,16);
SetLed(ui->p_red_w_1,1,16);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_red_w_2,1,16);
SetLed(ui->p_grean_w_2,0,16,2);
SetLed(ui->p_red_e_1,1,16);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_red_e_2,1,16);
SetLed(ui->p_grean_e_2,0,16,2);
//South
SetLed(ui->red_s,1,16);
SetLed(ui->yellow_s,0,16,3);
SetLed(ui->green_s,0,16,2);
//東西通行
//East
SetLed(ui->red_e,0,16,1);
SetLed(ui->yellow_e,0,16,3);
SetLed(ui->green_e,2,16);
//West
SetLed(ui->red_w,0,16,1);
SetLed(ui->yellow_w,0,16,3);
SetLed(ui->green_w,2,16);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen;
pen。setColor(Qt::black);
pen。setWidth(3);
painter。setPen(pen);
painter。drawLine(370,50,370,190);//畫直線
painter。drawLine(230,190,370,190);//畫直線
int n = 390;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(n,50,n,80);//畫直線
n+=20;
}
n = 210;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(230,n,260,n);//畫直線
n+=20;
}
painter。drawLine(550,50,550,190);//畫直線
painter。drawLine(550,190,690,190);//畫直線
painter。drawLine(370,350,370,490);//畫直線
painter。drawLine(230,350,370,350);//畫直線
n = 390;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(n,460,n,490);//畫直線
n+=20;
}
painter。drawLine(550,350,550,490);//畫直線
painter。drawLine(550,350,690,350);//畫直線
n = 210;
for(int i = 0; i < 8 ;i ++)
{
painter。drawLine(670,n,690,n);//畫直線
n+=20;
}
}
void MainWindow::LightTime()
{
//初始化變數
count_red1 = m_nmainTime;
count_green1 = -1;
count_yellow1 = 3;
count_red2 = -1;
count_green2 = m_nmainTime;
count_yellow2 = 3;
//顯示紅綠燈時間
ui->lcd_e->display(count_green2);
ui->lcd_n->display(count_red1);
myTimer = new QTimer();
//myTimer->start(1000); //啟動QTimer
connect(myTimer,SIGNAL(timeout()),
this,SLOT(doProcessTimeOut1()));//繫結槽
connect(myTimer,SIGNAL(timeout()),
this,SLOT(doProcessTimeOut2()));//繫結槽
}
//南北
void MainWindow::doProcessTimeOut1()
{
if(count_red1 < 6 && count_red1 != -1)
{
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->yellow_n,3,16); //green
SetLed(ui->yellow_s,3,16); //green
sleep(1000);
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->yellow_s,0,16,3); //green
}
//如果紅燈時間到,黃燈亮
if(count_red1 == 0)
{
count_yellow1 ——;
count_green1 = m_nsecondTime;
// if(m_nClickCnt == 1)
// count_green1 = 60;
// else
// count_green1 = 40;
count_yellow1 = 3;
count_red1 = -1;
ui->lcd_n->display(count_green1);
//重啟定時器
myTimer->start(1000);
}
//如果紅燈還有時間
else if(count_red1 > 0){
count_red1——;
ui->lcd_n->display(count_red1);
SetLed(ui->red_n,1,16); //
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->red_s,1,16); //
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->yellow_s,0,16,3); //green
SetLed(ui->p_red_e_1,1,16);
SetLed(ui->p_red_w_1,1,16);
SetLed(ui->p_red_e_2,1,16);
SetLed(ui->p_red_w_2,1,16);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_grean_e_2,0,16,2);
SetLed(ui->p_grean_w_2,0,16,2);
}
if(count_green1 < 11 && count_green1 != -1)
{
SetLed(ui->p_red_e_1,0,16,1);
SetLed(ui->p_red_w_1,0,16,1);
SetLed(ui->p_red_e_2,0,16,1);
SetLed(ui->p_red_w_2,0,16,1);
SetLed(ui->p_grean_e_1,0,16,2);
SetLed(ui->p_grean_w_1,0,16,2);
SetLed(ui->p_grean_e_2,0,16,2);
SetLed(ui->p_grean_w_2,0,16,2);
sleep(1000);
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_2,2,16);
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_2,2,16);
}
//如果綠燈時間到,黃燈亮
if(count_green1 == 0){
count_yellow1——;
SetLed(ui->green_n,0,16,2); //green
SetLed(ui->green_s,0,16,2); //green
SetLed(ui->red_n,0,16,1); //red
SetLed(ui->red_s,0,16,1); //red
SetLed(ui->yellow_n,3,16); //green
SetLed(ui->yellow_s,3,16); //green
count_green1 = -1;
count_red1 = m_nmainTime;
// if(m_nClickCnt == 1)
// count_red1 = 40;
// else
// count_red1 = 60;
count_yellow1 = 3;
//重啟定時器
myTimer->start(1000);
}
//如果綠燈還有時間
else if(count_green1 > 0){
count_green1——;
ui->lcd_n->display(count_green1);
SetLed(ui->green_n,2,16); //green
SetLed(ui->green_s,2,16); //green
SetLed(ui->red_n,0,16,1); //green
SetLed(ui->red_s,0,16,1); //green
SetLed(ui->yellow_n,0,16,3); //green
SetLed(ui->yellow_s,0,16,3); //green
SetLed(ui->p_grean_e_1,2,16);
SetLed(ui->p_grean_w_1,2,16);
SetLed(ui->p_grean_e_2,2,16);
SetLed(ui->p_grean_w_2,2,16);
SetLed(ui->p_red_e_1,0,16,1);
SetLed(ui->p_red_w_1,0,16,1);
SetLed(ui->p_red_e_2,0,16,1);
SetLed(ui->p_red_w_2,0,16,1);
SetLed(ui->p_grean_n_1,0,16,2);
SetLed(ui->p_grean_s_1,0,16,2);
SetLed(ui->p_grean_n_2,0,16,2);
SetLed(ui->p_grean_s_2,0,16,2);
SetLed(ui->p_red_n_1,1,16);
SetLed(ui->p_red_s_1,1,16);
SetLed(ui->p_red_n_2,1,16);
SetLed(ui->p_red_s_2,1,16);
//一會的小車程式碼0
if(carup_state == STOP){
carup_state = RUN;
up_through();
}
}
}
//東西
void MainWindow::doProcessTimeOut2()
{
//如果綠燈時間到,黃燈亮
if(count_green2< 11 && count_green2 != -1)
{
SetLed(ui->p_red_n_1,0,16,1);
SetLed(ui->p_red_s_1,0,16,1);
SetLed(ui->p_red_s_2,0,16,1);
SetLed(ui->p_red_n_2,0,16,1);
SetLed(ui->p_grean_n_1,0,16,2);
SetLed(ui->p_grean_s_1,0,16,2);
SetLed(ui->p_grean_s_2,0,16,2);
SetLed(ui->p_grean_n_2,0,16,2);
sleep(350);
SetLed(ui->p_grean_s_1,2,16);
SetLed(ui->p_grean_s_2,2,16);
SetLed(ui->p_grean_n_1,2,16);
SetLed(ui->p_grean_n_2,2,16);
}
if(count_green2 == 0){
count_yellow2——;
count_green2 = -1;
count_yellow2 = 3;
// if(m_nClickCnt == 1)
// count_red2 = 60;
// else
// count_red2 = 40;
count_red2 = m_nsecondTime;
//重啟定時器
myTimer->start(1000);
}
//如果綠燈還有時間
else if(count_green2 > 0){
count_green2——;
ui->lcd_e->display(count_green2);
SetLed(ui->green_e,2,16); //green
SetLed(ui->green_w,2,16); //green
SetLed(ui->red_e,0,16,1); //green
SetLed(ui->red_w,0,16,1); //green
SetLed(ui->yellow_e,0,16,3); //green
SetLed(ui->yellow_w,0,16,3); //green
SetLed(ui->p_grean_n_1,2,16);
SetLed(ui->p_grean_s_1,2,16);
SetLed(ui->p_grean_n_2,2,16);
SetLed(ui->p_grean_s_2,2,16);
SetLed(ui->p_red_n_1,0,16,1);
SetLed(ui->p_red_s_1,0,16,1);
SetLed(ui->p_red_s_2,0,16,1);
SetLed(ui->p_red_n_2,0,16,1);
//一會小車程式碼
}
//如果紅燈時間到,黃燈亮
if(count_red2<6 && count_red2 != -1)
{
SetLed(ui->green_e,0,16,2); //green
SetLed(ui->green_w,0,16,2); //green
SetLed(ui->yellow_e,3,16); //green
SetLed(ui->yellow_w,3,16); //green
sleep(350);
SetLed(ui->yellow_e,0,16,3); //green
SetLed(ui->yellow_w,0,16,3); //green
}
if(count_red2 == 0){
count_yellow2——;
SetLed(ui->green_e,0,16,2); //green
SetLed(ui->green_w,0,16,2); //green
SetLed(ui->red_e,0,16,1); //green
SetLed(ui->red_w,0,16,1); //green
SetLed(ui->yellow_e,3,16); //green
SetLed(ui->yellow_w,3,16); //green
// if(m_nClickCnt == 1)
// {
// count_green2 =40;
// }
// else
// count_green2 = 60;
count_green2 = m_nmainTime;
count_red2 = -1;
count_yellow2 = 3;
ui->lcd_e->display(count_green2);
//重啟定時器
myTimer->start(1000);
}
//如果紅燈還有時間
else if(count_red2 > 0){
count_red2——;
ui->lcd_e->display(count_red2);
SetLed(ui->green_e,0,16,2); //green
SetLed(ui->green_w,0,16,2); //green
SetLed(ui->red_e,1,16); //green
SetLed(ui->red_w,1,16); //green
SetLed(ui->yellow_e,0,16,3); //green
SetLed(ui->yellow_w,0,16,3); //green
SetLed(ui->p_grean_n_1,0,16,2);
SetLed(ui->p_grean_s_1,0,16,2);
SetLed(ui->p_grean_n_2,0,16,2);
SetLed(ui->p_grean_s_2,0,16,2);
SetLed(ui->p_red_n_1,1,16);
SetLed(ui->p_red_s_1,1,16);
SetLed(ui->p_red_s_2,1,16);
SetLed(ui->p_red_n_2,1,16);
}
}
void MainWindow::sleep(unsigned int msec)
{
QTime newTime = QTime::currentTime()。addMSecs(msec);
while(QTime::currentTime() < newTime)
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
void MainWindow::carup_run()
{
carup_state = RUN;
ui->car_up->setGeometry(ui->car_up->x(),ui->car_up->y(),
ui->car_up->width(),
ui->car_up->height());
//向上走
for(int i=ui->car_up->y();i+ui->car_up->height()>=ui->stopline_s->y()+120;——i)
{
ui->car_up->setGeometry(ui->car_up->x(),i,
ui->car_up->width(),
ui->car_up->height());
sleep(10);
}
if(count_red1>0) carup_stop();
if(count_green1>=0) up_through();
}
void MainWindow::carup_stop()
{
carup_state = STOP;
ui->car_up->setGeometry(ui->car_up->x(),ui->car_up->y(),
ui->car_up->width(),
ui->car_up->height());
}
void MainWindow::up_through()
{
for(int i=ui->car_up->y();i+ui->car_up->height()>0;——i)
{
ui->car_up->setGeometry(ui->car_up->x(),i,
ui->car_up->width(),
ui->car_up->height());
sleep(10);
}
}
void MainWindow::on_btn_start_clicked()
{
//啟動定時器
myTimer->start(1000);
}
void MainWindow::on_btn_stop_clicked()
{
//停止定時器
myTimer->stop();
}
void MainWindow::on_btn_change_clicked()
{
// if(m_nClickCnt >=1)
// {
// m_nClickCnt = 0;
// }else
// {
// m_nClickCnt +=1;
// }
m_nmainTime = ui->lineEdit_mainTime->text()。toInt();
m_nsecondTime = ui->lineEdit_secondTime->text()。toInt();
}
void MainWindow::on_btn_car_start_clicked()
{
carup_run();
}
推薦文章
- 原來刪除word空白頁這麼簡單!職場人員現在知道也不遲
其實【backspace回退鍵】是往前刪除,而在最後的空白頁需要使用【Delete刪除鍵】來解決...
- 我把你捧在手心,你卻將我踩在腳底,為什麼有些人總遇到渣男渣女
然而現實中我們發現,被寵愛的女生脾氣性格大多都有“作”的特點,她們可以任意哭笑,表達自己的不滿,而那些總被男生傷害的女生大多數為懂事體貼的女孩子,她們願意為愛情全心付出不求回報,她們更在乎經營自己的愛情...
- 19款不長脂肪粒的眼霜介紹,不長脂肪粒眼霜推薦?
這19款眼霜分別為:①蘭蔻補水眼霜、②蘭芝致美煥顏緊緻眼霜、③蘭可欣緊緻按摩眼霜、④韓後眼霜、⑤東方嬋妝眼霜 、⑥歐詩漫走珠眼霜 、⑦雅詩蘭黛小棕瓶眼霜、⑧珀萊雅海泉保溼水漾眼霜、⑨九葉草眼霜、⑩奧洛菲眼霜、相宜本草緊緻滋養眼霜、笛愛兒眼霜...