44 var uiCallback ;
55
66 function Buzzer ( board , name , signal , gnd ) {
7- window . b = board ;
8- this . _name = name ;
9- this . _gnd = gnd ;
107 this . _board = board ;
8+ this . _name = name ;
119 this . _signal = signal ;
10+ this . _gnd = gnd ;
1211 this . _buf = [ ] ;
13- this . playing = false ;
14- this . ctx = new AudioContext ( ) ;
12+ this . _ctx = new AudioContext ( ) ;
13+ this . _playing = false ;
14+
1515 var self = this ;
16- board . on ( webduino . BoardEvent . SYSEX_MESSAGE , function ( sysex ) {
16+
17+ this . _board . on ( webduino . BoardEvent . SYSEX_MESSAGE , function ( sysex ) {
1718 var data = sysex . message ;
1819 var pin = data [ 2 ] ;
1920 if ( data [ 0 ] != 4 || data [ 1 ] != 7 || pin != self . _signal ) {
2324 var time = parseInt ( data [ 5 ] ) * 100 ;
2425 self . _buf . push ( [ freq , time ] ) ;
2526 self . play ( ) ;
26- //action(self, data.pin);
2727 } ) ;
28+ this . _board . on ( webduino . BoardEvent . BEFOREDISCONNECT , this . destroy . bind ( this ) ) ;
29+ this . _board . on ( webduino . BoardEvent . ERROR , this . destroy . bind ( this ) ) ;
2830 }
2931
30- function action ( buzzer , pin ) {
31- if ( pin . _number != buzzer . _signal ) {
32- return ;
33- }
34- var jsonData = {
35- action : {
36- id : buzzer . _board . _options . device ,
37- type : buzzer . _board . constructor . name ,
38- connector : [ {
39- name : buzzer . _name ,
40- type : "Buzzer" ,
41- intensity : pin . _value
42- } ]
43- }
44- } ;
45- uiCallback ( jsonData ) ;
46- }
47-
48-
4932 Buzzer . prototype = Object . create ( Buzzer . prototype , {
5033 constructor : { value : Buzzer }
5134 } ) ;
5538 }
5639
5740 Buzzer . prototype . play = function ( ) {
58- this . osc = this . ctx . createOscillator ( ) ;
41+ this . osc = this . _ctx . createOscillator ( ) ;
5942 this . osc . type = "triangle" ;
6043 this . osc . start ( 0 ) ;
6144
62- if ( this . playing ) {
45+ if ( this . _playing ) {
6346 return ;
6447 }
65- this . playing = true ;
48+ this . _playing = true ;
6649 var data = this . _buf . shift ( ) ;
6750 var hz = data [ 0 ] ;
6851 var durtime = data [ 1 ] ;
6952 console . log ( "hz:" , hz , ",time:" , durtime ) ;
7053 this . osc . frequency . value = hz ;
71- this . osc . connect ( this . ctx . destination ) ;
54+ this . osc . connect ( this . _ctx . destination ) ;
7255 var self = this ;
7356 setTimeout ( function ( ) {
7457 self . osc . disconnect ( ) ;
75- self . playing = false ;
58+ self . _playing = false ;
7659 if ( self . _buf . length > 0 ) {
7760 self . play ( ) ;
7861 }
7962 } , durtime ) ;
8063 }
8164
65+ Buzzer . prototype . destroy = function ( ) {
66+ this . _ctx . close ( ) ;
67+ } ;
68+
8269 scope . Buzzer = Buzzer ;
8370} ) ( Engine ) ;
0 commit comments