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.

98 lines
2.2 KiB

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