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
a42e87e7
Commit
a42e87e7
authored
11 years ago
by
Micah Elizabeth Scott
Browse files
Options
Downloads
Patches
Plain Diff
Add a cute example for Processing
parent
3baaf462
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/Processing/dot/OPC.pde
+138
-0
138 additions, 0 deletions
examples/Processing/dot/OPC.pde
examples/Processing/dot/data/dot.png
+0
-0
0 additions, 0 deletions
examples/Processing/dot/data/dot.png
examples/Processing/dot/dot.pde
+35
-0
35 additions, 0 deletions
examples/Processing/dot/dot.pde
with
173 additions
and
0 deletions
examples/Processing/dot/OPC.pde
0 → 100644
+
138
−
0
View file @
a42e87e7
/*
* Simple Open Pixel Control client for Processing,
* designed to sample each LED's color from some point on the canvas.
*
* Micah Elizabeth Scott, 2013
* This file is released into the public domain.
*/
import
processing
.
net
.
Client
;
import
java
.
util
.
Arrays
;
public
class
OPC
{
PApplet
app
;
int
[]
pixelLocations
;
byte
[]
packetData
;
Client
client
;
String
host
;
int
port
;
boolean
enableShowLocations
;
OPC
(
PApplet
app
,
String
host
,
int
port
)
{
this
.
app
=
app
;
this
.
host
=
host
;
this
.
port
=
port
;
app
.
registerDraw
(
this
);
frameRate
(
30
);
}
// Should the pixel sampling locations be visible? This helps with debugging.
void
showLocations
(
boolean
enabled
)
{
enableShowLocations
=
enabled
;
}
// Set the location of a single LED
void
led
(
int
index
,
int
x
,
int
y
)
{
// For convenience, automatically grow the pixelLocations array. We do want this to be an array,
// instead of a HashMap, to keep draw() as fast as it can be.
if
(
pixelLocations
==
null
)
{
pixelLocations
=
new
int
[
index
+
1
];
}
else
if
(
index
>=
pixelLocations
.
length
)
{
pixelLocations
=
Arrays
.
copyOf
(
pixelLocations
,
index
+
1
);
}
pixelLocations
[
index
]
=
x
+
width
*
y
;
}
// Set the location of several LEDs arranged in a strip.
// Angle is in radians, measured clockwise from +X.
// (x,y) is the center of the strip.
void
ledStrip
(
int
index
,
int
count
,
float
x
,
float
y
,
float
spacing
,
float
angle
,
boolean
reversed
)
{
float
s
=
sin
(
angle
);
float
c
=
cos
(
angle
);
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
led
(
reversed
?
(
index
+
count
-
1
-
i
)
:
(
index
+
i
),
(
int
)(
x
+
(
i
-
(
count
-
1
)
/
2.0
)
*
spacing
*
c
+
0.5
),
(
int
)(
y
+
(
i
-
(
count
-
1
)
/
2.0
)
*
spacing
*
s
+
0.5
));
}
}
// Set the location of several LEDs arranged in a grid. The first strip is
// at 'angle', measured in radians clockwise from +X.
// (x,y) is the center of the grid.
void
ledGrid
(
int
index
,
int
stripLength
,
int
numStrips
,
float
x
,
float
y
,
float
ledSpacing
,
float
stripSpacing
,
float
angle
,
boolean
zigzag
)
{
float
s
=
sin
(
angle
+
HALF_PI
);
float
c
=
cos
(
angle
+
HALF_PI
);
for
(
int
i
=
0
;
i
<
numStrips
;
i
++
)
{
ledStrip
(
index
+
stripLength
*
i
,
stripLength
,
x
+
(
i
-
(
numStrips
-
1
)
/
2.0
)
*
stripSpacing
*
c
,
y
+
(
i
-
(
numStrips
-
1
)
/
2.0
)
*
stripSpacing
*
s
,
ledSpacing
,
angle
,
zigzag
&&
(
i
%
2
)
==
1
);
}
}
// Set the location of 64 LEDs arranged in a uniform 8x8 zig-zag grid.
// (x,y) is the center of the grid.
void
ledGrid8x8
(
int
index
,
float
x
,
float
y
,
float
spacing
,
float
angle
)
{
ledGrid
(
index
,
8
,
8
,
x
,
y
,
spacing
,
spacing
,
angle
,
true
);
}
// Automatically called at the end of each draw()
void
draw
()
{
if
(
pixelLocations
==
null
)
{
// No pixels defined yet
return
;
}
if
(
client
==
null
||
!
client
.
active
())
{
// Try to (re)connect
client
=
new
Client
(
app
,
host
,
port
);
return
;
}
int
numPixels
=
pixelLocations
.
length
;
int
numBytes
=
3
*
numPixels
;
int
packetLen
=
4
+
numBytes
;
if
(
packetData
==
null
||
packetData
.
length
!=
packetLen
)
{
// Set up our packet buffer
packetData
=
new
byte
[
packetLen
];
packetData
[
0
]
=
0
;
// Channel
packetData
[
1
]
=
0
;
// Command (Set pixel colors)
packetData
[
2
]
=
(
byte
)(
numBytes
>>
8
);
packetData
[
3
]
=
(
byte
)(
numBytes
&
0xFF
);
}
loadPixels
();
int
ledAddress
=
4
;
for
(
int
i
=
0
;
i
<
numPixels
;
i
++
)
{
int
pixelLocation
=
pixelLocations
[
i
];
int
pixel
=
pixels
[
pixelLocation
];
packetData
[
ledAddress
]
=
(
byte
)(
pixel
>>
16
);
packetData
[
ledAddress
+
1
]
=
(
byte
)(
pixel
>>
8
);
packetData
[
ledAddress
+
2
]
=
(
byte
)
pixel
;
ledAddress
+=
3
;
if
(
enableShowLocations
)
{
pixels
[
pixelLocation
]
=
0xFFFFFF
^
pixel
;
}
}
client
.
write
(
packetData
);
if
(
enableShowLocations
)
{
updatePixels
();
}
}
}
This diff is collapsed.
Click to expand it.
examples/Processing/dot/data/dot.png
0 → 100644
+
0
−
0
View file @
a42e87e7
4.41 KiB
This diff is collapsed.
Click to expand it.
examples/Processing/dot/dot.pde
0 → 100644
+
35
−
0
View file @
a42e87e7
OPC
opc
;
PImage
dot
;
void
setup
()
{
size
(
640
,
360
);
dot
=
loadImage
(
"dot.png"
);
// Connect to the local instance of fcserver. You can change this line to connect to another computer's fcserver
opc
=
new
OPC
(
this
,
"127.0.0.1"
,
7890
);
// Map an 8x8 grid of LEDs to the center of the window, scaled to take up most of the space
opc
.
ledGrid8x8
(
0
,
width
/
2
,
height
/
2
,
height
/
14
,
0
);
// Put two more 8x8 grids to the left and to the right of that one.
opc
.
ledGrid8x8
(
64
,
width
/
2
-
height
/
14
*
8
,
height
/
2
,
height
/
14
,
0
);
opc
.
ledGrid8x8
(
128
,
width
/
2
+
height
/
14
*
8
,
height
/
2
,
height
/
14
,
0
);
// Make the LED grid visible on-screen. By default, the LED sampling locations
// are hidden and don't affect Processing's output.
opc
.
showLocations
(
true
);
}
void
draw
()
{
background
(
0
);
// Change the dot size as a function of time, to make it "throb"
float
dotSize
=
height
*
0.6
*
(
1.0
+
0.2
*
sin
(
millis
()
*
0.01
));
// Draw it centered at the mouse location
image
(
dot
,
mouseX
-
dotSize
/
2
,
mouseY
-
dotSize
/
2
,
dotSize
,
dotSize
);
}
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