<commit_before>(function() {
  var Arduino = function(options) {
    options = options || {};

    // Private
    var commandBuffer = [],
    self = this;

    var onReceive = function(results) {
      $(this).trigger('receive', [results]);
    };

    // Public members
    this.comet = new window.Comet(options.comet);
    this.comet.onreceive = onReceive.bind(this);

    // Public methods
    this.pinMode = function(pin, mode) {
      commandBuffer.push({
        command: 'pinMode',
        pin: pin,
        // We want 'i' or 'o' for the pinmode
        args: [mode[0]]        
      });

      return this;
    };

    this.pinModes = function(modes) {
      var len = modes.length,
      i = 0;

      for (;i < len;i++) {
        var mode = modes[i];
        this.pinMode(mode[0], mode[1]);
      }

      return this;
    };

    this.digitalWrite = function(pin, value) {
      commandBuffer.push({
        command: 'digitalWrite',
        pin: pin,
        args: [value ? Arduino.HIGH : Arduino.LOW]        
      });

      return this;
    };

    this.digitalRead = function(pin) {
      commandBuffer.push({
        command: 'digitalRead',
        pin: pin
      });

      return this;
    };

    this.analogRead = function(pin) {
      commandBuffer.push({
        command: 'analogRead',
        pin: pin       
      });

      return this;
    };

    this.analogWrite = function(pin, value) {
      commandBuffer.push({
        command: 'analogWrite',
        pin: pin,
        args: [value]        
      });

      return this;
    };    

    this.flush = function() {
      var deferred = $.Deferred();
      if (commandBuffer.length == 0) { 
        deferred.resolveWith(this, [[]]); 
        return deferred.promise();
      }

      $(this).trigger('send', [commandBuffer]);
      this.comet.sendObject(commandBuffer, function(results) {
        deferred.resolveWith(self, [results]);
      });

      commandBuffer = [];

      return deferred.promise();
    };

    this.on = function(events, handler) {
      $(this).on(events, handler);
    };

    this.off = function(events, handler) {
      $(this).off(events, handler);
    };

    this.open = function(ready) {
      this.comet.open(ready.bind(this));
    };

    this.close = function() {
      this.comet.close();
    };
  };
<commit_msg>Added callbacks for individual commands<commit_after>(function() {
  var Arduino = function(options) {
    options = options || {};

    // Private
    var commandBuffer = [],
    self = this;

    var onReceive = function(results) {
      $(this).trigger('receive', [results]);
    };

    // Public members
    this.comet = new window.Comet(options.comet);
    this.comet.onreceive = onReceive.bind(this);

    // Public methods
    this.pinMode = function(pin, mode, callback) {
      commandBuffer.push({
        command: 'pinMode',
        pin: pin,
        // We want 'i' or 'o' for the pinmode
        args: [mode[0]],
        callback: callback        
      });

      return this;
    };

    this.pinModes = function(modes) {
      var len = modes.length,
      i = 0;

      for (;i < len;i++) {
        var mode = modes[i];
        this.pinMode(mode[0], mode[1]);
      }

      return this;
    };

    this.digitalWrite = function(pin, value, callback) {
      commandBuffer.push({
        command: 'digitalWrite',
        pin: pin,
        args: [value ? Arduino.HIGH : Arduino.LOW],
        callback: callback        
      });

      return this;
    };

    this.digitalRead = function(pin, callback) {
      commandBuffer.push({
        command: 'digitalRead',
        pin: pin,
        callback: callback
      });

      return this;
    };

    this.analogRead = function(pin, callback) {
      commandBuffer.push({
        command: 'analogRead',
        pin: pin,
        callback: callback              
      });

      return this;
    };

    this.analogWrite = function(pin, value, callback) {
      commandBuffer.push({
        command: 'analogWrite',
        pin: pin,
        args: [value],
        callback: callback              
      });

      return this;
    };    

    this.flush = function() {
      var deferred = $.Deferred();
      if (commandBuffer.length == 0) { 
        deferred.resolveWith(this, [[]]); 
        return deferred.promise();
      }

      $(this).trigger('send', [commandBuffer]);
      var callbacks = commandBuffer.map(function(o) { 
        var cb = o.callback;
        delete o.callback; // Note the side affect
        return cb;
      });

      this.comet.sendObject(commandBuffer, function(results) {
        var i = 0, len = callbacks.length;

        for (;i < len;i++) {
          var cb = callbacks[i];
          if (typeof cb === 'function') { cb.apply(self, [results[i]]); }
        }

        deferred.resolveWith(self, [results]);
      });

      commandBuffer = [];

      return deferred.promise();
    };

    this.on = function(events, handler) {
      $(this).on(events, handler);
    };

    this.off = function(events, handler) {
      $(this).off(events, handler);
    };

    this.open = function(ready) {
      this.comet.open(ready.bind(this));
    };

    this.close = function() {
      this.comet.close();
    };
  };

{'fraction_non_alphanumeric': 0.14609956131985505, 'fraction_numerical': 0.002098035475872592, 'mean_word_length': 1.867140513942045, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6859158', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1549, 'n_words': 334}