<commit_before>var HID = require('node-hid');

function usbController(pid,vid,config){
	var controller = new HID.HID(pid,vid);
	var eventTriggers = [];
	var previousValue;
	
	var reduceToConfVals = function(data,position){
		var keys = [];
		
		//TODO: we have to find the greatest possible value each time
		
		while(data !== 0){
			var greatest = [0,'key'];
		
			for(var prop in config){
				var curKey = config[prop]; 
				
				if(curKey.type === 'button' && curKey.pos === position && curKey.val <= data && curKey>greatest[0]){
					greatest[0] = curKey.val;
					greatest[1] = prop;
					keys.push(prop);
					data-= curKey.val;
				}
			}
			keys.push(greatest[1]);
			data-= greatest[0];
		}
		
		return keys;
	}
	
	var eventHandler = function(data,eventTrigger){
		var trigger = eventTrigger.eventTrigger;
		//console.log('triggered',trigger);
		var position = config[trigger].pos;
		var type = config[trigger].type;
		
		if(type === 'button'){
			var buttonVals = reduceToConfVals(data,position); //When multiple button are pressed we have to separate their values
			console.log(buttonVals);
			
			for(var i=0;i<buttonVals.length;i++){
				if(buttonVals[i] === trigger){
					eventTrigger.callback(data);
				}
			}
			
		}
		else if(type === 'axis'){
			eventTrigger.callback(data);
		}
	};
	
	controller.on('data',function(data){
		data = data.toJSON(); //By default, data is a Buffer object so we need to convert to an Array
		
		if(previousValue){
			//console.log('prevVal',previousValue);
			for(var i=0;i<eventTriggers.length;i++){
				var eventTrigger = eventTriggers[i].eventTrigger;
				var position = config[eventTrigger].pos;
				var callback = eventTriggers[i].callback;
			
				//if(config[eventTrigger].hasOwnProperty('defaultVal')){//Some inputs are not at 0 by default (perhaps not needed) try only checking for change
				if(data[position] !== previousValue[position]){//currently only checking for change<commit_msg>Fixed buttons registering<commit_after>var HID = require('node-hid');

function usbController(pid,vid,config){
	var controller = new HID.HID(pid,vid);
	var eventTriggers = [];
	var previousValue;
	
	var reduceToConfVals = function(data,position){
		var keys = [];
		
		//TODO: we have to find the greatest possible value each time
		
		while(data !== 0){
			var greatest = [0,'key'];
		
			for(var prop in config){
				var curKey = config[prop]; 
				
				if(curKey.type === 'button' && curKey.pos === position && curKey.val <= data && curKey.val>greatest[0]){
					greatest[0] = curKey.val;
					greatest[1] = prop;
				}
			}
			keys.push(greatest[1]);
			data-= greatest[0];
			//console.log(greatest);
		}
		
		return keys;
	}
	
	var eventHandler = function(data,eventTrigger){
		var trigger = eventTrigger.eventTrigger;
		//console.log('triggered',trigger);
		var position = config[trigger].pos;
		var type = config[trigger].type;
		
		if(type === 'button'){
			var buttonVals = reduceToConfVals(data,position); //When multiple button are pressed we have to separate their values
			//console.log(buttonVals);
			
			for(var i=0;i<buttonVals.length;i++){
				if(buttonVals[i] === trigger){
					eventTrigger.callback(data);
				}
			}
			
		}
		else if(type === 'axis'){
			eventTrigger.callback(data);
		}
	};
	
	controller.on('data',function(data){
		data = data.toJSON(); //By default, data is a Buffer object so we need to convert to an Array
		
		if(previousValue){
			//console.log('prevVal',previousValue);
			for(var i=0;i<eventTriggers.length;i++){
				var eventTrigger = eventTriggers[i].eventTrigger;
				var position = config[eventTrigger].pos;
				var callback = eventTriggers[i].callback;
			
				//if(config[eventTrigger].hasOwnProperty('defaultVal')){//Some inputs are not at 0 by default (perhaps not needed) try only checking for change
				if(data[position] !== previousValue[position]){//currently only checking for change
{'fraction_non_alphanumeric': 0.14560862865947613, 'fraction_numerical': 0.005136106831022085, 'mean_word_length': 3.698431845597105, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1138706', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1231, 'n_words': 303}