Python - pdsend datastream

时间:2015-03-30 13:48:48

标签: python string raspberry-pi os.system puredata

首先是一些背景: 四个MPR121分线板(https://www.sparkfun.com/products/9695)通过i2C连接到Raspberry Pi 2.一个python脚本从四个板上读取数据并通过pdsend将其发送到纯数据。

目前我已经设法获得了我需要在终端上打印得很好的所有数据。但是,我不确定如何在纯数据中获得相同的内容,因为我只收到短信(类似于“print:.join(map(str print:diff3))”)

我很确定我需要更改os.system行以适应变量,但我找不到如何执行此操作。

非常感谢你。

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)

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你只需要从传递给send2Pd的参数中删除引号,像send2Pd('\t'.join(map(str, diff1)))那样有效吗?

答案 1 :(得分:0)

确定已排序。

这段代码为纯数据中的每个上限提供了一行。

然而,它看起来确实非常耗费资源(使用率监视器标记在50%左右)并且没有运行pd补丁。是否有一种简单的方法可以提高效率?

谢谢!

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)