espruino-adafruit-led-backpack
Advanced tools
Comparing version 1.0.2 to 1.1.0
123
index.js
/** | ||
* Constructor for an Adafruit 8 x 8 LED Matrix Backpack. | ||
* Constructor for Adafruit LED Matrix Backpacks which can be used as a base for 8x8 and 16x8 matrices. | ||
* Provides drawing capabilities like the Adafruit Arduino Library API. | ||
@@ -9,3 +9,3 @@ * | ||
*/ | ||
function Matrix8x8(options) { | ||
function Matrix(options) { | ||
if(typeof options.scl === "undefined" || | ||
@@ -25,2 +25,3 @@ typeof options.sda === "undefined" || | ||
/** | ||
@@ -30,3 +31,3 @@ * Set the brightnes of the LEDs. A number 0 through 15. | ||
*/ | ||
Matrix8x8.prototype.setBrightness = function(brightness) { | ||
Matrix.prototype.setBrightness = function(brightness) { | ||
// brightness 0-15 | ||
@@ -36,12 +37,11 @@ I2C1.writeTo(this.address, 0xE0 | brightness); | ||
/** | ||
* Renders buffer/graphics context to the display. | ||
*/ | ||
Matrix8x8.prototype.render = function() { | ||
I2C1.writeTo(this.address, 0, | ||
//Because of how the 8x8 Matrix is wired you need to rotate right by one bit | ||
(new Uint8Array(this.context.buffer)).map(this.rotate) | ||
); | ||
Matrix.prototype.render = function() { | ||
I2C1.writeTo(this.address, 0, this.context.buffer); | ||
}; | ||
/** | ||
@@ -55,3 +55,3 @@ * Rotates an 8-bit binary value right one bit. | ||
*/ | ||
Matrix8x8.prototype.rotate = function(value) { | ||
Matrix.prototype.rotate = function(value) { | ||
//Shift everything right 1 bit | ||
@@ -66,13 +66,15 @@ //Then shift last bit over if switched on it'll switch on 2^7 | ||
/** | ||
* Clears graphics buffer. | ||
*/ | ||
Matrix8x8.prototype.clear = function() { | ||
Matrix.prototype.clear = function() { | ||
this.context.clear(); | ||
}; | ||
/** | ||
* Same as `render()`. | ||
*/ | ||
Matrix8x8.prototype.writeDisplay = function(){ | ||
Matrix.prototype.writeDisplay = function(){ | ||
this.render(); | ||
@@ -83,15 +85,2 @@ }; | ||
/** | ||
* Writes an array of 8 8-bit values to the display graphics context | ||
* @param uint8array | ||
*/ | ||
Matrix8x8.prototype.drawBitmap = function(uint8array){ | ||
var array = []; | ||
uint8array.forEach(function(i){ | ||
array.push(i); | ||
array.push(i); | ||
}); | ||
this.context.buffer = new Uint8Array(array); | ||
}; | ||
/** | ||
* Draws a pixel at the co-ordinates. | ||
@@ -102,6 +91,7 @@ * @param x is the x co-ordinate. 0-7. | ||
*/ | ||
Matrix8x8.prototype.drawPixel = function (x, y, state) { | ||
Matrix.prototype.drawPixel = function (x, y, state) { | ||
this.context.setPixel(x, y, state); | ||
}; | ||
/** | ||
@@ -115,6 +105,7 @@ * Draws a line from one set of co-ordinates to another | ||
*/ | ||
Matrix8x8.prototype.drawLine = function(x1, y1, x2, y2) { | ||
Matrix.prototype.drawLine = function(x1, y1, x2, y2) { | ||
this.context.drawLine(x1, y1, x2, y2); | ||
}; | ||
/** | ||
@@ -128,6 +119,7 @@ * Draws a rectangle at a set of co-ordinates of a given width and height | ||
*/ | ||
Matrix8x8.prototype.drawRect = function(x, y, width, height) { | ||
Matrix.prototype.drawRect = function(x, y, width, height) { | ||
this.context.drawRect(x, y, x + width - 1, y + height - 1); | ||
}; | ||
/** | ||
@@ -140,6 +132,77 @@ * Draws a filled in rectangle at a set of co-ordinates of a given width and height | ||
*/ | ||
Matrix8x8.prototype.fillRect = function(x, y, width, height) { | ||
this.context.fillRect(x, y, x + width - 1, y + height - 1); | ||
Matrix.prototype.fillRect = function(x, y, width, height) { | ||
this.context.fillRect(x, y, x + width - 1, y + height - 1); | ||
}; | ||
module.exports.Matrix8x8 = Matrix8x8; | ||
/** | ||
* Constructor for an Adafruit 8x8 LED Matrix Backpack. | ||
*/ | ||
function Matrix8x8(options) { | ||
Matrix.call(this, options); | ||
} | ||
Matrix8x8.prototype = Object.create(Matrix.prototype); | ||
Matrix8x8.prototype.constructor = Matrix; | ||
/** | ||
* Writes an array of 8 8-bit values to the display graphics context | ||
* @param uint8array | ||
*/ | ||
Matrix8x8.prototype.drawBitmap = function(uint8array){ | ||
var array = []; | ||
uint8array.forEach(function(i){ | ||
array.push(i); | ||
array.push(i); | ||
}); | ||
this.context.buffer = new Uint8Array(array); | ||
}; | ||
/** | ||
* Renders buffer/graphics context to the display. | ||
*/ | ||
Matrix8x8.prototype.render = function() { | ||
I2C1.writeTo(this.address, 0, | ||
//Because of how the 8x8 Matrix is wired you need to rotate right by one bit | ||
(new Uint8Array(this.context.buffer)).map(this.rotate) | ||
); | ||
}; | ||
module.exports.Matrix8x8 = Matrix8x8; | ||
/** | ||
* Constructor for an Adafruit 16x8 LED Matrix Backpack. | ||
*/ | ||
function Matrix16x8(options) { | ||
Matrix.call(this, options); | ||
} | ||
Matrix16x8.prototype = Object.create(Matrix.prototype); | ||
Matrix16x8.prototype.constructor = Matrix; | ||
/** | ||
* Writes an array of 16 8-bit values to the display graphics context | ||
* | ||
* The first two bytes of the array represent the first line and so on. | ||
* This: drawBitmap([0b00000000, 0b1111111, 0b00001111, 0b00110011, ...]) | ||
* ends up like this in a 16x8 Matrix: | ||
* | ||
* 0000000011111111 | ||
* 0000111100110011 | ||
* ... | ||
* | ||
* @param uint8array | ||
*/ | ||
Matrix16x8.prototype.drawBitmap = function(uint8array) { | ||
var array = []; | ||
uint8array.forEach(function(i){ | ||
array.push(i); | ||
}); | ||
this.context.buffer = new Uint8Array(array); | ||
}; | ||
module.exports.Matrix16x8 = Matrix16x8; |
{ | ||
"name": "espruino-adafruit-led-backpack", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Espruino Driver for Adafruit's LED Matrix + Backpack", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"backpack", | ||
"8x8" | ||
"8x8", | ||
"16x8" | ||
], | ||
@@ -22,0 +23,0 @@ "author": "Andrew Chalkley", |
# espruino-adafruit-led-backpack | ||
This is an driver that closely mirrors the [Adafruit's LED Backpack Library](https://github.com/adafruit/Adafruit-LED-Backpack-Library). | ||
This is a driver that closely mirrors the [Adafruit's LED Backpack Library](https://github.com/adafruit/Adafruit-LED-Backpack-Library). | ||
This package currently works with Adafruit 8x8 Single Color LED Backpacks. | ||
This package currently works with Adafruit 8x8 and 16x8 Single Color LED Backpacks. | ||
* 0.8" ([872](https://www.adafruit.com/products/872), [871](https://www.adafruit.com/products/871), [870](https://www.adafruit.com/products/870) ) | ||
* 1.2" ([1049](https://www.adafruit.com/products/1049), [1052](https://www.adafruit.com/products/1052), [1051](https://www.adafruit.com/products/1051), [1050](https://www.adafruit.com/products/1050), [1614](https://www.adafruit.com/products/1614), [1632](https://www.adafruit.com/products/1632), [1857](https://www.adafruit.com/products/1857), [1854](https://www.adafruit.com/products/1854), [1855](https://www.adafruit.com/products/1855), [1856](https://www.adafruit.com/products/1856)) | ||
* 8x8 0.8" ([872](https://www.adafruit.com/products/872), [871](https://www.adafruit.com/products/871), [870](https://www.adafruit.com/products/870)) | ||
* 8x8 1.2" ([1049](https://www.adafruit.com/products/1049), [1052](https://www.adafruit.com/products/1052), [1051](https://www.adafruit.com/products/1051), [1050](https://www.adafruit.com/products/1050), [1614](https://www.adafruit.com/products/1614), [1632](https://www.adafruit.com/products/1632), [1857](https://www.adafruit.com/products/1857), [1854](https://www.adafruit.com/products/1854), [1855](https://www.adafruit.com/products/1855), [1856](https://www.adafruit.com/products/1856)) | ||
* 16x8 1.2" ([2044](https://www.adafruit.com/products/2044), [2038](https://www.adafruit.com/products/2038), [2037](https://www.adafruit.com/products/2037), [2040](https://www.adafruit.com/products/2040), [2041](https://www.adafruit.com/products/2041), [2039](https://www.adafruit.com/products/2039), [2043](https://www.adafruit.com/products/2043), [2042](https://www.adafruit.com/products/2042), [2035](https://www.adafruit.com/products/2035), [2036](https://www.adafruit.com/products/2036), [2052](https://www.adafruit.com/products/2052), [2054](https://www.adafruit.com/products/2054)) | ||
* 16x8 LED Matrix Driver Backpack ([1427](https://www.adafruit.com/products/1427)) | ||
This library was tested on on the [Small 1.2" 8x8 Ultra Bright Square White LED Matrix + Backpack, PRODUCT ID: 1857](https://www.adafruit.com/products/1857). | ||
This library was tested on on the [Small 1.2" 8x8 Ultra Bright Square White LED Matrix + Backpack, PRODUCT ID: 1857](https://www.adafruit.com/products/1857) and on the [16x8 1.2" Ultra Bright Square Green LED Matrix + Backpack](https://www.adafruit.com/products/2042). | ||
This **doesn't** work with the 7-Segment backpacks or 8x16 backpacks. | ||
This **doesn't** work with the 7-Segment backpacks. | ||
@@ -121,3 +123,35 @@ ## Example Code for 8 x 8 Matrix | ||
## Example Code for 16x8 Matrix | ||
```javascript | ||
var Matrix16x8 = require("espruino-adafruit-led-backpack").Matrix16x8; | ||
var matrix = new Matrix16x8({scl:B6, sda:B7, address:0x70, brightness: 0}); | ||
// draw smile and frown side by side | ||
var smileFrownBmp = [ | ||
0b00111100, 0b00111100, | ||
0b01000010, 0b01000010, | ||
0b10100101, 0b10100101, | ||
0b10000001, 0b10000001, | ||
0b10100101, 0b10011001, | ||
0b10011001, 0b10100101, | ||
0b01000010, 0b01000010, | ||
0b00111100, 0b00111100 | ||
]; | ||
matrix.clear(); | ||
matrix.drawBitmap(smileFrownBmp); | ||
matrix.writeDisplay(); | ||
``` | ||
![](smile_frown.jpg) | ||
The functions `drawLine`, `drawPixel`, `drawRect` and `fillRect` work the same as for the 8x8 Matrix except you can address up to 16 pixels on the x axis: | ||
```javascript | ||
// Draws a diagonal line between x1 = 3, y1 = 3 and x2 = 15, y2 = 7 | ||
matrix.drawLine(3, 3, 15, 7); | ||
``` | ||
## Todos - A.K.A. Please Help | ||
@@ -131,3 +165,2 @@ - [ ] Finish Parity for 8 x 8 Display | ||
- [ ] `setRotation()` | ||
- [ ] Implement 7 Segment Display | ||
- [ ] Implement 8 x 16 LED Display | ||
- [ ] Implement 7 Segment Display |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
462707
7
164
164