Skip to content

Commit c30eeae

Browse files
committed
Factor neopixel out to a separate lib
CL: none PUBLISHED_FROM=a15af497728917ae8fca675a1ebaf86062ce51a3
1 parent 53a1e60 commit c30eeae

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Neopixel driver for Mongoose OS

mjs_fs/api_neopixel.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
load("api_bitbang.js");
2+
load("api_gpio.js");
3+
load("api_sys.js");
4+
5+
let NeoPixel = {
6+
RGB: 0,
7+
GRB: 1,
8+
BGR: 2,
9+
10+
// ## **`NeoPixel.create(pin, numPixels, order)`**
11+
// Create and return a NeoPixel strip object. Example:
12+
// ```javascript
13+
// let pin = 5, numPixels = 16, colorOrder = NeoPixel.GRB;
14+
// let strip = NeoPixel.create(pin, numPixels, colorOrder);
15+
// strip.setPixel(0 /* pixel */, 12, 34, 56);
16+
// strip.show();
17+
//
18+
// strip.clear();
19+
// strip.setPixel(1 /* pixel */, 12, 34, 56);
20+
// strip.show();
21+
// ```
22+
create: function(pin, numPixels, order) {
23+
GPIO.set_mode(pin, GPIO.MODE_OUTPUT);
24+
GPIO.write(pin, 0); // Keep in reset.
25+
let s = Object.create({
26+
pin: pin,
27+
len: numPixels * 3,
28+
// Note: memory allocated here is currently not released.
29+
// This should be ok for now, we don't expect strips to be re-created.
30+
data: Sys.malloc(numPixels * 3),
31+
order: order,
32+
setPixel: NeoPixel.set,
33+
clear: NeoPixel.clear,
34+
show: NeoPixel.show,
35+
});
36+
s.clear();
37+
return s;
38+
},
39+
40+
// ## **`strip.setPixel(i, r, g, b)`**
41+
// Set i-th's pixel's RGB value.
42+
// Note that this only affects in-memory value of the pixel.
43+
set: function(i, r, g, b) {
44+
let v0, v1, v2;
45+
if (this.order === NeoPixel.RGB) {
46+
v0 = r; v1 = g; v2 = b;
47+
} else if (this.order === NeoPixel.GRB) {
48+
v0 = g; v1 = r; v2 = b;
49+
} else if (this.order === NeoPixel.BGR) {
50+
v0 = b; v1 = g; v2 = r;
51+
} else {
52+
return;
53+
}
54+
this.data[i * 3] = v0;
55+
this.data[i * 3 + 1] = v1;
56+
this.data[i * 3 + 2] = v2;
57+
},
58+
59+
// ## **`strip.clear()`**
60+
// Clear in-memory values of the pixels.
61+
clear: function() {
62+
for (let i = 0; i < this.len; i++) {
63+
this.data[i] = 0;
64+
}
65+
},
66+
67+
// ## **`strip.show()`**
68+
// Output values of the pixels.
69+
show: function() {
70+
GPIO.write(this.pin, 0);
71+
Sys.usleep(60);
72+
BitBang.write(this.pin, BitBang.DELAY_100NSEC, 3, 8, 8, 6, this.data, this.len);
73+
GPIO.write(this.pin, 0);
74+
Sys.usleep(60);
75+
GPIO.write(this.pin, 1);
76+
},
77+
};

mos.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
author: mongoose-os
2+
type: lib
3+
description: Neopixel driver
4+
version: 1.0
5+
6+
sources:
7+
- src
8+
includes:
9+
- include
10+
11+
tags:
12+
- js
13+
- hw
14+
- neopixel
15+
16+
manifest_version: 2017-09-29

src/mgos_neopixel.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*/
5+
6+
#include <stdbool.h>
7+
8+
bool mgos_neopixel_init(void) {
9+
return true;
10+
}

0 commit comments

Comments
 (0)