led

package module
v0.0.0-...-d542b48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 3 Imported by: 2

README

LED

Go Reference golangci-lint Go Report Card

This drivers allows interact with a LED connected to a GPIO pin

Documentation

For full documentation, please visit Go Reference

Quick start

import (
	"fmt"
	"time"

	"github.com/raspberrypi-go-drivers/led"
	"github.com/stianeikeland/go-rpio/v4"
)

func main() {
	err := rpio.Open()
	if err != nil {
		os.Exit(1)
	}
	defer rpio.Close()

	l1 := led.NewLED(18)
	for {
		l1.Toggle()
		time.Sleep(1 * time.Second)
		state, _ := l1.GetState()
		fmt.Println(state)
	}
}

Raspberry Pi compatibility

This driver has has only been tested on an Raspberry Pi Zero WH using integrated bluetooth but may work well on other Raspberry Pi having integrated Bluetooth

License

MIT License


Special thanks to @stianeikeland

This driver is based on his work in stianeikeland/go-rpio

Documentation

Overview

Package led is a driver allowing to control a LED from GPIO pins

Example (Dimmable)

The LED will dim from 0% to 100% to 0% continuously

err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.Dimmable()
for {
	for i := 0; i <= 100; i++ {
		if err := l1.SetBrightness(i); err != nil {
			log.WithField("error", err).Error("impossible to change LED brightness")
		}
		time.Sleep(time.Millisecond * 10)
	}
	for i := 100; i >= 0; i-- {
		if err := l1.SetBrightness(i); err != nil {
			log.WithField("error", err).Error("impossible to change LED brightness")
		}
		time.Sleep(time.Millisecond * 10)
	}
}
Output:

Example (Toggle)

The LED will blink continuously and the current LED status will be displayed as output

err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
for {
	l1.Toggle()
	time.Sleep(1 * time.Second)
	state, _ := l1.GetState()
	fmt.Println(state)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LED

type LED struct {
	// contains filtered or unexported fields
}

LED instance

func NewLED

func NewLED(pinID uint8) *LED

NewLED creates a new LED instance

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

// 18 is one of the PWM compatible GPIO pin on the Raspberry Pi
l1 := led.NewLED(18)
l1.On()
Output:

func (*LED) Dimmable

func (led *LED) Dimmable()

Dimmable make the LED dimmable

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.Dimmable()
// LED is now dimmable (PWM mode is enabled)
Output:

func (*LED) GetState

func (led *LED) GetState() (bool, error)

GetState return the current state of the lED

returns: true for on, off for false

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.On()
state, _ := l1.GetState()
fmt.Println(state)
Output:

true

func (*LED) NonDimmable

func (led *LED) NonDimmable()

NonDimmable make the dimmable LED non dimmable

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.Dimmable()
// Here you can use SetBrightness
l1.NonDimmable()
// LED is not dimmable anymore (PWM mode is disabled)
Output:

func (*LED) Off

func (led *LED) Off()

Off switch off the LED

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.Off()
Output:

func (*LED) On

func (led *LED) On()

On switch on the LED

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.On()
Output:

func (*LED) SetBrightness

func (led *LED) SetBrightness(percentage int) error

SetBrightness set the brightness of the LED

If the LED is connected to a hardware PWM pin, SetBrightness will use hardware PWM
If the LED is connected to a non PWM pin, SetBrightness will use a software emulated PWM
Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
l1.Dimmable()
// set LED brightness to 50% of its maximum value
if err := l1.SetBrightness(50); err != nil {
	log.WithField("error", err).Error("impossible to change LED brightness")
}
Output:

func (*LED) Toggle

func (led *LED) Toggle()

Toggle change the state of the LED

Example
err := rpio.Open()
if err != nil {
	os.Exit(1)
}
defer rpio.Close()

l1 := led.NewLED(18)
// Switch on the led if off or swicth on if off
l1.Toggle()
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL