Hello everyone, still learning golang and tried to play around with an LED on a custom cape. I am perplexed by the following issue I am facing when doing a simple blink example:
When I used
maps to store the LedDrivers of each GPIO corresponding to the colour, the code worked perfectly, was able to get my LED to blink.
package main
import (
)
// BBB LED Pins
const gpioRed = "P8_9" // GPIO_69
const gpioGreen = "P8_12" // GPIO_44
const gpioBlue = "P8_11" // GPIO_45
const gpioPower = "P9_14" // GPIO_50
func main() {
beagleboneAdaptor := beaglebone.NewAdaptor()
ledPower := gpio.NewLedDriver(beagleboneAdaptor, gpioPower)
ledColors := map[string]*gpio.LedDriver{
"red": gpio.NewLedDriver(beagleboneAdaptor, gpioRed),
"green": gpio.NewLedDriver(beagleboneAdaptor, gpioGreen),
"blue": gpio.NewLedDriver(beagleboneAdaptor, gpioBlue),
}
work := func() {
// init
ledPower.Off()
ledColors["red"].On()
ledColors["blue"].On()
ledColors["green"].On()
//
ledColorSwitch("blue", ledColors)
gobot.Every(1*time.Second, func() {
ledPower.Toggle()
})
}
robot := gobot.NewRobot("blinkBot",
[]gobot.Connection{beagleboneAdaptor},
[]gobot.Device{ledPower, ledColors["red"], ledColors["green"], ledColors["blue"]},
work,
)
robot.Start()
}
func ledColorSwitch(color string, mLed map[string]*gpio.LedDriver) {
switch color {
case "red":
mLed["red"].Off()
case "green":
mLed["green"].Off()
case "blue":
mLed["blue"].Off()
case "cyan":
mLed["red"].Off()
mLed["green"].Off()
case "magenta":
mLed["red"].Off()
mLed["blue"].Off()
case "yellow":
mLed["green"].Off()
mLed["blue"].Off()
case "white":
mLed["red"].Off()
mLed["green"].Off()
mLed["blue"].Off()
default:
}
}
However when I tried to use an array, nothing seemed to work. I tried to read up on the data types but i could not get any idea where i went wrong still. Trying to understand where i went wrong or if i am not using it correctly.
package main
import (
)
// BBB LED Pins
const pinRed = "P8_9" // GPIO_69
const pinGreen = "P8_12" // GPIO_44
const pinBlue = "P8_11" // GPIO_45
const pinPower = "P9_14" // GPIO_50
func main() {
beagleboneAdaptor := beaglebone.NewAdaptor()
gpioPower := gpio.NewLedDriver(beagleboneAdaptor, pinPower)
gpioColorArray := [3]*gpio.LedDriver{
gpio.NewLedDriver(beagleboneAdaptor, pinRed),
gpio.NewLedDriver(beagleboneAdaptor, pinGreen),
gpio.NewLedDriver(beagleboneAdaptor, pinBlue),
}
work := func() {
// init
gpioPower.Off()
gpioColorArray[0].On()
gpioColorArray[1].On()
gpioColorArray[2].On()
// blue LED
gpioColorArray[2].Off()
gobot.Every(1*time.Second, func() {
gpioPower.Toggle()
})
}
robot := gobot.NewRobot("blinkBot",
[]gobot.Connection{beagleboneAdaptor},
[]gobot.Device{gpioPower, gpioColorArray[0], gpioColorArray[1], gpioColorArray[2]},
work,
)
robot.Start()
}
Thank you!