KRITSADA

Untitled

Dec 24th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2. Serial port;  // Создание объекта класса Serial
  3. int val;      // Данные, получаемые из сом-порта
  4. int[] values;
  5. float zoom;
  6. char flag = 0;
  7. char first_m = 1;
  8. String inputString = "";
  9.  
  10. String portValue = ""; //значення, яке ми отримуємо з СОМ-порту
  11. String buffer = "";
  12. int Ipoint = 0;
  13. int Upoint = 0;
  14. int Tpoint = 0;
  15. //int Xpoint_1 = 0;
  16. //int Ypoint_1 = 0;
  17.  
  18. int x0, y0, x1, y1;
  19. int x0_1, y0_1, x1_1, y1_1;
  20. int z = 0;
  21. float R_float;
  22. String R_out;
  23.  
  24.  
  25. void setup()
  26. {
  27.   size(640, 640);
  28.  
  29.  
  30.   // Открываем порт, к которому подключена плата
  31.  
  32.   //контроллера используя скорость (9600 bps)
  33.   port = new Serial(this, Serial.list()[0], 9600);
  34.   values = new int[3];
  35.   zoom = 1.0f;
  36.   smooth();
  37.  
  38.   background(0);
  39. }
  40.  
  41.  
  42. //Функція, яка викликається, коли з СОМ-порту приходять нові дані
  43. void serialEvent(Serial port)
  44. {
  45.   //Зчитуємо дані з СОМ-порту і записуємо у відповідну змінну
  46.   portValue = port.readStringUntil('\n');
  47.  
  48.  
  49.   if (portValue != null) {
  50.  
  51.     flag = 1;
  52.     inputString = portValue;
  53.   }
  54. }
  55.  
  56.  
  57.  
  58.  
  59. void pushValue(int value) {
  60.   for (int i=0; i<width-1; i++)
  61.     values[i] = values[i+1];
  62.   values[width-1] = value;
  63. }
  64.  
  65.  
  66. void drawLines2() {
  67.   stroke(255);
  68.  
  69.   int displayWidth = (int) (width / zoom);
  70.  
  71.  
  72.   line(x0, y0, x1, y1);
  73.   line(x0_1, y0_1, x1_1, y1_1);
  74.   //text(x0_1 + " " + y0_1 + " " + x1_1 + " " +Upoint/Ipoint, width-145, 50);  
  75.   x0 = x1;
  76.   y0 = y1;
  77.  
  78.   x0_1 = x1_1;
  79.   y0_1 = y1_1;
  80.  
  81.  
  82.   // text(y1, 30, 30);
  83. }
  84.  
  85. /*void drawLines3() {
  86.   stroke(255);
  87.  
  88.   int displayWidth = (int) (width / zoom);
  89.  
  90.  
  91.   line(x01, y01, x11, y11);
  92.   x01 = x11;
  93.   y01 = y11;
  94.  
  95.  
  96.   // text(y1, 30, 30);
  97. }
  98. */
  99.  
  100.  
  101.  
  102. void drawGrid() {
  103.   stroke(255, 0, 0);
  104.   line(15, height-350, width-15, height-350);
  105.   line(15, height-350, 15, 15);
  106.   line(width-15, height-350, width-30, height-360); //>x
  107.   line(width-15, height-350, width-30, height-340); //>x
  108.   line(15, 15, 25, 30);//>y
  109.   line(15, 15, 5, 30);//>y
  110.  
  111.  
  112.  
  113.   //second
  114.   line(15, height-15, width-15, height-15);
  115.   line(15, height-15, 15, height-300);
  116.   line(width-15, height-15, width-30, height-25); //>x
  117.   line(width-15, height-15, width-30, height-5);  //>x
  118.   line(15, height-300, 25, height-285);
  119.   line(15, height-300, 5, height-285);
  120.  
  121.  
  122. }
  123.  
  124.  
  125. void keyReleased() {
  126.   switch (key) {
  127.   case '+':
  128.     zoom *= 2.0f;
  129.     println(zoom);
  130.     if ( (int) (width / zoom) <= 1 )
  131.       zoom /= 2.0f;
  132.     break;
  133.   case '-':
  134.     zoom /= 2.0f;
  135.     if (zoom < 1.0f)
  136.       zoom *= 2.0f;
  137.     break;
  138.   }
  139. }
  140.  
  141. void draw()
  142. {
  143.  
  144.  
  145.   //
  146.  
  147.  
  148.   if (flag == 1) {
  149.  
  150.     drawGrid();
  151.  
  152.  
  153.  
  154.  
  155.     //=====================================
  156.     values = int(split(inputString, ","));
  157.  
  158.     Upoint=values[0];
  159.     Tpoint=values[1];
  160.     Ipoint=values[2];
  161.    
  162.     fill(0);
  163.     stroke(0);
  164.     rect( width-146, 19, width, 81);
  165.     fill(255);
  166.     textSize(18);
  167.     text("U = " + Upoint + " mV", width-145, 50);
  168.     text("I = " + Ipoint + " mA", width-145, 80);
  169.     text("I", 35, 35);
  170.     text("U", width-35, height- 370);
  171.     //=====================================
  172.     //for float R
  173.     //rect( width-30, height-150, width, 81);
  174.    
  175.    
  176.     /*Xpoint_1=values[2];
  177.     Ypoint_1=values[0];
  178.     */
  179.     fill(0);
  180.     stroke(0);
  181.     rect( width-146, height-330, width, height-550);
  182.     fill(255);
  183.     text("T = " + Tpoint + " °C", width-145, height-300);
  184.     //text(x1_1, width-145, height-300);
  185.     text("R = " + R_out + " Om", width-145, height-270);
  186.        //text(x1_1, width-145, height-240);
  187.     text("T", 35, height-285);
  188.     text("R", width-35, height- 35);
  189.     R_out = String.format("%.2f", R_float);
  190.    
  191.  
  192.  
  193.     x1 =  int(Upoint / 1023.0f / 30 * (width-30) + 15);
  194.     if(Ipoint > 0){
  195.     y1 = int(height - 350 - Ipoint * 1000 / 1023.0f / 75 * (height-30));
  196.     }
  197.     else{
  198.      
  199.     }
  200.    
  201.     //z=Xpoint/Ypoint;
  202.     //if(Ipoint!=0){
  203.     R_float = float(Upoint) / float(Ipoint*1000);
  204.     //R_float = 1 / 2;
  205.    
  206.     if(R_float > 0){
  207.     x1_1 = int(R_float *500 / 1023.0f * (width-30) + 15);
  208.     }
  209.     else{
  210.      R_float = 0;
  211.     }
  212.     //text(x1_1, width-35, height- 105);
  213.    
  214.     //}
  215.     //else{
  216.      //x1_1=0;
  217.     //}
  218.     y1_1 = int(height - 15 - Tpoint *2 / 1023.0f * (height-30)); //graphik 2
  219.     //y1_1 = int(height-Tpoint*2);
  220.    
  221.     //text(y1_1, width-145, height-240);
  222.  
  223.     //==================================================================
  224.     if (first_m == 1)
  225.     {
  226.       x0 = x1;
  227.       y0 = y1;
  228.      
  229.       x0_1 = x1_1;
  230.       y0_1 = y1_1;
  231.       first_m = 0;
  232.     } else
  233.       drawLines2();
  234.       flag = 0;
  235.   };
  236.  
  237.   // val = getValue();
  238.   /*
  239.   if (val != -1) {
  240.    pushValue(val);
  241.    }
  242.    drawLines();
  243.    */
  244. }
Add Comment
Please, Sign In to add comment