diff --git a/cmd/cmd b/cmd/cmd new file mode 100755 index 0000000..575a4e1 Binary files /dev/null and b/cmd/cmd differ diff --git a/cmd/main.go b/cmd/main.go index d3b4513..0b5df23 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,13 +1,15 @@ package main import ( - "time" - +// "time" marlinbox "github.com/itsscb/marlinbox" ) func main() { mb := marlinbox.New("playlist.json") mb.Run() - time.Sleep(time.Minute * 5) +/* for { + time.Sleep(time.Second * 1) + } +*/ } diff --git a/cmd/marlinbox b/cmd/marlinbox new file mode 100755 index 0000000..a569862 Binary files /dev/null and b/cmd/marlinbox differ diff --git a/cmd/playlist.json b/cmd/playlist.json index 4485c98..1266327 100644 --- a/cmd/playlist.json +++ b/cmd/playlist.json @@ -3,26 +3,26 @@ "volume": 1.0, "playlist": [ { - "id": "0002693373", - "file": "./music/ACDC - Back In Black.mp3" + "id": "0004106397", + "file": "./music/Badewannensitzpirat.mp3" }, { - "id": "0011415256", - "file": "./music/ACDC - TNT.mp3" + "id": "0017411992", + "file": "./music/Leo_Lausemaus_Titelsong.mp3" }, { - "id": "0011394336", - "file": "./music/ACDC - Thunderstruck.mp3" + "id": "0017356608", + "file": "./music/Party_im_Schneckenhaus.mp3" } ], "controlcards": [ { - "id": "0011462127", + "id": "0017458863", "function": "vol+" }, { - "id": "0011394129", + "id": "0017356401", "function": "vol-" } ] -} \ No newline at end of file +} diff --git a/marlinbox.go b/marlinbox.go index 5c66e77..b88356a 100644 --- a/marlinbox.go +++ b/marlinbox.go @@ -2,7 +2,6 @@ package marlinbox import ( "encoding/json" - "errors" "fmt" "log" "os" @@ -14,16 +13,17 @@ import ( ) type MarlinBox struct { - DeviceName string `json:"devicename"` - DevicePath string `json:"devicepath,omitempty"` - CurrentID string - Volume float64 `json:"volume,omitempty"` - Playlist []*PlayCard `json:"playlist,omitempty"` - ControlCards []*ControlCard `json:"controlcards,omitempty"` - Device *evdev.InputDevice - CurrentPlayCard *PlayCard - Player oto.Player - PlayerContext *oto.Context + DeviceName string `json:"devicename"` + DevicePath string `json:"devicepath,omitempty"` + ConfigPath string `json:"configpath,omitempty"` + CurrentID string `json:"-"` + Volume float64 `json:"-"` + Playlist []*PlayCard `json:"playlist,omitempty"` + ControlCards []*ControlCard `json:"controlcards,omitempty"` + Device *evdev.InputDevice `json:"-"` + CurrentPlayCard *PlayCard `json:"-"` + Player oto.Player `json:"-"` + PlayerContext *oto.Context `json:"-"` } type RFIDCard struct { @@ -75,6 +75,9 @@ func New(path string) *MarlinBox { } devices, err := evdev.ListInputDevices() + if err != nil { + log.Panicf("Error listing Input Devices: %s", err) + } for _, dev := range devices { if dev.Name == mb.DeviceName { @@ -86,6 +89,8 @@ func New(path string) *MarlinBox { panic("Device not found") } + mb.ConfigPath = path + return mb } @@ -97,45 +102,37 @@ func (mb *MarlinBox) Run() { } mb.Device.Grab() - go func() { - for { - events, err := mb.Device.Read() - if err != nil { - log.Println(err) - } - - for _, ev := range events { - if ev.Value != 0x1 { - continue - } - val, ok := KeyMap[ev.Code] - if !ok { - continue - } - - if val == "ENTER" { - err := mb.GetCurrentCard() - if err != nil { - log.Println(err) - } - fmt.Println(mb.CurrentPlayCard) - mb.CurrentID = "" - // err = - // if err != nil { - // log.Println(err) - // panic(err) - // } - continue - } - - mb.CurrentID += val - } + // go func() { + for { + events, err := mb.Device.Read() + if err != nil { + log.Println(err) } - }() + + for _, ev := range events { + if ev.Value != 0x1 { + continue + } + val, ok := KeyMap[ev.Code] + if !ok { + continue + } + + if val == "ENTER" || len(mb.CurrentID) == 9 { + if val != "ENTER" { + mb.CurrentID += val + } + mb.GetCurrentCard() + mb.CurrentID = "" + continue + } + + mb.CurrentID += val + } + } } -func (mb *MarlinBox) GetCurrentCard() error { - fmt.Println(mb.ControlCards) +func (mb *MarlinBox) GetCurrentCard() { for _, c := range mb.ControlCards { if mb.CurrentID == c.ID { switch c.Function { @@ -146,13 +143,12 @@ func (mb *MarlinBox) GetCurrentCard() error { } case "vol-": fmt.Println("vol-") - if mb.Volume > 0 { + if mb.Volume >= 0.2 { mb.Volume = mb.Volume - 0.2 } default: - return nil + return } - return nil } } for _, c := range mb.Playlist { @@ -162,10 +158,29 @@ func (mb *MarlinBox) GetCurrentCard() error { mb.Player.Reset() } mb.Play() - return nil + return } } - return errors.New("Card not found: " + mb.CurrentID) + mb.Playlist = append(mb.Playlist, &PlayCard{ + RFIDCard: RFIDCard{ + ID: mb.CurrentID, + }, + }) + + config, err := json.MarshalIndent(&mb, "", " ") + if err != nil { + log.Printf("\nCould not marshal output '%+v': %s", config, err) + } + + jsonFile, err := os.Create(mb.ConfigPath) + if err != nil { + log.Printf("\nCould not create json-File '%s': %s", mb.ConfigPath, err) + } + + _, err = jsonFile.Write(config) + if err != nil { + log.Printf("\nCould not write json-File '%s': %s", mb.ConfigPath, err) + } } func (mb *MarlinBox) Play() { diff --git a/playlist.json b/playlist.json new file mode 100644 index 0000000..fa51f98 --- /dev/null +++ b/playlist.json @@ -0,0 +1,65 @@ +{ + "devicename": "Sycreader RFID Technology Co., Ltd SYC ID\u0026IC USB Reader", + "devicepath": "/dev/input/event1", + "configpath": "playlist.json", + "playlist": [ + { + "id": "0004106397", + "file": "./music/01-01-Traditional-Der_Mond_ist_aufgegangen-320.mp3" + }, + { + "id": "0017411992", + "file": "./music/01-02-Heino_Gaze-La-Le-Lu-320.mp3" + }, + { + "id": "0017356608", + "file": "./music/01-01-Chris_Buseck-Party_im_Schneckenhaus-320.mp3" +}, +{ + "id": "0004148198", + "file": "./music/01-01-Heiko_Russe-Leo_Lausemaus_Titelsong-320.mp3" +}, +{ + "id": "0008323849", + "file": "./music/01-05-Markus_Schurjann-Ich_bin_ein_Einhorn-320.mp3" + }, + { + "id": "0004152218", + "file": "./music/01-06-Traditional-Die_Rader_vom_Bus-320.mp3" + }, + { + "id": "0012528320", + "file": "./music/01-06-Traditional-Schlaf_Kindlein_schlaf-320.mp3" + }, + { + "id": "0008324420", + "file": "./music/01-07-Georges_Moustaki-Das_rote_Pferd-320.mp3" + }, + { + "id": "0008324422", + "file": "./music/01-09-Markus_Schurjann-Wer_hat_den_Keks_aus_der_Dose_geklaut-320.mp3" + }, + { + "id": "0008324475", + "file": "./music/01-10-Traditional-Funf_kleine_Fische-320.mp3" + }, + { + "id": "0005409503", + "file": "./music/01-11-Traditional-Old_Mac_Donald_Had_a_Farm-320.mp3" + }, + { + "id": "0004148375", + "file": "./music/01-14-Traditional-Meine_Oma_fahrt_im_Huhnerstall_Motorrad-320.mp3" + } + ], + "controlcards": [ + { + "id": "0017458863", + "function": "vol+" + }, + { + "id": "0017356401", + "function": "vol-" + } + ] +} \ No newline at end of file