Skip to content

Commit 5f91cf8

Browse files
author
thyttan
committed
spotrem: add volume knob
1 parent 597a47e commit 5f91cf8

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed

apps/spotrem/ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ when fastloading.
1212
0.11: Further refactoring to shorten the code. Fixed search and play that was broken in v0.10.
1313
0.12: Fix some warnings from the linter.
1414
0.13: Move ui-handlers inside setUI-call.
15+
0.14: Add volume knob.
16+

apps/spotrem/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Swipe input:
88

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

11+
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.
12+
1113
It's possible to start tracks by searching with the remote. Search term without tags will override search with tags.
1214

1315
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).

apps/spotrem/app.js

Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,150 @@ let swipeHandler = function(LR, _) {
9797
}
9898
};
9999

100+
let dx = 0;
100101
let dy = 0;
102+
let volumeChangedThisGoAround = false;
103+
let knobTimeout;
101104
let dragHandler = function(e) {
102105

103106
let cb = ud => {
104-
if (ud) Bangle.musicControl(ud>0 ? "volumedown" : "volumeup");
107+
if (ud) Bangle.musicControl(ud<0 ? "volumedown" : "volumeup");
105108
}
106109

107-
// replace the ud callback functionality of setui "updown" mode (copy it here)
108-
// add on my volume knob.
110+
let resetOuterScopeVariables = ()=>{
111+
dy=0;
112+
dx=0;
113+
volumeChangedThisGoAround=false;
114+
}
115+
116+
dx += e.dx;
109117
dy += e.dy;
110-
if (!e.b) dy=0;
118+
if (!e.b) {resetOuterScopeVariables();}
119+
111120
while (Math.abs(dy)>32) {
112-
if (dy>0) { dy-=32; cb(1) }
113-
else { dy+=32; cb(-1) }
121+
if (dy>0) { dy-=32; cb(-1) }
122+
else { dy+=32; cb(1) }
123+
volumeChangedThisGoAround = true;
114124
Bangle.buzz(20);
115125
}
126+
127+
if (volumeChangedThisGoAround && Math.abs(dx)>32) {
128+
// setup volume knob here.
129+
cb(Math.sign(dx))
130+
resetOuterScopeVariables();
131+
let volumeKnob = dial(cb);
132+
let timingOutVolumeKnob = (e)=>{
133+
if (!e.b) {
134+
setKnobTimeout();
135+
} else if (knobTimeout) {
136+
clearTimeout(knobTimeout);
137+
knobTimeout = undefined;
138+
}
139+
volumeKnob(e);
140+
}
141+
let swipeMask = ()=>{
142+
E.stopEventPropagation();
143+
}
144+
let setKnobTimeout = ()=>{
145+
knobTimeout = setTimeout(()=>{
146+
Bangle.removeListener("drag", timingOutVolumeKnob)
147+
Bangle.removeListener("swipe", swipeMask);
148+
knobTimeout = undefined;
149+
print("removed volume knob")
150+
}, 350);
151+
}
152+
Bangle.prependListener("drag", timingOutVolumeKnob);
153+
Bangle.prependListener("swipe", swipeMask);
154+
}
116155
};
117156

157+
158+
dial = function(cb, options) {
159+
"ram";
160+
options = options || {};
161+
162+
const SCREEN_W = g.getWidth();
163+
const SCREEN_H = g.getHeight();
164+
165+
const DIAL_RECT = options.dialRect || {
166+
x: 0,
167+
y: 0,
168+
x2: SCREEN_W - 1,
169+
y2: SCREEN_H - 1,
170+
w: SCREEN_W,
171+
h: SCREEN_H,
172+
};
173+
174+
const CENTER = {
175+
x: DIAL_RECT.x + DIAL_RECT.w / 2,
176+
y: DIAL_RECT.y + DIAL_RECT.h / 2,
177+
};
178+
179+
const BASE_SCREEN_W = 176;
180+
const STEPS_PER_TURN = options.stepsPerWholeTurn || 10;
181+
const BASE_THRESHOLD = 50;
182+
const THRESHOLD =
183+
BASE_THRESHOLD *
184+
(10 / STEPS_PER_TURN) *
185+
(DIAL_RECT.w / BASE_SCREEN_W);
186+
187+
let cumulative = 0;
188+
189+
function onDrag(e) {
190+
"ram";
191+
192+
if (
193+
e.x < DIAL_RECT.x ||
194+
e.x > DIAL_RECT.x2 ||
195+
e.y < DIAL_RECT.y ||
196+
e.y > DIAL_RECT.y2
197+
) {
198+
return;
199+
}
200+
201+
if (e.y < CENTER.y) {
202+
cumulative += e.dx;
203+
} else {
204+
cumulative -= e.dx;
205+
}
206+
207+
if (e.x < CENTER.x) {
208+
cumulative -= e.dy;
209+
} else {
210+
cumulative += e.dy;
211+
}
212+
213+
function stepHandler(step) {
214+
Bangle.buzz(20, 0.3);
215+
cumulative -= THRESHOLD * step;
216+
cb(step);
217+
}
218+
219+
while (cumulative > THRESHOLD) {
220+
stepHandler(1);
221+
}
222+
while (cumulative < -THRESHOLD) {
223+
stepHandler(-1);
224+
}
225+
226+
E.stopEventPropagation();
227+
}
228+
229+
return onDrag;
230+
/*
231+
if (exports.dial._handler) {
232+
Bangle.removeListener("drag", dial._handler);
233+
}
234+
235+
exports.dial._handler = onDrag;
236+
Bangle.prependListener("drag", onDrag);
237+
238+
Bangle.prependListener("swipe", ()=>{
239+
E.stopEventPropagation();
240+
}
241+
);*/
242+
};
243+
118244
// Navigation input on the main layout
119245
let setUI = function() {
120246
Bangle.setUI(

apps/spotrem/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "spotrem",
33
"name": "Remote for Spotify",
4-
"version": "0.13",
4+
"version": "0.14",
55
"description": "Control spotify on your android device.",
66
"readme": "README.md",
77
"type": "app",

0 commit comments

Comments
 (0)