Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Fadecandy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
scanlime
Fadecandy
Commits
ebfbd8f5
Commit
ebfbd8f5
authored
11 years ago
by
Micah Elizabeth Scott
Browse files
Options
Downloads
Patches
Plain Diff
Simple example for Node.js
parent
e2653a17
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/README.md
+2
-0
2 additions, 0 deletions
examples/README.md
examples/node/README.md
+10
-0
10 additions, 0 deletions
examples/node/README.md
examples/node/opc.js
+73
-0
73 additions, 0 deletions
examples/node/opc.js
examples/node/strip_redblue.js
+22
-0
22 additions, 0 deletions
examples/node/strip_redblue.js
with
107 additions
and
0 deletions
examples/README.md
+
2
−
0
View file @
ebfbd8f5
...
...
@@ -7,6 +7,8 @@ This directory contains sample projects and configuration files for Fadecandy.
*
Examples for
[
Processing 2
](
http://processing.org/
)
, a popular tool for creative coding
*
`html`
*
Browser-based examples, written in JavaScript with HTML5
*
`node`
*
JavaScript examples for Node.js
*
`python`
*
Some examples written in
[
Python
](
http://python.org/
)
*
`config`
...
...
This diff is collapsed.
Click to expand it.
examples/node/README.md
0 → 100644
+
10
−
0
View file @
ebfbd8f5
Fadecandy Node.js Examples
==========================
This directory contains examples for Fadecandy written in
[
Node.js
](
http://nodejs.org/
)
.
Stable Examples
---------------
*
`strip_redblue`
*
An example for LED strips. A fade of blue, white, and red washes across the LEDs.
This diff is collapsed.
Click to expand it.
examples/node/opc.js
0 → 100755
+
73
−
0
View file @
ebfbd8f5
/*
* Simple Open Pixel Control client for Node.js
*
* 2013 Micah Elizabeth Scott
* This file is released into the public domain.
*/
var
net
=
require
(
'
net
'
);
var
OPC
=
function
(
host
,
port
)
{
this
.
host
=
host
;
this
.
port
=
port
;
this
.
pixelBuffer
=
null
;
};
OPC
.
prototype
.
_reconnect
=
function
()
{
var
_this
=
this
;
this
.
socket
=
new
net
.
Socket
()
this
.
connected
=
false
;
this
.
socket
.
onclose
=
function
()
{
console
.
log
(
"
Connection closed
"
);
_this
.
socket
=
null
;
_this
.
connected
=
false
;
}
this
.
socket
.
connect
(
this
.
port
,
this
.
host
,
function
()
{
console
.
log
(
"
Connected to
"
+
_this
.
socket
.
remoteAddress
);
_this
.
connected
=
true
;
_this
.
socket
.
setNoDelay
();
});
}
OPC
.
prototype
.
writePixels
=
function
()
{
if
(
!
this
.
socket
)
{
this
.
_reconnect
();
}
if
(
!
this
.
connected
)
{
return
;
}
this
.
socket
.
write
(
this
.
pixelBuffer
);
}
OPC
.
prototype
.
setPixelCount
=
function
(
num
)
{
var
length
=
4
+
num
*
3
;
if
(
this
.
pixelBuffer
==
null
||
this
.
pixelBuffer
.
length
!=
length
)
{
this
.
pixelBuffer
=
new
Buffer
(
length
);
}
// Initialize OPC header
this
.
pixelBuffer
.
writeUInt8
(
0
,
0
);
// Channel
this
.
pixelBuffer
.
writeUInt8
(
0
,
1
);
// Command
this
.
pixelBuffer
.
writeUInt16BE
(
num
*
3
,
2
);
// Length
}
OPC
.
prototype
.
setPixel
=
function
(
num
,
r
,
g
,
b
)
{
var
offset
=
4
+
num
*
3
;
if
(
this
.
pixelBuffer
==
null
||
offset
+
3
>
this
.
pixelBuffer
.
length
)
{
this
.
setPixelCount
(
num
+
1
);
}
this
.
pixelBuffer
.
writeUInt8
(
Math
.
max
(
0
,
Math
.
min
(
255
,
r
|
0
)),
offset
);
this
.
pixelBuffer
.
writeUInt8
(
Math
.
max
(
0
,
Math
.
min
(
255
,
g
|
0
)),
offset
+
1
);
this
.
pixelBuffer
.
writeUInt8
(
Math
.
max
(
0
,
Math
.
min
(
255
,
b
|
0
)),
offset
+
2
);
}
module
.
exports
=
OPC
;
This diff is collapsed.
Click to expand it.
examples/node/strip_redblue.js
0 → 100755
+
22
−
0
View file @
ebfbd8f5
// Simple red/blue fade with Node and opc.js
var
OPC
=
new
require
(
'
./opc
'
)
var
client
=
new
OPC
(
'
localhost
'
,
7890
);
var
draw
=
function
()
{
var
millis
=
new
Date
().
getTime
();
for
(
var
pixel
=
0
;
pixel
<
512
;
pixel
++
)
{
var
t
=
pixel
*
0.2
+
millis
*
0.002
;
var
red
=
128
+
96
*
Math
.
sin
(
t
);
var
green
=
128
+
96
*
Math
.
sin
(
t
+
0.1
);
var
blue
=
128
+
96
*
Math
.
sin
(
t
+
0.3
);
client
.
setPixel
(
pixel
,
red
,
green
,
blue
);
}
client
.
writePixels
();
}
setInterval
(
draw
,
30
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment