turt2live

Untitled

Jun 16th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.78 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. Serial port;
  4. char buzzer = '1';
  5. char led = '0';
  6. color defaultButton = color(255, 255, 255);
  7. color hoverButton = color(150, 150, 150);
  8. color defaultText = color(0, 102, 153);
  9. color hoverText = color(0, 0, 0);
  10. int buttonH = 50;
  11. int buttonW = 300;
  12.  
  13. color stateOn = color(38, 191, 0);
  14. color stateOff = color(25, 115, 0);
  15. color stateOnText = color(47, 235, 0);
  16. color stateOffText = color(17, 87, 0);
  17. color ledStateOn = stateOn;
  18. color ledStateOff = stateOff;
  19. color ledStateOnText = stateOnText;
  20. color ledStateOffText = stateOffText;
  21. color buzzerStateOn = stateOn;
  22. color buzzerStateOff = stateOff;
  23. color buzzerStateOnText = stateOnText;
  24. color buzzerStateOffText = stateOffText;
  25. int stateW = buttonW;
  26. int stateH = buttonH;
  27.  
  28. int ledAlarmTicks = 10;
  29. int ledTicks = 0;
  30. boolean ledOn = false;
  31. boolean aLedOn = false;
  32.  
  33. int ledBlinkX = 10;
  34. int ledBlinkY = 10;
  35. int ledBlinkW = buttonW;
  36. int ledBlinkH = buttonH;
  37. color ledBlinkTextColor = defaultText;
  38. color ledBlinkButtonColor = defaultButton;
  39.  
  40. int ledToggleX = ledBlinkX+ledBlinkW+10;
  41. int ledToggleY = 10;
  42. int ledToggleW = buttonW;
  43. int ledToggleH = buttonH;
  44. color ledToggleTextColor = defaultText;
  45. color ledToggleButtonColor = defaultButton;
  46.  
  47. int buzzerAlarmTicks = 30;
  48. int buzzerTicks = 0;
  49. boolean buzzerOn = false;
  50. boolean aBuzzerOn = false;
  51.  
  52. int buzzerBlinkX = ledBlinkX;
  53. int buzzerBlinkY = ledBlinkY+ledBlinkH+10;
  54. int buzzerBlinkW = buttonW;
  55. int buzzerBlinkH = buttonH;
  56. color buzzerBlinkTextColor = defaultText;
  57. color buzzerBlinkButtonColor = defaultButton;
  58.  
  59. int buzzerToggleX = buzzerBlinkX+buzzerBlinkW+10;
  60. int buzzerToggleY = ledBlinkY+ledBlinkH+10;
  61. int buzzerToggleW = buttonW;
  62. int buzzerToggleH = buttonH;
  63. color buzzerToggleTextColor = defaultText;
  64. color buzzerToggleButtonColor = defaultButton;
  65.  
  66. int bothBlinkX = buzzerBlinkX;
  67. int bothBlinkY = buzzerBlinkY+buzzerBlinkH+10;
  68. int bothBlinkW = buttonW;
  69. int bothBlinkH = buttonH;
  70. color bothBlinkTextColor = defaultText;
  71. color bothBlinkButtonColor = defaultButton;
  72.  
  73. int bothToggleX = bothBlinkX+bothBlinkW+10;
  74. int bothToggleY = buzzerBlinkY+buzzerBlinkH+10;
  75. int bothToggleW = buttonW;
  76. int bothToggleH = buttonH;
  77. color bothToggleTextColor = defaultText;
  78. color bothToggleButtonColor = defaultButton;
  79.  
  80. void setup(){
  81.   size(630, 500);
  82.   frame.setTitle("Arduino Alarm");
  83.   port = new Serial(this, "COM3", 9600);
  84.   port.bufferUntil('\n');
  85. }
  86.  
  87. void draw(){
  88.    PFont font = createFont(PFont.list()[0], 32);
  89.    background(0, 0, 0);
  90.    stroke(256);
  91.    
  92.    // Buttons
  93.    fill(ledBlinkButtonColor);
  94.    rect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH);
  95.    textFont(font);
  96.    fill(ledBlinkTextColor);
  97.    text("BLINK LED", ledBlinkX+10, ledBlinkY+35);
  98.    
  99.    fill(ledToggleButtonColor);
  100.    rect(ledToggleX, ledToggleY, ledToggleW, ledToggleH);
  101.    textFont(font);
  102.    fill(ledToggleTextColor);
  103.    text("TOGGLE LED", ledToggleX+10, ledToggleY+35);
  104.    
  105.    fill(buzzerBlinkButtonColor);
  106.    rect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH);
  107.    textFont(font);
  108.    fill(buzzerBlinkTextColor);
  109.    text("TIMED BUZZER", buzzerBlinkX+10, buzzerBlinkY+35);
  110.    
  111.    fill(buzzerToggleButtonColor);
  112.    rect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH);
  113.    textFont(font);
  114.    fill(buzzerToggleTextColor);
  115.    text("TOGGLE BUZZER", buzzerToggleX+10, buzzerToggleY+35);
  116.    
  117.    fill(bothBlinkButtonColor);
  118.    rect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH);
  119.    textFont(font);
  120.    fill(bothBlinkTextColor);
  121.    text("PULSE BOTH", bothBlinkX+10, bothBlinkY+35);
  122.    
  123.    fill(bothToggleButtonColor);
  124.    rect(bothToggleX, bothToggleY, bothToggleW, bothToggleH);
  125.    textFont(font);
  126.    fill(bothToggleTextColor);
  127.    text("TOGGLE BOTH", bothToggleX+10, bothToggleY+35);
  128.    
  129.    // Status
  130.    if(aLedOn){
  131.       fill(ledStateOn);
  132.    }else{
  133.       fill(ledStateOff);
  134.    }
  135.    rect(bothBlinkX, bothBlinkY+bothBlinkH+10, stateW, stateH);
  136.    textFont(font);
  137.    if(aLedOn){
  138.      fill(ledStateOnText);
  139.    }else{
  140.      fill(ledStateOffText);
  141.    }
  142.    text("LED", bothBlinkX+10, bothBlinkY+bothBlinkH+45);  
  143.    if(aBuzzerOn){
  144.       fill(buzzerStateOn);
  145.    }else{
  146.       fill(buzzerStateOff);
  147.    }
  148.    rect(bothToggleX, bothToggleY+bothToggleH+10, stateW, stateH);
  149.    textFont(font);
  150.    if(aBuzzerOn){
  151.      fill(buzzerStateOnText);
  152.    }else{
  153.      fill(buzzerStateOffText);
  154.    }
  155.    text("BUZZER", bothToggleX+10, bothToggleY+bothToggleH+45);  
  156.    
  157.    // Handle loops
  158.    if(ledOn){
  159.      if(ledTicks>=ledAlarmTicks){
  160.        ledTicks = 0;
  161.        port.write(led);
  162.      }
  163.      ledTicks++;
  164.    }else{
  165.     ledTicks = 0;
  166.    }
  167.    if(buzzerOn){
  168.      if(buzzerTicks>=buzzerAlarmTicks){
  169.         buzzerTicks = 0;
  170.         port.write(buzzer);
  171.      }
  172.      buzzerTicks++;
  173.    }else{
  174.      buzzerTicks = 0;
  175.    }
  176.    
  177.    // Handle hover
  178.    if(overRect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH)){
  179.      resetButtons();
  180.      ledBlinkTextColor = hoverText;
  181.      ledBlinkButtonColor = hoverButton;
  182.    }else if(overRect(ledToggleX, ledToggleY, ledToggleW, ledToggleH)){
  183.      resetButtons();
  184.      ledToggleTextColor = hoverText;
  185.      ledToggleButtonColor = hoverButton;
  186.    }else if(overRect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH)){
  187.      resetButtons();
  188.      buzzerBlinkTextColor = hoverText;
  189.      buzzerBlinkButtonColor = hoverButton;
  190.    }else if(overRect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH)){
  191.      resetButtons();
  192.      buzzerToggleTextColor = hoverText;
  193.      buzzerToggleButtonColor = hoverButton;
  194.    }else if(overRect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH)){
  195.      resetButtons();    
  196.      bothBlinkTextColor = hoverText;
  197.      bothBlinkButtonColor = hoverButton;
  198.    }else if(overRect(bothToggleX, bothToggleY, bothToggleW, bothToggleH)){
  199.      resetButtons();    
  200.      bothToggleTextColor = hoverText;
  201.      bothToggleButtonColor = hoverButton;
  202.    }else{
  203.      resetButtons();
  204.    }
  205. }
  206.  
  207. void mousePressed(){
  208.    if(overRect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH)){
  209.      ledOn = !ledOn;
  210.      if(!ledOn && aLedOn){
  211.          port.write(led);
  212.      }
  213.    }else if(overRect(ledToggleX, ledToggleY, ledToggleW, ledToggleH)){
  214.      port.write(led);
  215.    }else if(overRect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH)){
  216.      buzzerOn = !buzzerOn;
  217.      if(!buzzerOn && aBuzzerOn){
  218.         port.write(buzzer);
  219.      }
  220.    }else if(overRect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH)){
  221.       port.write(buzzer);
  222.    }else if(overRect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH)){
  223.      ledOn = !ledOn;
  224.      if(!ledOn && aLedOn){
  225.          port.write(led);
  226.      }
  227.      buzzerOn = !buzzerOn;
  228.      if(!buzzerOn && aBuzzerOn){
  229.         port.write(buzzer);
  230.      }
  231.    }else if(overRect(bothToggleX, bothToggleY, bothToggleW, bothToggleH)){
  232.      port.write(buzzer);
  233.      port.write(led);
  234.    }
  235. }
  236.  
  237. void serialEvent(Serial port){
  238.    float in = float(port.readStringUntil('\n'));
  239.    if(in == 1){
  240.       aLedOn = true;
  241.    }else if(in == 0){
  242.       aLedOn = false;
  243.    }else if(in == 2){
  244.       aBuzzerOn = false;
  245.    }else if(in == 3){
  246.       aBuzzerOn = true;
  247.    }
  248. }
  249.  
  250. void resetButtons(){
  251.      ledBlinkTextColor = defaultText;
  252.      ledBlinkButtonColor = defaultButton;
  253.      ledToggleTextColor = defaultText;
  254.      ledToggleButtonColor = defaultButton;
  255.      buzzerBlinkTextColor = defaultText;
  256.      buzzerBlinkButtonColor = defaultButton;
  257.      buzzerToggleTextColor = defaultText;
  258.      buzzerToggleButtonColor = defaultButton;
  259.      bothBlinkTextColor = defaultText;
  260.      bothBlinkButtonColor = defaultButton;
  261.      bothToggleTextColor = defaultText;
  262.      bothToggleButtonColor = defaultButton;
  263. }
  264.  
  265. boolean overRect(int x, int y, int width, int height){
  266.   if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height){
  267.     return true;
  268.   }else{
  269.     return false;
  270.   }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment