Skip to content

spotrem: add volume knob gesture #3847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
597a47e
spotrem: refactor copying setui ud logic into app
May 9, 2025
5f91cf8
spotrem: add volume knob
May 9, 2025
2966051
spotrem: lint error fix
May 9, 2025
8a78873
spotrem: tweak knob steps per turn, tweak buzz
May 14, 2025
09ee996
spotrem: add info re volume knob to readme
May 16, 2025
0b693cd
spotrem: increase buzz strength for volume dial
May 16, 2025
3df424b
spotrem: refactor moving buzzes to callback function
May 16, 2025
915546d
spotrem: add double buzz on deactivating volume knob
May 16, 2025
732c633
Merge remote-tracking branch 'upstream/master' into spotrem
May 25, 2025
7be4771
spotrem: tweak defaults of dial, call w/o options
May 25, 2025
068675e
Dial: new module providing a circular dial
May 25, 2025
0696b12
spotrem: refactor to use the Dial module
May 25, 2025
d99f528
Dial: add documentation (skeleton for now)
May 25, 2025
be7c1de
Dial: tweaks
May 25, 2025
4d8ff49
Dial: move to correct folder
May 25, 2025
9af1e6a
Dial: tweak stepsPerWholeTurn
May 31, 2025
e7b0af4
Dial: small refactor
May 31, 2025
656294b
Dial: add contents to documentation
May 31, 2025
0d7fb84
Dial_Display: complementary graphics to Dial module
May 31, 2025
9445bf4
Dial_Display: WIP generate a function drawing the dial
Jun 1, 2025
807add0
Merge remote-tracking branch 'upstream/master' into spotrem
Aug 1, 2025
08e23d8
Dial_Display: tweaks to debugging code
Aug 1, 2025
b4bcc6f
Dial_Display: drop debugging code
Aug 12, 2025
15ceb23
Dial: tweak docs
Aug 12, 2025
5ed691b
Dial_Display: improve docs
Aug 12, 2025
d5f5b22
Merge remote-tracking branch 'upstream/master' into spotrem
Aug 12, 2025
ea1a716
Dial_Display: tweak logic and docs
Aug 12, 2025
cb82231
spotrem: integrate dial graphics
Aug 12, 2025
9adab33
Dial_Display: add future work comment to docs
Aug 12, 2025
902bbf8
Dial: update docs re Dial_Display module
Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/spotrem/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ when fastloading.
0.11: Further refactoring to shorten the code. Fixed search and play that was broken in v0.10.
0.12: Fix some warnings from the linter.
0.13: Move ui-handlers inside setUI-call.
0.14: Add volume knob.

2 changes: 2 additions & 0 deletions apps/spotrem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Swipe input:

Swipe left/right to go to previous/next track. Swipe up/down to change the volume.

If you have changed the volume by swipe up/down, you can use a "volume knob" to continuously change the volume. Clock wise circle gesture on the screen increases volume and vice versa. The knob will deactivate shortly after you release the finger from the watch screen, indicated by a double buzz.

It's possible to start tracks by searching with the remote. Search term without tags will override search with tags.

To start playing 'from cold' the command for previous/next track via touch or swipe can be used. Pressing just play/pause is not guaranteed to initiate spotify in all circumstances (this will probably change with subsequent releases).
Expand Down
72 changes: 67 additions & 5 deletions apps/spotrem/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,80 @@ let swipeHandler = function(LR, _) {
}
};

let dx = 0;
let dy = 0;
let volumeChangedThisGoAround = false;
let knobTimeout;
let dragHandler = function(e) {

let DialDisplay = require("Dial_Display");
let volumeKnobVisual = new DialDisplay();

let cb = ud => {
Bangle.musicControl(ud<0 ? "volumedown" : "volumeup");
Bangle.buzz(20);
}

let resetOuterScopeVariables = ()=>{
dy=0;
dx=0;
volumeChangedThisGoAround=false;
}

dx += e.dx;
dy += e.dy;
if (!e.b) {resetOuterScopeVariables();}

while (Math.abs(dy)>32) {
if (dy>0) { dy-=32; cb(-1) }
else { dy+=32; cb(1) }
volumeChangedThisGoAround = true;
}

if (volumeChangedThisGoAround && Math.abs(dx)>32) {
// setup volume knob here.
let cbVisual = (step, value, reinit)=>{cb(step); volumeKnobVisual(step, value, reinit)};
cbVisual(Math.sign(dx)*Math.sign(g.getHeight()/2-e.y), 0, true)
resetOuterScopeVariables();
let volumeKnob = require("Dial")(cbVisual);
let timingOutVolumeKnob = (e)=>{
if (!e.b) {
setKnobTimeout();
} else if (knobTimeout) {
clearTimeout(knobTimeout);
knobTimeout = undefined;
}
volumeKnob(e);
}
let swipeMask = ()=>{
E.stopEventPropagation();
}
let setKnobTimeout = ()=>{
knobTimeout = setTimeout(()=>{
Bangle.removeListener("drag", timingOutVolumeKnob)
Bangle.removeListener("swipe", swipeMask);
Bangle.buzz(40);
setTimeout(Bangle.buzz, 150, 40, 0.8)
gfx();
knobTimeout = undefined;
print("removed volume knob")
}, 350);
}
Bangle.prependListener("drag", timingOutVolumeKnob);
Bangle.prependListener("swipe", swipeMask);
}
};

// Navigation input on the main layout
let setUI = function() {
Bangle.setUI(
{mode : "updown",
{mode : "custom",
touch: touchHandler,
swipe: swipeHandler,
drag: dragHandler,
btn: ()=>load(),
remove : ()=>widgetUtils.show(),
},
ud => {
if (ud) Bangle.musicControl(ud>0 ? "volumedown" : "volumeup");
}
}
);
};

Expand Down
2 changes: 1 addition & 1 deletion apps/spotrem/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "spotrem",
"name": "Remote for Spotify",
"version": "0.13",
"version": "0.14",
"description": "Control spotify on your android device.",
"readme": "README.md",
"type": "app",
Expand Down
75 changes: 75 additions & 0 deletions modules/Dial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function Dial(cb, options) {
"ram";
const SCREEN_W = g.getWidth();
const SCREEN_H = g.getHeight();

options = Object.assign(
{ stepsPerWholeTurn : 7, // 7 chosen as it felt the best in use.
dialRect : {
x: 0,
y: 0,
w: SCREEN_W,
h: SCREEN_H,
},
}, options);

const DIAL_RECT = options.dialRect;

const CENTER = {
x: DIAL_RECT.x + DIAL_RECT.w / 2,
y: DIAL_RECT.y + DIAL_RECT.h / 2,
};

const BASE_SCREEN_W = 176;
const STEPS_PER_TURN = options.stepsPerWholeTurn;
const BASE_THRESHOLD = 50;
const THRESHOLD =
BASE_THRESHOLD *
(10 / STEPS_PER_TURN) *
(DIAL_RECT.w / BASE_SCREEN_W);

let cumulative = 0;

function onDrag(e) {
"ram";

if (
e.x < DIAL_RECT.x ||
e.x > DIAL_RECT.x+DIAL_RECT.w-1 ||
e.y < DIAL_RECT.y ||
e.y > DIAL_RECT.y+DIAL_RECT.h-1
) {
return;
}

if (e.y < CENTER.y) {
cumulative += e.dx;
} else {
cumulative -= e.dx;
}

if (e.x < CENTER.x) {
cumulative -= e.dy;
} else {
cumulative += e.dy;
}

function stepHandler(step) {
cumulative -= THRESHOLD * step;
cb(step);
}

while (cumulative > THRESHOLD) {
stepHandler(1);
}
while (cumulative < -THRESHOLD) {
stepHandler(-1);
}

E.stopEventPropagation();
}

return onDrag;
}

exports = Dial;
60 changes: 60 additions & 0 deletions modules/Dial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Bangle.js Dial Library
======================

> Take a look at README.md for hints on developing with this library.

Usage
-----

```JS
var Dial = require("Dial");
var dial = new Dial(cb, options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use Dial here as a constructor (no this and it returns an object explicitly) so the new (while it works) might confuse users. What about:

Suggested change
var dial = new Dial(cb, options)
var dial = dial(cb, options)

(suggested lowercase dial as it's no longer a class too, don't mind if you prefer it Titlecase though)

Copy link
Collaborator Author

@thyttan thyttan Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - if that makes more sense I'm all for it.

I looked at the Layout module for inspiration when developing and taking a quick look now I fail to see how they differ in this regard. Out of interest, do they?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think I get what you mean now!

Bangle.on("drag", dial);

or

var dial = require("Dial")(cb, options);
Bangle.on("drag", dial);
```

For example:

```JS
let cb = (plusOrMinusOne)=>{print(plusOrMinusOne)};
let options = {
stepsPerWholeTurn : 6,
dialRect : {
x: 0,
y: 0,
w: g.getWidth()/2,
h: g.getHeight()/2,
}
}

let dial = require("Dial")(cb, options);
Bangle.on("drag", dial);
```

`cb` (first argument) is a callback function that should expect either `+1` or `-1` as argument when called.

`options` (second argument) (optional) is an object containing:

`stepsPerWholeTurn` - how many steps there are in one complete turn of the dial.
`dialRect` - decides the area of the screen the dial is set up on.

Defaults:
```js
{ stepsPerWholeTurn : 7
dialRect : {
x: 0,
y: 0,
w: g.getWidth(),
h: g.getHeight(),
},
}
```

Notes
-----

A complementary library for drawing graphics is provided in the Dial_Display module.
59 changes: 59 additions & 0 deletions modules/Dial_Display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
let dialDisplayGenerator = function(options) {
"ram";
const SCREEN_W = g.getWidth();
const SCREEN_H = g.getHeight();

options = Object.assign(
{ stepsPerWholeTurn : 7, // 7 chosen as it felt the best in use.
dialRect : {
x: 0,
y: 0,
w: SCREEN_W,
h: SCREEN_H,
},
}, options);

const DIAL_RECT = options.dialRect;

const CENTER = {
x: DIAL_RECT.x + DIAL_RECT.w / 2,
y: DIAL_RECT.y + DIAL_RECT.h / 2,
};

let dialDisplay = function(step, value, isReinit) {
let prevValue = this.value;
if (value!==undefined) this.value = value;
if (!this.value) this.value = 0;
if (this.isFirstDraw===undefined || isReinit) this.isFirstDraw = true;
this.value += step;
//g.setFont("Vector:30");
//g.drawString(this.value);

let drawCircle = (value, R, G, B, rad, isFill)=>{
let x = CENTER.x+27*Math.sin(value*(2*Math.PI/options.stepsPerWholeTurn));
let y = CENTER.y-27*Math.cos(value*(2*Math.PI/options.stepsPerWholeTurn));
g.setColor(R,G,B)
if (!isFill) g.drawCircle(x, y, rad);
if (isFill) g.fillCircle(x, y, rad);
Comment on lines +36 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps:

Suggested change
if (!isFill) g.drawCircle(x, y, rad);
if (isFill) g.fillCircle(x, y, rad);
if (!isFill) g.drawCircle(x, y, rad);
else g.fillCircle(x, y, rad);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole drawCircle should be removed I guess, when I think about it. I intended to use it more but now it's only used to make the small marks around the dial. (I had another design in mind at first where there would be small hollow circles around the dial, except for the one the indicator pointed to which would be filled.)

}
if (this.isFirstDraw) {
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 25);
Copy link
Collaborator Author

@thyttan thyttan Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the radii for the drawing operations should not be hardcoded to 25, 23, 9 etc but depend on the DIAL_RECT...

Copy link
Collaborator Author

@thyttan thyttan Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding a additional displayRect setting to the options object that if is not set defaults to what the dialRect option is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is ok for now but we could have a follow-up PR which infers a radius from the rect? Or like you say, adds a setting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, lets leave it as is for now :)

g.setColor(1,1,1).drawCircle(CENTER.x, CENTER.y, 25);
for (let i=0; i<options.stepsPerWholeTurn; i++) {
drawCircle(i, 1, 1, 1, 1, true);
}
this.isFirstDraw = false;
}

//drawCircle(this.value, 1, 1, 1, 2, false);
//drawCircle(prevValue, 0, 0, 0, 2, false);
g.setColor(0,0,0).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(prevValue*(2*Math.PI/options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(prevValue*(2*Math.PI/options.stepsPerWholeTurn)));
g.setColor(1,1,1).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(this.value*(2*Math.PI/options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(this.value*(2*Math.PI/options.stepsPerWholeTurn)));
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 9);

return this.value;
}
return dialDisplay;
}

exports = dialDisplayGenerator;
65 changes: 65 additions & 0 deletions modules/Dial_Display.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Bangle.js Dial Display Library
======================


> Take a look at README.md for hints on developing with this library.

Usage
-----

```JS
var DialDisplay = require("Dial_Display");
var dialDisplay = new DialDisplay(options);
Copy link
Collaborator

@bobrippling bobrippling Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same again (as the other comment), but here I think we'll want to change to make it more class-like, or alter how this is used within the dialDisplay function. At the moment it'll be writing to the global object.

For example, in the IDE:

> cb
=function (step) { ... }
>cb(1)
=undefined
>cb(1)
=undefined

// the dial renders successfully
// `this` variables are written to the global:

>isFirstDraw
=false
>value
=14

Happy to go through a few suggestions for it, if you like

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to go through a few suggestions for it, if you like

Please do! Fun with a learning opportunity :) Feel free to commit to this PR.

var value = dialDisplay(-1, 0, true)
```

For example in use with the Dial module:

```JS
var options = {
stepsPerWholeTurn : 6,
dialRect : {
x: 0,
y: 0,
w: g.getWidth()/2,
h: g.getHeight()/2,
}
}

var DialDisplay = require("Dial_Display");
var dialDisplay = new DialDisplay(options);

var cb = (step)=>{
var value = dialDisplay(step, undefined, true);
};

var Dial = require("Dial");
var dial = new Dial(cb, options)
Bangle.on("drag", dial);
```

`options` (first argument) (optional) is an object containing:

`stepsPerWholeTurn` - how many steps there are in one complete turn of the dial.
`dialRect` - decides the area of the screen the dial is set up on.

Defaults:
```js
{ stepsPerWholeTurn : 7
dialRect : {
x: 0,
y: 0,
w: g.getWidth(),
h: g.getHeight(),
},
}
```

The generated function takes three arguments:
`step` - +1 or -1
`value` - the previous value the step acts on.
`isReinit` - true/false, whether to draw all of the dial or just the indicator.

Notes:
======
- It would be nice to choose what colors are used. Something for the future.