11let connection ;
2- let ws_queue ;
3- let ws_cooldown_timer ;
2+ let queue ;
3+ let cooldownTimer ;
44
55function openWebsocketConnection ( ) {
6- const ws_uri = 'ws://' + ( location . hostname ? location . hostname : 'localhost' ) + ':81/' ;
7- connection = new WebSocket ( ws_uri , [ 'arduino' ] ) ;
6+ const uri = 'ws://' + ( location . hostname ? location . hostname : 'localhost' ) + ':81/' ;
7+ connection = new WebSocket ( uri , [ 'arduino' ] ) ;
88 connection . binaryType = 'arraybuffer' ;
99 connection . onopen = function ( e ) {
1010 document . getElementById ( 'navbarTitle' ) . innerHTML = 'FastLEDHub' ;
@@ -23,6 +23,45 @@ function openWebsocketConnection() {
2323 } ;
2424}
2525
26+ function setupSpinners ( ) {
27+ const spinners = document . getElementsByClassName ( 'spinner' ) ;
28+ [ ] . forEach . call ( spinners , ( spinnerDiv ) => {
29+ const inputElement = spinnerDiv . getElementsByTagName ( 'input' ) [ 0 ] ;
30+ inputElement . classList . add ( 'spinner-input' ) ;
31+
32+ const buttonDec = document . createElement ( "span" ) ;
33+ buttonDec . classList . add ( 'btn-spinner' , 'btn-spinner-dec' ) ;
34+ buttonDec . innerHTML = '−' ;
35+ buttonDec . addEventListener ( 'click' , ( ev ) => {
36+ const attrMin = inputElement . getAttribute ( 'min' ) ;
37+ const attrStep = inputElement . getAttribute ( 'step' ) ;
38+ const value = Number ( inputElement . value ) ;
39+ const step = attrStep ? Number ( attrStep ) : 1 ;
40+ const newValue = value - step ;
41+ if ( value !== newValue ) {
42+ inputElement . value = attrMin ? Math . max ( Number ( attrMin ) , newValue ) : newValue ;
43+ }
44+ } ) ;
45+
46+ const buttonInc = document . createElement ( "span" ) ;
47+ buttonInc . classList . add ( 'btn-spinner' , 'btn-spinner-inc' ) ;
48+ buttonInc . innerHTML = '+' ;
49+ buttonInc . addEventListener ( 'click' , ( ev ) => {
50+ const attrMin = inputElement . getAttribute ( 'max' ) ;
51+ const attrStep = inputElement . getAttribute ( 'step' ) ;
52+ const value = Number ( inputElement . value ) ;
53+ const step = attrStep ? Number ( attrStep ) : 1 ;
54+ const newValue = value + step ;
55+ if ( value !== newValue ) {
56+ inputElement . value = attrMin ? Math . min ( Number ( attrMin ) , newValue ) : newValue ;
57+ }
58+ } ) ;
59+
60+ inputElement . insertAdjacentElement ( 'beforebegin' , buttonDec ) ;
61+ inputElement . insertAdjacentElement ( 'afterend' , buttonInc ) ;
62+ } ) ;
63+ }
64+
2665function displayConnectedState ( connected ) {
2766 document . getElementById ( 'settingsButton' ) . style . display = connected ? 'block' : 'none' ;
2867 document . getElementById ( 'controlsWrapper' ) . style . display = connected ? 'block' : 'none' ;
@@ -168,27 +207,27 @@ function sendConfig() {
168207}
169208
170209function sendBytes ( bytes ) {
171- ws_queue = bytes ;
210+ queue = bytes ;
172211
173- if ( ws_cooldown_timer )
212+ if ( cooldownTimer )
174213 return ;
175214
176215 sendBytesQueue ( )
177- ws_cooldown_timer = setTimeout ( ( ) => {
178- ws_cooldown_timer = null ;
216+ cooldownTimer = setTimeout ( ( ) => {
217+ cooldownTimer = null ;
179218 sendBytesQueue ( ) ;
180219 } , 15 ) ;
181220}
182221
183222function sendBytesQueue ( ) {
184- if ( ! ws_queue || connection . readyState != 1 )
223+ if ( ! queue || connection . readyState != 1 )
185224 return ;
186225
187- const data = Uint8Array . from ( ws_queue ) ;
226+ const data = Uint8Array . from ( queue ) ;
188227 connection . send ( data . buffer ) ;
189228 console . log ( 'Sent bytes: ' + data ) ;
190229
191- ws_queue = null ;
230+ queue = null ;
192231}
193232
194233function sendText ( text ) {
@@ -204,12 +243,9 @@ function sendText(text) {
204243window . onload = ( ) => {
205244 openWebsocketConnection ( ) ;
206245
246+ setupSpinners ( ) ;
247+
207248 settingsOffcanvas . addEventListener ( 'hidden.bs.offcanvas' , function ( ) {
208249 sendConfig ( ) ;
209250 } ) ;
210-
211- $ ( '#alarmDuration' ) . TouchSpin ( { min : 1 , max : 1439 , postfix : 'minutes' } ) ;
212- $ ( '#timeZone' ) . TouchSpin ( { min : - 23 , max : 23 , prefix : 'GMT+' } ) ;
213- $ ( '#sunsetDuration' ) . TouchSpin ( { min : 1 , max : 1439 , postfix : 'minutes' } ) ;
214- $ ( '#sunsetOffset' ) . TouchSpin ( { min : - 1439 , max : 1439 , postfix : 'minutes' } ) ;
215251} ;
0 commit comments