<commit_before>var fs        = require('fs');
var microtime = require('microtime');

/*
 A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi
 @copyright 2013 <NAME> (MIT-License)
*/

function RPiWS2801(){
  this.spiDevice = '/dev/spidev0.0';
  this.numLEDs = 32;
  this.spiFd = null; //filedescriptor for spidevice
  this.inverted = false;
  this.reversed = false;	
  this.gamma = 2.5;
  this.redIndex = 0;
  this.greenIndex = 1;
  this.blueIndex = 2;
  this.gammatable = new Array(256);
  this.bytePerPixel = 3; //RGB
  this.channelCount = this.numLEDs*this.bytePerPixel;
  this.values = new Buffer(this.channelCount);
  this.rowResetTime = 1000; // number of us CLK has to be pulled low (=no writes) for frame reset
    						            // manual of WS2801 says 500 is enough, however we need at least 1000
  this.lastWriteTime = microtime.now()-this.rowResetTime-1; //last time something was written to SPI
    												                                //required for save WS2801 reset	
  // clear buffer    												                                
  for( var i = 0; i < this.channelCount; i++) {
    this.values[i] = 0;
  }
}

RPiWS2801.prototype = {
  /*
   * connect to SPI port
   */
  connect: function(numLEDs, spiDevice, gamma){
    // sanity check for params
    if ((numLEDs !== parseInt(numLEDs)) || (numLEDs<1)) {
      console.error("invalid param for number of LEDs, plz use integer >0");
      return false;
    }
    if (spiDevice){
      this.spiDevice = spiDevice;
    }
    // connect synchronously
    try{
      this.spiFd = fs.openSync(this.spiDevice, 'w');
    } catch (err) {
      console.error("error opening SPI device "+this.spiDevice, err);
      return false;
    }
    this.numLEDs = numLEDs;
    this.gamma = gamma ? gamma : 2.5; //set gamma correction value
    // compute gamma correction table
    for (var i=0; i<256; i++)
      this.gammatable[i] = Math.round(255*Math.pow(i/255, this.gamma));
    //console.log("gammatable" + this.gammatable);
  },

  /*
   * disconnect from SPI port
   */
  disconnect : function(){
    if (this.spiFd) fs.closeSync(this.spiFd);
  },

  /*
   * send stored buffer with RGB values to WS2801 stripe
   */
  update: function(){
    if (this.spiFd) {
      this.sendRgbBuffer(this.values);
    }  
  },
  
  /*
   * invert all colors
   */
  invert: function(){
    this.inverted = !this.inverted;
  }, // end invert
  
  /*
   * send buffer with RGB values to WS2801 stripe in reverse order
   */        
  reverse: function(){
    this.reversed = !this.reversed;
  }, // end reverse

  /*
   * clear RGB stripe
   */  
  clear: function(){
    this.fill(0x00, 0x00, 0x00); 
  }, // end clear

  /*
   * set new RGB index order
   */
  setColorIndex: function(redIndex, greenIndex, blueIndex){
    this.redIndex = redIndex;
    this.greenIndex = greenIndex;
    this.blueIndex = blueIndex;
  }, // end setColorIndex

  /*
   * send buffer with RGB values to WS2801 stripe
   */
  sendRgbBuffer : function(buffer){
    // checking if enough time passed for resetting stripe
    if (microtime.now() > (this.lastWriteTime + this.rowResetTime)){
      // yes, its o.k., lets write
      // but first do gamma correction
      var adjustedBuffer = new Buffer(buffer.length);
      for (var i=0; i < buffer.length; i++){
        adjustedBuffer[i]=this.gammatable[buffer[i]];
      }
      fs.writeSync(this.spiFd, adjustedBuffer, 0, buffer.length, null);
      this.lastWriteTime = microtime.now();
      return true;
    }
    console.log('writing to fast, data dropped');
    return false;	
  }, // end sendRgbBuffer<commit_msg>Fixed typo<commit_after>var fs        = require('fs');
var microtime = require('microtime');

/*
 A node.js library to control a WS2801 RGB LED stripe via SPI with your Raspberry Pi
 @copyright 2013 <NAME> (MIT-License)
*/

function RPiWS2801(){
  this.spiDevice = '/dev/spidev0.0';
  this.numLEDs = 32;
  this.spiFd = null; //filedescriptor for spidevice
  this.inverted = false;
  this.reversed = false;	
  this.gamma = 2.5;
  this.redIndex = 0;
  this.greenIndex = 1;
  this.blueIndex = 2;
  this.gammatable = new Array(256);
  this.bytePerPixel = 3; //RGB
  this.channelCount = this.numLEDs*this.bytePerPixel;
  this.values = new Buffer(this.channelCount);
  this.rowResetTime = 1000; // number of us CLK has to be pulled low (=no writes) for frame reset
    						            // manual of WS2801 says 500 is enough, however we need at least 1000
  this.lastWriteTime = microtime.now()-this.rowResetTime-1; //last time something was written to SPI
    												                                //required for save WS2801 reset	
  // clear buffer    												                                
  for( var i = 0; i < this.channelCount; i++) {
    this.values[i] = 0;
  }
}

RPiWS2801.prototype = {
  /*
   * connect to SPI port
   */
  connect: function(numLEDs, spiDevice, gamma){
    // sanity check for params
    if ((numLEDs !== parseInt(numLEDs)) || (numLEDs<1)) {
      console.error("invalid param for number of LEDs, plz use integer >0");
      return false;
    }
    if (spiDevice){
      this.spiDevice = spiDevice;
    }
    // connect synchronously
    try{
      this.spiFd = fs.openSync(this.spiDevice, 'w');
    } catch (err) {
      console.error("error opening SPI device "+this.spiDevice, err);
      return false;
    }
    this.numLEDs = numLEDs;
    this.gamma = gamma ? gamma : 2.5; //set gamma correction value
    // compute gamma correction table
    for (var i=0; i<256; i++)
      this.gammatable[i] = Math.round(255*Math.pow(i/255, this.gamma));
    //console.log("gammatable" + this.gammatable);
  },

  /*
   * disconnect from SPI port
   */
  disconnect : function(){
    if (this.spiFd) fs.closeSync(this.spiFd);
  },

  /*
   * send stored buffer with RGB values to WS2801 stripe
   */
  update: function(){
    if (this.spiFd) {
      this.sendRgbBuffer(this.values);
    }  
  },
  
  /*
   * invert all colors
   */
  invert: function(){
    this.inverted = !this.inverted;
  }, // end invert
  
  /*
   * send buffer with RGB values to WS2801 stripe in reverse order
   */        
  reverse: function(){
    this.reversed = !this.reversed;
  }, // end reverse

  /*
   * clear RGB stripe
   */  
  clear: function(){
    this.fill(0x00, 0x00, 0x00); 
  }, // end clear

  /*
   * set new RGB index order
   */
  setColorIndex: function(redIndex, greenIndex, blueIndex){
    this.redIndex = redIndex;
    this.greenIndex = greenIndex;
    this.blueIndex = blueIndex;
  }, // end setColorIndex

  /*
   * send buffer with RGB values to WS2801 stripe
   */
  sendRgbBuffer : function(buffer){
    // checking if enough time passed for resetting stripe
    if (microtime.now() > (this.lastWriteTime + this.rowResetTime)){
      // yes, its o.k., lets write
      // but first do gamma correction
      var adjustedBuffer = new Buffer(buffer.length);
      for (var i=0; i < buffer.length; i++){
        adjustedBuffer[i]=this.gammatable[buffer[i]];
      }
      fs.writeSync(this.spiFd, adjustedBuffer, 0, buffer.length, null);
      this.lastWriteTime = microtime.now();
      return true;
    }
    console.log('writing too fast, data dropped');
    return false;	
  }, // end sendRgbBuffer
{'fraction_non_alphanumeric': 0.12603192074848651, 'fraction_numerical': 0.024215740231150248, 'mean_word_length': 2.390391791044776, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '4762271', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2346, 'n_words': 648}