简体   繁体   English

如何使用mcp3008中的数据显示和动态更新多个wxpython静态文本?

[英]how to display and dynamically update multiple wxpython static text with data from a mcp3008?

i have a python program that take data from a mcp3008 and a rain water sensor. 我有一个从mcp3008和雨水传感器获取数据的python程序。 i want to display it in a gui using wxpython. 我想使用wxpython在gui中显示它。 This is my sensor program: 这是我的传感器程序:

import spidev
from time import sleep
import os

spi = spidev.SpiDev()
spi.open(0,0)

def getAdc (channel):
    if ((channel>7)or(channel<0)):
        return -1

    r = spi.xfer2([1, (8+channel) << 4, 0])

    adcOut = ((r[1]&3) << 8) + r[2]
    percent = int(round(adcOut/10.24))
    volts = ((adcOut/1023) * 5)
    if adcOut >= 0 and adcOut <= 300:
            print "--------------------------------------------------------------"
            print ("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
            print ("Rain Condition : Heavy Rain")
            sleep(5)

    elif adcOut >= 0 and adcOut <= 500:
            print "--------------------------------------------------------------"
            print ("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
            print ("Rain Condition : Moderate Rain")
            sleep(5)

    elif adcOut >= 0 and adcOut <= 700:
            print "--------------------------------------------------------------"
            print ("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
            print ("Rain Condition : Light Rain")
            sleep(5)

    else :
            print "--------------------------------------------------------------"
            print ("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
            print ("Rain Condition : No Rain")
            sleep(5)
while True:
    getAdc(0)

And here is my wxpython program i create to display it. 这是我创建的wxpython程序来显示它。 Help me on how to combine the two program into one to display the data. 帮助我如何将两个程序合并为一个以显示数据。

import datetime

global current_time
current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y    %H:%M:%S')

try:
    import wx
except ImportError:
    raise ImportError, "The wxPython module is required to run this program."

class RainSensorApp_wx(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, size = (500, 300))
        self.SetBackgroundColour(wx.BLUE)
        self.parent = parent
        self.initialize()

    def initialize(self):
        sizer = wx.GridBagSizer()
        font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        self.SetFont(font)

        self.label = wx.StaticText(self, -1, label = u'Rain Sensor Level:   {0:4d}  Percentage:     {1:3}%  Voltage:    {2} V'.format(adcOut, percent, volts))
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label, (1,0), (1,2), wx.EXPAND)

        self.label = wx.StaticText(self, -1, label = u'Rain Condition:  {}'.format(rain_condition))
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label, (2,0), (1,3), wx.EXPAND)

        self.label = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
        self.label.SetBackgroundColour(wx.BLUE)
        self.label.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label, (3,0), (1,4), wx.EXPAND)

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self):
        wx.CallLater(1000, self.on_timer)

if __name__ == "__main__":
    app = wx.App()
    frame = RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
    app.Mainloop()
    getAdc(0)

after this i will add timer using CallLater to dynamically update multiple wxpython static text as i just learn it yesterday. 之后,我将使用CallLater添加计时器,以动态更新多个wxpython静态文本,因为我昨天才刚刚学习。 I appreciate for who will help me and read my post. 感谢您能帮助我并阅读我的帖子。

It's a bit of a hack, as I have to emulate the spidev bit but this should be enough to get you started. 这有点骇人听闻,因为我不得不模仿spidev,但这足以让您入门。
The code is documented where I think that it is important. 我认为很重要的地方记录了代码。

import datetime
#import spidev
from time import sleep
import os

#spi = spidev.SpiDev()
#spi.open(0,0)
#Setup some variables as I don't have spidev
global adcOut
adcOut = 200
percent = int(round(adcOut/10.24))
volts = ((adcOut/1023) * 5)
rain_condition="none"

global current_time
current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y    %H:%M:%S')

try:
    import wx
except ImportError:
    raise ImportError, "The wxPython module is required to run this program."

class RainSensorApp_wx(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, size = (500, 300))
        self.SetBackgroundColour(wx.BLUE)
        self.parent = parent
        self.initialize()

    def initialize(self):
        sizer = wx.GridBagSizer()
        font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        self.SetFont(font)
        self.label1 = wx.StaticText(self, -1, label = u'Rain Sensor Level:   {0:4d}  Percentage:     {1:3}%  Voltage:    {2} V'.format(adcOut, percent, volts))

        #Give the labels unique names

        self.label1.SetBackgroundColour(wx.BLUE)
        self.label1.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)

        self.label2 = wx.StaticText(self, -1, label = u'Rain Condition:  {}'.format(rain_condition))
        self.label2.SetBackgroundColour(wx.BLUE)
        self.label2.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)

        self.label3 = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
        self.label3.SetBackgroundColour(wx.BLUE)
        self.label3.SetForegroundColour(wx.WHITE)
        sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)

    #Create a timer and perform on_timer every 1000 milliseconds(change to 5000)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1000)

        self.SetSizer(sizer)
        self.Show(True)

    def on_timer(self,event):
        channel = 0
        if ((channel>7)or(channel<0)):
            return -1
    #Hack out spidev references and use globals to emulate something happening

#        r = spi.xfer2([1, (8+channel) << 4, 0])
#        adcOut = ((r[1]&3) << 8) + r[2]

        global adcOut
        adcOut = adcOut +1
        percent = int(round(adcOut/10.24))
        volts = ((adcOut/1023) * 5)
        if adcOut >= 0 and adcOut <= 300:

        # Update the screen output 

                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Heavy Rain")

        elif adcOut >= 0 and adcOut <= 500:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Moderate Rain")

        elif adcOut >= 0 and adcOut <= 700:
                self.label1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.label2.SetLabel("Rain Condition : Light Rain")

        else :
                self.lable1.SetLabel("ADC Output:     {0:4d}  Percentage: {1:3}%  Voltage : {2} V".format(adcOut,percent,volts))
                self.lable2.SetLabel("Rain Condition : No Rain")

if __name__ == "__main__":
    Rs = wx.App()
    RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
    Rs.MainLoop()

in the code, 在代码中

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(1000)

how this change to, if we have incomming data with unknown delay 如果我们有未知延迟的传入数据,这将如何变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改将mcp3008读取并显示降雨传感器数据的wxpython程序更改为软件spi? - How to change my wxpython program that read and display rain sensor data from mcp3008 to software spi? 如何动态更新多个wxpython静态文本? - how to dynamically update multiple wxpython static text? 我可以使用MCP3008输出吗? - could i use a MCP3008 to output? Python 使用 MCP3008 采样频率 - Python using MCP3008 sample frequency Python,RPi + MCP3008 + 2xForce敏感电阻-正方形 - Python, RPi + MCP3008 + 2xForce Sensitive Resistor - Square 使用 Python 在 Raspberry Pi 上以菊花链方式连接 MCP3008 SPI 的问题 - Issue with daisychaining MCP3008 SPI on Raspberry Pi with Python 有什么办法可以让我的 Mcp3008 采样统一? - Is there any way to make my Mcp3008 sampling uniform? 无法使用 python 在 rpi0 上设置多个 mcp3008 ADC 芯片。任何关于错误的想法都适用 - Trouble setting up multiple mcp3008 ADC chips on rpi0 using python. Any ideas as to what is wrong are appriciated 尝试通过SPI(MCP3304,MCP3204或MCP3008)在RPi 2 b +上读取带有Cython的ADC吗? - Trying to read an ADC with Cython on an RPi 2 b+ via SPI (MCP3304, MCP3204, or MCP3008)? 如何在wxPython静态文本小部件中显示插入符号? - How to display caret symbol in wxPython static text widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM