github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/board/raspberrypi/pi1/led.go (about)

     1  // Raspberry Pi 1 LED support
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) the pi1 package authors
     5  //
     6  // Use of this source code is governed by the license
     7  // that can be found in the LICENSE file.
     8  
     9  package pi1
    10  
    11  import (
    12  	"errors"
    13  
    14  	"github.com/usbarmory/tamago/soc/bcm2835"
    15  )
    16  
    17  // LED GPIO lines
    18  const (
    19  	// Activity LED
    20  	// change the value to 0x10 for older Pi A and Pi B boards, keep the value for newer A+ and B+ boards
    21  	ACTIVITY = 0x2f
    22  	// Power LED - not controllable by the CPU on older Pi A and Pi B boards, only on newer A+ and B+ boards
    23  	POWER = 0x23
    24  )
    25  
    26  var activity *bcm2835.GPIO
    27  var power *bcm2835.GPIO
    28  
    29  func init() {
    30  	var err error
    31  
    32  	activity, err = bcm2835.NewGPIO(ACTIVITY)
    33  
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	power, err = bcm2835.NewGPIO(POWER)
    39  
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	activity.Out()
    45  	power.Out()
    46  }
    47  
    48  // LED turns on/off an LED by name.
    49  func (b *board) LED(name string, on bool) (err error) {
    50  	var led *bcm2835.GPIO
    51  
    52  	switch name {
    53  	case "activity", "Activity", "ACTIVITY":
    54  		led = activity
    55  	case "power", "Power", "POWER":
    56  		led = power
    57  	default:
    58  		return errors.New("invalid LED")
    59  	}
    60  
    61  	if on {
    62  		led.High()
    63  	} else {
    64  		led.Low()
    65  	}
    66  
    67  	return
    68  }