First implementation
This commit is contained in:
		
							
								
								
									
										173
									
								
								led/led.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								led/led.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,173 @@
 | 
			
		||||
package led
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"periph.io/x/periph/conn/gpio"
 | 
			
		||||
	"periph.io/x/periph/host"
 | 
			
		||||
	"periph.io/x/periph/host/rpi"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	// Load all the drivers:
 | 
			
		||||
	if _, err := host.Init(); err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New() *PiColorLed {
 | 
			
		||||
	return &PiColorLed{
 | 
			
		||||
		pinRed:          rpi.P1_22,
 | 
			
		||||
		pinGreen:        rpi.P1_23,
 | 
			
		||||
		pinBlue:         rpi.P1_24,
 | 
			
		||||
		redValue:        0,
 | 
			
		||||
		greenValue:      0,
 | 
			
		||||
		blueValue:       0,
 | 
			
		||||
		cancelBlinkChan: make(chan interface{}),
 | 
			
		||||
		blinkEnabled:    false,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Led interface {
 | 
			
		||||
	SetBlink(freq float64)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ColoredLed interface {
 | 
			
		||||
	Led
 | 
			
		||||
	SetRed(value int)
 | 
			
		||||
	SetGreen(value int)
 | 
			
		||||
	SetBlue(value int)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type PiColorLed struct {
 | 
			
		||||
	muPinRed, muPinGreen, muPinBlue sync.Mutex
 | 
			
		||||
	pinRed                          gpio.PinIO
 | 
			
		||||
	pinGreen                        gpio.PinIO
 | 
			
		||||
	pinBlue                         gpio.PinIO
 | 
			
		||||
 | 
			
		||||
	muColorValue                    sync.RWMutex
 | 
			
		||||
	redValue, greenValue, blueValue int
 | 
			
		||||
 | 
			
		||||
	cancelBlinkChan chan interface{}
 | 
			
		||||
 | 
			
		||||
	muBlink      sync.Mutex
 | 
			
		||||
	blinkEnabled bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *PiColorLed) SetBlink(freq float64) {
 | 
			
		||||
	l.muBlink.Lock()
 | 
			
		||||
	defer l.muBlink.Unlock()
 | 
			
		||||
	if freq > 0 {
 | 
			
		||||
		if !l.blinkEnabled {
 | 
			
		||||
			l.blinkEnabled = true
 | 
			
		||||
			go l.blink(freq)
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		if l.blinkEnabled {
 | 
			
		||||
			l.blinkEnabled = false
 | 
			
		||||
			l.cancelBlinkChan <- struct{}{}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *PiColorLed) blink(freq float64) {
 | 
			
		||||
	factor := 0
 | 
			
		||||
	ticker := time.NewTicker(time.Duration(float64(time.Second) / freq))
 | 
			
		||||
	red := l.Red()
 | 
			
		||||
	green := l.Green()
 | 
			
		||||
	blue := l.Blue()
 | 
			
		||||
	var tmpR, tmpG, tmpB int
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ticker.C:
 | 
			
		||||
		case <-l.cancelBlinkChan:
 | 
			
		||||
			// Restore values
 | 
			
		||||
			l.SetRed(red)
 | 
			
		||||
			l.SetGreen(green)
 | 
			
		||||
			l.SetBlue(blue)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		tmpR = l.Red()
 | 
			
		||||
		tmpG = l.Green()
 | 
			
		||||
		tmpB = l.Blue()
 | 
			
		||||
		if factor == 1 {
 | 
			
		||||
			// Led is off
 | 
			
		||||
			if tmpR > 0 {
 | 
			
		||||
				red = tmpR
 | 
			
		||||
			}
 | 
			
		||||
			if tmpG > 0 {
 | 
			
		||||
				green = tmpG
 | 
			
		||||
			}
 | 
			
		||||
			if tmpB > 0 {
 | 
			
		||||
				blue = tmpB
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			// Led on: get updated value
 | 
			
		||||
			red = tmpR
 | 
			
		||||
			green = tmpG
 | 
			
		||||
			blue = tmpB
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		l.SetRed(red * factor)
 | 
			
		||||
		l.SetGreen(green * factor)
 | 
			
		||||
		l.SetBlue(blue * factor)
 | 
			
		||||
 | 
			
		||||
		if factor == 0 {
 | 
			
		||||
			factor = 1
 | 
			
		||||
		} else {
 | 
			
		||||
			factor = 0
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
	mutex.Lock()
 | 
			
		||||
	defer mutex.Unlock()
 | 
			
		||||
	lvl := gpio.High
 | 
			
		||||
	if v == 0 {
 | 
			
		||||
		lvl = gpio.Low
 | 
			
		||||
	}
 | 
			
		||||
	err := led.Out(lvl)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("unable to sed pin to %v: %v", lvl, err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *PiColorLed) Red() int {
 | 
			
		||||
	l.muColorValue.RLock()
 | 
			
		||||
	defer l.muColorValue.RUnlock()
 | 
			
		||||
	return l.redValue
 | 
			
		||||
}
 | 
			
		||||
func (l *PiColorLed) SetRed(v int) {
 | 
			
		||||
	setLed(v, l.pinRed, &l.muPinRed)
 | 
			
		||||
	l.muColorValue.Lock()
 | 
			
		||||
	defer l.muColorValue.Unlock()
 | 
			
		||||
	l.redValue = v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *PiColorLed) Green() int {
 | 
			
		||||
	l.muColorValue.RLock()
 | 
			
		||||
	defer l.muColorValue.RUnlock()
 | 
			
		||||
	return l.greenValue
 | 
			
		||||
}
 | 
			
		||||
func (l *PiColorLed) SetGreen(v int) {
 | 
			
		||||
	setLed(v, l.pinGreen, &l.muPinGreen)
 | 
			
		||||
	l.muColorValue.Lock()
 | 
			
		||||
	defer l.muColorValue.Unlock()
 | 
			
		||||
	l.greenValue = v
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *PiColorLed) Blue() int {
 | 
			
		||||
	l.muColorValue.RLock()
 | 
			
		||||
	defer l.muColorValue.RUnlock()
 | 
			
		||||
	return l.blueValue
 | 
			
		||||
}
 | 
			
		||||
func (l *PiColorLed) SetBlue(v int) {
 | 
			
		||||
	setLed(v, l.pinBlue, &l.muPinBlue)
 | 
			
		||||
	l.muColorValue.Lock()
 | 
			
		||||
	defer l.muColorValue.Unlock()
 | 
			
		||||
	l.blueValue = v
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										214
									
								
								led/led_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										214
									
								
								led/led_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,214 @@
 | 
			
		||||
package led
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"periph.io/x/periph/conn/gpio"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestColorLed_Red(t *testing.T) {
 | 
			
		||||
	setLedBackup := setLed
 | 
			
		||||
	defer func() { setLed = setLedBackup }()
 | 
			
		||||
 | 
			
		||||
	var colorValue int
 | 
			
		||||
	setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
		mutex.Lock()
 | 
			
		||||
		defer mutex.Unlock()
 | 
			
		||||
		colorValue = v
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l := New()
 | 
			
		||||
	if l.Red() != 0 {
 | 
			
		||||
		t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 0)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l.SetRed(255)
 | 
			
		||||
	if l.Red() != 255 {
 | 
			
		||||
		t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 255)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestColorLed_Green(t *testing.T) {
 | 
			
		||||
	setLedBackup := setLed
 | 
			
		||||
	defer func() { setLed = setLedBackup }()
 | 
			
		||||
 | 
			
		||||
	var colorValue int
 | 
			
		||||
	setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
		mutex.Lock()
 | 
			
		||||
		defer mutex.Unlock()
 | 
			
		||||
		colorValue = v
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l := New()
 | 
			
		||||
	if l.Green() != 0 {
 | 
			
		||||
		t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 0)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l.SetGreen(255)
 | 
			
		||||
	if l.Green() != 255 {
 | 
			
		||||
		t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 255)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestColorLed_Blue(t *testing.T) {
 | 
			
		||||
	setLedBackup := setLed
 | 
			
		||||
	defer func() { setLed = setLedBackup }()
 | 
			
		||||
 | 
			
		||||
	var colorValue int
 | 
			
		||||
	setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
		mutex.Lock()
 | 
			
		||||
		defer mutex.Unlock()
 | 
			
		||||
		colorValue = v
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l := New()
 | 
			
		||||
	if l.Blue() != 0 {
 | 
			
		||||
		t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 0)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l.SetBlue(255)
 | 
			
		||||
	if l.Blue() != 255 {
 | 
			
		||||
		t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255)
 | 
			
		||||
	}
 | 
			
		||||
	if colorValue != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", colorValue, 255)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestColorLed_SetBlink(t *testing.T) {
 | 
			
		||||
	setLedBackup := setLed
 | 
			
		||||
	defer func() { setLed = setLedBackup }()
 | 
			
		||||
 | 
			
		||||
	var muFakeValue sync.Mutex
 | 
			
		||||
	var colorValue int
 | 
			
		||||
	setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
		mutex.Lock()
 | 
			
		||||
		defer mutex.Unlock()
 | 
			
		||||
		muFakeValue.Lock()
 | 
			
		||||
		defer muFakeValue.Unlock()
 | 
			
		||||
		colorValue = v
 | 
			
		||||
	}
 | 
			
		||||
	readValue := func() int {
 | 
			
		||||
		muFakeValue.Lock()
 | 
			
		||||
		defer muFakeValue.Unlock()
 | 
			
		||||
		return colorValue
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l := New()
 | 
			
		||||
	l.SetBlue(255)
 | 
			
		||||
	v := readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
	l.SetBlink(100)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 0)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Stop blink
 | 
			
		||||
	l.SetBlink(0)
 | 
			
		||||
	time.Sleep(5 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) {
 | 
			
		||||
	setLedBackup := setLed
 | 
			
		||||
	defer func() { setLed = setLedBackup }()
 | 
			
		||||
 | 
			
		||||
	var muFakeValue sync.Mutex
 | 
			
		||||
	var colorValue int
 | 
			
		||||
	setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
 | 
			
		||||
		mutex.Lock()
 | 
			
		||||
		defer mutex.Unlock()
 | 
			
		||||
		muFakeValue.Lock()
 | 
			
		||||
		defer muFakeValue.Unlock()
 | 
			
		||||
		colorValue = v
 | 
			
		||||
	}
 | 
			
		||||
	readValue := func() int {
 | 
			
		||||
		muFakeValue.Lock()
 | 
			
		||||
		defer muFakeValue.Unlock()
 | 
			
		||||
		return colorValue
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	l := New()
 | 
			
		||||
	l.SetBlue(255)
 | 
			
		||||
	l.SetBlink(100)
 | 
			
		||||
	v := readValue()
 | 
			
		||||
	if v != 255 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 255)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(6 * time.Millisecond)
 | 
			
		||||
	l.SetBlue(128)
 | 
			
		||||
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 128 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 128)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(6 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 128 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 128)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 0 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Stop blink
 | 
			
		||||
	l.SetBlink(0)
 | 
			
		||||
	time.Sleep(5 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 128 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 128)
 | 
			
		||||
	}
 | 
			
		||||
	time.Sleep(12 * time.Millisecond)
 | 
			
		||||
	v = readValue()
 | 
			
		||||
	if v != 128 {
 | 
			
		||||
		t.Errorf("colorValue: %v, wants %v", v, 128)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user