go leds


原文链接: go leds

AIO-3288J 开发板上有 2 个 LED 灯,如下表所示:

LED GPIO ref. GPIO number
Blue GPIO8_A1 257
Yellow GPIO8_A2 258
可通过使用 LED 设备子系统或者直接操作 GPIO 控制该 LED。

以设备的方式控制 LED
标准的 Linux 专门为 LED 设备定义了 LED 子系统。 在 AIO-3288J 开发板中的两个 LED 均以设备的形式被定义。

用户可以通过 /sys/class/leds/ 目录控制这两个 LED。

更详细的说明请参考 leds-class.txt 。

开发板上的 LED 的默认状态为:

Blue: 系统上电时打开
Yellow:用户自定义

用户可以通过 echo 向其 brightness属性输入命令控制每一个 LED:

root@rk3399_firefly_box:~ # echo 0 >/sys/class/leds/firefly:blue:power/brightness //蓝灯灭
root@rk3399_firefly_box:~ # echo 1 >/sys/class/leds/firefly:blue:power/brightness //蓝灯亮

用户可以通过 echo 向其 trigger 属性输入命令控制每一个 LED:

root@firefly:~ # echo none >/sys/class/leds/firefly:blue:power/trigger //蓝灯灭
root@firefly:~ # echo default-on >/sys/class/leds/firefly:blue:power/trigger //蓝灯亮
root@firefly:~ # echo timer >/sys/class/leds/firefly:blue:power/trigger //蓝灯闪烁
用户还可以使用 cat 命令获取 trigger 的可用值:

root@firefly:~ # cat /sys/class/leds/firefly:blue:power/trigger
none kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock ir-power-click mmc0 mmc1 mmc2 [timer] oneshot default-on rfkill0 rfkill1 rfkill2 rfkill3

func LedWrite(led string, value []byte) {
	f, err := os.OpenFile("/sys/class/leds/"+led+"/brightness", os.O_WRONLY, 0666)
	if err != nil {
		log.Fatal(err)
	}
	f.Write(value)
	f.Close()
}

https://github.com/gkiryaziev/go-mqtt-client/blob/master/raspberry/syscore/sys_led.go

package syscore

import (
	"io/ioutil"
	"log"
	"strings"

	mqtt "github.com/eclipse/paho.mqtt.golang"
)

/*
Struct led provides system LED[0,1] control
Topics:
	Subscribe:
		name + "/SYSTEM/LED[0,1]/ACTION		{0, 1, STATUS}
	Publish:
		name + "/SYSTEM/LED[0,1]/STATUS		{0, 1}
Methods:
	Subscribe
	Unsubscribe
	PublishStatus
Functions:
	Set trigger to [none] when subscribe
		echo none | sudo tee /sys/class/leds/led0/trigger
	Set trigger to [mmc0] when unsubscribe
		echo mmc0 | sudo tee /sys/class/leds/led0/trigger
	Set brightness to 1 when ON
		echo 1 | sudo tee /sys/class/leds/led0/brightness
	Set brightness to 0 when OFF
		echo 0 | sudo tee /sys/class/leds/led0/brightness
	Get brightness status
		sudo cat /sys/class/leds/led0/brightness
TODO:
[ ] catch errors in ledMessageHandler
*/
type led struct {
	client mqtt.Client
	debug  bool
	topic  string
	status string
	ledID  string
}

// a[len(a)-1:] last char

// newLed return new led object.
func newLed(c mqtt.Client, topic string, debug bool) *led {
	return &led{
		client: c,
		debug:  debug,
		topic:  topic,
		status: "0",
		ledID:  topic[len(topic)-1:],
	}
}

// Subscribe to topic
func (l *led) Subscribe(qos byte) {

	topic := l.topic + "/ACTION"

	log.Println("[RUN] Subscribing:", qos, topic)

	if token := l.client.Subscribe(topic, qos, l.ledMessageHandler); token.Wait() && token.Error() != nil {
		log.Println(token.Error())
	}
}

// UnSubscribe from topic
func (l *led) UnSubscribe() {

	topic := l.topic + "/ACTION"

	log.Println("[RUN] UnSubscribing:", topic)

	if token := l.client.Unsubscribe(topic); token.Wait() && token.Error() != nil {
		log.Println(token.Error())
	}
}

// PublishStatus Led status
func (l *led) PublishStatus(qos byte) {

	topic := l.topic + "/STATUS"

	// publish result
	if token := l.client.Publish(topic, qos, false, l.status); token.Wait() && token.Error() != nil {
		log.Println(token.Error())
	}

	// debug
	if l.debug {
		log.Println("[PUB]", qos, topic, l.status)
	}
}

// ledMessageHandler set Led to ON or OFF and get STATUS
func (l *led) ledMessageHandler(client mqtt.Client, msg mqtt.Message) {

	// debug
	if l.debug {
		log.Println("[SUB]", msg.Qos(), msg.Topic(), string(msg.Payload()))
	}

	// receive message and DO
	switch string(msg.Payload()) {
	case "0":
		// logic when OFF
		setBrightness(l.ledID, string(msg.Payload()))
		l.status, _ = getBrightness(l.ledID)
		l.PublishStatus(0)
	case "1":
		// logic when ON
		setBrightness(l.ledID, string(msg.Payload()))
		l.status, _ = getBrightness(l.ledID)
		l.PublishStatus(0)
	case "STATUS":
		// publish status
		l.status, _ = getBrightness(l.ledID)
		l.PublishStatus(0)
	}
}

// getBrightness
func getBrightness(ledID string) (string, error) {
	dat, err := ioutil.ReadFile("/sys/class/leds/led" + ledID + "/brightness")
	if err != nil {
		return "", err
	}

	return strings.Trim(string(dat), "\r\n"), nil
}

// setBrightness
func setBrightness(ledID, data string) error {
	err := ioutil.WriteFile("/sys/class/leds/led"+ledID+"/brightness", []byte(data), 0644)
	if err != nil {
		return err
	}
	return nil
}
`