You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.9 KiB

5 years ago
  1. import processing.core.*;
  2. import processing.xml.*;
  3. import processing.serial.*;
  4. import java.applet.*;
  5. import java.awt.Dimension;
  6. import java.awt.Frame;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.FocusEvent;
  10. import java.awt.Image;
  11. import java.io.*;
  12. import java.net.*;
  13. import java.text.*;
  14. import java.util.*;
  15. import java.util.zip.*;
  16. import java.util.regex.*;
  17. public class LaunchPad_Temp_GUI extends PApplet {
  18. /**
  19. * LaunchPad GUI
  20. *
  21. * Select COM port that LaunchPad is connected to
  22. * receive serial communication from LaunchPad for
  23. * instantaneous temperature readings.
  24. *
  25. **/
  26. PFont fontA;
  27. char instruct;
  28. int backColor;
  29. int dataRead;
  30. boolean portChosen = false;
  31. int COMPort;
  32. int [] keyIn = new int[3];
  33. int i, keyIndex=0;
  34. // The serial port:
  35. Serial myPort;
  36. public void setup()
  37. {
  38. //setup window
  39. size(1000, 500);
  40. smooth();
  41. //load font
  42. fontA = loadFont("CourierNew36.vlw");
  43. // Set the font, its size (in units of pixels), and alignment
  44. textAlign(CENTER);
  45. //Set background color
  46. int baseColor = color(70);
  47. backColor = baseColor;
  48. //import serial library
  49. // List all the available serial ports, then give prompt
  50. println(Serial.list());
  51. println("Please type in the serial COM port that your LaunchPad is connected to.");
  52. background(backColor);
  53. stroke(255);
  54. textFont(fontA, 13);
  55. text("Please, select the COM port that your LaunchPad is connected to.", 265, 20);
  56. textAlign(LEFT);
  57. for(i=0; i<Serial.list().length; i++){
  58. text("[" + i + "] " + Serial.list()[i], 215, 38+13*i);
  59. }
  60. //wait for keypress --> Refer to void keyPressed()
  61. }
  62. public void draw()
  63. {
  64. if(portChosen == true){
  65. dataRead= myPort.read();
  66. if(dataRead != -1){
  67. //clear previous temperature reading from screen
  68. background(backColor);
  69. stroke(255);
  70. //Update console
  71. print("Temp: ");
  72. print(dataRead);
  73. println("\u00b0");
  74. //Update on screen GUI
  75. text("Current Temperature: ", 420, 60);
  76. text(dataRead, 835, 60);
  77. text("\u00b0", 900, 60);
  78. }
  79. }
  80. }
  81. //wait for key press. Once key is entered, initialize serial com port
  82. public void keyPressed() {
  83. if(portChosen == false){
  84. if (key != 10) //Enter
  85. keyIn[keyIndex++] = key-48;
  86. else
  87. {
  88. COMPort = 0;
  89. for (i = 0; i < keyIndex; i++)
  90. COMPort = COMPort * 10 + keyIn[i];
  91. println(COMPort);
  92. myPort = new Serial(this, Serial.list()[COMPort], 2400);
  93. portChosen = true;
  94. textFont(fontA, 60); // change font size & alignment for temp readings
  95. textAlign(CENTER);
  96. }
  97. }
  98. }
  99. static public void main(String args[]) {
  100. PApplet.main(new String[] { "--bgcolor=#E2E2E2", "LaunchPad_Temp_GUI" });
  101. }
  102. }