在 Kivy 标签中显示来自 python 程序的心跳传感器读数

我正在尝试在我的 Kivy 应用程序中显示从我的心跳传感器获得的值。我已经尝试使用应用于其他传感器的数据提取方法,但它不适用于该传感器,因为它不包含任何功能。


我尝试了多种不同的方法,但它们都没有返回所需的输出。有人可以看看传感器代码并建议一些方法在我的 kivy 应用程序中显示输出。


Heartbeatsensot.py



import time

# Import the ADS1x15 module.

import Adafruit_ADS1x15



if __name__ == '__main__':


    adc = Adafruit_ADS1x15.ADS1015()

    # initialization 

    GAIN = 2/3  

    curState = 0

    thresh = 525  # mid point in the waveform

    P = 512

    T = 512

    stateChanged = 0

    sampleCounter = 0

    lastBeatTime = 0

    firstBeat = True

    secondBeat = False

    Pulse = False

    IBI = 600

    rate = [0]*10

    amp = 100


    lastTime = int(time.time()*1000)


    # Main loop. use Ctrl-c to stop the code

    while True:

        # read from the ADC

        Signal = adc.read_adc(0, gain=GAIN)   #TODO: Select the correct ADC channel. I have selected A0 here

        curTime = int(time.time()*1000)


        sampleCounter += curTime - lastTime;      #                   # keep track of the time in mS with this variable

        lastTime = curTime

        N = sampleCounter - lastBeatTime;     #  # monitor the time since the last beat to avoid noise

        #print N, Signal, curTime, sampleCounter, lastBeatTime


        ##  find the peak and trough of the pulse wave

        if Signal < thresh and N > (IBI/5.0)*3.0 :  #       # avoid dichrotic noise by waiting 3/5 of last IBI

            if Signal < T :                        # T is the trough

              T = Signal;                         # keep track of lowest point in pulse wave 


        if Signal > thresh and  Signal > P:           # thresh condition helps avoid noise

            P = Signal;                             # P is the peak

                                                # keep track of highest point in pulse wave



** 我正在尝试将读数添加到的 kivy 应用程序**


梵蒂冈之花
浏览 215回答 1
1回答

米琪卡哇伊

我认为你可以通过一些小的修改来完成你想要的。首先添加一个StringProperty到你的ScreenThermo类,并在on_enter()方法中启动一个线程来运行你的Heartbeatsensot代码:from Heartbeatsensot import hearbeatsensotclass ScreenThermo(Screen):&nbsp; &nbsp; BPM_string = StringProperty('BPM: Not Detected')&nbsp; &nbsp; def on_enter(self, *args):&nbsp; &nbsp; &nbsp; &nbsp; Thread(target=hearbeatsensot, args=(self,)).start()在您的kv文件中,添加对 new 的引用StringProperty:<ScreenThermo>:&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; text: " Pulse rate"&nbsp; &nbsp; &nbsp; &nbsp; font_size: 50&nbsp; &nbsp; &nbsp; &nbsp; pos: (35, 100)&nbsp; &nbsp; Label:&nbsp; &nbsp; &nbsp; &nbsp; id: TempLabel&nbsp; &nbsp; &nbsp; &nbsp; text: root.BPM_string&nbsp; # references new StringProperty&nbsp; &nbsp; &nbsp; &nbsp; font_size: 60&nbsp; &nbsp; &nbsp; &nbsp; halign: 'center'&nbsp; &nbsp; &nbsp; &nbsp; valign: 'middle'现在您只需要将要显示的任何内容TempLabel放入BPM_string属性中即可。为此,请更改Heartbeatsensot.py以定义可在Thread. if __name__ == '__main__':只需将该文件中的替换为def hearbeatsensot(screenThermo):如下所示:import time# Import the ADS1x15 module.import Adafruit_ADS1x15def hearbeatsensot(screenThermo):&nbsp; &nbsp; adc = Adafruit_ADS1x15.ADS1015()&nbsp; &nbsp; # initialization&nbsp; &nbsp; GAIN = 2/3&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; screenThermo.BPM_string = 'BPM: 65'&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .然后,在该方法中,只需使用 somethink like screenThermo.BPM_string = 'BPM: 65'(或任何您想要设置的内容)。BPM_string文件中的引用kv将自动设置绑定以更新TempLabel每当BPM_string被修改时。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python