Coder Social home page Coder Social logo

mpr121-grapher's Introduction

Bare Conductive

Bare Conductive MPR121 grapher

Processing sketch that graphs capacitive electrode data from the Bare Conductive Touch Board or Bare Conductive Pi Cap over time.

Requirements

Install

  1. Close the Processing IDE if you have it open.

  2. Download the .zip or clone the repository to your local machine - if downloading the .zip, extract the contents somewhere that suits you.

  3. Take the mpr121_grapher folder and move it to Processing Sketchbook Folder. This will be different for each operating system:

    Windows

    Libraries\Documents\Processing

    or

    My Documents\Processing

    Mac

    Documents/Processing

    Linux (Ubuntu)

    Home/Processing

    If this folder does not exist, create it first.

  4. Reopen the Processing IDE - you should now be able to open the sketch in the File -> Sketchbook menu.

Note

The new functions in this version of the Grapher use EEPROM to save and restore thresholds automatically when setting them. However, if you're using a SAMD21 or SAMD51 board (like the Arduino Zero or Adafruit Metro M4), these MCUs don't have onboard EEPROM. As a result, these functions will not work.

mpr121-grapher's People

Contributors

loosepascal avatar peter-krige avatar stefandz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mpr121-grapher's Issues

ControlP5 parts broken

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);

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.