1. 程式人生 > 實用技巧 >【linux】led子系統

【linux】led子系統

目錄


前言

  • 接下來記錄的是 led子系統
  • 目前不涉及驅動原始碼

linux子系統

  • 在 Linux 系統中
    • 絕大多數硬體裝置都有非常成熟的驅動框架
    • 驅動工程師使用這些框架新增與板子相關的硬體支援
      • 建立硬體與Linux核心的聯絡
    • 核心再通過統一檔案系統介面呈現給使用者
    • 使用者通過對應的裝置檔案控制硬體。

led子系統

  • led子系統相關描述可在核心原始碼 Documentation/leds/leds-class.txt 瞭解。
  • led子系統是一個簡單的 linux子系統 ,在目錄 /sys/class/leds 下展示該子系統裝置。
/sys/class/leds下的目錄 對應的LED燈裝置
input2::capslock 鍵盤大寫鎖定指示燈
input2::numlock 鍵盤數字鍵盤指示燈
input2::scrolllock 鍵盤ScrollLock指示燈
  • 一些屬性值

    • brightness
      • brightness 的最大值在 max_brightness 檔案中定義。
      • brightness 的值在 brightness 檔案中定義。
      • 注意:大部分 led 不支援亮度調節。
    • trigger.
      • 常見的觸發方式
        • none:無觸發方式
        • disk-activity:硬碟活動
        • nand-disknand:flash活動
        • mtd:mtd裝置活動
        • timer:定時器
        • heartbeat:系統心跳
      • 檢視觸發方式
        • cat triggerx86平臺
          • 檢視該檔案的內容時,該檔案會 列出它的所有可用觸方式,而當前使用的觸發方式會以“[]”符號括起。

      • 修改觸發方式
        • 例子:echo none > /sys/class/leds/ledA/trigger
          • none:none觸發方式
          • ledA:ledA裝置
        • 修改後,便按新的觸發方式觸發
  • 設計哲學

    • 簡單哈哈
  • 推薦命名格式

    • "裝置名字:顏色:功能"
  • API(後面分析驅動再介紹

    • led_set_brightness
    • led_set_brightness_sync
    • led_classdev_register
    • led_classdev

led子系統實戰-系統呼叫-ARM平臺

  • 先在終端執行

    • 查詢觸發方式:cat trigger注意圖中 []

    • 修改觸發方式 (注意圖中 []

  • 使用系統呼叫方式 APP

    • main.c 檔案
/** @file         main.c
 *  @brief        簡要說明
 *  @details      詳細說明
 *  @author       lzm
 *  @date         2020-11-10 17:01:15
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *
 **********************************************************
 *  @LOG 修改日誌:
 **********************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

//ARM 開發板 LED 裝置的路徑
#define RLED_DEV_PATH "/sys/class/leds/red/brightness"
#define GLED_DEV_PATH "/sys/class/leds/green/brightness"
#define BLED_DEV_PATH "/sys/class/leds/blue/brightness"

int main(int argc, char *argv[])
{
    int res = 0;
    int r_fd;
    int g_fd;
    int b_fd;
    
    printf("this is the led demo\n");

    // 獲取 LED 裝置檔案描述符
    r_fd = open(RLED_DEV_PATH, O_WRONLY);
    if(r_fd < 0){
        printf("Fail to open %s device\n",RLED_DEV_PATH);
        exit(1);
    }
    g_fd = open(GLED_DEV_PATH, O_WRONLY);
    if(g_fd < 0){
        printf("Fail to open %s device\n",GLED_DEV_PATH);
        exit(1);
    }
    b_fd = open(BLED_DEV_PATH, O_WRONLY);
    if(b_fd < 0){
        printf("Fail to open %s device\n",BLED_DEV_PATH);
        exit(1);
    }

    while(1){
        write(r_fd, "255", 3);
        sleep(2);
        write(g_fd, "255", 3);
        sleep(1);
        write(r_fd, "0", 1);
        sleep(2);
        write(g_fd, "0", 1);
        sleep(1);
        write(b_fd, "255", 3);
        sleep(1);
        write(b_fd, "0", 1);
        sleep(1);
    }
}