Skip to content

Instantly share code, notes, and snippets.

@bwhite
Created March 6, 2014 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwhite/9390329 to your computer and use it in GitHub Desktop.
Save bwhite/9390329 to your computer and use it in GitHub Desktop.
[wearscript] Neopixel + bluetooth
<html style="width:100%; height:100%; overflow:hidden">
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<canvas id="canvas" width="640" height="360" style="display:block"></canvas>
<script>
function NeoPixel(bluetooth) {
this.bluetooth = bluetooth;
this.buffer = ['arduinoneopixel'];
this.concat = function (x) {
this.buffer = this.buffer.concat(x);
}
this.setPixelColor = function (led, r, g, b) {
this.concat([255, 0, led, r, g, b]);
}
this.setBrightness = function (l) {
this.concat([255, 1, l, 0, 0, 0]);
}
this.show = function () {
this.concat([255, 2, 0, 0, 0, 0]);
}
this.delay = function (l) {
this.concat([255, 3, l, 0, 0, 0]);
}
this.startLoop = function () {
this.concat([255, 4, 0, 0, 0, 0]);
}
this.finishLoop = function () {
this.concat([255, 5, 0, 0, 0, 0]);
}
this.send = function () {
if (!this.bluetooth)
WS.publish.apply(WS, this.buffer);
else {
var data = this.buffer.slice(1).map(function (x) {return String.fromCharCode(x)}).join('');
WS.bluetoothWrite(this.bluetooth, data);
}
this.buffer = ['arduinoneopixel'];
}
}
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
if (h && s === undefined && v === undefined) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);h
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.floor(r * 255),
g: Math.floor(g * 255),
b: Math.floor(b * 255)
};
}
function change() {
// NOTE(brandyn): Set this to undefined if you want to use serial
var bluetooth = '00:14:01:21:13:71';
var pixel = new NeoPixel(bluetooth);
pixel.setBrightness(BRIGHT);
var color = HSVtoRGB(ANGLE / 360, 1, 1);
ctx.fillStyle = 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')';
ctx.fillRect(0, 0, 640, 360);
offset = Math.round(Math.random() * 256);
for (var i = 0; i < 1; i++) {
pixel.setPixelColor(i, color.r, color.g, color.b);
}
pixel.show();
pixel.send();
}
function server() {
BRIGHT = 100;
ANGLE = 0;
MODE = false;
WS.sensorOn('orientation', .25, function (data) {
// Changes canvas color with head rotation
ANGLE = data['values'][0];
change();
});
change();
WS.gestureCallback('onGesture', function (name) {
if (name == 'SWIPE_LEFT') {
BRIGHT = Math.max(0, BRIGHT - 30);
WS.say('Bright ' + BRIGHT)
} else if (name == 'SWIPE_RIGHT') {
BRIGHT = Math.min(100, BRIGHT + 30);
WS.say('Bright ' + BRIGHT)
}
});
}
function main() {
if (WS.scriptVersion(1)) return;
ctx = document.getElementById('canvas').getContext("2d");
WS.serverConnect('{{WSUrl}}', server);
}
window.onload = main;
</script></body></html>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);
uint8_t *data;
uint8_t loop_data[512];
int loop_pos = 0;
int loop_state = 0; // 0: no loop, 1: saving loop data, 2: looping
int i, j;
SoftwareSerial mySerial(3, 2); // RX, TX
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(9600);
mySerial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
data = loop_data + loop_pos;
if (Serial.available() < 6 && mySerial.available() < 6) {
if (loop_state != 2)
return;
} else {
if (loop_state == 2) {
// If data comes in while we loop, clear the loop
loop_state = 0;
loop_pos = 0;
}
if (Serial.available())
data[0] = Serial.read();
else
data[0] = mySerial.read();
if (data[0] != 255)
return;
for (i = 0; i < 5; i++) {
if (Serial.available())
data[i] = Serial.read();
else
data[i] = mySerial.read();
}
}
if (loop_state != 0)
loop_pos += 5;
switch (data[0]) {
case 0:
strip.setPixelColor(data[1], data[2], data[3], data[4]);
break;
case 1:
strip.setBrightness(data[1]);
break;
case 2:
strip.show();
break;
case 3:
delay(data[1]);
break;
case 4: // start loop
loop_state = 1;
loop_pos = 0;
break;
case 5: // finish loop
loop_state = 2;
loop_pos = 0;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment