Segundo applet, fechando-o e botões transparentes no Processamento 3

Para todos os KU. Meu primeiro post, por favor, não julgue estritamente.

Havia necessidade de trabalhos para ler visualmente o status do equipamento e, se necessário, reiniciar o criocompressor remotamente. O Arduino UNO estava à mão, um sensor de luz foi comprado para ele, até agora 2 relés e 2 solenóides a 12 V (não é o ponto). No futuro, esse negócio ficará cheio de sensores de temperatura, umidade e pressão, mas até agora não está pronto.

O motivo para escrever a postagem é que havia muito pouca informação na Internet sobre os recursos do Processing 3, especialmente na parte em russo. Espero que alguns manequins como eu sejam úteis algum dia para não serem estúpidos como eu na semana passada.

O esboço para o Arduino é tão simples quanto três rublos: após o recebimento de certos valores da porta de comunicação, o dunk define o valor nos pinos e escreve a resposta na porta de comunicação, mede a iluminação e também informa sobre ela. No futuro, será ampliado.

Esboço Arduino:

#include <Wire.h> #include <BH1750.h> int val; BH1750 lightMeter; void setup() { Serial.begin(9600); lightMeter.begin(); Serial.println("Running..."); pinMode (7, OUTPUT); pinMode(6, OUTPUT); digitalWrite(7, HIGH); digitalWrite(6, HIGH); } void loop() { uint16_t lux = lightMeter.readLightLevel(); if (Serial.available()) { val = Serial.read(); if (val == '0') { digitalWrite(7, HIGH); digitalWrite(6, HIGH); Serial.println("  "); } if (val == '1') { digitalWrite(7, HIGH); digitalWrite(6, LOW); Serial.println(" 1 "); } if (val == '2') { digitalWrite(7, LOW); digitalWrite(6, HIGH); Serial.println("  2"); } if (val == '3') { digitalWrite(7, LOW); digitalWrite(6, LOW); Serial.println("  "); } if (val == '4') { if (lux >= 800) { Serial.print("Light: "); Serial.print(lux); Serial.println(" lx   "); } else { Serial.print("Light: "); Serial.print(lux); Serial.println(" lx  !!!!!!!"); } } } } 

Eu acho que isso não é óbvio, mas devido ao fato de outras pessoas usarem a funcionalidade do meu Pribluda no futuro, eu queria escrever uma GUI simples, mas compreensível. A escolha recaiu sobre o Processing, pois eu já conhecia o IDE do Arduino, que, por sua vez, foi construído com base no Processing.

O applet usa a biblioteca ControlP5 para renderizar controles.

Pessoalmente, tive problemas ao implementar e procurar informações sobre as seguintes coisas: 1) iniciar a segunda janela do aplicativo, 2) fechar a 2ª janela, mas para que a janela principal continue a funcionar, 3) por diversão - definir imagens como plano de fundo e botões, botões translúcidos.

Então, o esboço Processando:

Listagem de código
 import processing.serial.*; import controlP5.*; import java.util.Date; Serial port; Textfield recived; Textfield recived1; Textfield recived2; Textfield recived3; Textfield recived4; Textfield recived5; String val; String data; String DateTab; String Dat; int data2=0; String time; Button vyvod; PrintWriter output; Table table2; int i=0; ControlP5 cp5; ControlP5 ax; PFont font; PFont font2; String[] args = {"YourSketchNameHere"}; SecondApplet sa; PImage fon; PImage fon2; PImage titlebaricon; //     void setup() { titlebaricon = loadImage("data/myicon.png"); surface.setIcon(titlebaricon); surface.setTitle(" "); //     ))) printArray(Serial.list()); port = new Serial(this, "/dev/ttyUSB0", 9600); // com  ( Ubuntu,  Windows  //com port) cp5 = new ControlP5(this); font = createFont("Arial", 20); //  fon = loadImage("data/phil.bmp"); //     fon2 = loadImage("data/fon2.bmp"); /*        void settings()*/ PImage img; img = loadImage("data/image1.bmp"); // -     "data"  image(img, 0, 0); tint(0, 255, 0, 126); //  - 50%      image(img, 50, 0); PImage img2; img2 = loadImage("data/image2.bmp"); image(img2, 0, 0); image(img2, 50, 0); //       // tint()       . PImage img3; //       . img3 = loadImage("data/image3.bmp"); image(img3, 0, 0); image(img3, 50, 0); PImage img4; img4 = loadImage("data/image4.bmp"); image(img4, 0, 0); image(img4, 50, 0); PImage img5; img5 = loadImage("data/imagezapros.bmp"); image(img5, 0, 0); image(img5, 50, 0); PImage img6; img6 = loadImage("data/temper.bmp"); image(img6, 0, 0); image(img6, 50, 0); cp5.addButton("Relay_1") .setSize (100, 50) .setPosition(50, 50) .setFont(font) .setImage(img) .setLabel(" 1"); cp5.addButton("Relay_2") .setSize (100, 50) .setPosition(250, 50) .setFont(font) .setImage(img2) .setLabel(" 2"); cp5.addButton("Relay_ON") .setSize (300, 50) .setPosition(50, 120) .setFont(font) .setLabel(" ") .setImage(img3); cp5.addButton("Relay_OFF") .setSize (300, 50) .setPosition(50, 190) .setFont(font) .setLabel(" ") .setImage(img4); cp5.addButton("Zapros_sostoyaniya") .setSize (300, 50) .setPosition(50, 260) .setFont(font) .setLabel(" ") .setImage(img5); recived=cp5.addTextfield(" ") .setSize(380,100) .setPosition(10, 340) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); cp5.addButton("temps") .setSize (380, 50) .setPosition(10, 500) .setColorBackground(color(70, 100, 0)) .setColorForeground(color(0, 0, 0)) .setFont(font) .setLabel("") .setImage(img6); } void settings(){ size(400, 560); } void draw() { background(fon); fill(0, 0, 0); textFont(font); text(" ", 135, 30); int s = second(); int m = minute(); int h = hour(); int d = day(); int mo = month(); int y = year(); DateTab = str(d)+ "." + str(mo) + "." + str(y) + (" ") + str(h) + (":") + str(m) + (":") + str(s); if ( port.available() > 0){ val = port.readString(); if(data2==0){ //        -   , recived.setText(val); // - com ,  -   . } /* data2 - ,    Second Applet     textfield   . */ } sa = new SecondApplet(); } void Relay_1(){ port.write("1"); } void Relay_2(){ port.write("2"); } void Relay_ON(){ port.write("3"); } void Relay_OFF(){ port.write("0"); } void Zapros_sostoyaniya(){ data2=0; port.write("4"); } void save123(){ //       data2=1; port.write("4"); delay(600); port.available(); Dat=DateTab + " " + val; table2 = loadTable("data/Journaltemp.ods"); //   table2.setString(0, 0, "t"); //     0.0 table2.removeColumn(0); /*  0,          */ TableRow newRow = table2.addRow(); //   newRow.setString(0, Dat); /*  0        ( )*/ saveTable(table2, "data/Journaltemp.ods"); //    data2=0; } void temps(){ PApplet.runSketch(args, sa); //  Second Applet } public class SecondApplet extends PApplet { public void setup(){ surface.setTitle(" "); surface.setIcon(titlebaricon); font2 = createFont("Arial", 20); /*   , ,    ..*/ ax = new ControlP5(this); ax.addButton("test1") .setSize(159, 70) .setPosition(1, 300) .setFont(font2) .setLabel(""); ax.addButton("test2") .setSize(158, 70) .setPosition(161, 300) .setFont(font2) .setLabel(""); ax.addButton("test3") .setSize(159, 70) .setPosition(320, 300) .setFont(font2) .setLabel(""); ax.addButton("test4") .setSize(159, 70) .setPosition(480, 300) .setFont(font2) .setLabel(""); ax.addButton("test5") .setSize(159, 70) .setPosition(640, 300) .setFont(font2) .setLabel(""); ax.addButton("obj") .setSize (790, 50) .setPosition(5, 540) .setColorBackground(color(70, 100, 0)) .setColorForeground(color(0, 0, 0)) .setFont(font) .setLabel(""); recived1 = ax.addTextfield(" ") .setSize(157, 70) .setPosition(2, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived2=ax.addTextfield(" ") .setSize(156, 70) .setPosition(162, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived3=ax.addTextfield(" ") .setSize(157, 70) .setPosition(321, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived4=ax.addTextfield(" ") .setSize(157, 70) .setPosition(481, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived5=ax.addTextfield(" ") .setSize(157, 70) .setPosition(641, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); } public void settings() { size(800, 600); //    } public void draw() { background(fon2); //  )) textFont(font2); } public void test1(){ recived1.setText(" 11111"); } public void test2(){ recived2.setText(" 22222"); } public void test3(){ recived3.setText(" 33333"); //    Second Applet } public void test4(){ recived4.setText(" 44444"); } public void test5(){ recived5.setText(" 55555"); } public void obj(){ save123(); /*              */ } public void exitActual(){} //      [X]    . } //      ,       [X]. 


O programa que recebi:

Vezes
Dois

Peço a você para não chutar muito - meu primeiro projeto em Processing, espero que alguém possa descobrir isso com coisas simples. E se alguém me disser como otimizar meu código g ... ficarei feliz.

Source: https://habr.com/ru/post/pt478552/


All Articles