UNPKG

johnny-five

Version:

Firmata based Arduino Programming Framework.

94 lines (69 loc) 1.65 kB
```js var strips = new WeakMap(); Led.Strip = function(opts) { // ... make a strip device object with "opts" strips.set(this, { pixels: new Pixels({ addr: i, clock: opts.clock, data: opts.data, firmata: this.firmata }) }); }; Led.Strip.prototype.pixel = function(addr) { var strip = strips.get(this); return strip.pixels[addr]; }; var pixels = new WeakMap(); function Pixel(opts) { pixels.set(this, { addr: opts.addr, clock: opts.clock, data: opts.data color: ... }); } Pixel.prototype.write = function() { var pixel = pixels.get(this); // do stuff with pi }; Pixel.prototype.color = function(color) { var pixel = pixels.get(this); if (color) { // color (name|hex) => value translations // send value to device // update pixel.color = value return this; } else { return pixel.color; } }; ``` Then use of the API looks like this: ```js var strip = new five.Led.Strip({ pixels: 5, // Number of leds in the strip clock: 2, // Clock pin data: 3 // Data pin }); // Get the pixel at address 10 var p = strip.pixel(10); // set pixel 10 to red p.color("#FF0000"); // ...OR... // Get pixel 10, set it to the color blue! strip.pixel(10).color("blue"); // What color is pixel 10? strip.pixel(10).color(); // outputs the current color... ``` I'm not sure what form the return value of color should be... Maybe it could return an object with some representation of several formats? Like this: ```js // What color is pixel 10? strip.pixel(10).color("red").color(); // returns a plain object like: { hex: "FF0000", name: "red" } ```