diff --git a/peripheral/communication/communication.go b/peripheral/communication/communication.go new file mode 100644 index 0000000..c59a6c8 --- /dev/null +++ b/peripheral/communication/communication.go @@ -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 +} diff --git a/peripheral/communication.go b/peripheral/communication/walkieTalkie.go similarity index 79% rename from peripheral/communication.go rename to peripheral/communication/walkieTalkie.go index bc23dc3..59598ae 100644 --- a/peripheral/communication.go +++ b/peripheral/communication/walkieTalkie.go @@ -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 } diff --git a/peripheral/device.go b/peripheral/device.go index e3b2183..c615b97 100644 --- a/peripheral/device.go +++ b/peripheral/device.go @@ -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) { diff --git a/peripheral/identificationPacket.go b/peripheral/identificationPacket.go index dabaaa6..712cbb8 100644 --- a/peripheral/identificationPacket.go +++ b/peripheral/identificationPacket.go @@ -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 }