<commit_before>// BigRedButton.js - Dream Cheeky Big Red Button node.js/node-hid driver
// MIT licensed. (C) <NAME> 2013

var HID = require('node-hid');
var util = require('util');
var events = require('events');

var allDevices;
var cmd_status=new Buffer([ 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 ]);
var last_state;

var LID_DOWN=0x15, LID_UP=0x17, BUTTON_DOWN=0x16;

function getAllDevices()
{
    if (!allDevices) {
        allDevices = HID.devices(7476,13);
    }
    return allDevices;
}

function BigRedButton(index)
{
    if (!arguments.length) {
            index = 0;
    }

    var bigRedButton = getAllDevices();
    if (!bigRedButton.length) {
        throw new Error("No BigRedButton could be found");
    }
    if (index > bigRedButton.length || index < 0) {
        throw new Error("Index " + index + " out of range, only " + bigRedButton.length + " BigRedButton found");
    }
    this.button = bigRedButton[index];
    this.hid = new HID.HID(bigRedButton[index].path);

    this.hid.write(cmd_status);
    
    var that=this;
    this.hid.read(function(error,data) {
    	last_state=data[0];
    	that.hid.read(that.interpretData.bind(that));
    });
    this.interval = setInterval(this.askForStatus.bind(this),100);
    this.close = function() {
       clearInterval(this.interval);
       this.interval = false;
       setTimeout(function() {
          this.hid.close();
       }.bind(this), 100);
    };
}

util.inherits(BigRedButton, events.EventEmitter);

BigRedButton.prototype.askForStatus = function() {
	 this.hid.write(cmd_status);
};

BigRedButton.prototype.interpretData = function(error, data) {
   if (!this.interval || error || !data) {
      this.close();
      return;
   }
   var n_state=data[0];

   if(last_state!=n_state) {
   		if(last_state==LID_DOWN && n_state==LID_UP) {
   			this.emit("lidRaised");
   		} else if (last_state==LID_UP && n_state==BUTTON_DOWN) {
   			this.emit("buttonPressed");
   		} else if(last_state==BUTTON_DOWN && n_state==LID_UP) {
   			this.emit("buttonReleased");
   		} else if(last_state==BUTTON_DOWN && n_state==LID_DOWN) {
   			this.emit("buttonReleased");
   			this.emit("lidClosed");
   		} else if(last_state==LID_UP && n_state==LID_DOWN) {
   			this.emit("lidClosed");
   		}
   		last_state=n_state;
   }

   this.hid.read(this.interpretData.bind(this));
}

BigRedButton.prototype.isLidUp = function() {
	return last_state==LID_UP || last_state==BUTTON_DOWN;
}

BigRedButton.prototype.isButtonPressed = function() {
	return last_state==BUTTON_DOWN;
}

BigRedButton.prototype.isLidDown = function() {
	return last_state==LID_DOWN;
}

exports.BigRedButton = BigRedButton;
exports.deviceCount = function () { return getAllDevices().length; }


       <commit_msg>Have consistent spacing.<commit_after>// BigRedButton.js - Dream Cheeky Big Red Button node.js/node-hid driver
// MIT licensed. (C) <NAME> 2013

var HID = require('node-hid');
var util = require('util');
var events = require('events');

var allDevices;
var cmd_status=new Buffer([ 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 ]);
var last_state;

var LID_DOWN=0x15, LID_UP=0x17, BUTTON_DOWN=0x16;

function getAllDevices()
{
   if (!allDevices) {
      allDevices = HID.devices(7476,13);
   }
   return allDevices;
}

function BigRedButton(index)
{
   if (!arguments.length) {
      index = 0;
   }

   var bigRedButton = getAllDevices();
   if (!bigRedButton.length) {
      throw new Error("No BigRedButton could be found");
   }
   if (index > bigRedButton.length || index < 0) {
      throw new Error("Index " + index + " out of range, only " + bigRedButton.length + " BigRedButton found");
   }
   this.button = bigRedButton[index];
   this.hid = new HID.HID(bigRedButton[index].path);

   this.hid.write(cmd_status);

   var that=this;
   this.hid.read(function(error,data) {
      last_state=data[0];
      that.hid.read(that.interpretData.bind(that));
   });
   this.interval = setInterval(this.askForStatus.bind(this),100);
   this.close = function() {
      clearInterval(this.interval);
      this.interval = false;
      setTimeout(function() {
         this.hid.close();
      }.bind(this), 100);
   };
}

util.inherits(BigRedButton, events.EventEmitter);

BigRedButton.prototype.askForStatus = function() {
   this.hid.write(cmd_status);
};

BigRedButton.prototype.interpretData = function(error, data) {
   if (!this.interval || error || !data) {
      this.close();
      return;
   }
   var n_state=data[0];

   if (last_state!=n_state) {
      if (last_state==LID_DOWN && n_state==LID_UP) {
         this.emit("lidRaised");
      } else if (last_state==LID_UP && n_state==BUTTON_DOWN) {
         this.emit("buttonPressed");
      } else if (last_state==BUTTON_DOWN && n_state==LID_UP) {
         this.emit("buttonReleased");
      } else if (last_state==BUTTON_DOWN && n_state==LID_DOWN) {
         this.emit("buttonReleased");
         this.emit("lidClosed");
      } else if (last_state==LID_UP && n_state==LID_DOWN) {
         this.emit("lidClosed");
      }
      last_state=n_state;
   }

   this.hid.read(this.interpretData.bind(this));
}

BigRedButton.prototype.isLidUp = function() {
   return last_state==LID_UP || last_state==BUTTON_DOWN;
}

BigRedButton.prototype.isButtonPressed = function() {
   return last_state==BUTTON_DOWN;
}

BigRedButton.prototype.isLidDown = function() {
   return last_state==LID_DOWN;
}

exports.BigRedButton = BigRedButton;
exports.deviceCount = function () { return getAllDevices().length; }
{'fraction_non_alphanumeric': 0.14754990925589836, 'fraction_numerical': 0.019600725952813067, 'mean_word_length': 3.5283483976992605, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1250437', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1973, 'n_words': 339}