Hi and hmm
it seems that unpack does not suffice - but the filtering works out well (only bangs when touched)

alt text
- When I touch pin 0 of the MPR121 it prints out 0: bang
- When I touch pin 1 of the MPR121 it prints out 0: bang
- When I touch pin 5 of the MPR121 it prints out 0: bang

In the Arduino description I found they pack before unpacking - but that's another pd library which is not in Vanilla.

So close - and yet so far

[r sensorValue]
|
[print]
---> Did not give a list
alt text

    dywen

    > [r sensorValue]
    > |
    > [print]

    ---> Did not give a list

    what is your C++ code at this point?
    Are you sending this?

    	        libpd_start_message(8);
                    for(int n = 0; n < 8; ++n)
                        libpd_add_float(sensorValue[n]);
    		libpd_finish_list("sensorValue");

    IMPORTANT: are you sending something else?

      giuliomoro
      hey,

      yes, it's in:

      // start DSP:
      	// [; pd dsp 1(
      	libpd_start_message(8); //get data from all 8 MPR121 pins
      	libpd_add_float(1.0f);
      	for(int n = 0; n < 8; ++n)
          	libpd_add_float(sensorValue[n]);
      	libpd_finish_list("sensorValue");
      	libpd_finish_message("pd", "dsp");
      

      --> if I'm sending anything else, good question.
      The render.cpp from luiszayas has a lot of extra stuff - midi etc - but I don't know what can be kept and what is too much.
      It's pasted here:
      https://framabin.org/p/?5d16d77f039d8958#QYF2tXlzMwCrGdw0525a0wGyhfVgd5f2zpvurT7Hr3E=

      In what function is it? By the looks of it, it is in setup(). It should be in render() instead.

      yep, it was in setup, not render.
      (I had a - bad - reason for that)

      Looking better!

      Now it needs serious debouncing - I think
      Touching once, briefly, gives 100 bangs (or more - who knows)

      ----> Too many messages have been printed to the console too quickly. Reduce your printing frequency

      so now you are sending one message every block(16 samples). You could add a [change] to your patch after the [unpack] to only let a message through when it is different from the previous one.

        Now the debouncer blocks too much
        alt text

        I used your debouncer for floats (and these are not floats, of course)
        alt text

        hmm..

        giuliomoro so now you are sending one message every block(16 samples). You could add a [change] to your patch after the [unpack] to only let a message through when it is different from the previous one.

        I don't think you need debouncing, I think you just want to limit the rate of the messages, which is achieved with a [change] object.

        ok, better - except that sensor 0 - co-bangs with all sensors
        (When I touch 1, 3 or 6 - 0 prints bang as well)
        alt text
        There must be another sensorValue disrupting the flow - I tried to make this render.cpp "cleaner" but I got tons of error messages (and bela stopping)

        If someone feels like taking out the midi stuff of this render.cpp and finding two renderings of sensorValue - I would appreciate that
        https://framabin.org/p/?5d16d77f039d8958#QYF2tXlzMwCrGdw0525a0wGyhfVgd5f2zpvurT7Hr3E=

        pfewww
        I'm an autodidact - thanks for your patience Guilormo - it was truly great - I have learnt a lot!

        hey,

        I have a friend who's a crack at C and he had a look at the code of the render.cpp I had
        Here's an outline what he proposed:
        in the original code, libpd start message is at the beginning of render:

        void render(BelaContext *context, void *userData)
        {
        	int num;
        libpd_start_message(8); //get data from all 8 MPR121 pins
        	for(int n = 0; n < 8; ++n)
            	libpd_add_float(sensorValue[n]);
            	
        libpd_finish_list("sensorValue");
        libpd_finish_message("pd", "dsp");
        

        His proposal:
        "Try to combine the 'float' and add_float from with the code in render, and put it at the VERY END of render() but inside render, remove the for loop from readMPR121."

            libpd_start_message(8); //get data from all 8 MPR121 pins
            for(int n = 0; n < 8; ++n){
                libpd_add_float(sensorValue[n]);
            }
            libpd_finish_list("sensorValue");
            libpd_finish_message("pd", "dsp");
        
           libpd_start_message(8); //get data from all 8 MPR121 pins
            for(int n = 0; n < 8; ++n){
                libpd_add_float((float)n);
            }
            libpd_finish_list("sensorIndex");
            libpd_finish_message("pd", "dsp");
        

        OLD read at the end of render.cpp:

        void readMPR121(void*)
        {
        	for(int i = 0; i < NUM_TOUCH_PINS; i++) {
        		sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
        		sensorValue[i] -= threshold;
        		if(sensorValue[i] < 0)
        			sensorValue[i] = 0;
        		libpd_float("sensorIndex", (float)i);
        		//rt_printf("%d ", sensorIndex[i]); //error: use of undeclared identifier 'sensorIndex'
        		libpd_float("sensorValue", (float)sensorValue[i]);
        #ifdef DEBUG_MPR121
        		rt_printf("%d ", sensorValue[i]); //prints the values of the sensor when DEBUG_MPR121 is define at beginning of programme
        #endif
        	}
        #ifdef DEBUG_MPR121
        	rt_printf("\n");
        #endif
        	
        // You can use this to read binary on/off touch state more easily
        //rt_printf("Touched: %x\n", mpr121.touched());
        

        -----> NEW read:

        void readMPR121(void*){
            for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
                sensorValue[i] -= threshold;
                if(sensorValue[i] < 0)
                    sensorValue[i] = 0;
        #ifdef DEBUG_MPR121
                rt_printf("%d ", sensorValue[i]); //prints the values of the sensor when DEBUG_MPR121 is define at beginning of programme
        #endif
            }
        #ifdef DEBUG_MPR121
            rt_printf("\n");
        #endif
           
        // You can use this to read binary on/off touch state more easily
        //rt_printf("Touched: %x\n", mpr121.touched());
        }
        

        It WORKS!
        alt text
        Also with the little piece of handwoven e-textile (80% polyester, 20%stainless steel)

        alt text
        (image is green because of curtains - heatwave)

        Latest render.cpp is here - it desperately needs a clean-up.
        https://framabin.org/p/?79ecd118364dce6a#PgFmcFomCItZkIJRkx5azj8hnZuxZs4f76OEgMIOefk=
        (you need javascript to decrypt)

        End of August - beginning of September I will write about this in full detail and post the project.

          dywen remove the for loop from readMPR121."

          hmm:

          giuliomoro proper way is to move the message sending to the render() function.

          I guess that "move" here was the key word, also

          giuliomoro IMPORTANT: are you sending something else?

          I meant exactly: are you still sending to it from the other thread (that is, readMPR121()).

          dywen "Try to combine the 'float' and add_float from with the code in render, a

          that would not work (libpd_float() and libpd_add_float() cannot be combined), and in fact it is not what you have done.

          dywen

          >    libpd_start_message(8); //get data from all 8 MPR121 pins
          >     for(int n = 0; n < 8; ++n){
          >         libpd_add_float(sensorValue[n]);
          >     }
          >     libpd_finish_list("sensorValue");
          >     libpd_finish_message("pd", "dsp");
          >    libpd_start_message(8); //get data from all 8 MPR121 pins
          >     for(int n = 0; n < 8; ++n){
          >         libpd_add_float((float)n);
          >     }
          >     libpd_finish_list("sensorIndex");
          >     libpd_finish_message("pd", "dsp");

          You don't really need to send the sensorIndex at this point, you can just assume that the elements of the list are in order. So you don't need the second message.

          libpd_finish_message("pd", "dsp");

          This line does nothing and should be removed.

          So, after removing this line and the useless "sensorIndex" message, you end up with exactly this:

          giuliomoro libpd_start_message(8);
          for(int n = 0; n < 8; ++n)
          libpd_add_float(sensorValue[n]);
          libpd_finish_list("sensorValue");

          which I sent you a few days ago.

          dywen d put it at the VERY END of render()

          This would work, but it adds one block of latency with respect to having it at the beginning of render().

            giuliomoro dywen remove the for loop from readMPR121."

            hmm:

            giuliomoro proper way is to move the message sending to the render() function.

            I guess that "move" here was the key word, also

            Yes, indeed
            (I removed the old code and put in the new one ;-)

            giuliomoro IMPORTANT: are you sending something else?

            I meant exactly: are you still sending to it from the other thread (that is, readMPR121()).

            Honestly, I don't know
            With the code on Midi - this render.cpp is too hard to read for me (I'm a linguist/artist - who did a quarter of a moog on coding - time money and parenthood limiting me at this moment from doing more)

            dywen "Try to combine the 'float' and add_float from with the code in render, a

            that would not work (libpd_float() and libpd_add_float() cannot be combined), and in fact it is not what you have done.

            dywen

            libpd_start_message(8); //get data from all 8 MPR121 pins
            for(int n = 0; n < 8; ++n){
            libpd_add_float(sensorValue[n]);
            }
            libpd_finish_list("sensorValue");
            libpd_finish_message("pd", "dsp");
            libpd_start_message(8); //get data from all 8 MPR121 pins
            for(int n = 0; n < 8; ++n){
            libpd_add_float((float)n);
            }
            libpd_finish_list("sensorIndex");
            libpd_finish_message("pd", "dsp");if("undefined"!==typeof hljs)hljs.ha();else if("undefined"===typeof hljsLoading){hljsLoading=1;var a=document.getElementsByTagName("head")[0],e=document.createElement("link");e.type="text/css";e.rel="stylesheet";e.href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/styles/default.min.css";a.appendChild(e);e=document.createElement("script");e.type="text/javascript";e.onload=function(){var d={},f=0;hljs.hb=function(b){b.removeAttribute("data-hljs");var c=b.innerHTML;c in d?b.innerHTML=d[c]🙁7<++f&&(d={},f=0),hljs.highlightBlock(b.firstChild),d[c]=b.innerHTML)};hljs.ha=function(){for(var b=document.querySelectorAll("pre[data-hljs]"),c=b.length;0<c😉hljs.hb(b.item(--c))};hljs._ha()};e.async=!0;e.src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/highlight.min.js";a.appendChild(e)}

            You don't really need to send the sensorIndex at this point, you can just assume that the elements of the list are in order. So you don't need the second message.

            libpd_finish_message("pd", "dsp");if("undefined"!==typeof hljs)hljs.ha();else if("undefined"===typeof hljsLoading){hljsLoading=1;var a=document.getElementsByTagName("head")[0],e=document.createElement("link");e.type="text/css";e.rel="stylesheet";e.href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/styles/default.min.css";a.appendChild(e);e=document.createElement("script");e.type="text/javascript";e.onload=function(){var d={},f=0;hljs.hb=function(b){b.removeAttribute("data-hljs");var c=b.innerHTML;c in d?b.innerHTML=d[c]🙁7<++f&&(d={},f=0),hljs.highlightBlock(b.firstChild),d[c]=b.innerHTML)};hljs.ha=function(){for(var b=document.querySelectorAll("pre[data-hljs]"),c=b.length;0<c😉hljs.hb(b.item(--c))};hljs._ha()};e.async=!0;e.src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/highlight.min.js";a.appendChild(e)}
            This line does nothing and should be removed.

            So, after removing this line and the useless "sensorIndex" message, you end up with exactly this:

            giuliomoro libpd_start_message(8);
            for(int n = 0; n < 8; ++n)
            libpd_add_float(sensorValue[n]);
            libpd_finish_list("sensorValue");

            which I sent you a few days ago.

            True, but I did not know what to remove :-/
            So I was conservative on deletion. I am over my head on this job (I never estimated it would become so complex to be able to have several audio files playing simultaneously)

            dywen d put it at the VERY END of render()

            This would work, but it adds one block of latency with respect to having it at the beginning of render().

            Ok! nose back to the grind stone!
            By the way I really like the responsiveness & console feedback of the Bela Ide whilst editing render.cpp

            giuliomoro I meant exactly: are you still sending to it from the other thread (that is, readMPR121()).
            There are three instances for readMPR121() - I see no doubles
            For documentation::

            -- line 77 says --internal stuff -- do not change

             
            // ---- internal stuff -- do not change -----
            
            I2C_MPR121 mpr121;			// Object to handle MPR121 sensing
            AuxiliaryTask i2cTask;		// Auxiliary task to read I2C
            
            int readCount = 0;			// How long until we read again...
            int readIntervalSamples = 0; // How many samples between reads
            
            void readMPR121(void*);
            
            /*********/;

            -- line 389 - To have the data via i2c

            	i2cTask = Bela_createAuxiliaryTask(readMPR121, 50, "bela-mpr121");

            -- line 776 - to connect NUM_TOUCH_PINS to sensorValue - so Pure Data can receive these values

            void readMPR121(void*)
            {
                for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                    sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
                    sensorValue[i] -= threshold;
                    if(sensorValue[i] < 0)
                        sensorValue[i] = 0;

            -- Full render.cpp as it is now

            /*
             ____  _____ _        _    
            | __ )| ____| |      / \   
            |  _ \|  _| | |     / _ \  
            | |_) | |___| |___ / ___ \ 
            |____/|_____|_____/_/   \_\
            
            The platform for ultra-low latency audio and sensor processing
            
            http://bela.io
            
            A project of the Augmented Instruments Laboratory within the
            Centre for Digital Music at Queen Mary University of London.
            http://www.eecs.qmul.ac.uk/~andrewm
            
            (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
            	Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
            	Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
            
            The Bela software is distributed under the GNU Lesser General Public License
            (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
            */
            
            /*
             *	USING A CUSTOM RENDER.CPP FILE FOR PUREDATA PATCHES - LIBPD
             *  ===========================================================
             *  ||                                                       ||
             *  || OPEN THE ENCLOSED _main.pd PATCH FOR MORE INFORMATION ||
             *  || ----------------------------------------------------- ||
             *  ===========================================================
             */
            
            #include <Bela.h>
            #include <DigitalChannelManager.h>
            #include <cmath>
            #include <stdio.h>
            #include <libpd/z_libpd.h>
            extern "C" {
            #include <libpd/s_stuff.h>
            };
            #include <UdpServer.h>
            #include <Midi.h>
            #include <Scope.h>
            #include <string>
            #include <sstream>
            #include "I2C_MPR121.h"
            
            
            void Bela_userSettings(BelaInitSettings *settings)
            {
            	settings->uniformSampleRate = 1;
            	settings->interleave = 0;
            	settings->analogOutputsPersist = 0;
            }
            
            /*
             *  MODIFICATION
             *  ------------
             *  MPR121 Stuff
             */
            
            // How many pins there are
            #define NUM_TOUCH_PINS 12
            
            // Define this to print data to terminal - function prints data from sensor
            #define DEBUG_MPR121
            
            // Change this to change how often the MPR121 is read (in Hz)
            int readInterval = 50;
            
            // Change this threshold to set the minimum amount of touch
            int threshold = 40;
            
            // This array holds the continuous sensor values
            int sensorValue[NUM_TOUCH_PINS];
            
            // ---- internal stuff -- do not change -----
            
            I2C_MPR121 mpr121;			// Object to handle MPR121 sensing
            AuxiliaryTask i2cTask;		// Auxiliary task to read I2C
            
            int readCount = 0;			// How long until we read again...
            int readIntervalSamples = 0; // How many samples between reads
            
            void readMPR121(void*);
            
            /*********/
            
            
            float* gInBuf;
            float* gOutBuf;
            #define PARSE_MIDI
            static std::vector<Midi*> midi;
            std::vector<std::string> gMidiPortNames;
            
            void dumpMidi()
            {
            	if(midi.size() == 0)
            	{
            		printf("No MIDI device enabled\n");
            		return;
            	}
            	printf("The following MIDI devices are enabled:\n");
            	printf("%4s%20s %3s %3s %s\n",
            			"Num",
            			"Name",
            			"In",
            			"Out",
            			"Pd channels"
            	      );
            	for(unsigned int n = 0; n < midi.size(); ++n)
            	{
            		printf("[%2d]%20s %3s %3s (%d-%d)\n", 
            			n,
            			gMidiPortNames[n].c_str(),
            			midi[n]->isInputEnabled() ? "x" : "_",
            			midi[n]->isOutputEnabled() ? "x" : "_",
            			n * 16 + 1,
            			n * 16 + 16
            		);
            	}
            }
            
            Midi* openMidiDevice(std::string name, bool verboseSuccess = false, bool verboseError = false)
            {
            	Midi* newMidi;
            	newMidi = new Midi();
            	newMidi->readFrom(name.c_str());
            	newMidi->writeTo(name.c_str());
            #ifdef PARSE_MIDI
            	newMidi->enableParser(true);
            #else
            	newMidi->enableParser(false);
            #endif /* PARSE_MIDI */
            	if(newMidi->isOutputEnabled())
            	{
            		if(verboseSuccess)
            			printf("Opened MIDI device %s as output\n", name.c_str());
            	}
            	if(newMidi->isInputEnabled())
            	{
            		if(verboseSuccess)
            			printf("Opened MIDI device %s as input\n", name.c_str());
            	}
            	if(!newMidi->isInputEnabled() && !newMidi->isOutputEnabled())
            	{
            		if(verboseError)
            			fprintf(stderr, "Failed to open  MIDI device %s\n", name.c_str());
            		return nullptr;
            	} else {
            		return newMidi;
            	}
            }
            
            static unsigned int getPortChannel(int* channel){
            	unsigned int port = 0;
            	while(*channel > 16){
            		*channel -= 16;
            		port += 1;
            	}
            	if(port >= midi.size()){
            		// if the port number exceeds the number of ports available, send out
            		// of the first port 
            		rt_fprintf(stderr, "Port out of range, using port 0 instead\n");
            		port = 0;
            	}
            	return port;
            }
            
            void Bela_MidiOutNoteOn(int channel, int pitch, int velocity) {
            	int port = getPortChannel(&channel);
            	rt_printf("noteout _ port: %d, channel: %d, pitch: %d, velocity %d\n", port, channel, pitch, velocity);
            	midi[port]->writeNoteOn(channel, pitch, velocity);
            }
            
            void Bela_MidiOutControlChange(int channel, int controller, int value) {
            	int port = getPortChannel(&channel);
            	rt_printf("ctlout _ port: %d, channel: %d, controller: %d, value: %d\n", port, channel, controller, value);
            	midi[port]->writeControlChange(channel, controller, value);
            }
            
            void Bela_MidiOutProgramChange(int channel, int program) {
            	int port = getPortChannel(&channel);
            	rt_printf("pgmout _ port: %d, channel: %d, program: %d\n", port, channel, program);
            	midi[port]->writeProgramChange(channel, program);
            }
            
            void Bela_MidiOutPitchBend(int channel, int value) {
            	int port = getPortChannel(&channel);
            	rt_printf("bendout _ port: %d, channel: %d, value: %d\n", port, channel, value);
            	midi[port]->writePitchBend(channel, value);
            }
            
            void Bela_MidiOutAftertouch(int channel, int pressure){
            	int port = getPortChannel(&channel);
            	rt_printf("touchout _ port: %d, channel: %d, pressure: %d\n", port, channel, pressure);
            	midi[port]->writeChannelPressure(channel, pressure);
            }
            
            void Bela_MidiOutPolyAftertouch(int channel, int pitch, int pressure){
            	int port = getPortChannel(&channel);
            	rt_printf("polytouchout _ port: %d, channel: %d, pitch: %d, pressure: %d\n", port, channel, pitch, pressure);
            	midi[port]->writePolyphonicKeyPressure(channel, pitch, pressure);
            }
            
            void Bela_MidiOutByte(int port, int byte){
            	rt_printf("port: %d, byte: %d\n", port, byte);
            	if(port > (int)midi.size()){
            		// if the port is out of range, redirect to the first port.
            		rt_fprintf(stderr, "Port out of range, using port 0 instead\n");
            		port = 0;
            	}
            	midi[port]->writeOutput(byte);
            }
            
            void Bela_printHook(const char *received){
            	rt_printf("%s", received);
            }
            
            static DigitalChannelManager dcm;
            
            void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
            	libpd_float((char*)receiverName, (float)state);
            //	rt_printf("%s: %d\n", (char*)receiverName, state);
            }
            
            #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
            
            void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
            	if(strcmp(source, "bela_setMidi") == 0){
            		int num[3] = {0, 0, 0};
            		for(int n = 0; n < argc && n < 3; ++n)
            		{
            			if(!libpd_is_float(&argv[n]))
            			{
            				fprintf(stderr, "Wrong format for Bela_setMidi, expected:[hw 1 0 0(");
            				return;
            			}
            			num[n] = libpd_get_float(&argv[n]);
            		}
            		std::ostringstream deviceName;
            		deviceName << symbol << ":" << num[0] << "," << num[1] << "," << num[2];
            		printf("Adding Midi device: %s\n", deviceName.str().c_str());
            		Midi* newMidi = openMidiDevice(deviceName.str(), false, true);
            		if(newMidi)
            		{
            			midi.push_back(newMidi);
            			gMidiPortNames.push_back(deviceName.str());
            		}
            		dumpMidi();
            		return;
            	}
            	if(strcmp(source, "bela_setDigital") == 0){
            		// symbol is the direction, argv[0] is the channel, argv[1] (optional)
            		// is signal("sig" or "~") or message("message", default) rate
            		bool isMessageRate = true; // defaults to message rate
            		bool direction = 0; // initialize it just to avoid the compiler's warning
            		bool disable = false;
            		if(strcmp(symbol, "in") == 0){
            			direction = INPUT;
            		} else if(strcmp(symbol, "out") == 0){
            			direction = OUTPUT;
            		} else if(strcmp(symbol, "disable") == 0){
            			disable = true;
            		} else {
            			return;
            		}
            		if(argc == 0){
            			return;
            		} else if (libpd_is_float(&argv[0]) == false){
            			return;
            		}
            		int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
            		if(disable == true){
            			dcm.unmanage(channel);
            			return;
            		}
            		if(argc >= 2){
            			t_atom* a = &argv[1];
            			if(libpd_is_symbol(a)){
            				char *s = libpd_get_symbol(a);
            				if(strcmp(s, "~") == 0  || strncmp(s, "sig", 3) == 0){
            					isMessageRate = false;
            				}
            			}
            		}
            		dcm.manage(channel, direction, isMessageRate);
            		return;
            	}
            }
            
            void Bela_floatHook(const char *source, float value){
            	/*
            	 *  MODIFICATION
             	 *  ------------
            	 *  Empty
            	 */
            
            /*********/
            
            // let's make this as optimized as possible for built-in digital Out parsing
            // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
            static int prefixLength = 15; // strlen("bela_digitalOut")
            if(strncmp(source, "bela_digitalOut", prefixLength)==0){
            	if(source[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
            		if(source[prefixLength + 1] != 0){
            			// quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
            			int receiver = ((source[prefixLength] - 48) * 10);
            			receiver += (source[prefixLength+1] - 48);
            			unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number
            			if(channel < 16){ //16 is the hardcoded value for the number of digital channels
            				dcm.setValue(channel, value);
            			}
            		}
            	}
            }
            }
            
            char receiverNames[16][21]={
            	{"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
            	{"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
            	{"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
            	{"bela_digitalIn26"}
            };
            
            static unsigned int gAnalogChannelsInUse;
            static unsigned int gLibpdBlockSize;
            // 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
            static const unsigned int gChannelsInUse = 30;
            //static const unsigned int gFirstAudioChannel = 0;
            static const unsigned int gFirstAnalogInChannel = 2;
            static const unsigned int gFirstAnalogOutChannel = 2;
            static const unsigned int gFirstDigitalChannel = 10;
            static const unsigned int gFirstScopeChannel = 26;
            static char multiplexerArray[] = {"bela_multiplexer"};
            static int multiplexerArraySize = 0;
            static bool pdMultiplexerActive = false;
            
            #ifdef PD_THREADED_IO
            void fdLoop(void* arg){
            	t_pdinstance* pd_that = (t_pdinstance*)arg;
            	while(!gShouldStop){
            		sys_doio(pd_that);
            		usleep(3000);
            	}
            }
            #endif /* PD_THREADED_IO */
            
            Scope scope;
            unsigned int gScopeChannelsInUse = 4;
            float* gScopeOut;
            void* gPatch;
            bool gDigitalEnabled = 0;
            
            bool setup(BelaContext *context, void *userData)
            {
            	// Check Pd's version
            	int major, minor, bugfix;
            	sys_getversion(&major, &minor, &bugfix);
            	printf("Running Pd %d.%d-%d\n", major, minor, bugfix);
            	// We requested in Bela_userSettings() to have uniform sampling rate for audio
            	// and analog and non-interleaved buffers.
            	// So let's check this actually happened
            	if(context->analogSampleRate != context->audioSampleRate)
            	{
            		fprintf(stderr, "The sample rate of analog and audio must match. Try running with --uniform-sample-rate\n");
            		return false;
            	}
            	if(context->flags & BELA_FLAG_INTERLEAVED)
            	{
            		fprintf(stderr, "The audio and analog channels must be interleaved.\n");
            		return false;
            	}
            
            if(context->digitalFrames > 0 && context->digitalChannels > 0)
            	gDigitalEnabled = 1;
            
            /*
             *  MODIFICATION
             *  ------------
             *  MPR121 Stuff
             */
            
            if(!mpr121.begin(1, 0x5A)) {
            	rt_printf("Error initialising MPR121\n");
            	return false;
            }
            
            i2cTask = Bela_createAuxiliaryTask(readMPR121, 50, "bela-mpr121");
            readIntervalSamples = context->audioSampleRate / readInterval;
            
            
            /*********/
            
            // add here other devices you need 
            gMidiPortNames.push_back("hw:1,0,0");
            //gMidiPortNames.push_back("hw:0,0,0");
            //gMidiPortNames.push_back("hw:1,0,1");
            
            scope.setup(gScopeChannelsInUse, context->audioSampleRate);
            gScopeOut = new float[gScopeChannelsInUse];
            
            // Check first of all if the patch file exists. Will actually open it later.
            char file[] = "_main.pd";
            char folder[] = "./";
            unsigned int strSize = strlen(file) + strlen(folder) + 1;
            char* str = (char*)malloc(sizeof(char) * strSize);
            snprintf(str, strSize, "%s%s", folder, file);
            if(access(str, F_OK) == -1 ) {
            	printf("Error file %s/%s not found. The %s file should be your main patch.\n", folder, file, file);
            	return false;
            }
            free(str);
            if(context->analogInChannels > 8 || context->analogOutChannels > 8)
            {
            	fprintf(stderr, "This project can run with a maximum of 8 analog channels\n");
            	// change LIBPD_DIGITAL_OFFSET if you want more
            	return false;
            }
            // analog setup
            gAnalogChannelsInUse = context->analogInChannels;
            
            // digital setup
            if(gDigitalEnabled)
            {
            	dcm.setCallback(sendDigitalMessage);
            	if(context->digitalChannels > 0){
            		for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
            			dcm.setCallbackArgument(ch, receiverNames[ch]);
            		}
            	}
            }
            
            for(unsigned int n = 0; n < gMidiPortNames.size(); ++n)
            {
            }
            unsigned int n = 0;
            while(n < gMidiPortNames.size())
            {
            	Midi* newMidi = openMidiDevice(gMidiPortNames[n], false, false);
            	if(newMidi)
            	{
            		midi.push_back(newMidi);
            		++n;
            	} else {
            		gMidiPortNames.erase(gMidiPortNames.begin() + n);
            	}
            }
            dumpMidi();
            
            // check that we are not running with a blocksize smaller than gLibPdBlockSize
            gLibpdBlockSize = libpd_blocksize();
            if(context->audioFrames < gLibpdBlockSize){
            	fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
            	return false;
            }
            
            // set hooks before calling libpd_init
            libpd_set_printhook(Bela_printHook);
            libpd_set_floathook(Bela_floatHook);
            libpd_set_messagehook(Bela_messageHook);
            libpd_set_noteonhook(Bela_MidiOutNoteOn);
            libpd_set_controlchangehook(Bela_MidiOutControlChange);
            libpd_set_programchangehook(Bela_MidiOutProgramChange);
            libpd_set_pitchbendhook(Bela_MidiOutPitchBend);
            libpd_set_aftertouchhook(Bela_MidiOutAftertouch);
            libpd_set_polyaftertouchhook(Bela_MidiOutPolyAftertouch);
            libpd_set_midibytehook(Bela_MidiOutByte);
            
            //initialize libpd. This clears the search path
            libpd_init();
            //Add the current folder to the search path for externals
            libpd_add_to_search_path(".");
            libpd_add_to_search_path("../pd-externals");
            
            libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
            gInBuf = get_sys_soundin();
            gOutBuf = get_sys_soundout();
            
            
            // Bind your receivers here
            libpd_bind("bela_digitalOut11");
            libpd_bind("bela_digitalOut12");
            libpd_bind("bela_digitalOut13");
            libpd_bind("bela_digitalOut14");
            libpd_bind("bela_digitalOut15");
            libpd_bind("bela_digitalOut16");
            libpd_bind("bela_digitalOut17");
            libpd_bind("bela_digitalOut18");
            libpd_bind("bela_digitalOut19");
            libpd_bind("bela_digitalOut20");
            libpd_bind("bela_digitalOut21");
            libpd_bind("bela_digitalOut22");
            libpd_bind("bela_digitalOut23");
            libpd_bind("bela_digitalOut24");
            libpd_bind("bela_digitalOut25");
            libpd_bind("bela_digitalOut26");
            libpd_bind("bela_setDigital");
            libpd_bind("bela_setMidi");
            
            /*
             *  MODIFICATION
             *  ------------
             *  Empty
             */
            
            /*********/
            
            // open patch:
            gPatch = libpd_openfile(file, folder);
            if(gPatch == NULL){
            	printf("Error: file %s/%s is corrupted.\n", folder, file); 
            	return false;
            }
            
            // If the user wants to use the multiplexer capelet,
            // the patch will have to contain an array called "bela_multiplexer"
            // and a receiver [r bela_multiplexerChannels]
            if(context->multiplexerChannels > 0 && libpd_arraysize(multiplexerArray) >= 0){
            	pdMultiplexerActive = true;
            	multiplexerArraySize = context->multiplexerChannels * context->analogInChannels;
            	// [; bela_multiplexer ` multiplexerArraySize` resize(
            	libpd_start_message(1);
            	libpd_add_float(multiplexerArraySize);
            	libpd_finish_message(multiplexerArray, "resize");
            	// [; bela_multiplexerChannels `context->multiplexerChannels`(
            	libpd_float("bela_multiplexerChannels", context->multiplexerChannels);
            }
            
            // Tell Pd that we will manage the io loop,
            // and we do so in an Auxiliary Task
            #ifdef PD_THREADED_IO
            	sys_dontmanageio(1);
            	AuxiliaryTask fdTask;
            	fdTask = Bela_createAuxiliaryTask(fdLoop, 50, "libpd-fdTask", (void*)pd_this);
            	Bela_scheduleAuxiliaryTask(fdTask);
            #endif /* PD_THREADED_IO */
            
            return true;
            }
            
            void render(BelaContext *context, void *userData)
            {
            	int num;
            	
            libpd_start_message(8);
            	for(int n = 0; n < 8; ++n)
            libpd_add_float(sensorValue[n]);
            libpd_finish_list("sensorValue");
            
            #ifdef PARSE_MIDI
            	for(unsigned int port = 0; port < midi.size(); ++port){
            		while((num = midi[port]->getParser()->numAvailableMessages()) > 0){
            			static MidiChannelMessage message;
            			message = midi[port]->getParser()->getNextChannelMessage();
            			rt_printf("On port %d (%s): ", port, gMidiPortNames[port].c_str());
            			message.prettyPrint(); // use this to print beautified message (channel, data bytes)
            			switch(message.getType()){
            				case kmmNoteOn:
            				{
            					int noteNumber = message.getDataByte(0);
            					int velocity = message.getDataByte(1);
            					int channel = message.getChannel();
            					libpd_noteon(channel + port * 16, noteNumber, velocity);
            					break;
            				}
            				case kmmNoteOff:
            				{
            					/* PureData does not seem to handle noteoff messages as per the MIDI specs,
            					 * so that the noteoff velocity is ignored. Here we convert them to noteon
            					 * with a velocity of 0.
            					 */
            					int noteNumber = message.getDataByte(0);
            	//				int velocity = message.getDataByte(1); // would be ignored by Pd
            					int channel = message.getChannel();
            					libpd_noteon(channel + port * 16, noteNumber, 0);
            					break;
            				}
            				case kmmControlChange:
            				{
            					int channel = message.getChannel();
            					int controller = message.getDataByte(0);
            					int value = message.getDataByte(1);
            					libpd_controlchange(channel + port * 16, controller, value);
            					break;
            				}
            				case kmmProgramChange:
            				{
            					int channel = message.getChannel();
            					int program = message.getDataByte(0);
            					libpd_programchange(channel + port * 16, program);
            					break;
            				}
            				case kmmPolyphonicKeyPressure:
            				{
            					int channel = message.getChannel();
            					int pitch = message.getDataByte(0);
            					int value = message.getDataByte(1);
            					libpd_polyaftertouch(channel + port * 16, pitch, value);
            					break;
            				}
            				case kmmChannelPressure:
            				{
            					int channel = message.getChannel();
            					int value = message.getDataByte(0);
            					libpd_aftertouch(channel + port * 16, value);
            					break;
            				}
            				case kmmPitchBend:
            				{
            					int channel = message.getChannel();
            					int value =  ((message.getDataByte(1) << 7)| message.getDataByte(0)) - 8192;
            					libpd_pitchbend(channel + port * 16, value);
            					break;
            				}
            				case kmmSystem:
            				// currently Bela only handles sysrealtime, and it does so pretending it is a channel message with no data bytes, so we have to re-assemble the status byte
            				{
            					int channel = message.getChannel();
            					int status = message.getStatusByte();
            					int byte = channel | status;
            					libpd_sysrealtime(port, byte);
            					break;
            				}
            				case kmmNone:
            				case kmmAny:
            					break;
            			}
            		}
            	}
            #else
            	int input;
            	for(unsigned int port = 0; port < NUM_MIDI_PORTS; ++port){
            		while((input = midi[port].getInput()) >= 0){
            			libpd_midibyte(port, input);
            		}
            	}
            #endif /* PARSE_MIDI */
            	unsigned int numberOfPdBlocksToProcess = context->audioFrames / gLibpdBlockSize;
            
            // Remember: we have non-interleaved buffers and the same sampling rate for
            // analogs, audio and digitals
            for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick)
            {
            	//audio input
            	for(int n = 0; n < context->audioInChannels; ++n)
            	{
            		memcpy(
            			gInBuf + n * gLibpdBlockSize,
            			context->audioIn + tick * gLibpdBlockSize + n * context->audioFrames, 
            			sizeof(context->audioIn[0]) * gLibpdBlockSize
            		);
            	}
            
            	// analog input
            	for(int n = 0; n < context->analogInChannels; ++n)
            	{
            		memcpy(
            			gInBuf + gLibpdBlockSize * gFirstAnalogInChannel + n * gLibpdBlockSize,
            			context->analogIn + tick * gLibpdBlockSize + n * context->analogFrames, 
            			sizeof(context->analogIn[0]) * gLibpdBlockSize
            		);
            	}
            	// multiplexed analog input
            	if(pdMultiplexerActive)
            	{
            		// we do not disable regular analog inputs if muxer is active, because user may have bridged them on the board and
            		// they may be using half of them at a high sampling-rate
            		static int lastMuxerUpdate = 0;
            		if(++lastMuxerUpdate == multiplexerArraySize){
            			lastMuxerUpdate = 0;
            			libpd_write_array(multiplexerArray, 0, (float *const)context->multiplexerAnalogIn, multiplexerArraySize);
            		}
            	}
            
            	unsigned int digitalFrameBase = gLibpdBlockSize * tick;
            	unsigned int j;
            	unsigned int k;
            	float* p0;
            	float* p1;
            	// digital input
            	if(gDigitalEnabled)
            	{
            		// digital in at message-rate
            		dcm.processInput(&context->digital[digitalFrameBase], gLibpdBlockSize);
            
            		// digital in at signal-rate
            		for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
            			unsigned int digitalFrame = digitalFrameBase + j;
            			for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
            					k < 16; ++k, p1 += gLibpdBlockSize) {
            				if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
            					*p1 = digitalRead(context, digitalFrame, k);
            				}
            			}
            		}
            	}
            
            	libpd_process_sys(); // process the block
            
            	// digital outputs
            	if(gDigitalEnabled)
            	{
            		// digital out at signal-rate
            		for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
            			unsigned int digitalFrame = (digitalFrameBase + j);
            			for (k = 0, p1 = p0  + gLibpdBlockSize * gFirstDigitalChannel;
            				k < context->digitalChannels; k++, p1 += gLibpdBlockSize)
            			{
            				if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
            				digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
            				}
            			}
            		}
            
            		// digital out at message-rate
            		dcm.processOutput(&context->digital[digitalFrameBase], gLibpdBlockSize);
            	}
            
            	// scope output
            	for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
            		for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
            			gScopeOut[k] = *p1;
            		}
            		scope.log(gScopeOut[0], gScopeOut[1], gScopeOut[2], gScopeOut[3]);
            	}
            
            	/*
            	 *  MODIFICATION
             	 *  ------------
            	 *  MPR121 Stuff
            	 */
            
            	// Keep this code: it schedules the touch sensor readings
            	if(++readCount >= readIntervalSamples) {
            		readCount = 0;
            		Bela_scheduleAuxiliaryTask(i2cTask);
            	}
            
            		/*********/
            	// audio output
            	for(int n = 0; n < context->audioInChannels; ++n)
            	{
            		memcpy(
            			context->audioOut + tick * gLibpdBlockSize + n * context->audioFrames, 
            			gOutBuf + n * gLibpdBlockSize,
            			sizeof(context->audioOut[0]) * gLibpdBlockSize
            		);
            	}
            
            	//analog output
            	for(int n = 0; n < context->analogOutChannels; ++n)
            	{
            		memcpy(
            			context->analogOut + tick * gLibpdBlockSize + n * context->analogFrames, 
            			gOutBuf + gLibpdBlockSize * gFirstAnalogOutChannel + n * gLibpdBlockSize,
            			sizeof(context->analogOut[0]) * gLibpdBlockSize
            		);
            	}
            }
            }
            
            void cleanup(BelaContext *context, void *userData)
            {
            	for(auto a : midi)
            	{
            		delete a;
            	}
            	libpd_closefile(gPatch);
            	delete [] gScopeOut;
            }
            
            
            // Auxiliary task to read the I2C board
            
            void readMPR121(void*)
            {
                for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                    sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
                    sensorValue[i] -= threshold;
                    if(sensorValue[i] < 0)
                        sensorValue[i] = 0;
            #ifdef DEBUG_MPR121
                    rt_printf("%d ", sensorValue[i]); //prints the values of the sensor when DEBUG_MPR121 is define at beginning of programme
            #endif
                }
            #ifdef DEBUG_MPR121
                rt_printf("\n");
            #endif
               
            // You can use this to read binary on/off touch state more easily
            //rt_printf("Touched: %x\n", mpr121.touched());
            
            }
            
            
            2 months later

            Awesome! I've just seen this now, sorry, but thanks both for fixing stuff. I'll update my code when I get a chance to.

            8 months later

            Greetings. is anybody else having a serious lag on the responsiveness using this render.cpp? tried rearranging and loosing extra baggage, yet i has not helped at all. The capSense example is of course flawless. Is it possible to get the same responsiveness when generating sound in pd, using the mpr121 readings? Thanks.

              eccot are you using heavy compiled pd patches or standard pd files? the latter has some latency inherent since it is not compiled code.

                lokki are you using heavy compiled pd patches or standard pd files? the latter has some latency inherent since it is not compiled code.

                There is no inherent latency in Pd (I mean, yes, the minimum block size we set is 16 samples per block), but there should be intrinsic latency when reading these sensors. However, if the CPU usage of the audio thread (or of the Scope) is fairly high, then the sensor readings will come in less often, and this could be the cause of the latency you perceive.

                So, what is the CPU usage of the patch, as displayed by the IDE? If it's above 50%, try closing the Scope tab, if it's open, and see if that improves the situation.

                  giuliomoro There is no inherent latency in Pd

                  sorry my bad. i was under the impression that pd patches run with higher latency.