RK3568学习笔记6:LED操作

Dr.Guo
发布于 2024-01-16 / 33 阅读
0
0

RK3568学习笔记6:LED操作

1. LED电路原理

  1. 正点原子已经将LED注册成了GPIO-LEDS类型设备
  2. LED文件 /sys/class/leds/work/brightness
  3. 默认是出厂系统的LED为心跳的触发方式,想要控制LED,需要先关闭心跳,然后再设置为none system("echo none > /sys/class/leds/work/trigger");

2. 程序架构

创建led子文件夹,将该led文件夹添加到父项目中。新建cmake项目led。
父文件夹cmake文件如下

cmake_minimum_required(VERSION 3.16)
project(Rk3568Demos)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

if (WIN32)
    set(CMAKE_PREFIX_PATH "C:\\Qt\\5.15.2\\mingw81_64")
    find_package(Qt5 COMPONENTS
            Core
            Gui
            Widgets
            REQUIRED)
else ()
    set(CMAKE_PREFIX_PATH  "/opt/atk-dlrk356x-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/lib/cmake/")
    find_package(Qt5 COMPONENTS
            Core
            Gui
            Widgets
            REQUIRED
            PATHS "/opt/atk-dlrk356x-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/lib/")
endif ()
add_subdirectory(led)
#add_executable(Rk3568Demos
#        main.cpp)

target_link_libraries(led
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets)

子项目cmake文件

project(led)

add_executable(led main.cpp
        main.cpp
        ledcontrolwidget.cpp
        ledcontrolwidget.h
        ledcontrolwidget.ui)

target_link_libraries(led
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()

Clion的交叉编译按照 https://blog.guoxiaozhong.cn/archives/1705066871554 设置即可。

我的建议是,现在本机把QT的界面和程序逻辑调试好,然后在远程debug 与设备相关的程序。

3. LED操作

LED读取状态操作

void LedControlWidget::GetLedState() {
    if (!this->ledExist) {
        return;
    }
    if (!this->ledFile.open(QIODevice::ReadWrite)) {
        qDebug() << this->ledFile.errorString();
        this->ui->initialButton->setText("Open Led File Failed");
        return;
    }
    this->ui->controlButton->setEnabled(true);
    QTextStream in(&this->ledFile);
    QString line = in.readLine();
    if (line == "0") {
        this->ledState = false;
        this->ui->controlButton->setText("ON");
    } else if (line == "1") {
        this->ledState = true;
        this->ui->controlButton->setText("OFF");
    } else {
        this->ui->initialButton->setText("Led Operate Error");
    }
    this->ledFile.close();
}

LED状态写操作

void LedControlWidget::SetLedState(bool state) {
    if (!this->ledExist) {
        return;
    }
    if (!this->ledFile.open(QIODevice::ReadWrite)) {
        qDebug() << this->ledFile.errorString();
        this->ui->initialButton->setText("Open Led File Failed");
        return;
    }
    QTextStream in(&this->ledFile);
    if (state) {
        in << "1";
        this->ui->controlButton->setText("OFF");
    } else {
        in << "0";
        this->ui->controlButton->setText("ON");
    }
    this->ledFile.close();

4. 实验

远程部署程序
可以看到到LED按照操作亮灭


评论