Skip to content

Commit

Permalink
Add support to force a Buzzer setting on connection for Felicita scale
Browse files Browse the repository at this point in the history
  • Loading branch information
fako1024 committed Jul 29, 2024
1 parent 860d8ea commit 88e0fe4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 35 deletions.
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
module github.com/fako1024/btscale

go 1.18
go 1.20

require (
github.com/fako1024/gatt v1.0.4
github.com/fatih/stopwatch v1.0.0
github.com/gofiber/fiber/v2 v2.51.0
go.uber.org/zap v1.26.0
github.com/gofiber/fiber/v2 v2.52.5
go.uber.org/zap v1.27.0
)

require (
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.22.0 // indirect
)
38 changes: 19 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fako1024/gatt v1.0.4 h1:5euK7RK4nhaHYgg4v1iS53zxWK+lGRBeZjQFBxtaUYs=
github.com/fako1024/gatt v1.0.4/go.mod h1:TTf+fxGvaVhUZJWD9h+MMhjxsbWRBTtNC77jrCL0HtU=
github.com/fatih/stopwatch v1.0.0 h1:sTac5Q8e+Ql27wjVze0rOHWVQqkKmhSlPmQbNlAsZz0=
github.com/fatih/stopwatch v1.0.0/go.mod h1:OJI5FjXD3U1Y019APt3bVM7Fm94Ambcu2RTXh5FUVMs=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/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/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
30 changes: 24 additions & 6 deletions pkg/felicita/felicita.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type Felicita struct {

timer *stopwatch.Stopwatch

deviceID string
deviceName string
deviceID string
deviceName string
forceBuzzerSettingOnConnect BuzzerSetting
hasReceivedData bool

stateChangeHandler func(status scale.ConnectionStatus)
stateChangeChan chan scale.ConnectionStatus
Expand Down Expand Up @@ -254,7 +256,7 @@ func (f *Felicita) ElapsedTime() time.Duration {
func (f *Felicita) Close() error {
close(f.doneChan)

f.btDevice.StopScanning()
_ = f.btDevice.StopScanning()
return f.btDevice.RemoveAllServices()
}

Expand Down Expand Up @@ -354,7 +356,7 @@ func (f *Felicita) onPeriphConnected(p gatt.Peripheral, connErr error) {

f.setStatus(scale.StateConnected, nil)
defer func() {
p.Device().CancelConnection(p)
_ = p.Device().CancelConnection(p)
f.setStatus(scale.StateDisconnected, connErr)
}()

Expand Down Expand Up @@ -405,7 +407,7 @@ func (f *Felicita) onPeriphConnected(p gatt.Peripheral, connErr error) {
f.logger.Debugf("released peripheral `%s/%s`", p.Name(), p.ID())
}

func (f *Felicita) onPeriphDisconnected(p gatt.Peripheral, err error) {
func (f *Felicita) onPeriphDisconnected(p gatt.Peripheral, _ error) {

if !f.thisDevice(p) {
return
Expand Down Expand Up @@ -437,7 +439,7 @@ func (f *Felicita) disconnect() {
}
}

func (f *Felicita) receiveData(c *gatt.Characteristic, req []byte, err error) {
func (f *Felicita) receiveData(_ *gatt.Characteristic, req []byte, err error) {

if err != nil || len(req) != 18 {
return
Expand All @@ -456,6 +458,11 @@ func (f *Felicita) receiveData(c *gatt.Characteristic, req []byte, err error) {
f.isBuzzingOnTouch = parseSignalFlag(req[14])
f.unit = dataPoint.Unit

// Upon first data reception, check if the Buzzer is configured as expected and
// attempt to force the setting if not (unles not configured)
f.forceBuzzerSetting()
f.hasReceivedData = true

// Call handler function, if any
if f.dataHandler != nil {
f.dataHandler(dataPoint)
Expand Down Expand Up @@ -495,6 +502,17 @@ func (f *Felicita) waitForBuzzer(targetState bool) error {
return fmt.Errorf("target buzzer state %v was not reached within %v", targetState, time.Duration(btSettleRetries)*btSettleDelay)
}

func (f *Felicita) forceBuzzerSetting() {
if !f.hasReceivedData && f.forceBuzzerSettingOnConnect != "" {
if f.isBuzzingOnTouch && f.forceBuzzerSettingOnConnect == BuzzerSettingOff ||
!f.isBuzzingOnTouch && f.forceBuzzerSettingOnConnect == BuzzerSettingOn {
if err := f.ToggleBuzzingOnTouch(); err != nil {
f.logger.Warnf("failed to force buzzer setting to `%s`: %s", f.forceBuzzerSettingOnConnect, err)
}
}
}
}

////////////////////////////////////////////////////////////////////////////////

func parseUnit(data []byte) scale.Unit {
Expand Down
20 changes: 20 additions & 0 deletions pkg/felicita/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import (
"github.com/fako1024/gatt"
)

// BuzzerSetting denotes the basic Buzzer (on touch) setting of the scale
type BuzzerSetting string

const (

// BuzzerSettingOn denotes that the basic Buzzer (on touch) setting is on
BuzzerSettingOn = "ON"

// BuzzerSettingOff denotes that the basic Buzzer (on touch) setting is off
BuzzerSettingOff = "OFF"
)

// WithDeviceID sets the Bluetooth device ID
func WithDeviceID(deviceID string) func(*Felicita) {
return func(f *Felicita) {
Expand Down Expand Up @@ -32,3 +44,11 @@ func WithLogger(logger scale.Logger) func(*Felicita) {
f.logger = logger
}
}

// WithForceBuzzerSettingOnConnect ensures that the basic Buzzer (on touch) setting
// is correct upon connection
func WithForceBuzzerSettingOnConnect(setting BuzzerSetting) func(*Felicita) {
return func(f *Felicita) {
f.forceBuzzerSettingOnConnect = setting
}
}

0 comments on commit 88e0fe4

Please sign in to comment.