1+ let EventEmitter = require ( 'events' )
2+ let util = require ( 'util' )
3+
4+ let _ = require ( 'lodash' )
5+
16let { JsPrototype, JsDevkit } = require ( './nativeBinding' )
27
3- class Plinth {
8+ class Plinth extends EventEmitter {
49 constructor ( model = 'devkit' ) {
10+ super ( )
11+
512 if ( model == 'prototype' ) {
613 this . plinth = new JsPrototype ( )
714 }
@@ -21,15 +28,22 @@ class Plinth {
2128 }
2229}
2330
24- class Well {
31+ class Well extends EventEmitter {
2532 constructor ( id , plinth ) {
33+ super ( )
34+
2635 this . id = id
2736 this . plinth = plinth
2837 this . maxMemory = 4096 // bytes. basically 4kb
2938 this . dimensions = {
3039 x : 128 ,
3140 y : 296 ,
3241 }
42+
43+ this . buttonPressBuffer = new Map ( ) // for detecting chorded button presses
44+ this . chordTimeout = 35 // amount of time in milliseconds between button presses which will count as being pressed at the same time to form a chord
45+ this . endOfChordTimer = setTimeout ( ( ) => { } , 1 )
46+ this . on ( 'buttonPress' , this . _emitChordedButtonPress )
3347 }
3448
3549 // display an image on the e-paper display of the wyldcard present in this well
@@ -69,25 +83,52 @@ class Well {
6983 // register a callback to be called when Switch A (the top button) for this well is pressed
7084 onAButtonPress = function ( cb ) {
7185 validateCallback ( cb )
86+ cb = this . _wrapCallbackToEmitEvents ( cb , 'a' )
7287 this . plinth . setSwitchCallback ( this . id , 'a' , cb )
7388 }
7489
7590 // register a callback to be called when Switch B (the middle button) for this well is pressed
7691 onBButtonPress = function ( cb ) {
7792 validateCallback ( cb )
93+ cb = this . _wrapCallbackToEmitEvents ( cb , 'b' )
7894 this . plinth . setSwitchCallback ( this . id , 'b' , cb )
7995 }
8096
8197 // register a callback to be called when Switch C (the bottom button) for this well is pressed
8298 onCButtonPress = function ( cb ) {
8399 validateCallback ( cb )
100+ cb = this . _wrapCallbackToEmitEvents ( cb , 'c' )
84101 this . plinth . setSwitchCallback ( this . id , 'c' , cb )
85102 }
103+
104+ _wrapCallbackToEmitEvents = function ( cb , buttonId ) {
105+ let well = this
106+
107+ return async ( ) => {
108+ well . emit ( 'buttonPress' , { well : well . id , button : buttonId , ts : Date . now ( ) } )
109+ cb ( )
110+ }
111+ }
112+
113+ _emitChordedButtonPress = function ( buttonPressEvent ) {
114+ let well = this
115+ clearTimeout ( well . endOfChordTimer )
116+ let chordTimeout = well . chordTimeout
117+ well . buttonPressBuffer . set ( buttonPressEvent . button , buttonPressEvent . ts )
118+
119+ let recentPresses = _ . filter ( Array . from ( this . buttonPressBuffer . entries ( ) ) , ( [ button , ts ] ) => ts > buttonPressEvent . ts - chordTimeout )
120+ recentPresses = recentPresses . map ( ( [ button , ts ] ) => button )
121+
122+ well . endOfChordTimer = setTimeout ( ( ) => {
123+ well . emit ( 'chordedButtonPress' , { well : well . id , buttons : recentPresses } )
124+ well . buttonPressBuffer = new Map ( )
125+ } , chordTimeout )
126+ }
86127}
87128
88129function validateCallback ( cb ) {
89130 if ( ! util . types . isAsyncFunction ( cb ) ) {
90- throw new error ( 'callbacks passed to `onXButtonPress()` handlers must be async functions' )
131+ throw new Error ( 'callbacks passed to `onXButtonPress()` handlers must be async functions' )
91132 }
92133}
93134
0 commit comments