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
}
@@ -0,0 +1,33 @@
package packets
import (
"esdi/peripheral/communication/constvar"
"fmt"
)
type IdentificationPacket struct {
StartMarker byte
DeviceID uint8
PktType PacketType
Name [32]byte
EndMarker byte
}
func (pkt *IdentificationPacket) Validate() bool {
fmt.Println("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓")
fmt.Println(pkt.StartMarker)
fmt.Println(pkt.PktType.String())
fmt.Println(string(pkt.Name[:]))
fmt.Println(pkt.EndMarker)
fmt.Println("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑")
// Change this to return an error and add a validation against available
// device ids
if pkt.StartMarker != constvar.StartOfText ||
pkt.EndMarker != constvar.EndOfText {
return false
}
return true
}
@@ -0,0 +1,26 @@
// Package packets will define the available packets we have
package packets
type Packet interface {
Validate() bool
}
type PacketType uint8
const (
identificationPacket PacketType = iota
)
const (
identificationPacketStr = "identificationPacket"
unknownPacketStr = "unknownPacket"
)
func (p PacketType) String() string {
switch p {
case identificationPacket:
return identificationPacketStr
default:
return unknownPacketStr
}
}
+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
}