Yeah, I kinda gave up, i will fix it later or something I recon
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package cdashdisplay
|
||||
|
||||
import (
|
||||
"esdi/peripheral/communication"
|
||||
"esdi/peripheral/communication/packets"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/tarm/serial"
|
||||
)
|
||||
|
||||
func listPorts() ([]string, error) {
|
||||
ttyUSBs, err := filepath.Glob("/dev/ttyUSB*")
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
return ttyUSBs, nil
|
||||
}
|
||||
|
||||
func probe(WT *communication.WalkieTalkie) error {
|
||||
err := WT.TurnOn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send the identification command
|
||||
cmd := communication.CmdRequestID
|
||||
var response packets.IdentificationPacket
|
||||
err = WT.SendCommand(cmd, []byte{0x06, 0x07, 0x08, 0x09}, &response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.DeviceID != 0x01 {
|
||||
return fmt.Errorf("wrong ID")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func findDisplayPort() (*communication.WalkieTalkie, error) {
|
||||
ports, err := listPorts()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pLogger.Info(fmt.Sprintf("Looking into %v", ports))
|
||||
|
||||
var wt *communication.WalkieTalkie
|
||||
for _, port := range ports {
|
||||
wt = &communication.WalkieTalkie{
|
||||
Cfg: &serial.Config{
|
||||
Name: port,
|
||||
Baud: 115200,
|
||||
ReadTimeout: 500 * time.Millisecond,
|
||||
},
|
||||
}
|
||||
|
||||
err = probe(wt)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
||||
pLogger.Info(fmt.Sprintf("wasn't port %s", port))
|
||||
wt = nil
|
||||
}
|
||||
|
||||
if wt == nil {
|
||||
return nil, fmt.Errorf("couldn't find cdashdisplay")
|
||||
}
|
||||
|
||||
pLogger.Info(fmt.Sprintf("found cdashdisplay on port: %s", wt.Cfg.Name))
|
||||
return wt, nil
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
// Package cdashdisplay will handle the USB connections
|
||||
package cdashdisplay
|
||||
|
||||
import (
|
||||
helper "esdi/helpers"
|
||||
"esdi/peripheral/communication"
|
||||
"esdi/peripheral/communication/packets"
|
||||
"esdi/peripheral/types"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
var pLogger *slog.Logger
|
||||
|
||||
func SetLogger(l *slog.Logger) {
|
||||
pLogger = l
|
||||
}
|
||||
|
||||
const (
|
||||
newWindowCMDID types.Command = 3
|
||||
destroyWindowCMDID types.Command = 4
|
||||
moveWindowCMDID types.Command = 5
|
||||
newLayoutCMDID types.Command = 6
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultDecorations = UIDecorations{
|
||||
HasBorder: 1,
|
||||
BGColour: 0x1041, // Look in the eslabsCurses library for these colours
|
||||
FGColour: 0xffff,
|
||||
TitleColour: 0xffff,
|
||||
BorderColour: 0xf800,
|
||||
TitleSize: 2,
|
||||
TextSize: 4,
|
||||
Padding: 0x00,
|
||||
}
|
||||
)
|
||||
|
||||
type UIDimensions struct {
|
||||
X0 uint16
|
||||
Y0 uint16
|
||||
Width uint16
|
||||
Height uint16
|
||||
}
|
||||
|
||||
type UIDecorations struct {
|
||||
BGColour uint16
|
||||
FGColour uint16
|
||||
TitleColour uint16
|
||||
BorderColour uint16
|
||||
TitleSize uint8
|
||||
TextSize uint8
|
||||
HasBorder uint8
|
||||
Padding uint8
|
||||
}
|
||||
|
||||
type UIWindow struct {
|
||||
Dims UIDimensions
|
||||
Decor UIDecorations
|
||||
Title [32]byte
|
||||
}
|
||||
|
||||
type LayoutTree struct {
|
||||
Windows map[int16]UIWindow
|
||||
}
|
||||
|
||||
func NewLayoutTree() *LayoutTree {
|
||||
return &LayoutTree{
|
||||
Windows: make(map[int16]UIWindow),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LayoutTree) AddWindow(idx int16, w UIWindow) {
|
||||
l.Windows[idx] = w
|
||||
}
|
||||
|
||||
type CDashState struct {
|
||||
Layout *LayoutTree
|
||||
}
|
||||
|
||||
func NewCDashState() *CDashState {
|
||||
return &CDashState{
|
||||
Layout: NewLayoutTree(),
|
||||
}
|
||||
}
|
||||
|
||||
type CDashDisplay struct {
|
||||
WT *communication.WalkieTalkie
|
||||
State *CDashState
|
||||
}
|
||||
|
||||
func NewCDashDisplay() (*CDashDisplay, error) {
|
||||
// Look for the port
|
||||
p, err := findDisplayPort()
|
||||
if err != nil {
|
||||
pLogger.Info("failed to find cdashdisplay port: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &CDashDisplay{
|
||||
WT: p,
|
||||
State: NewCDashState(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *CDashDisplay) CreateWindow(win UIWindow) (int16, error) {
|
||||
bytes, err := helper.StructToBytes(win)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
// Send the command
|
||||
var wID packets.NewWindowID
|
||||
err = d.WT.SendCommand(newWindowCMDID, bytes, &wID)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
pLogger.Info(fmt.Sprintf("Recived ID message: %v", wID))
|
||||
|
||||
d.State.Layout.AddWindow(wID.ID, win)
|
||||
|
||||
return wID.ID, nil
|
||||
}
|
||||
|
||||
func (d *CDashDisplay) DestroyWindow(wID int16) error {
|
||||
type UIWindowDestructPacket struct {
|
||||
WinID int16
|
||||
}
|
||||
|
||||
packet := UIWindowDestructPacket{
|
||||
WinID: wID,
|
||||
}
|
||||
|
||||
bytes, err := helper.StructToBytes(packet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ack packets.AckPacket
|
||||
err = d.WT.SendCommand(destroyWindowCMDID, bytes, &ack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// NODE: add this
|
||||
// d.State.Layout.RemoveWindow(wID)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user