树莓派3B 之UI

这篇博客主要介绍了如何结合GPIO/IIC/UART在树莓派3B上创建一个上位机,并重点讲解了使用Qt通过拖拽控件方式实现UI的部分,涉及到代码包含*.cpp和*.h文件,同时利用定时器进行线程运行。
摘要由CSDN通过智能技术生成

今天这一篇我们主要讲如何把前面的GPIO/IIC/UART综合起来。

做成一个上位机。

前面的部分不用讲了,今天主要讲UI部分。

Qt我们使用的是拖拽控件的方式。

下面是实现的代码部分:

***.cpp

#include "tw_lightingui.h"
#include "ui_tw_lightingui.h"
#include "bsp_key.h"
#include "uart.h"
#include "jy901_Init.h"
#include "QMutex"
#include <QString>

//------------------------------Button define value set------------------------------------
#define SEL_FUN_HCALI              0        //option of LRTRAP
#define SEL_FUN_VCALI              1        //option of UDTRAP
#define SEL_FUN_ZROTA              2        //option of FRAMEROLL
#define SEL_FUN_DIGFOCUS           3        //option of UDSHIFT
#define SEL_FUN_UNIFOBRI           4        //option of LRSHIFT
#define SEL_FUN_HLINEAR            5        //option of LRLINEAR
#define SEL_FUN_VLINEAR            6        //option of UDLINEAR
#define SEL_OPT_CALILOCA           7        //option of LOCATE
#define SEL_OPT_RECTIFY            8        //option of RECTIFY
#define SEL_OPT_RESET              9        //option of RESET
#define SEL_OPT_LIGHRASPSW         10       //switch of SWITCH_FUNCTION

#define SEL_SW_HCALI               11       //switch of LTSW
#define SEL_SW_VCALI               12       //switch of UTSW
#define SEL_SW_ZROTA               13       //switch of FRSW
#define SEL_SW_DIGFO               14       //switch of SPSW
#define SEL_SW_UNIFOBRI            15       //switch of BRSW
#define SEL_SW_HLINEAR             16       //switch of LRSW
#define SEL_SW_VLINEAR             17       //switch of URSW

#define SEL_SW_FPCORR              18       //option of FPCORRSW
#define SEL_POINTA                 19       //option of POINTA
#define SEL_POINTB                 20       //option of POINTB
#define SEL_POINTC                 21       //option of POINTC
#define SEL_POINTD                 22       //option of POINTD
#define SEL_DFTVAL                 23       //option of DFTVAL
#define SEL_RETVAL                 24       //option of RETVAL

#define SEL_GYRO_OPT               25       //option of GYRO

extern float x_angle;  // roll  src data
extern float y_angle;  // pitch src data
extern float z_angle;  // yaw   src data

//MODEL
//unsigned char hex_send[] = {0x55, 0xAA, 0x55, 0xAA,             // head     4bytes    [0-3]
//                            0x01,                               // cmd      1byte     [4]
//                            0x45,                               // Func_val 1byte     [5]
//                            0xFF,0x9B,                          // X_val    2bytes    [6-7]
//                            0x00,0x7B,                          // Y_val    2bytes    [8-9]
//                            0xFF,0xAB,                          // Z_val    2bytes    [10-11]
//                            0x00,0x07};                         // CRC      2bytes    [12-13]

unsigned char hexData_send[14] = {0x55, 0xAA, 0x55, 0xAA};        //The whole string


static unsigned char   opt_fun = 0;         //value of button press option

static signed char  value_hCali     = 0;
static signed char  value_vCali     = 0;
static signed char  value_zRota     = 0;
static signed char  value_digFo     = 0;
static signed char  value_unifoBri  = 0;
static signed char  value_hLinear   = 0;
static signed char  value_vLinear   = 0;

static signed char  value_PointAX   = 0;
static signed char  value_PointAY   = 0;
static signed char  value_PointBX   = 0;
static signed char  value_PointBY   = 0;
static signed char  value_PointCX   = 0;
static signed char  value_PointCY   = 0;
static signed char  value_PointDX   = 0;
static signed char  value_PointDY   = 0;

bool ValOpt_Rectify = false;        //flag to rectify
bool status_fpcorr = false;         //flag to FPCorr

//reocrd ok position
signed short value_localx = 0;
signed short value_localy = 0;
signed short value_localz = 0;
//-----------------------------------------------------------------------------------------

TW_LightingUI::TW_LightingUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TW_LightingUI)
{
    ui->setupUi(this);

    timer_gyro = new QTimer(this);
    connect(timer_gyro, SIGNAL(timeout()), this, SLOT(on_GYRO_clicked()));
    timer_gyro->start(40);

    timer_senddata = new QTimer(this);
    connect(timer_senddata, SIGNAL(timeout()), this, SLOT(tw_UIangleDisplay()));
    timer_senddata->start(200);
}

TW_LightingUI::~TW_LightingUI()
{
    delete ui;
}


/*
 * brief:   Get CRC value of all parameter sum. Then convert to hex
 * param:   data_tmp hexData_send except CRC bit
 * return:  value_crc
 * notice:  none
 */
static signed short tw_crcValueCalculate(unsigned char crc_tmp[])
{
    //int i;
    //signed short value_crc;

    typedef struct tw_function
    {
        int             head;
        unsigned char   cmd;
        signed   char   func_val;
        signed  short   x_val;
        signed  short   y_val;
        signed  short   z_val;
        signed  short   crc;
    };

    tw_function tw_function_t;

    tw_function_t.cmd       =  crc_tmp[4];
    tw_function_t.func_val  =  crc_tmp[5];
    tw_function_t.x_val     = (crc_tmp[6] <<8  | crc_tmp[7]);
    tw_function_t.y_val     = (crc_tmp[8] <<8  | crc_tmp[9]);
    tw_function_t.z_val     = (crc_tmp[10]<<8  | crc_tmp[11]);

    tw_function_t.crc = tw_function_t.cmd + tw_function_t.func_val + tw_function_t.x_val + tw_function_t.y_val + tw_function_t.z_val;

    return tw_function_t.crc;
}

/*
 * brief:   Send whole string to uart.
 * param:   string
 * return:  0 fialed   1 success
 * notice:  none
 */
static unsigned char tw_SendHexData(unsigned char str[])
{
    int i;

    for(i = 0; i < 14; i++)
    {
        serial_SendData(str[i]);
    }

    if(i == 13)         //estimate string is integrity
        return 1;
    else
        return 0;
}

/*
 * brief:   When press option button, select function
 * param:   none
 * return:  none
 * notice:  none
 */
void tw_optionSelected()
{
    if(Key_PressCheck(SW5_OPTION) == 1)
    {
       opt_fun++;

       if(opt_fun>24)   //overflow to selected,return to 0
           opt_fun = 0;
    }
}

/*
 * brief:   update buttons color, default color is red.
 * param:   none
 * return:  none
 * notice:  none
 */
void TW_LightingUI::tw_button_update()
{
    ui->FUN_HCali->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_VCali->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_ZRota->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_DigFocus->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_UnifoBri->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_HLinear->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->FUN_VLinear->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->OPT_CaliLoca->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->OPT_Rectify->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->OPT_Reset->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->OPT_LighRaspSW->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_HCali->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_VCali->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_ZRota->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_DigFo->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_UnifoBri->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_HLinear->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_VLinear->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->SW_FPCorr->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->POINT_A->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->POINT_B->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->POINT_C->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->POINT_D->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->DEFAULT_VALUE->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->RETURN_BEFORE_VALUE->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
    ui->GYRO->setStyleSheet(
                "QPushButton{background-color:pink}"
                "QPushButton:checked{background-color:red}");
}

/*
 * brief:   Change selected button's color,if selected  button color changed to yellow
 * param:   button value
 * return:  none
 * notice:  none
 */
void TW_LightingUI::tw_button_selected(unsigned char value_button)
{
    switch(value_button)
    {
        case SEL_FUN_HCALI:
            ui->FUN_HCali->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_VCALI:
            ui->FUN_VCali->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_ZROTA:
            ui->FUN_ZRota->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_DIGFOCUS:
            ui->FUN_DigFocus->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_UNIFOBRI:
            ui->FUN_UnifoBri->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_HLINEAR :
            ui->FUN_HLinear->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_FUN_VLINEAR:
            ui->FUN_VLinear->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;


        case SEL_OPT_CALILOCA:
            ui->OPT_CaliLoca->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_OPT_RECTIFY :
            ui->OPT_Rectify->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_OPT_RESET:
            ui->OPT_Reset->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_OPT_LIGHRASPSW:
            ui->OPT_LighRaspSW->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_SW_HCALI:
            ui->SW_HCali->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_SW_VCALI :
            ui->SW_VCali->setStyleSheet(
                        "QPushButton{background-color:yellow}"
                        "QPushButton:checked{background-color:red}");
            break;

        case SEL_SW_ZROTA:
            ui->SW_ZRota->setStyleSheet(
                     
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值