Skip to content
Newer
Older
100644 192 lines (165 sloc) 5.04 KB
9adb298 @cibomahto Add colors.
cibomahto authored
1 import hypermedia.net.*;
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
2
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
3 /**
4 * This class can be added to your sketches to make them compatible with the sign.
5 * Use Sketch..Add File and choose this file to copy it into your sketch.
6 *
7 * void setup() {
8 * // Constructor takes this, width, height.
9 * Dacwes dacwes = new Dacwes(this, 16, 16);
10 *
11 * // Change this depending on how the sign is configured.
12 * dacwes.setAddressingMode(Dacwes.ADDRESSING_VERTICAL_FLIPFLOP);
13 *
14 * // Include this to talk to the emulator.
15 * dacwes.setAddress("127.0.0.1");
16 *
17 * // The class will scale things for you, but it may not be full brightness
18 * // unless you match the size.
19 * size(320,320);
20 * }
21 *
22 * void draw() {
23 * doStuff();
24 *
25 * // Call this in your draw loop to send data to the sign.
26 * dacwes.sendData();
27 * }
28 *
29 **/
30
0a80936 @potatono Made comm compatible with Dacwes again
potatono authored
31 public class Sculpture {
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
32 public static final int ADDRESSING_VERTICAL_NORMAL = 1;
33 public static final int ADDRESSING_VERTICAL_HALF = 2;
34 public static final int ADDRESSING_VERTICAL_FLIPFLOP = 3;
35 public static final int ADDRESSING_HORIZONTAL_NORMAL = 4;
36 public static final int ADDRESSING_HORIZONTAL_HALF = 5;
37 public static final int ADDRESSING_HORIZONTAL_FLIPFLOP = 6;
15268ec @potatono New routines, Gamma.
potatono authored
38
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
39 PApplet parent;
9adb298 @cibomahto Add colors.
cibomahto authored
40 UDP udp;
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
41 String address;
42 int port;
43 int w;
44 int h;
45 int addressingMode;
46 byte buffer[];
47 int pixelsPerChannel;
15268ec @potatono New routines, Gamma.
potatono authored
48 float gammaValue = 2.5;
49 boolean enableGammaCorrection = false;
50 boolean isRGB = false;
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
51
0a80936 @potatono Made comm compatible with Dacwes again
potatono authored
52 public Sculpture(PApplet parent, int w, int h, boolean isRGB) {
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
53 this.parent = parent;
9adb298 @cibomahto Add colors.
cibomahto authored
54 this.udp = new UDP(parent);
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
55 this.address = "192.168.1.130";
56 this.port = 58082;
57 this.w = w;
58 this.h = h;
15268ec @potatono New routines, Gamma.
potatono authored
59 this.isRGB = isRGB;
0a80936 @potatono Made comm compatible with Dacwes again
potatono authored
60 int bufferSize = (isRGB ? 3 : 1)*(w*h)+1;
27fac25 @mhenstell fixed buffer init
mhenstell authored
61 buffer = new byte[bufferSize];
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
62 this.addressingMode = ADDRESSING_VERTICAL_NORMAL;
0a80936 @potatono Made comm compatible with Dacwes again
potatono authored
63 // TODO Detect this based on VERTICAL (h/2) vs. HORIZONTAL (w/2)
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
64 this.pixelsPerChannel = 8;
15268ec @potatono New routines, Gamma.
potatono authored
65
27fac25 @mhenstell fixed buffer init
mhenstell authored
66 for (int i=0; i<bufferSize; i++) {
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
67 buffer[i] = 0;
68 }
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
69 }
70
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
71 public void setAddress(String address) {
72 this.address = address;
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
73 }
74
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
75 public void setPort(int port) {
76 this.port = port;
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
77 }
15268ec @potatono New routines, Gamma.
potatono authored
78
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
79 public void setAddressingMode(int mode) {
80 this.addressingMode = mode;
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
81 }
15268ec @potatono New routines, Gamma.
potatono authored
82
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
83 public void setPixelsPerChannel(int n) {
84 this.pixelsPerChannel = n;
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
85 }
15268ec @potatono New routines, Gamma.
potatono authored
86
87 public void setGammaValue(float gammaValue) {
88 this.gammaValue = gammaValue;
89 }
90
91 public void setEnableGammaCorrection(boolean enableGammaCorrection) {
92 this.enableGammaCorrection = enableGammaCorrection;
93 }
94
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
95 private int getAddress(int x, int y) {
96 if (addressingMode == ADDRESSING_VERTICAL_NORMAL) {
3063cae @potatono Remerged in proper Dacwes handling.
potatono authored
97 return (x * h + y);
98 }
99 else if (addressingMode == ADDRESSING_VERTICAL_HALF) {
100 return ((y % pixelsPerChannel) + floor(y / pixelsPerChannel)*pixelsPerChannel*w + x*pixelsPerChannel);
101 }
102 else if (addressingMode == ADDRESSING_VERTICAL_FLIPFLOP) {
103 if (y>=pixelsPerChannel) {
104 int endAddress = (x+1) * h - 1;
105 int address = endAddress - (y % pixelsPerChannel);
106 return address;
107 }
108 else {
109 return (x * h + y);
110 }
111 }
112 else if (addressingMode == ADDRESSING_HORIZONTAL_NORMAL) {
c11ebf2 @mhenstell fixed addressing?
mhenstell authored
113 return (y * w + x);
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
114 }
3063cae @potatono Remerged in proper Dacwes handling.
potatono authored
115 else if (addressingMode == ADDRESSING_HORIZONTAL_HALF) {
116 return ((x % pixelsPerChannel) + floor(x / pixelsPerChannel)*pixelsPerChannel*h + y*pixelsPerChannel);
117 }
118 else if (addressingMode == ADDRESSING_HORIZONTAL_FLIPFLOP) {
119 if (x>=pixelsPerChannel) {
120 int endAddress = (y+1) * w - 1;
121 int address = endAddress - (x % pixelsPerChannel);
122 return address;
123 }
124 else {
125 return (y * h + x);
126 }
127 }
15268ec @potatono New routines, Gamma.
potatono authored
128
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
129 return 0;
3063cae @potatono Remerged in proper Dacwes handling.
potatono authored
130 }
15268ec @potatono New routines, Gamma.
potatono authored
131
54afb2a @cibomahto Send the mode name as a UDP packet with a different magic number
cibomahto authored
132 public void sendMode(String modeName) {
133 byte modeBuffer[] = new byte[modeName.length()+1];
15268ec @potatono New routines, Gamma.
potatono authored
134
54afb2a @cibomahto Send the mode name as a UDP packet with a different magic number
cibomahto authored
135 modeBuffer[0] = 2;
15268ec @potatono New routines, Gamma.
potatono authored
136 for (int i = 0; i < modeName.length(); i++) {
54afb2a @cibomahto Send the mode name as a UDP packet with a different magic number
cibomahto authored
137 modeBuffer[i+1] = (byte)modeName.charAt(i);
138 }
15268ec @potatono New routines, Gamma.
potatono authored
139
140 udp.send(modeBuffer, address, port);
54afb2a @cibomahto Send the mode name as a UDP packet with a different magic number
cibomahto authored
141 }
15268ec @potatono New routines, Gamma.
potatono authored
142
a6b64d8 @potatono Refactored everything into separate classes.
potatono authored
143 int maxSentByte = 0;
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
144 public void sendData() {
145 PImage image = get();
15268ec @potatono New routines, Gamma.
potatono authored
146
147 // if (image.width != w || image.height != h) {
148 // image.resize(w,h);
149 // }
150
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
151 image.loadPixels();
a6b64d8 @potatono Refactored everything into separate classes.
potatono authored
152 loadPixels();
15268ec @potatono New routines, Gamma.
potatono authored
153
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
154 int r;
9bc540a @mhenstell add some color
mhenstell authored
155 int g;
156 int b;
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
157 buffer[0] = 1;
158 for (int y=0; y<h; y++) {
159 for (int x=0; x<w; x++) {
15268ec @potatono New routines, Gamma.
potatono authored
160
161 if (isRGB) {
162 r = int(red(image.pixels[y*w+x]));
163 g = int(green(image.pixels[y*w+x]));
164 b = int(blue(image.pixels[y*w+x]));
165
166 if (enableGammaCorrection) {
167 r = (int)(Math.pow(r/256.0,this.gammaValue)*256);
168 g = (int)(Math.pow(g/256.0,this.gammaValue)*256);
169 b = (int)(Math.pow(b/256.0,this.gammaValue)*256);
170 }
171
172 buffer[(getAddress(x, y)*3)+1] = byte(r);
173 buffer[(getAddress(x, y)*3)+2] = byte(g);
174 buffer[(getAddress(x, y)*3)+3] = byte(b);
175 }
176 else {
177 r = int(brightness(image.pixels[y*w+x]));
178
179 if (enableGammaCorrection) {
180 r = (int)(Math.pow(r/256.0,this.gammaValue)*256);
181 }
182
183 buffer[(getAddress(x, y)+1)] = byte(r);
184 }
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
185 }
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
186 }
a6b64d8 @potatono Refactored everything into separate classes.
potatono authored
187 updatePixels();
15268ec @potatono New routines, Gamma.
potatono authored
188 udp.send(buffer, address, port);
189 }
c74e35d @madsrune Original version with modifications for Gratitude 2010
madsrune authored
190 }
cf21ed8 @potatono mv to acw.pde, add Dacwes lib.
potatono authored
191
Something went wrong with that request. Please try again.