Python - pdsend 数据流
Python - pdsend datastream
首先是一些上下文:
四个 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)
如果我理解正确,你只需要从传递给 send2Pd 的参数中删除引号,像 send2Pd('\t'.join(map(str, diff1)))
这样的东西行得通吗?
确定排序。
此代码为纯数据中的每个上限提供一行。
但是,它似乎非常耗费资源(使用情况监视器标记为大约 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)
首先是一些上下文: 四个 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)
如果我理解正确,你只需要从传递给 send2Pd 的参数中删除引号,像 send2Pd('\t'.join(map(str, diff1)))
这样的东西行得通吗?
确定排序。
此代码为纯数据中的每个上限提供一行。
但是,它似乎非常耗费资源(使用情况监视器标记为大约 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)