LED
This commit is contained in:
parent
d65e7e6b6d
commit
95664cbebf
@ -1,8 +1,13 @@
|
||||
package pcb
|
||||
|
||||
import "th7/ports"
|
||||
import (
|
||||
"th7/pcb/led"
|
||||
"th7/ports"
|
||||
)
|
||||
|
||||
func NewAdapter(LED bool) (ports.PCBPort, error) {
|
||||
|
||||
return NewDummyAdapter(LED)
|
||||
ledController := led.NewLEDController()
|
||||
|
||||
return NewDummyAdapter(LED, ledController)
|
||||
}
|
||||
|
42
pcb/dummy.go
42
pcb/dummy.go
@ -3,50 +3,26 @@ package pcb
|
||||
import (
|
||||
"fmt"
|
||||
"th7/data/pcb"
|
||||
"th7/pcb/led"
|
||||
)
|
||||
|
||||
type DummyAdapter struct {
|
||||
table pcb.Table
|
||||
led_on bool
|
||||
use_led bool
|
||||
table pcb.Table
|
||||
use_led bool
|
||||
ledController *led.LEDController
|
||||
}
|
||||
|
||||
func NewDummyAdapter(led bool) (*DummyAdapter, error) {
|
||||
func NewDummyAdapter(led bool, controller *led.LEDController) (*DummyAdapter, error) {
|
||||
var adapter DummyAdapter
|
||||
|
||||
adapter.use_led = led
|
||||
adapter.LED_Disable()
|
||||
adapter.ledController = controller
|
||||
|
||||
adapter.ledController.Off()
|
||||
|
||||
return &adapter, nil
|
||||
}
|
||||
|
||||
func (d *DummyAdapter) LED_Enable() {
|
||||
if !d.use_led {
|
||||
return
|
||||
}
|
||||
//....
|
||||
d.led_on = true
|
||||
}
|
||||
|
||||
func (d *DummyAdapter) LED_Disable() {
|
||||
if !d.use_led {
|
||||
return
|
||||
}
|
||||
// ...
|
||||
d.led_on = false
|
||||
}
|
||||
|
||||
func (d *DummyAdapter) LED_Toggle() {
|
||||
if !d.use_led {
|
||||
return
|
||||
}
|
||||
|
||||
if d.led_on {
|
||||
d.LED_Disable()
|
||||
} else {
|
||||
d.LED_Enable()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DummyAdapter) Deinit() error {
|
||||
// turn LED off ..
|
||||
fmt.Println("dummy pcb adapter deinit")
|
||||
|
100
pcb/led/led.go
Normal file
100
pcb/led/led.go
Normal file
@ -0,0 +1,100 @@
|
||||
package led
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DurationFlick = 40 * time.Millisecond
|
||||
)
|
||||
|
||||
type LEDController struct {
|
||||
led1 bool
|
||||
led2 bool
|
||||
mu1 sync.Mutex
|
||||
mu2 sync.Mutex
|
||||
}
|
||||
|
||||
func NewLEDController() *LEDController {
|
||||
|
||||
var controller LEDController
|
||||
controller.led1 = false
|
||||
controller.led2 = false
|
||||
|
||||
return &controller
|
||||
}
|
||||
|
||||
// turn both off
|
||||
func (c *LEDController) Off() {
|
||||
|
||||
c.led1 = false
|
||||
c.led2 = false
|
||||
|
||||
c.Disable1()
|
||||
c.Disable2()
|
||||
}
|
||||
|
||||
func (c *LEDController) Enable1() {
|
||||
|
||||
if c.led1 {
|
||||
return
|
||||
}
|
||||
|
||||
c.led1 = true
|
||||
|
||||
// gpio code to turn LED 1 on
|
||||
}
|
||||
|
||||
func (c *LEDController) Enable2() {
|
||||
|
||||
if c.led2 {
|
||||
return
|
||||
}
|
||||
|
||||
c.led2 = true
|
||||
|
||||
// gpio code to turn LED 2 on
|
||||
}
|
||||
|
||||
func (c *LEDController) Disable1() {
|
||||
|
||||
if !c.led1 {
|
||||
return
|
||||
}
|
||||
|
||||
c.led1 = false
|
||||
|
||||
// gpio code to turn LED 1 off
|
||||
}
|
||||
|
||||
func (c *LEDController) Disable2() {
|
||||
|
||||
if !c.led2 {
|
||||
return
|
||||
}
|
||||
|
||||
c.led2 = false
|
||||
|
||||
// gpio code to turn LED 2 off
|
||||
}
|
||||
|
||||
func (c *LEDController) Flick1() {
|
||||
c.mu1.Lock()
|
||||
defer c.mu1.Unlock()
|
||||
|
||||
// assume it is off
|
||||
c.Enable1()
|
||||
time.Sleep(DurationFlick)
|
||||
c.Disable1()
|
||||
}
|
||||
|
||||
func (c *LEDController) Flick2() {
|
||||
c.mu2.Lock()
|
||||
defer c.mu2.Unlock()
|
||||
|
||||
// assume it is off
|
||||
c.Enable2()
|
||||
time.Sleep(DurationFlick)
|
||||
c.Disable2()
|
||||
}
|
@ -10,7 +10,4 @@ type PCBPort interface {
|
||||
GetTable() pcb.Table
|
||||
ReadChannel(int) (float64, error)
|
||||
ReadPCBTemp() (float64, error)
|
||||
LED_Disable()
|
||||
LED_Enable()
|
||||
LED_Toggle()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user