完成授书游戏节点功能
This commit is contained in:
@@ -9,40 +9,40 @@ import (
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
chip *gpiocdev.Chip // GPIO 设备
|
||||
inLines *gpiocdev.Lines // 输入针脚
|
||||
pushLine *gpiocdev.Line // 发卡针脚
|
||||
pullLine *gpiocdev.Line // 回收针脚
|
||||
resetLine *gpiocdev.Line // 重置针脚
|
||||
status map[inGpioLine]int // 状态
|
||||
lines *LineGroup
|
||||
chip *gpiocdev.Chip // GPIO 设备
|
||||
inLines *gpiocdev.Lines // 输入针脚
|
||||
pushLine *gpiocdev.Line // 发卡针脚
|
||||
pullLine *gpiocdev.Line // 回收针脚
|
||||
resetLine *gpiocdev.Line // 重置针脚
|
||||
status map[int]*StatusLine // 状态
|
||||
cache map[int]int
|
||||
}
|
||||
|
||||
// statusEventHandler 状态事件处理
|
||||
func (d *Device) statusEventHandler(evt gpiocdev.LineEvent) {
|
||||
offset := inGpioLine(evt.Offset)
|
||||
offset := evt.Offset
|
||||
eType := evt.Type
|
||||
|
||||
t := false
|
||||
defer func() {
|
||||
if t {
|
||||
zap.S().Infof("状态: %s-%d", offset, d.status[offset])
|
||||
}
|
||||
}()
|
||||
|
||||
if v, ok := d.status[offset]; ok {
|
||||
defer func() {
|
||||
v.SubEvent(func(i int) {
|
||||
labels := d.lines.AllLabel()
|
||||
zap.S().Infof("状态: %s-%d", labels[offset], i)
|
||||
})
|
||||
}()
|
||||
|
||||
if eType == gpiocdev.LineEventFallingEdge {
|
||||
if v == 0 {
|
||||
if v.Get() == 0 {
|
||||
return
|
||||
} else {
|
||||
t = true
|
||||
d.status[offset] = 0
|
||||
v.AfterSet(0)
|
||||
}
|
||||
} else if eType == gpiocdev.LineEventRisingEdge {
|
||||
if v == 1 {
|
||||
if v.Get() == 1 {
|
||||
return
|
||||
} else {
|
||||
t = true
|
||||
d.status[offset] = 1
|
||||
v.AfterSet(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,15 +57,17 @@ func (d *Device) initStatus() error {
|
||||
return err
|
||||
}
|
||||
for i := 0; i < len(status); i++ {
|
||||
d.status[inGpioLine(offsets[i])] = status[i]
|
||||
d.status[offsets[i]] = DefaultStatusLine(status[i])
|
||||
}
|
||||
|
||||
sv := make([]string, len(offsets))
|
||||
for _, g := range allInGpio() {
|
||||
sv = append(sv, fmt.Sprintf("%s-%d", g, d.status[g]))
|
||||
labels := d.lines.AllLabel()
|
||||
for _, g := range d.lines.AllInLines() {
|
||||
sv = append(sv, fmt.Sprintf("%s-%s", labels[g], d.status[g]))
|
||||
}
|
||||
strings.Join(sv, " ")
|
||||
zap.S().Infof("初始状态: %s", strings.Join(sv, " "))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -105,20 +107,41 @@ func (d *Device) Reset() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
|
||||
func New(name string) (*Device, error) {
|
||||
func (d *Device) GetOutOk() int {
|
||||
return d.status[d.lines.OutOK].Get()
|
||||
}
|
||||
|
||||
func (d *Device) GetLower() int {
|
||||
return d.status[d.lines.Lower].Get()
|
||||
}
|
||||
|
||||
func (d *Device) GetError() int {
|
||||
return d.status[d.lines.Error].Get()
|
||||
}
|
||||
|
||||
func (d *Device) GetEmpty() int {
|
||||
return d.status[d.lines.Empty].Get()
|
||||
}
|
||||
|
||||
func New(name string, lines *LineGroup) (*Device, error) {
|
||||
if !lines.Ok() {
|
||||
return nil, fmt.Errorf("针脚配置错误")
|
||||
}
|
||||
|
||||
chip, err := gpiocdev.NewChip(name, gpiocdev.AsActiveLow)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("打开 GPIO 设备失败: %w", err)
|
||||
}
|
||||
|
||||
d := &Device{
|
||||
lines: lines,
|
||||
chip: chip,
|
||||
status: make(map[inGpioLine]int),
|
||||
status: make(map[int]*StatusLine),
|
||||
}
|
||||
|
||||
// 初始化输入针脚并监听针脚
|
||||
inLines, err := chip.RequestLines(
|
||||
allInGpioInt(),
|
||||
lines.AllInLines(), // 请求所有输入引脚
|
||||
gpiocdev.AsInput, // 请求引脚作为输入
|
||||
gpiocdev.WithPullUp, // 使用上拉电阻
|
||||
gpiocdev.WithRealtimeEventClock, // 使用实时时钟
|
||||
@@ -137,23 +160,23 @@ func New(name string) (*Device, error) {
|
||||
}
|
||||
|
||||
// 初始化发卡引脚
|
||||
push, err := chip.RequestLine(int(PushLine), gpiocdev.AsOutput())
|
||||
push, err := chip.RequestLine(lines.Push, gpiocdev.AsOutput())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为发卡失败: %w", PushLine, err)
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为发卡失败: %w", lines.Push, err)
|
||||
}
|
||||
d.pushLine = push
|
||||
|
||||
// 初始化回收引脚
|
||||
pull, err := chip.RequestLine(int(PullLine), gpiocdev.AsOutput())
|
||||
pull, err := chip.RequestLine(lines.Pull, gpiocdev.AsOutput())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为回收失败: %w", PullLine, err)
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为回收失败: %w", lines.Pull, err)
|
||||
}
|
||||
d.pullLine = pull
|
||||
|
||||
// 初始化重置引脚
|
||||
reset, err := chip.RequestLine(int(ResetLine), gpiocdev.AsOutput())
|
||||
reset, err := chip.RequestLine(lines.Reset, gpiocdev.AsOutput())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为重置失败: %w", ResetLine, err)
|
||||
return nil, fmt.Errorf("请求引脚 %d 作为重置失败: %w", lines.Reset, err)
|
||||
}
|
||||
d.resetLine = reset
|
||||
|
||||
|
||||
Reference in New Issue
Block a user