Posted By

Pjotor on 09/17/09


Tagged

fun Processing blinkenlichten


Versions (?)

Blink


 / Published in: Processing
 

the Amazing Blinkenlights (w/o images... or documentation for that part)

  1. import processing.opengl.*;
  2. import javax.media.opengl.*;
  3.  
  4. // import libraries
  5. import hypermedia.net.*;
  6.  
  7.  
  8. UDP udp; // define the UDP object
  9. PImage bg, bgRed, sl, light, trig, e404, e500;
  10. float radius, top_env, left_env,a;
  11. String timeStr;
  12. String b0rkenMsg = "";
  13. int totalUsers, activeUsers;
  14. PFont led;
  15. boolean b0rken = false;
  16. PGraphicsOpenGL pgl;
  17. GL gl;
  18. float noiseScale=0.02;
  19.  
  20. Dot[] dots;
  21. Line[] lines;
  22.  
  23. void setup() {
  24. size(screen.width, screen.height, OPENGL);
  25. background(0);
  26. smooth();
  27. noStroke();
  28. noCursor();
  29.  
  30. bg = loadImage("worldmap.green.png");
  31. bgRed = loadImage("world-map-square-projection-red.png");
  32.  
  33. sl = loadImage("scanlines.png");
  34.  
  35. light = loadImage("dot.green.png");
  36. trig = loadImage("trig.png");
  37. e404 = loadImage("404.png");
  38. e500 = loadImage("500.png");
  39.  
  40. led = loadFont("DS55.vlw");
  41.  
  42. totalUsers = 1200;
  43. activeUsers = 1200;
  44.  
  45. top_env = (screen.width/2 - bg.width/2);
  46. left_env = (screen.height/2 - bg.height/2);
  47.  
  48. dots = new Dot[0];
  49. lines = new Line[0];
  50.  
  51. udp = new UDP( this, 1984 );
  52. udp.listen( true );
  53. // udp.log( true );
  54. radius = ( 1024 / TWO_PI );
  55.  
  56. }
  57.  
  58. void draw(){
  59. background(0);
  60. pgl = (PGraphicsOpenGL) g;
  61. gl = pgl.gl;
  62.  
  63. if(b0rken){
  64. image(bgRed, top_env, left_env);
  65. textAlign(CENTER);
  66. textFont(led, 50);
  67. text(b0rkenMsg, 512+top_env, 334+left_env);
  68. fill(#FF0000);
  69. } else {
  70. noTint();
  71. image(bg, top_env, left_env);
  72. setTotalUsers();
  73. setActiveUsers();
  74. setTime();
  75. }
  76.  
  77.  
  78. pgl.beginGL();
  79.  
  80. for(int i=0; i<height; i+=2) {
  81. gl.glColor4f( 0, 0, 0, random(.1, .3));
  82. gl.glRectf(0, i, width, i+1);
  83. }
  84.  
  85. for(int i=0; i<width; i+=5) {
  86. gl.glColor4f( 0, 0, 0, random(0, .1));
  87. gl.glRectf(i, 0,i+1, height);
  88. }
  89.  
  90. // This fixes the overlap issue
  91. gl.glDisable(GL.GL_DEPTH_TEST);
  92.  
  93. // Turn on the blend mode
  94. gl.glEnable(GL.GL_BLEND);
  95.  
  96. // Define the blend mode
  97. gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_DST_ALPHA);
  98.  
  99. if(!b0rken){
  100. for(int i=0; i<dots.length; i++) {
  101. if(dots[i] instanceof Dot){
  102. dots[i].update();
  103. dots[i].draw();
  104. if(dots[i].a < 0) dots = removeDot(i, dots);
  105. }
  106. }
  107.  
  108. for(int i=0; i<lines.length; i++) {
  109. if(lines[i] instanceof Line){
  110. lines[i].update();
  111. lines[i].draw();
  112. if(lines[i].a < 0) lines = removeLine(i, lines);
  113. }
  114. }
  115. }
  116.  
  117. pgl.endGL();
  118.  
  119. }
  120.  
  121. void receive( byte[] data, String ip, int port ) { // <-- extended handler
  122. //data = contract(data, data.length-1);
  123. String message = new String( data );
  124.  
  125. // print the result
  126. println("recived: " + message + " from IP: " + ip + " on Port: " + port);
  127. handle(message);
  128. }
  129.  
  130. void addDot(float x, float y, PImage type, float rate, String msg, color col){
  131. dots = (Dot[])expand(dots, dots.length+1);
  132. dots[dots.length-1] = new Dot(x, y, 255, type, rate, msg, col);
  133. }
  134.  
  135. void addLine(float x, float y, float xx, float yy, float rate){
  136. lines = (Line[])expand(lines, lines.length+1);
  137. lines[lines.length-1] = new Line(x, y, xx, yy, 255, rate);
  138. }
  139.  
  140. void handle(String s){
  141. float x = -100; float xx = -100;
  142. float y = -100; float yy = -100;
  143. String msg = ""; String msg2 = "";
  144. if( s.indexOf(":") > -1 && !b0rken ){
  145.  
  146. String action[] = split(s, ":");
  147.  
  148. if( s.indexOf(",") > -1 ){
  149. String coordinates[] = split(action[1], ",");
  150. y = latToY( float(coordinates[0]) );
  151. x = longToX( float(coordinates[1]) );
  152. if(coordinates.length > 3) msg = coordinates[2];
  153. if(action.length > 3){
  154. String coordinates2[] = split(action[2], ",");
  155. yy = latToY( float(coordinates2[0]) );
  156. xx = longToX( float(coordinates2[1]) );
  157. if(coordinates2.length > 2) msg2 = coordinates2[2];
  158. }
  159. }
  160.  
  161. if( action[0].indexOf("request") > -1){
  162. color col;
  163. float r, r_time;
  164. if( action.length == 3){
  165. r_time = float(action[2]);
  166. r_time = constrain(r_time, 1.0, 16.0);
  167. r = ( r_time-1.0 ) / float(15);
  168. r_time = r_time/30;
  169. col = color(r*255, 255-(r*255), 0);
  170. } else {
  171. col = color(0,255,0);
  172. r_time = 10;
  173. }
  174. addDot(x, y, light, r_time, msg, col);
  175. }
  176.  
  177. if( action[0].indexOf("500") > -1){
  178. addDot(x, y, e500, 0.1, msg, #FF0000);
  179. }
  180.  
  181. if( action[0].indexOf("404") > -1){
  182. addDot(x, y, e404, 0.1, "error", #FFFF00);
  183. }
  184.  
  185. if( action[0].indexOf("logged") > -1){
  186. activeUsers = int(action[1]);
  187. }
  188.  
  189. if( action[0].indexOf("registered") > -1){
  190. totalUsers = int(action[1]);
  191. }
  192.  
  193. if( action[0].indexOf("build_failure") > -1){
  194. b0rken = true;
  195. b0rkenMsg = action[1];
  196. }
  197.  
  198. if( action[0].indexOf("trig") > -1){
  199. addDot(x, y, trig, 0.1, msg, #0000FF);
  200. addDot(xx, yy, trig, 0.1, msg2, #0000FF);
  201. addLine(x, y, xx, yy, 0.1);
  202. }
  203.  
  204. } else {
  205.  
  206. if( s.indexOf("login") > -1){
  207. activeUsers++;
  208. }
  209.  
  210. if( s.indexOf("logout") > -1){
  211. activeUsers--;
  212. }
  213.  
  214. if( s.indexOf("signup") > -1){
  215. totalUsers++;
  216. }
  217.  
  218. if( s.indexOf("build_success") > -1){
  219. b0rken = false;
  220. }
  221.  
  222. }
  223. }
  224.  
  225. float longToX(float longitudeDegrees){
  226. float longitude = radians(longitudeDegrees);
  227. return ( (radius * longitude) + 461 + top_env )*2;
  228. }
  229.  
  230. float latToY(float latitudeDegrees){
  231. float latitude = radians(latitudeDegrees);
  232. float y = radius/2.0 * log( (1.0 + sin(latitude)) / (1.0 - sin(latitude)) );
  233. return ((y * -1.0) + 450 + left_env)*2;
  234. }
  235.  
  236. void mousePressed(){
  237. addDot(float(mouseX-20)*2, float(mouseY-20)*2, light, 10, mouseX + "," + mouseY, #00FF00);
  238. }
  239.  
  240. class Line{
  241. float x, y, xx, yy, a, rate;
  242.  
  243. Line(float ix, float iy, float ixx, float iyy, float ia, float irate) {
  244. x = ix;
  245. y = iy;
  246. xx = ixx;
  247. yy = iyy;
  248. a = ia;
  249. rate = irate;
  250. }
  251.  
  252. void update(){
  253. if(a > 0) a = a - rate;
  254. }
  255.  
  256. void draw(){
  257. float diff;
  258. if(a > 0){
  259. stroke(#0046FF, a);
  260. noFill();
  261. diff = (x - xx)*2;
  262. if(diff < 0) diff = diff * -1;
  263. curve(x+diff, y+diff, x+20, y+20, xx+20, yy+20, xx+diff, yy+diff);
  264. }
  265. }
  266. }
  267.  
  268. class Dot{
  269. float x, y, a, rate;
  270. PImage type = light;
  271. String msg;
  272. color col;
  273.  
  274. Dot(float ix, float iy, float ia, PImage itype, float irate, String imsg, color icol) {
  275. x = ix;
  276. y = iy;
  277. a = ia;
  278. type = itype;
  279. rate = irate;
  280. msg = imsg;
  281. col = icol;
  282. }
  283.  
  284. void update() {
  285. if(a > 0) a = a - rate;
  286. }
  287.  
  288. void draw() {
  289. if(a > 0){
  290. tint(255, a);
  291. fill(col, a);
  292. textFont(led, 24);
  293. text(msg, x+40, y+20);
  294. image(type, x, y, 80, 80);
  295. }
  296. }
  297. }
  298.  
  299. void setActiveUsers(){
  300. fill(#73FF02);
  301. textFont(led, 59);
  302. textAlign(LEFT);
  303. text(activeUsers, 5+top_env, 55+left_env);
  304. }
  305.  
  306. void setTotalUsers(){
  307. fill(#73FF02);
  308. textFont(led, 59);
  309. textAlign(RIGHT);
  310. text(totalUsers, 1019+top_env, 55+left_env);
  311. }
  312.  
  313. void setTime(){
  314. fill(#73FF02);
  315. textFont(led, 59);
  316. textAlign(LEFT);
  317.  
  318. String h = str(hour());
  319. String m = str(minute());
  320.  
  321. if(h.length() < 2) h = "0" + h;
  322. if(m.length() < 2) m = "0" + m;
  323.  
  324. text(h + ":" + m, 5+top_env, 645+left_env);
  325. }
  326.  
  327. Dot[] removeDot(int _i, Dot[] _a){
  328. Dot[] b = new Dot[_a.length-1];
  329. arraycopy(_a, 0, b, 0, _i);
  330. arraycopy(_a, _i+1, b, _i, b.length-_i);
  331. return b;
  332. }
  333.  
  334. Line[] removeLine(int _i, Line[] _a){
  335. Line[] b = new Line[_a.length-1];
  336. arraycopy(_a, 0, b, 0, _i);
  337. arraycopy(_a, _i+1, b, _i, b.length-_i);
  338. return b;
  339. }

Report this snippet  

You need to login to post a comment.