matemat/uncanny/can.go

132 lines
2.9 KiB
Go
Raw Normal View History

2021-01-31 16:56:33 +01:00
package uncanny
import (
"errors"
"fmt"
"log"
"github.com/brutella/can"
)
type Can struct {
bus *can.Bus
swstate FeedbackMessage
dispstate int
}
func NewCan(intf string) (*Can, error) {
bus, err := can.NewBusForInterfaceWithName(intf)
if err != nil {
return nil, errors.New(fmt.Sprintf("Can't open %s: %v", intf, err))
}
can := &Can{
bus: bus,
dispstate: -1,
}
bus.Subscribe(can)
return can, nil
}
func (c *Can) Start() {
go func() {
2021-01-31 18:30:22 +01:00
// send the init sequence, but (hopefully) defer until after we start listening
// FIXME this is inherently racy :(
go c.initialize()
2021-01-31 16:56:33 +01:00
// ConnectAndPublish will block, so we'll just leave the Printf hanging here
log.Printf("CAN bus shut down: %v", c.bus.ConnectAndPublish())
}()
}
func (c *Can) Stop() {
c.bus.Disconnect()
}
func (c *Can) Handle(frame can.Frame) {
2021-01-31 18:25:21 +01:00
log.Printf("Got CAN frame to 0x%08x", frame.ID)
2021-01-31 16:56:33 +01:00
typ, obj, err := DecodeMessage(frame)
if err != nil {
if err == UnsupportedMessageType {
log.Printf("Unsupported message type: 0x%08x", frame.ID)
} else {
log.Printf("Cannot decode message: %v", err)
}
} else {
switch typ {
case MessageTypeFeedback:
2021-01-31 18:25:21 +01:00
log.Printf("Got feedback message: %+v", obj.(FeedbackMessage))
2021-01-31 18:37:47 +01:00
log.Printf("Data: 0x%02x 0x%02x", frame.Data[0], frame.Data[1])
2021-01-31 16:56:33 +01:00
c.swstate = obj.(FeedbackMessage)
2021-01-31 18:37:47 +01:00
// when all end switches are off: send stop command
if !c.swstate.EmptyD && !c.swstate.EmptyE && !c.swstate.EmptyF && !c.swstate.EmptyG && !c.swstate.EmptyH {
log.Printf("All end switches off, stopping dispenser")
c.Cancel()
}
2021-01-31 16:56:33 +01:00
case MessageTypeFeedbackRequest:
// ignore
case MessageTypePower:
2021-01-31 18:25:21 +01:00
log.Printf("Got power message: %+v", obj.(PowerMessage))
2021-01-31 16:56:33 +01:00
// ignore
case MessageTypePowerRequest:
// ignore
case MessageTypeDispense:
2021-01-31 18:25:21 +01:00
log.Printf("Got dispense message: %+v", obj.(DispenseMessage))
2021-01-31 16:56:33 +01:00
c.dispstate = obj.(DispenseMessage).Slot
case MessageTypeAuto:
// ignore
}
}
}
func (c *Can) IsEmpty(slot int) bool {
switch (slot) {
case 0:
return c.swstate.EmptyD
case 1:
return c.swstate.EmptyE
case 2:
return c.swstate.EmptyF
case 3:
return c.swstate.EmptyG
case 4:
return c.swstate.EmptyH
}
return false
}
func (c *Can) IsDispensing(slot int) bool {
switch (slot) {
case 0:
return c.swstate.EndD
case 1:
return c.swstate.EndE
case 2:
return c.swstate.EndF
case 3:
return c.swstate.EndG
case 4:
return c.swstate.EndH
}
return false
}
func (c *Can) ActiveDispenser() int {
return c.dispstate
}
func (c *Can) Dispense(slot int) error {
2021-01-31 18:41:50 +01:00
log.Printf("Starting dispense on slot %d", slot)
2021-01-31 16:56:33 +01:00
return c.bus.Publish(DispenseMessage{slot}.Encode())
}
func (c *Can) Cancel() error {
2021-01-31 18:41:50 +01:00
log.Printf("Stopping dispense")
2021-01-31 16:56:33 +01:00
return c.bus.Publish(DispenseMessage{DispenseSlotOff}.Encode())
}
2021-01-31 18:30:22 +01:00
func (c *Can) initialize() {
2021-01-31 18:41:50 +01:00
log.Printf("Initializing")
2021-01-31 16:56:33 +01:00
// enable automatic status updates
2021-01-31 18:30:22 +01:00
c.bus.Publish(AutoMessage{true}.Encode())
c.bus.Publish(RequestFeedbackMessage())
c.bus.Publish(RequestPowerMessage())
2021-01-31 16:56:33 +01:00
}