You are reading a single comment by @unknowndomain and its replies. Click here to read the full conversation.
  • I've been working on creating a module for DMX... Not done this before, how does this stack up?

    var exports = {};
    
    function DMX( serial, interrupt ) {
      this.data = new Uint8Array( 512 );
      this.id = 0;
      this.serial = serial;
      this.interrupt = interrupt;
      
      // Setup serial port
      this.serial.setup( 250000 );
      this.serial.on( 'data', this.newData );
      
      // Listeners
      this.dataListeners = [];
      this.channelListeners = {};
      
      // Listen for the interrupt pin to drop
      setWatch( this.newFrame, this.interrupt, { repeat: true, edge: 'falling' } );
    }
    
    // Handle new bytes of data as they come in
    DMX.prototype.newData = function( d ) {
      // Store the bytes received at the buffer index
      DMX.data.set( d, DMX.id );
      
      // Move the buffer index along.
      DMX.id += d.length;
    };
    
    // Send out notifications to all event listeners when frame is complete.
    DMX.prototype.newFrame = function() {
      // Reset the byte buffer index to 0 for the next frame of data.
      DMX.id = 0;
      
      // Itterate through all the registered data listeners and send them the data
      for ( var i in DMX.dataListeners )
        DMX.dataListeners[i]( DMX.data );
    
      // Itterate through all the registered channel listeners and send them their channel values
      for ( var ch in DMX.channelListeners )
        for ( var j in DMX.channelListeners[ch] )
          DMX.channelListeners[ch][j]( DMX.data[ ch ] );
    };
    
    // Handle registration to event notifications
    DMX.prototype.on = function( type, handler ) {
      // If data listener: push function onto array
      if ( type == 'data' ) DMX.dataListeners.push( handler ); 
      
      // If channel listener
      if ( type >= 1 && type <= 512 ) {
        
        // Check channel for existing listeners
        if ( DMX.channelListeners[type] !== undefined ) {
          // Push additional listener onto array
          DMX.channelListeners[type].push( handler );
        } else {
          // Otherwise: create new array with listener in it
          DMX.channelListeners[type] = [ handler ];
        }
      }
    };
    
    // Export the module
    exports.connect = function( serial, interrupt ) {
      return new DMX( serial, interrupt );
    };
    
    ///////////////
    
    var DMX = exports.connect( Serial2, B9 );
    
    // Listen for changes on channel 1
    DMX.on( 1, function( val ) {
      analogWrite( LED1, val / 255, { forceSoft: true } );
    } );
    
    // Listen for changes on channel 2
    DMX.on( 2, function( val ) {
      analogWrite( LED2, val / 255, { forceSoft: true } );
    } );
    
    // Listen for changes on channel 3
    DMX.on( 3, function( val ) {
      analogWrite( LED3, val / 255, { forceSoft: true } );
    } );
    
    // Listen for full frames of data
    DMX.on( 'data', function( data ) {
    //  console.log( data );
    } );
    
About