BareConductive / mpr121-grapher

Bare Conductive MPR121 Grapher Processing Sketch
MIT License
15 stars 4 forks source link

ControlP5 parts broken #1

Closed kasperkamperman closed 9 years ago

kasperkamperman commented 9 years ago

A lot of functions used don't seem to be supported. Dropdown is not a group anymore but a controller.

I've modified a part of the code, see below. I've commented the old code. Still the script doesn't work completely (the graph positions don't seem to change well).

mpr121_grapher.pde

/*******************************************************************************

 Bare Conductive MPR121 output grapher / debug plotter for Touch Board
 ---------------------------------------------------------------------

 mpr121_grapher.pde - processing grapher for raw data from Touch Board

 requires controlp5 to be in your processing libraries folder: 
 http://www.sojamo.de/libraries/controlP5/

 requires datastream on the Touch Board: 
 https://github.com/BareConductive/mpr121/tree/public/Examples/DataStream

 Bare Conductive code written by Stefan Dzisiewski-Smith.

 This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 
 Unported License (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

*******************************************************************************/

import processing.serial.*; 
import controlP5.*;

final int baudRate = 57600;

final int numElectrodes = 13; // includes proximity electrode 
final int numGraphPoints = 300;
final int tenBits = 1024;

final int graphsLeft = 20;
final int graphsTop = 20;
final int graphsWidth = 984;
final int graphsHeight = 540;
final int numVerticalDivisions = 8;

final int filteredColour = color(255,0,0,200);
final int baselineColour = color(0,0,255,200);
final int touchedColour = color(255,128,0,200);
final int releasedColour = color(0,128,128,200);
final int textColour = color(60);
final int touchColour = color(255,0,255,200);
final int releaseColour = color(255, 255, 255, 200);

final int graphFooterLeft = 20;
final int graphFooterTop = graphsTop + graphsHeight + 20;

final int numFooterLabels = 6;

boolean serialSelected = false;
boolean firstRead = true;
boolean paused = false;

ControlP5 cp5;
DropdownList electrodeSelector, serialSelector;
Textlabel labels[], serialPrompt, pauseInstructions;

Serial inPort;        // The serial port
String[] serialList;
String inString;      // Input string from serial port
String[] splitString; // Input string array after splitting 
int lf = 10;          // ASCII linefeed 
int[] filteredData, baselineVals, diffs, touchThresholds, releaseThresholds, status, lastStatus; 
int[][] filteredGraph, baselineGraph, touchGraph, releaseGraph, statusGraph;
int globalGraphPtr = 0;

int electrodeNumber = 0;
int serialNumber = 4;

void setup(){
  size(1024, 600);

  setupGraphs();

  serialList = Serial.list();
  println(serialList); 

  setupSerialPrompt();
}

void draw(){ 
  background(200); 
  stroke(255);
  if(serialSelected){
    drawGrid();
    drawGraphs(filteredGraph,electrodeNumber, filteredColour);
    drawGraphs(baselineGraph,electrodeNumber, baselineColour);
    drawGraphs(touchGraph,electrodeNumber, touchedColour);
    drawGraphs(releaseGraph,electrodeNumber, releasedColour);
    drawStatus(electrodeNumber);
  }
}

void serialEvent(Serial p) {

  if(serialSelected && !paused){ 

    int[] dataToUpdate;

    inString = p.readString(); 
    splitString = splitTokens(inString, ": ");

    if(firstRead && splitString[0].equals("DIFF")){
      firstRead = false;
    } else {
      if(splitString[0].equals("TOUCH")){
        updateArray(status); 
      } else if(splitString[0].equals("TTHS")){
        updateArray(touchThresholds); 
      } else if(splitString[0].equals("RTHS")){
        updateArray(releaseThresholds);
      } else if(splitString[0].equals("FDAT")){
        updateArray(filteredData); 
      } else if(splitString[0].equals("BVAL")){
        updateArray(baselineVals);
      } else if(splitString[0].equals("DIFF")){
        updateArray(diffs);
        updateGraphs(); // update graphs when we get a DIFF line
                        // as this is the last of our dataset
      }
    }
  }
} 

void controlEvent(ControlEvent theEvent) {

  // fix it seems ControlGroup right now....

  // DropdownList is of type ControlGroup.
  // A controlEvent will be triggered from inside the ControlGroup class.
  // therefore you need to check the originator of the Event with
  // if (theEvent.isGroup())
  // to avoid an error message thrown by controlP5.

  if (theEvent.isGroup()) {

    // check if the Event was triggered from a ControlGroup
    //println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());

  } 
  else if (theEvent.isController()) {

    if(theEvent.getController().getName() == "electrodeSel"){
      electrodeNumber = (int)theEvent.getController().getValue();
    } else if(theEvent.getController().getName() == "serialSel") {
      serialNumber = (int)theEvent.getController().getValue();

      println(serialNumber);

      inPort = new Serial(this, Serial.list()[serialNumber], baudRate);
      inPort.bufferUntil(lf);

      disableSerialPrompt();
      setupRunGUI();
      setupLabels();
      serialSelected = true;
    }

    //if(theEvent.getController().getName() == "serialSel") println("bla");

    //println(theEvent.getController().getName());
  }
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {

      println(electrodeSelector.getValue());

      if(electrodeSelector.getValue()>0){
        //electrodeSelector.setIndex((int)electrodeSelector.getValue()-1);
        electrodeSelector.setValue((int)electrodeSelector.getValue()-1);
      }
    } else if (keyCode == RIGHT) {

      println(electrodeSelector.getValue());

      if(electrodeSelector.getValue()<numElectrodes){
        //electrodeSelector.setIndex((int)electrodeSelector.getValue()+1);
        electrodeSelector.setValue((int)electrodeSelector.getValue()+1);
      }
    } 
  } else if (key == 'p' || key == 'P') {
    paused = !paused;
  }
}

GUIhelpers.pde

void customiseDL(DropdownList ddl) {
  // a convenience function to customize a DropdownList
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(20);
  ddl.setBarHeight(15);
  //ddl.captionLabel().set("dropdown");
  //ddl.captionLabel().style().marginTop = 3;
  //ddl.captionLabel().style().marginLeft = 3;
  ddl.getCaptionLabel().set("dropdown");
  //ddl.getCaptionLabel().style().marginTop = 3;
  //ddl.getCaptionLabel().style().marginLeft = 3;
  //ddl.valueLabel().style().marginTop = 3;
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255, 128));
  ddl.setWidth(200);
}

void setupLabels(){

  labels = new Textlabel[numFooterLabels + numVerticalDivisions + 1];
  String footerLabels[] = {"FILTERED DATA", "BASELINE DATA", "TOUCHED LEVEL", "RELEASED LEVEL", "TOUCH EVENT", "RELEASE EVENT"};
  int footerColours[] = {filteredColour, baselineColour, touchedColour, releasedColour, touchColour, releaseColour};

  for(int i=0; i<numVerticalDivisions+1; i++){
    labels[i] = cp5.addTextlabel(String.valueOf(tenBits-(i*tenBits/numVerticalDivisions)))
                    .setText(String.valueOf(tenBits-(i*tenBits/numVerticalDivisions)))
                    .setPosition(graphsLeft,  graphsTop+i*(graphsHeight/numVerticalDivisions)-10)
                    .setColorValue(textColour)
                    ; 
  } 

  for(int i=0; i<numFooterLabels; i++){
    labels[i+numVerticalDivisions+1] = cp5.addTextlabel(footerLabels[i])
                    .setText(footerLabels[i])
                    .setPosition(graphFooterLeft+200+100*i,  graphFooterTop)
                    .setColorValue(footerColours[i])
                    ; 
  } 

}

void setupRunGUI(){

  electrodeSelector = cp5.addDropdownList("electrodeSel").setPosition(graphsLeft+graphsWidth-300, 75);
  customiseDL(electrodeSelector);
  //electrodeSelector.captionLabel().set("electrode number");
  electrodeSelector.setCaptionLabel("electrode number");
  for (int i=0;i<numElectrodes;i++) {
    electrodeSelector.addItem("electrode "+i, i);
  }
  //electrodeSelector.setIndex(electrodeNumber);  
  electrodeSelector.setId(electrodeNumber);  

  pauseInstructions = cp5.addTextlabel("pauseInstructions")
                .setText("PRESS P TO PAUSE, PRESS IT AGAIN TO RESUME")
                .setPosition(graphsLeft+graphsWidth-300,40)
                .setColorValue(textColour)
                ;   
}

void setupSerialPrompt(){
  cp5 = new ControlP5(this);

  serialPrompt = cp5.addTextlabel("serialPromptLabel")
                  .setText("SELECT THE SERIAL PORT THAT YOUR BARE CONDUCTIVE TOUCH BOARD IS CONNECTED TO SO WE CAN BEGIN:")
                  .setPosition(100,  100)
                  .setColorValue(textColour)
                  ;   

  serialSelector = cp5.addDropdownList("serialSel").setPosition(100, 150);
  customiseDL(serialSelector);
  //serialSelector.captionLabel().set("serial port");
  serialSelector.setCaptionLabel("serial port"); //.set();
  for (int i=0;i<serialList.length;i++) {
    serialSelector.addItem(serialList[i], i);
  }

}

void disableSerialPrompt(){

  serialPrompt.setVisible(false);
  serialSelector.setVisible(false);

}
stefandz commented 9 years ago

What version of Processing and ControlP5 are you using? The existing code works fine for me with Processing 2.2.1 and ControlP5 2.0.4.

kasperkamperman commented 9 years ago

Sorry. Processing 2.2.1 and ControlP5 2.2.3 (however 2.2.5 was also broken, that's why I tried 2.2.3).

It seems a lot of functions have changed. I filled already an issue on ControlP5, because in the docs the old functions are still visible. In their examples not.

stefandz commented 9 years ago

For the time being, we're holding this as being compatible with ControlP5 2.0.4. As and when ControlP5 becomes compatible with Processing 3.0 we may make Datastream compatible with future versions. At this stage, can you work around by using ControlP5 2.0.4?

kasperkamperman commented 9 years ago

No problem. But I have used the newest version of ControlP5 without problems in Processing 3.0. Although I know they say it's not officially compatible.

Maybe you can add the version number in the introduction?

requires controlp5 (version 2.0.4) to be in your processing libraries folder:

stefandz commented 9 years ago

I shall add the version requirements to the comments block - and see if I can dedicate some time to getting it working with 3.0 if that looks promising already! Do you mind if I ask what you are using Datastream to do? We're always interested to see how our customers use our code and interested to hear if there are any feature requests users have.

kasperkamperman commented 9 years ago

Well was trying proximity with a MPR121 board and tried different libraries. The plotting function looked handy to visualise some data (because of the multiple colours). Although I'm not really in need of the user interface.

I case it might become handy I did some plotting code too. Feel free to use it: https://gist.github.com/kasperkamperman/52a1c253f5689f37991f

stefandz commented 9 years ago

Thanks - looks very interesting. Do you mind my asking what the end project is?

On 20 October 2015 at 15:34, kasperkamperman notifications@github.com wrote:

Well was trying proximity with a MPR121 board and tried different libraries. The plotting function looked handy to visualise some data (because of the multiple colours). Although I'm not really in need of the user interface.

I case it might become handy I did some plotting code too. Feel free to use it: https://gist.github.com/kasperkamperman/52a1c253f5689f37991f

— Reply to this email directly or view it on GitHub https://github.com/BareConductive/mpr121-grapher/issues/1#issuecomment-149569845 .

kasperkamperman commented 9 years ago

I working on a touch control for a light. But it's also some general research. I've a lot of students that are interested working with capacitive touch. So I can help them a bit when they get stuck.

stefandz commented 9 years ago

Ah, sounds cool. We've just released the Touch Board Workshop Guide (http://www.bareconductive.com/wp-content/uploads/2015/10/TouchBoard_Workshop_Manual.pdf) which might be a useful resource for you and your students. We're also just about to release a workshop pack of multiple Touch Boards at a discounted rate which might be useful if you decide to use the Touch Board in the future - details will be on the website when it launches.

On 20 October 2015 at 15:47, kasperkamperman notifications@github.com wrote:

I working on a touch control for a light. But it's also some general research. I've a lot of students that are interested working with capacitive touch. So I can help them a bit when they get stuck.

— Reply to this email directly or view it on GitHub https://github.com/BareConductive/mpr121-grapher/issues/1#issuecomment-149573103 .