Espruino and WS2801

Here is a quick and ugly code to drive 50 WS2801 LED modules on a Espruino board:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var nrOfPixels = 50;
var rgb = new Uint8Array(nrOfPixels*3);

var k=0,jj=0;

SPI2.setup({baud:1000000, mosi:B15, sck:B13 });
USB.setup({baudrate:115200});
var serialBuffer = createRingBuffer(3);

function sendDataOut() {
  SPI2.send(rgb);
}


setInterval(function() {
    jj=k;
    for (var i=0;i<rgb.length;i+=3) {
      wheel(i, k++);
    }
    k=(jj+1)%255;
    sendDataOut();
  //}
}, 15);

console.log("init done");
           
Pin.prototype.startFlashing = function(period) {
  var me = this;
  var on = false;
  setInterval(function() {
    on = !on;
    digitalWrite(me, on);
  }, period);
};
LED3.startFlashing(500);

function wheel(ofs, pos) {
  pos %= 255;
  if (pos < 86) {
    Color(ofs, pos * 3, 255 - pos * 3, 0);
  }
  else if (pos < 171) {
    pos -= 85;
    Color(ofs, 255 - pos * 3, 0, pos * 3);
  }
  else {
    pos -= 170;
    Color(ofs, 0, pos * 3, 255 - pos * 3);
  }
}

function Color(ofs, r,g,b) {
  rgb[ofs] = r%255;
  rgb[ofs+1] = g%255;
  rgb[ofs+2] = b%255;
}

This sketch displays a simple rainbow pattern. I goal is a Espruino firmware for PixelController… we’ll see

2 Comments

  1. loop23
    loop23 February 3, 2014 at 22:19 . Reply

    Hi, some questions:
    Where is createRingBuffer defined? I can’t find it, and neither can the interpreter!
    The WS2811 has just data in, what use for sck?
    How did you come up with that baud rate?

    1. loop23
      loop23 February 3, 2014 at 22:24 . Reply

      Bummer! you have a 2801 strip, I have a 2811; I always get them wrong, so yours have a clock thing too!

Leave a Reply

Warning: require_once(/home/httpd/vhosts/pixelinvaders.ch/httpdocs/neophob.com/wp-content/themes/fanwood/sidebar-after-content.php): failed to open stream: Permission denied in /home/httpd/vhosts/pixelinvaders.ch/httpdocs/neophob.com/wp-includes/template.php on line 501 Fatal error: require_once(): Failed opening required '/home/httpd/vhosts/pixelinvaders.ch/httpdocs/neophob.com/wp-content/themes/fanwood/sidebar-after-content.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/httpd/vhosts/pixelinvaders.ch/httpdocs/neophob.com/wp-includes/template.php on line 501