Search code examples
pythonstringraspberry-pios.systempuredata

Python - pdsend datastream


First of all some context: Four MPR121 Breakout Boards (https://www.sparkfun.com/products/9695) connected via i2C to a Raspberry Pi 2. A python script reads the data from the four boards and sends it to pure data with pdsend.

At the moment I have managed to get all the data I need to print nicely on the terminal. However, I am not sure how to get the same in pure data as I am getting text messages only (something like "print: .join(map(str print: diff3))")

I am pretty sure I need to change the os.system line to accomodate for the variables but I can't find how to do this.

Thank you very much in advance.

def send2Pd (message=' '):
os.system("echo '" + message + "' | pdsend 3000");

while True:

diff1 = [cap1.baseline_data(i)-cap1.filtered_data(i) for i in range(12)]
print 'Diff1:', '\t'.join(map(str, diff1))
send2Pd ('.join(map(str, diff1));')

diff2 = [cap2.baseline_data(i)-cap2.filtered_data(i) for i in range(12)]
print 'Diff2:', '\t'.join(map(str, diff2))
send2Pd ('.join(map(str, diff2));')

diff3 = [cap3.baseline_data(i)-cap3.filtered_data(i) for i in range(12)]
send2Pd ('.join(map(str, diff3));')
print 'Diff3:', '\t'.join(map(str, diff3))

diff4 = [cap4.baseline_data(i)-cap4.filtered_data(i) for i in range(12)]
print 'Diff4:', '\t'.join(map(str, diff4))
send2Pd ('.join(map(str, diff4));')

time.sleep(0.1)

Solution

  • OK sorted.

    This code gives me one line for each cap in pure data.

    However, it does seem very resource hungry (the usage monitor marks around 50%) and that is without running the pd patch. Is there a simple way of making this more efficient?

    Thank you!

    import os
    import sys
    import time
    import captouch as MPR121
    
    # Create MPR121 instances
    cap1 = MPR121.MPR121()
    cap2 = MPR121.MPR121()
    cap3 = MPR121.MPR121()
    cap4 = MPR121.MPR121()
    
    # Initialize communication with all 4 MPR121s
    cap1.begin( 0x5a )
    cap2.begin( 0x5b )
    cap3.begin( 0x5d )
    cap4.begin( 0x5c )
    
    # Define send2Pd function
    def send2Pd (message=' '):
        os.system("echo '" + message + "' | pdsend 3000");
    
    # Start loop
    while True:
    
        # Filtered and baseline data are commented out
        # filtered = [cap1.filtered_data(i) for i in range(12)]
        # print 'Filt:', '\t'.join(map(str, filtered))
        # base = [cap1.baseline_data(i) for i in range(12)]
        # print 'Base:', '\t'.join(map(str, base))
    
        # Difference for all 4 boards are calculated, printed in terminal and sent to Pure Data
        diff1 = [cap1.baseline_data(i)-cap1.filtered_data(i) for i in range(12)]
        print 'Diff1:', '\t'.join(map(str, diff1))
        send2Pd ('diff1: ' +  '\t'.join(map(str, diff1)) + ';')
    
        diff2 = [cap2.baseline_data(i)-cap2.filtered_data(i) for i in range(12)]
        print 'Diff2:', '\t'.join(map(str, diff2))
        send2Pd ('diff2: ' +  '\t'.join(map(str, diff2)) + ';')
    
        diff3 = [cap3.baseline_data(i)-cap3.filtered_data(i) for i in range(12)]
        print 'Diff3:', '\t'.join(map(str, diff3))
        send2Pd ('diff3: ' +  '\t'.join(map(str, diff3)) + ';')
    
        diff4 = [cap4.baseline_data(i)-cap4.filtered_data(i) for i in range(12)]
        print 'Diff4:', '\t'.join(map(str, diff4))
        send2Pd ('diff4: ' +  '\t'.join(map(str, diff4)) + ';')
    
        # Short pause before repeating loop
        time.sleep(0.1)