Advertisement
tiodocomputador

Processing - código base - um botao

Sep 19th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /*
  2. * pa_PushButton
  3. *
  4. * Reads the values which represent the state of a push button
  5. * from the serial port and draws a graphical representation.
  6. *
  7. * This file is part of the Arduino meets Processing Project.
  8. * For more information visit http://www.arduino.cc.
  9. *
  10. * copyleft 2005 by Melvin Ochsmann for Malm� University
  11. *
  12. */
  13.  
  14. // importing the processing serial class
  15. import processing.serial.*;
  16.  
  17. // the display item draws background and grid
  18. DisplayItems di;
  19.  
  20. // definition of window size and framerate
  21. int xWidth = 512;
  22. int yHeight = 256;
  23. int fr = 24;
  24.  
  25. // attributes of the display
  26. boolean bck = true;
  27. boolean grid = true;
  28. boolean g_vert = false;
  29. boolean g_horiz = true;
  30. boolean g_values = true;
  31. boolean output = false;
  32.  
  33. // variables for serial connection, portname and baudrate have to be set
  34. Serial port;
  35. String portname = "/dev/ttyACM0";
  36. int baudrate = 9600;
  37. int value = 0;
  38.  
  39. // variables to draw graphics
  40. int actVal = 0;
  41. int num = 6;
  42. float valBuf[] = new float[num];
  43. int i;
  44.  
  45. // lets user control DisplayItems properties and value output in console
  46. void keyPressed() {
  47. if (key == 'b' || key == 'B') bck=!bck; // background black/white
  48. if (key == 'g' || key == 'G') grid=!grid; // grid ON/OFF
  49. if (key == 'v' || key == 'V') g_values=!g_values; // grid values ON/IFF
  50. if (key == 'o' || key == 'O') output=!output; //turns value output ON/OFF
  51. }
  52.  
  53. void setup() {
  54. // set size and framerate
  55. size(xWidth, yHeight);
  56. frameRate(fr);
  57. // establish serial port connection
  58. port = new Serial(this, portname, 9600);
  59. println(port);
  60. // create DisplayItems object
  61. di = new DisplayItems();
  62. // clear value buffer
  63. for (i=0; i < num; i++)
  64. {
  65. valBuf[0] = 0;
  66. }
  67. }
  68.  
  69. void drawPushButtonState()
  70. {
  71. // read through the value buffer
  72. // and shift the values to the left
  73. for (i=1; i < num; i++)
  74. {
  75. valBuf[i-1] = valBuf[i];
  76. }
  77. // add new values to the end of the array
  78. valBuf[num-1] = actVal;
  79. noStroke();
  80. // reads through the value buffer and draws lines
  81. for (int i=0; i < num; i=i+2)
  82. {
  83. fill(int((valBuf[i]*255)/height), int((valBuf[i]*255)/height), 255);
  84. rect(0, height-valBuf[i], width, 3);
  85. fill(int((valBuf[i+1]*255)/height), 255, 0 );
  86. rect(0, height-valBuf[i+1], width, 3);
  87. }
  88. // display value
  89. fill(((bck) ? 185 : 75));
  90. text( ""+(actVal), 96, height-actVal);
  91. }
  92.  
  93. void serialEvent(int serial)
  94. {
  95. // if serial event is 'H' actVal is increased
  96. if (serial=='H')
  97. {
  98. actVal = (actVal < height - (height/16)) ? (actVal + int(actVal/(height/2))+1) : (actVal = height - (height/(height/2)));
  99. if (output) println("Value read from serial port is 'H' - actualValue is now "+actVal);
  100. }
  101. else
  102. {
  103. // if serial event is 'L' actVal is decreased
  104. actVal = (actVal > 1) ? (actVal = actVal - int(actVal/64)-1) : (actVal=0);
  105. if (output) println("Value read from serial port is 'L' - actualValue is now "+actVal);
  106. }
  107. }
  108.  
  109. void draw()
  110. {
  111. // listen to serial port and trigger serial event
  112. while (port.available () > 0)
  113. {
  114. value = port.read();
  115. serialEvent(value);
  116. }
  117. // draw background, then PushButtonState and finally rest of DisplayItems
  118. di.drawBack();
  119. drawPushButtonState();
  120. di.drawItems();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement