修复投影仪控制
This commit is contained in:
@@ -3,13 +3,10 @@ package common
|
||||
import "sync"
|
||||
|
||||
type CtrlWait struct {
|
||||
// 用于暂停的chan
|
||||
P chan struct{}
|
||||
// 用于恢复的chan
|
||||
R chan struct{}
|
||||
C chan int8
|
||||
|
||||
// 状态
|
||||
s bool
|
||||
|
||||
m sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -18,7 +15,7 @@ func (c *CtrlWait) Pause() {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if c.s {
|
||||
c.P <- struct{}{}
|
||||
c.C <- 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +24,7 @@ func (c *CtrlWait) Resume() {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if c.s {
|
||||
c.R <- struct{}{}
|
||||
c.C <- 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +43,7 @@ func (c *CtrlWait) Close() {
|
||||
// NewCtrlWait 创建一个控制等待
|
||||
func NewCtrlWait() *CtrlWait {
|
||||
return &CtrlWait{
|
||||
P: make(chan struct{}),
|
||||
R: make(chan struct{}),
|
||||
C: make(chan int8),
|
||||
s: false,
|
||||
}
|
||||
}
|
||||
|
||||
53
internal/common/pause_sub.go
Normal file
53
internal/common/pause_sub.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package common
|
||||
|
||||
import "sync"
|
||||
|
||||
type PauseSub struct {
|
||||
ctrl *CtrlWait
|
||||
// 回调函数
|
||||
items []chan int8
|
||||
m sync.RWMutex
|
||||
}
|
||||
|
||||
// Add 添加一个暂停项
|
||||
func (p *PauseSub) Add(item chan int8) {
|
||||
p.m.Lock()
|
||||
defer p.m.Unlock()
|
||||
p.items = append(p.items, item)
|
||||
}
|
||||
|
||||
// Remove 移除一个暂停项
|
||||
func (p *PauseSub) Remove(item chan int8) {
|
||||
p.m.Lock()
|
||||
defer p.m.Unlock()
|
||||
for i, v := range p.items {
|
||||
if v == item {
|
||||
p.items = append(p.items[:i], p.items[i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run 开始监听
|
||||
func (p *PauseSub) Run() {
|
||||
p.ctrl.Open()
|
||||
defer p.ctrl.Close()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-p.ctrl.C:
|
||||
go func() {
|
||||
p.m.RLock()
|
||||
defer p.m.RUnlock()
|
||||
for _, item := range p.items {
|
||||
item <- 1
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewPauseSub(c *CtrlWait) *PauseSub {
|
||||
return &PauseSub{
|
||||
ctrl: c,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user