Move the communication stuff to its own package

This commit is contained in:
2025-12-14 15:24:51 +00:00
parent c23441908f
commit 3ffad42922
4 changed files with 45 additions and 36 deletions
+33
View File
@@ -0,0 +1,33 @@
// Package communication will handle the data transmission with the peripheral
// device
package communication
import (
"github.com/tarm/serial"
)
const (
StartOfText = 0x02
EndOfText = 0x03
)
type CommState uint8
const (
CommIdle CommState = iota
CommOff
)
type Command uint8
const (
CmdRequestID Command = iota
CmdAckID
CmdCreateScreen
CmdCreateWindow
)
type DataReceiver interface {
Read(dev *serial.Port) error
Validate(data []byte) bool
}
@@ -1,4 +1,4 @@
package peripheral
package communication
import (
"bytes"
@@ -9,32 +9,6 @@ import (
"github.com/tarm/serial"
)
const (
startOfText = 0x02
endOfText = 0x03
)
type CommState uint8
const (
CommIdle CommState = iota
CommOff
)
type Command uint8
const (
CmdRequestID Command = iota
CmdAckID
CmdCreateScreen
CmdCreateWindow
)
type DataReceiver interface {
Read(dev *serial.Port) error
Validate(data []byte) bool
}
type WalkieTalkie struct {
Serial *serial.Port
Cfg *serial.Config
@@ -52,7 +26,7 @@ func (wt *WalkieTalkie) ReadFramedData(size int, packet any) error {
return err
}
if b[0] == startOfText {
if b[0] == StartOfText {
buf[0] = b[0]
break
}
+6 -5
View File
@@ -1,6 +1,7 @@
package peripheral
import (
comm "esdi/peripheral/communication"
"esdi/peripheral/devices"
"time"
@@ -36,18 +37,18 @@ func (s PeripheralDeviceState) String() string {
type PeripheralDevice struct {
State PeripheralDeviceState
CommState CommState
CommState comm.CommState
ID uint8
Name string
WT *WalkieTalkie
WT *comm.WalkieTalkie
DeviceAPI *devices.Device
}
func NewPeripheralDevice(port string) *PeripheralDevice {
return &PeripheralDevice{
State: StateUnknown,
CommState: CommOff,
WT: &WalkieTalkie{
CommState: comm.CommOff,
WT: &comm.WalkieTalkie{
Cfg: &serial.Config{
Name: port,
Baud: 115200,
@@ -92,7 +93,7 @@ func (p *PeripheralDevice) Probe() error {
func (p *PeripheralDevice) ToConnectedIdling() {
p.State = StateConnected
p.CommState = CommIdle
p.CommState = comm.CommIdle
}
func (p *PeripheralDevice) Merge(packet *IdentificationPacket) {
+4 -3
View File
@@ -2,6 +2,7 @@ package peripheral
import (
"encoding/binary"
comm "esdi/peripheral/communication"
"fmt"
)
@@ -13,7 +14,7 @@ type IdentificationPacket struct {
EndMarker byte
}
func (pkt *IdentificationPacket) Read(wt *WalkieTalkie) error {
func (pkt *IdentificationPacket) Read(wt *comm.WalkieTalkie) error {
err := wt.ReadFramedData(binary.Size(IdentificationPacket{}), pkt)
if err != nil {
return err
@@ -37,8 +38,8 @@ func (pkt *IdentificationPacket) Validate() bool {
// Change this to return an error and add a validation against available
// device ids
if pkt.StartMarker != startOfText ||
pkt.EndMarker != endOfText {
if pkt.StartMarker != comm.StartOfText ||
pkt.EndMarker != comm.EndOfText {
return false
}