More organization - need to use the new flow for probing

This commit is contained in:
2025-12-15 23:47:01 +00:00
parent 3ffad42922
commit 65b915c3ff
8 changed files with 146 additions and 40 deletions
+2 -15
View File
@@ -1,14 +1,8 @@
// Package communication will handle the data transmission with the peripheral
// device
package communication
import (
"github.com/tarm/serial"
)
const (
StartOfText = 0x02
EndOfText = 0x03
"esdi/peripheral/types"
)
type CommState uint8
@@ -18,16 +12,9 @@ const (
CommOff
)
type Command uint8
const (
CmdRequestID Command = iota
CmdRequestID types.Command = iota
CmdAckID
CmdCreateScreen
CmdCreateWindow
)
type DataReceiver interface {
Read(dev *serial.Port) error
Validate(data []byte) bool
}
@@ -0,0 +1,8 @@
// Package constvar will hold constants and vars for communication
package constvar
const (
StartOfText = 0x02
EndOfText = 0x03
ACK = 0x06
)
+22
View File
@@ -0,0 +1,22 @@
package packets
import "esdi/peripheral/communication/constvar"
type AckPacket struct {
StartMarker byte
AckByte byte
EndMarker byte
}
func (pkt *AckPacket) Validate() bool {
if pkt.StartMarker != constvar.StartOfText ||
pkt.EndMarker != constvar.EndOfText {
return false
}
if pkt.AckByte != constvar.ACK {
return false
}
return true
}
@@ -1,8 +1,7 @@
package peripheral
package packets
import (
"encoding/binary"
comm "esdi/peripheral/communication"
"esdi/peripheral/communication/constvar"
"fmt"
)
@@ -14,19 +13,6 @@ type IdentificationPacket struct {
EndMarker byte
}
func (pkt *IdentificationPacket) Read(wt *comm.WalkieTalkie) error {
err := wt.ReadFramedData(binary.Size(IdentificationPacket{}), pkt)
if err != nil {
return err
}
if !pkt.Validate() {
return fmt.Errorf("invalid data")
}
return nil
}
func (pkt *IdentificationPacket) Validate() bool {
fmt.Println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓")
fmt.Println(pkt.StartMarker)
@@ -38,8 +24,8 @@ func (pkt *IdentificationPacket) Validate() bool {
// Change this to return an error and add a validation against available
// device ids
if pkt.StartMarker != comm.StartOfText ||
pkt.EndMarker != comm.EndOfText {
if pkt.StartMarker != constvar.StartOfText ||
pkt.EndMarker != constvar.EndOfText {
return false
}
@@ -1,4 +1,9 @@
package peripheral
// Package packets will define the available packets we have
package packets
type Packet interface {
Validate() bool
}
type PacketType uint8
+97 -3
View File
@@ -6,6 +6,10 @@ import (
"fmt"
"io"
"esdi/peripheral/communication/constvar"
"esdi/peripheral/communication/packets"
"esdi/peripheral/types"
"github.com/tarm/serial"
)
@@ -26,7 +30,7 @@ func (wt *WalkieTalkie) ReadFramedData(size int, packet any) error {
return err
}
if b[0] == StartOfText {
if b[0] == constvar.StartOfText {
buf[0] = b[0]
break
}
@@ -76,6 +80,96 @@ func (wt *WalkieTalkie) AknowledgeIdentification() (int, error) {
return wt.Serial.Write([]byte{uint8(CmdAckID)})
}
func (wt *WalkieTalkie) SendCommand(payload []byte) (int, error) {
return wt.Serial.Write(payload)
type header struct {
StartByte uint8
CMD types.Command
EndByte uint8
}
func (wt *WalkieTalkie) sendPacket(data any) error {
// Prepare the payload
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, data)
if err != nil {
return err
}
// Send the payload
_, err = wt.Serial.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
func (wt *WalkieTalkie) readPacket(resp packets.Packet) error {
size := binary.Size(resp)
if size < 0 {
return fmt.Errorf("invalid packet size")
}
err := wt.ReadFramedData(size, resp)
if err != nil {
return err
}
if !resp.Validate() {
return fmt.Errorf("badly formatted response")
}
return nil
}
func (wt *WalkieTalkie) sendHeader(h *header) error {
err := wt.sendPacket(h)
if err != nil {
return err
}
var ack packets.AckPacket
err = wt.readPacket(&ack)
if err != nil {
return err
}
return nil
}
func (wt *WalkieTalkie) sendBody(payload any, resp packets.Packet) error {
err := wt.sendPacket(payload)
if err != nil {
return err
}
err = wt.readPacket(resp)
if err != nil {
return err
}
return nil
}
func (wt *WalkieTalkie) SendCommand(cmd types.Command, payload any,
responseBody packets.Packet) error {
// Prepare the header
header := header{
StartByte: constvar.StartOfText,
CMD: cmd,
EndByte: constvar.EndOfText,
}
// Send the header
err := wt.sendHeader(&header)
if err != nil {
return err
}
// Send the body
err = wt.sendBody(payload, responseBody)
if err != nil {
return err
}
return nil
}
+3 -2
View File
@@ -2,6 +2,7 @@ package peripheral
import (
comm "esdi/peripheral/communication"
pack "esdi/peripheral/communication/packets"
"esdi/peripheral/devices"
"time"
@@ -71,7 +72,7 @@ func (p *PeripheralDevice) Probe() error {
}
// Verify the papers
var pkt IdentificationPacket
var pkt pack.IdentificationPacket
err = pkt.Read(p.WT)
if err != nil {
// Throw the man in the gulag!
@@ -96,7 +97,7 @@ func (p *PeripheralDevice) ToConnectedIdling() {
p.CommState = comm.CommIdle
}
func (p *PeripheralDevice) Merge(packet *IdentificationPacket) {
func (p *PeripheralDevice) Merge(packet *pack.IdentificationPacket) {
p.Name = string(packet.Name[:])
p.ID = packet.DeviceID
}
+4 -1
View File
@@ -1,4 +1,7 @@
// Package types defines the types necessary to handle peripherals
package types
type CommandID uint8
type (
CommandID uint8
Command uint8
)