1+ + ( function ( factory ) {
2+ if ( typeof exports === 'undefined' ) {
3+ factory ( webduino || { } ) ;
4+ } else {
5+ module . exports = factory ;
6+ }
7+ } ( function ( scope ) {
8+ 'use strict' ;
9+
10+ var Module = scope . Module ,
11+ BoardEvent = scope . BoardEvent ,
12+ proto ;
13+
14+ var MAX44009_MESSAGE = [ 0x04 , 0x42 ] ;
15+
16+ var MAX44009Event = {
17+ MESSAGE : 'message'
18+ } ;
19+
20+ function MAX44009 ( board , sda , scl ) {
21+ Module . call ( this ) ;
22+ this . board = board ;
23+ this . init = false ;
24+ this . lux = 0 ;
25+ this . _sda = sda ;
26+ this . _scl = scl ;
27+ this . callback = function ( ) { } ;
28+ this . messageHandler = onMessage . bind ( this ) ;
29+ this . board . send ( [ 0xf0 , 0x04 , 0x42 , 0x00 , this . _sda , this . _scl , 0xf7 ] ) ;
30+ }
31+
32+ function onMessage ( event ) {
33+ var msg = event . message ;
34+ if ( msg [ 0 ] == MAX44009_MESSAGE [ 0 ] && msg [ 1 ] == MAX44009_MESSAGE [ 1 ] ) {
35+ this . emit ( MAX44009Event . MESSAGE , msg . slice ( 2 ) ) ;
36+ }
37+ }
38+
39+ MAX44009 . prototype = proto = Object . create ( Module . prototype , {
40+ // constructor: {
41+ // value: MAX44009
42+ // },
43+ // state: {
44+ // get: function() {
45+ // return this.state;
46+ // },
47+ // set: function(val) {
48+ // this.state = val;
49+ // }
50+ // }
51+ } ) ;
52+
53+ proto . on = function ( callback ) {
54+ this . board . send ( [ 0xf0 , 0x04 , 0x42 , 0x01 , 0xf7 ] ) ;
55+ if ( typeof callback !== 'function' ) {
56+ callback = function ( ) { } ;
57+ }
58+ this . callback = function ( rawData ) { // rawData為array
59+ var val = '' ;
60+ console . log ( rawData ) ;
61+ for ( var i = 0 ; i < rawData . length ; i ++ ) {
62+ val += String . fromCharCode ( rawData [ i ] ) ;
63+ }
64+ var StrAra = '[' + val + ']' ;
65+ var info = eval ( StrAra ) ;
66+ this . lux = info [ 0 ] ;
67+ callback ( this . lux ) ;
68+ } ;
69+ this . state = 'on' ;
70+ this . board . on ( BoardEvent . SYSEX_MESSAGE , this . messageHandler ) ;
71+ this . addListener ( MAX44009Event . MESSAGE , this . callback ) ;
72+ } ;
73+
74+ proto . off = function ( ) {
75+ this . state = 'off' ;
76+ this . board . send ( [ 0xf0 , 0x04 , 0x42 , 0x02 , 0xf7 ] ) ;
77+ this . board . removeListener ( BoardEvent . SYSEX_MESSAGE , this . messageHandler ) ;
78+ this . removeListener ( MAX44009Event . MESSAGE , this . callback ) ;
79+ this . callback = null ;
80+ } ;
81+
82+ scope . module . MAX44009 = MAX44009 ;
83+ } ) ) ;
0 commit comments