var STK = require('stk500');
var colors = require('colors');
var tools = require('./tools');
var Protocol = require('./protocol');
var util = require('util');

var Stk500v1 = function(options) {
  options.protocol = STK;
  Protocol.call(this, options);
};

util.inherits(Stk500v1, Protocol);

/**
 * Uploads the provided hex file to the board, via the stk500v1 protocol
 *
 * @param {string} file - path to hex file for uploading
 * @param {function} callback - function to run upon completion/error
 */
Stk500v1.prototype._upload = function(file, callback) {
  var _this = this;

  this.serialPort = this.connection.serialPort;

  // open/parse supplied hex file
  var hex = tools._parseHex(file);
  if (!Buffer.isBuffer(hex)) {
    return callback(hex);
  }

  // open connection
  _this.serialPort.open(function(error) {
    if (error) { return callback(error); }

    _this.debug('connected');

    // reset
    _this._reset(function(error) {
      if (error) { return callback(error); }

      _this.debug('flashing, please wait...');

      // flash
      _this.chip.bootload(_this.serialPort, hex, _this.board, function(error) {
        var color = (error ? colors.red : colors.green);

        _this.debug(color('flash complete.'));

        // Always close the serialport
        _this.serialPort.close();

        return callback(error);
      });
    });
  });
};

Stk500v1.prototype._reset = function(callback) {
  var _this = this;

  _this.connection._setDTR(true, 250, function(error) {
    if (!error) {
      _this.debug('reset complete.');
    }

    return callback(error);
  });
};

module.exports = Stk500v1;

@@ -1439,16 +1439,119 @@
  this;%0A%0A
+  _this.connection._setDTR(false, 250, function(error) %7B%0A    if (error) %7B return callback(error); %7D%0A%0A  
   _this.
@@ -1575,17 +1575,16 @@
 R(true, 
-2
 50, func
@@ -1597,24 +1597,26 @@
 rror) %7B%0A    
+ 
+ 
 if (!error) 
@@ -1613,24 +1613,26 @@
  (!error) %7B%0A
+  
       _this.
@@ -1661,19 +1661,23 @@
 ');%0A    
+ 
+ 
 %7D%0A%0A
+  
     retu
@@ -1696,16 +1696,24 @@
 error);%0A
+    %7D);%0A
   %7D);%0A%7D;

reset like avrdude - DTR low 250 ms, then DTR high 50 ms