jankssi/main.go
2024-08-20 23:40:32 +02:00

49 lines
890 B
Go

package main
import (
"context"
"fmt"
"log"
"net/http"
"go.bug.st/serial"
)
type FileSerial struct {
f serial.Port
}
func (f *FileSerial) Write(ctx context.Context, data []byte) error {
_, err := f.f.Write(data)
return err
}
func (f *FileSerial) Read(ctx context.Context, data []byte) (int, error) {
n, err := f.f.Read(data)
return n, err
}
func main() {
f, err := serial.Open("/dev/ttyACM0", &serial.Mode{})
if err != nil {
log.Fatalf("Failed to open serial port: %v", err)
}
bC := make(chan []byte)
ssi := NewSSI(&FileSerial{f: f}, func(ty uint8, data []byte) {
log.Printf("Barcode: %x/%s", ty, data)
select {
case bC <- data:
default:
}
})
ctx := context.Background()
go ssi.Run(ctx)
http.HandleFunc("/barcode", func(w http.ResponseWriter, r *http.Request) {
b := <-bC
fmt.Fprintf(w, "barcode: %s", b)
})
http.ListenAndServe(":2137", nil)
}