2016-05-03 10 views
1

ワイヤレスNRF24モジュールを介して接続されているラズベリーのKivyのArduinoからいくつかの値を 'プル'したいと思います。私はthisライブラリを使用していますpython wrapperKivy Python - 部分的なコールバック関数

純粋なPythonではコードはうまくいきましたが、今はKivyに統合したいと思います。そのため

Iは、zimmerwetter.py内部に二つの機能を作った:

(アプリケーションが開始されると実行されなければならない)、無線デバイスを設定し、無線オブジェクトを返すための一つ:

def radiosetup(): 
    radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ) 

    # doing setup stuff... 

    return radio 

及び他の機能いくつかの環境日時(温度、湿度など)を提供するArduinoにリクエストを送信します。提案されているよう

def getenviroment(self,radio): 

    millis = lambda: int(round(time.time() * 1000)) 
    # send command 
    send_payload = 'getdata' 
    # First, stop listening so we can talk. 

    radio.stopListening() 

    # Take the time, and send it. This will block until complete 
    print 'Now sending length ', len(send_payload), ' ... ', 
    radio.write(send_payload[:len(send_payload)]) 

    a = datetime.datetime.now() 

    # Now, continue listening 
    radio.startListening() 

    # Wait here until we get a response, or timeout 
    started_waiting_at = millis() 
    timeout = False 
    while (not radio.available()) and (not timeout): 
     if (millis() - started_waiting_at) > 1000: 
      timeout = True 

    # Describe the results 
    if timeout: 
     b = datetime.datetime.now() 
     #  print(b - a) 
     print 'failed, response timed out.' 
    else: 
     # Grab the response, compare, and send to debugging spew 
     length = radio.getDynamicPayloadSize() 
     receive_payload = [] 
     receive_payload = radio.read(length) 

     print 'got response size=', length 
     print struct.unpack("bbbbhbbbb", ''.join(chr(c) for c in receive_payload)) 
     b = datetime.datetime.now() 
     print(b - a) 
     return receive_payload 

getenviroment機能がkivyアプリからすべてのX秒後に呼び出されなければなりませんが、一部の機能がthe kivy clock module

from zimmerwetter import * 

class PyowmApp(App): 
    def build(self): 
     radio = radiosetup() 
     Clock.schedule_interval(partial(getenviroment,radio), 10) 

で使用されるエラーは次のとおりです。

File "/home/pi/pyscripts/pyowm/zimmerwetter.py", line 83, in getenviroment 
    radio.stopListening() 
AttributeError: 'float' object has no attribute 'stopListening' 

I何故floatオブジェクトが返されているのか疑問に思っていますが、ラジオオブジェクトをヘルプ(ラジオ)で印刷すると、class RF24(Boost.Python.instance)とstplistening()という関数が返されます。 ts。

答えて

2

Clock.schedule_intervalによって呼び出される関数は、partialを通過した後に、引数としてdtを受け取ります。あなたの関数の署名はgetenviroment(self,radio)なので、radioselfに割り当てられ、dtradioに割り当てられます。代わりに

lambdaを使用します。

Clock.schedule_once(lambda dt: self.getenviroment(radio), 10) 
+0

文を 'Clock.schedule_once(lambda dt:getenviroment(radio)、10)'に変更し、functonからself引数を取り除いても機能しています(関数istはApp) – Triscus

+0

@Triscus 'getenviroment'がどこに定義されているかという質問はないので、私は' PyownApp'と仮定しました。 'self'を' getenvironment'を持つクラスの正しいインスタンスに置き換えます。あるいは、パラメータ 'self'を変更することを検討してください。 – zeeMonkeez

+0

関数はクラス内では定義されておらず、 'def'ステートメントでのみ定義されていました。私はOOPに慣れるべきだと思う、あなたのインスピレーションのためにありがとう – Triscus

0

私は

Clock.schedule_interval(partial(getenviroment,radio=radio), 10) 

にスケジュール文を変更してトリックをした、自分でそれを見つけました。

+0

これは動作しますが、それは非常に明確ではありません:何ここで起こることは、 'schedule_interval'(' dt')によって渡されて 'partial'位置引数が割り当てられてしまうことです'getenvironment'(' self')の最初の引数です。 'self'は後で使用されないので、問題はありません。そして、 'radio'は名前で期待値を割り当てられます。したがって、 'self'が実際に何らかの意味を持つ場合、このアプローチはうまくいかず、予期しない結果やエラーが発生します。ちょうど私がそれを指摘したと思った... – zeeMonkeez

関連する問題