From e60ee4b4f5011f941697eb7e1f67c99959be493a Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 12 Aug 2024 14:57:40 +0200 Subject: [PATCH 1/2] feat: add buzzer --- buzzer/buzzer.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ config.example.yaml | 30 +++++++++++++++++++++++++++ config.example.yml | 27 ------------------------- config.template.yaml | 3 +++ config/config.go | 6 ++++++ go.mod | 2 ++ go.sum | 4 ++++ 7 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 buzzer/buzzer.go create mode 100644 config.example.yaml delete mode 100644 config.example.yml diff --git a/buzzer/buzzer.go b/buzzer/buzzer.go new file mode 100644 index 0000000..eef8226 --- /dev/null +++ b/buzzer/buzzer.go @@ -0,0 +1,48 @@ +package buzzer + +import ( + "log" + "scc/config" + + "github.com/a-h/beeper" + "github.com/stianeikeland/go-rpio" +) + +var buzzerOptions = map[string]func(rpio.Pin){ + "default": playMusic, +} + +func playBuzzer() { + err := rpio.Open() + if err != nil { + log.Printf("Error: Unable to open pin: %s", err) + return + } + defer rpio.Close() + + pin := rpio.Pin(config.GetConfig().Buzzer.Pin) + + buzzerSong := config.GetConfig().Buzzer.Song + val, ok := buzzerOptions[buzzerSong] + if !ok { + log.Printf("Error: Selected buzzer song: %s does not exist\n", buzzerSong) + return + } + + val(pin) +} + +func playMusic(pin rpio.Pin) { + bpm := 300 + music := beeper.NewMusic(pin, bpm) + + music.Note("A5", beeper.Quaver) + music.Note("B5", beeper.Quaver) + music.Note("D5", beeper.Quaver) + music.Note("B5", beeper.Quaver) + music.Note("E5", beeper.Crotchet) + music.Note("E5", beeper.Crotchet) + music.Note("D5", beeper.Quaver) + music.Note("C#5", beeper.Quaver) + music.Note("B4", beeper.Quaver) +} diff --git a/config.example.yaml b/config.example.yaml new file mode 100644 index 0000000..b3ae3c5 --- /dev/null +++ b/config.example.yaml @@ -0,0 +1,30 @@ +cammie: + blocked_names: + - Name 1 + - Name 2 + blocked_ips: + - 10.0.0.10 + max_message_length: 200 +buzzer: + pin: 12 + song: default +spotify: + client_id: abcd + client_secret: dcba +tap: + url: "https://tap.zeus.gent/recent" + beer: + - Schelfaut + - Duvel + - Fourchette + - Jupiler + - Karmeliet + - Kriek + - Chouffe + - Maes + - Somersby + - Sportzot + - Stella +zess: + url: "https://zess.zeus.gent/recent_scans" + day_amount: 40 diff --git a/config.example.yml b/config.example.yml deleted file mode 100644 index 1d4120a..0000000 --- a/config.example.yml +++ /dev/null @@ -1,27 +0,0 @@ -cammie: - blocked_names: - - "Name 1" - - "Name 2" - blocked_ips: - - "10.0.0.10" - max_message_length: 200 -spotify: - client_id: "abcd" - client_secret: "dcba" -tap: - url: "https://tap.zeus.gent/recent" - beer: - - "Schelfaut" - - "Duvel" - - "Fourchette" - - "Jupiler" - - "Karmeliet" - - "Kriek" - - "Chouffe" - - "Maes" - - "Somersby" - - "Sportzot" - - "Stella" -zess: - url: "https://zess.zeus.gent/recent_scans" - day_amount: 40 diff --git a/config.template.yaml b/config.template.yaml index b6533df..77d45ed 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -2,6 +2,9 @@ cammie: blocked_names: [] # List of names that don't get shown blocked_ips: [] # List of IP's that don't get shown max_message_length: # Max message list +buzzer: + pin: # PWM pin used by the buzzer + song: # Options are - default spotify: client_id: # Client ID of the Zeus spotify account client_secret: # Client secret of the Zeus spotify account diff --git a/config/config.go b/config/config.go index d54383c..08a8849 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,11 @@ type cammieConfig struct { MaxMessageLength int `yaml:"max_message_length"` } +type buzzerConfig struct { + Pin int `yaml:"pin"` + Song string `yaml:"song"` +} + type spotifyConfig struct { ClientID string `yaml:"client_id"` ClientSecret string `yaml:"client_secret"` @@ -31,6 +36,7 @@ type zessConfig struct { type Config struct { Cammie cammieConfig `yaml:"cammie"` + Buzzer buzzerConfig `yaml:"buzzer"` Spotify spotifyConfig `yaml:"spotify"` Tap tapConfig `yaml:"tap"` Zess zessConfig `yaml:"zess"` diff --git a/go.mod b/go.mod index 794f1ab..774de9e 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( ) require ( + github.com/a-h/beeper v0.0.0-20190929170045-fc4b1a97b0b2 github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect @@ -30,6 +31,7 @@ require ( github.com/navidys/tvxwidgets v0.7.0 github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/stianeikeland/go-rpio v4.2.0+incompatible github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect diff --git a/go.sum b/go.sum index 8b2b6b0..e592563 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/a-h/beeper v0.0.0-20190929170045-fc4b1a97b0b2 h1:QXbKN2ADvvIPBw0RA5TT61Uk50vHxXflk2bDtWKD3zI= +github.com/a-h/beeper v0.0.0-20190929170045-fc4b1a97b0b2/go.mod h1:DxLcR2/OjZbhGzSvGfrtm6Kfij2L+mYFnw3YuxGcee4= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= @@ -70,6 +72,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo= +github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= From a66696e3eda35fb58a31cfc66f2c334031fb9a65 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 12 Aug 2024 15:02:44 +0200 Subject: [PATCH 2/2] chore: play buzzer when new message received --- api/cammie.go | 2 ++ buzzer/buzzer.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/api/cammie.go b/api/cammie.go index 2e36dc1..94b6f66 100644 --- a/api/cammie.go +++ b/api/cammie.go @@ -3,6 +3,7 @@ package api import ( "fmt" "net/http" + "scc/buzzer" "scc/config" "scc/screen" "slices" @@ -77,6 +78,7 @@ func cammiePostMessage(app *screen.ScreenApp, c *gin.Context) { cammieMessages++ app.Cammie.Update(newMessage) + go buzzer.PlayBuzzer() c.JSON(http.StatusOK, gin.H{"message": "Message received"}) } diff --git a/buzzer/buzzer.go b/buzzer/buzzer.go index eef8226..ffb2299 100644 --- a/buzzer/buzzer.go +++ b/buzzer/buzzer.go @@ -12,7 +12,7 @@ var buzzerOptions = map[string]func(rpio.Pin){ "default": playMusic, } -func playBuzzer() { +func PlayBuzzer() { err := rpio.Open() if err != nil { log.Printf("Error: Unable to open pin: %s", err)