Avatar for konsumer

konsumer

Member since Jun 2014 • Last active Jun 2014
  • 1 conversations
  • 5 comments

Most recent activity

    • 6 comments
    • 2,800 views
  • in JavaScript
    Avatar for konsumer

    As a complete sidenote, I have a lot of experience with native javascript apps. If you guys are interested, I'd be happy to work on a port of the app to a node-webkit standalone. The benefits would be native file access (save files with actual save-file dialog) and no need for chrome install. Is the chrome-only stuff pretty well separated?

  • in JavaScript
    Avatar for konsumer

    It was pretty obvious, once I read the docs. I just now had some time to play with it, and pulled it out of a drawer. No fault of the docs, I should have done this, right away. Very impressed. Super-easy to get started.

  • in JavaScript
    Avatar for konsumer

    Also, as a sidenote, maybe my light string is funny, but even the regular functional form doesn't seem to work right:

    SPI2.setup({baud:3200000, mosi:B15});
    SPI2.send4bit([255,0,0, 0,255,0, 0,0,255], 0b0001, 0b0011);
    

    It lights up the first LED in a single color or not at all, and that's it.

    This is probably related to baude, but maybe should be noted here.

    Update: I spoke too soon. I updated the firmware, and it worked like a charm.

    SPI2.setup({baud:3200000, mosi:B15});
    SPI2.send4bit([255,0,0, 0,255,0, 0,0,255], 1, 3);
    

    sets LEDs 1, 2, &3 to red, green, blue.

    Update 2: It seems my code works great after the update, in it's original form. here is the complete lib,if anybody is interested:

    /**
     * Simple WS2811 interface
     * @param {Number} interval (100) Milliseconds for update interval
     * @param {Number} len      (50) length of light-string
     * @param {Number} baud     (3200000) bauderate of SPI
     * @param {Pin} pin         (B15) data-pin
     */
    function WS2811(interval, len, baud, pin){
      var self = this;
      self.interval = interval ||100;
      self.len = len || 50;
      self.leds = new Uint8Array(self.len * 3);
      SPI2.setup({baud:baud || 3200000, mosi:pin || B15});
      
      self.reset();
      
      // update at interval to copy vals to pixel array
      self.interval = setInterval(function(){
        self.setArray(self.leds);
      }, self.interval);
    }
    
    /**
     * Set all pixels to black
     */
    WS2811.prototype.reset = function(){
      this.leds = this.leds.map(function(){ return 0; });
    };
    
    /**
     * Send array in pixel-friendly format
     * @param {Array} data the pixels in R,G,B,R,G,B,... format
     */
    WS2811.prototype.setArray = function(data){
      SPI2.send4bit(data, 1,3);
    };
    
    /**
     * Set a pixel to an RGB value
     * @param {Number} i     The pixel ID
     * @param {Array} color  An RGB color, 0-255, in array
     */
    WS2811.prototype.setRGB = function(i, color){
      this.leds[i*3    ] = color[0];
      this.leds[i*3 + 1] = color[1];
      this.leds[i*3 + 2] = color[2];
    };
    
    
    // USAGE
    
    var lights = new WS2811();
    
    // scroll all pixels through white colors
    function whiteScroll(){
      var pos = 0;
      setInterval(function(){
        pos++;
        for (var i=0; i<lights.leds.length; i+=3){
          var col = (Math.sin(i+pos*0.2)+1) * 127;
          lights.setRGB(i/3, [col,col,col]);
        }
      }, 100);
    }
    
    
    // blink red, green,blue for pixels 1,2,3 to tell us we are starting
    // then, run our sequence function
    lights.setRGB(0, [255,0,0]);
    lights.setRGB(1, [0,255,0]);
    lights.setRGB(2, [0,0,255]);
    setTimeout(function(){
      lights.reset();
      whiteScroll();
    }, 100);
    
  • in JavaScript
    Avatar for konsumer

    I am pretty used to Javascript OOP prototypical programming, but am brand new to Espruino & having some issues. I am making a WS2811 lib, to get my feet wet with Espruino.

    function WS2811(len, baud, pin){
      SPI2.setup({baud:baud, mosi:pin});
    }
    
    WS2811.prototype.send = function(data){
      SPI2.send4bit(data, 1, 3);
    };
    
    // usage
    
    var light = new WS2811(50, 3200000, B15);
    light.send([255,0,0, 0,255,0, 0,0,255]);
    

    I get this:

    ERROR: Constructor should be a function at line 1 col 6.

    If I change the constructor to another form:

    var WS2811 = function(len, baud, pin)
    

    I get:
    ERROR: Function not found! Skipping. at line 1 col 13

    What am I doing wrong?

Actions