Advertisement
Johanneszockt1

Untitled

Mar 29th, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. ControlP5 cp5;
  2. import processing.serial.*;  // Import serial library
  3. import controlP5.*;  // Import controlP5 library
  4. Serial myPort;
  5.  
  6. int Octave=50;
  7. boolean Start_conversion =true;
  8.  
  9. boolean serialConnected = false;
  10.  
  11. //code 2
  12. PFont f;
  13. String saved="";
  14. String typing="";
  15.  
  16. void setup() {
  17.   myPort = new Serial(this, "COM5", 115200);
  18.   myPort.clear();          // clear the serial port buffer
  19.   delay(20);
  20.   //size(500, 400);
  21.  
  22.   //code 2
  23.   size(480, 270);
  24.   f = createFont("Arial", 16);
  25.  
  26.   cp5 = new ControlP5(this);
  27.   cp5.addTextfield("input")
  28.     .setPosition(20, 100)
  29.     .setSize(200, 40)
  30.     .setFont(f)
  31.     .setFocus(true)
  32.     .setColor(color(255, 0, 0))
  33.     ;
  34.  
  35.   textFont(f);
  36. }
  37.  
  38. void draw() {
  39.  
  40.   //background(100, 100, 100);
  41.   //text("Laser Harp Control", 150, 50);
  42.   //textSize(30);
  43.   background(255);
  44.   int indent =25;
  45.   textFont(f);
  46.   fill(0);
  47.   text("Input: " + typing, indent, 190);
  48.   text("Saved text: " + saved, indent, 230);
  49.  
  50.   //println(Octave,Scale,Transposition);
  51.   /*if (Start_conversion == true) {
  52.    
  53.    if (serialConnected) {
  54.    myPort.write("A");
  55.    myPort.write(saved);
  56.    myPort.write("\r\n");
  57.    //println(Octave);
  58.    }
  59.    }*/
  60. }
  61.  
  62. void serialEvent(Serial myPort) {
  63.   // read a byte from the serial port:
  64.   while (myPort.available() > 0) {
  65.     char input = myPort.readChar();
  66.     print(input);
  67.     if (input == 'A') {
  68.       serialConnected = true;
  69.       print("match");
  70.     }
  71.   }
  72. }
  73. String StringArray[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
  74.  
  75. void keyPressed() {
  76.   /*if (key=='\n') {
  77.    saved=typing;
  78.    typing="";
  79.    if (Start_conversion == true) {
  80.    if (serialConnected) {
  81.    for (int i=0; i<12; i++) {
  82.    if (StringArray[i]==saved) {
  83.    println("found value at index position: "+i);
  84.    }
  85.    }
  86.    //myPort.write("A");
  87.    //myPort.write(saved);
  88.    //myPort.write("\r\n");
  89.    //println(Octave);
  90.    }
  91.    }
  92.    //println(saved);
  93.    } //else if (key==50) {
  94.    
  95.    //
  96.    else {
  97.    //typing = typing+key;
  98.    }*/
  99. }
  100.  
  101. void controlEvent(ControlEvent theEvent) {
  102.   if (theEvent.isAssignableFrom(Textfield.class)) {
  103.     /*println("controlEvent: accessing a string from controller '"
  104.      +theEvent.getName()+"': "
  105.      +theEvent.getStringValue()
  106.      );*/
  107.      int indexValue = 0;
  108.     saved = theEvent.getStringValue();
  109.     if (Start_conversion == true) {
  110.       if (serialConnected) {
  111.         for (int i=0; i<12; i++) {
  112.           if (StringArray[i].equals(saved)) {
  113.             println("found value at index position: "+i);
  114.             indexValue = i;
  115.           }
  116.         }
  117.         myPort.write("A");
  118.         myPort.write(str(indexValue));
  119.         myPort.write("\r\n");
  120.         //println(Octave);
  121.       }
  122.     }
  123.   }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement