1

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)
  • send2Pd ('diff1' + str(diff1) + ';') managed to get the data in pd but with each of the 12 numbers on different lines: print: diff1[-4 print: -4 print: -3 print: -1 print: -1 print: -1 print: -2 print: -2 print: -4 print: -1 print: -1 print: -2 – howlinthurston3 Mar 30 '15 at 16:57
  • not what you were asking, but why are you using `os.system('echo | pdsend')` when you just need to open a TCP/IP socket and communicate directly? it will be *way faster* and *portable* and less error prone. – umläute Mar 31 '15 at 07:39
  • Sorry, I am very new to this. It doesn't sound hard to do, where should I look or what should I google for? I guess it's worth mentioning both Python and PD are running on the same raspberry pi. Thanks a lot for your help. – howlinthurston3 Apr 02 '15 at 13:24
  • googling reveals https://wiki.python.org/moin/TcpCommunication which is a good start: consider `s.send(...)` as a replacement for `send2Pd(...)` – umläute Apr 02 '15 at 19:08

2 Answers2

0

If I understand correctly, you just need to remove the quotes from the arguments you pass to send2Pd, does something like send2Pd('\t'.join(map(str, diff1))) work?

Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • No, by doing that, PD receives blank messages, it just says 'EOF on socket 13' (it always says that after anything it receives). – howlinthurston3 Mar 30 '15 at 15:40
  • you get the `EOF` message, because you are opening (and *closing*) a new connection for each and every call to `send2Pd`; you really should handle the TCP/IP connection from python – umläute Mar 31 '15 at 10:10
0

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)