User Tools

Site Tools


bridgeclient.py

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bridgeclient.py [2014/05/27 19:27] (current)
zoza created
Line 1: Line 1:
 +====== BridgeClient module to interface Python to Arduino ======
  
 +found at /​usr/​lib/​python.27/​bridge
 +
 +<code python>
 +from tcp import TCPJSONClient ​                                                  
 +from time import sleep                                                          ​
 +                                                                                ​
 +class BridgeClient: ​                                                            
 +  def wait_response(self,​ json, timeout): ​                                      
 +    while timeout>​=0: ​                                                          
 +      r = json.recv() ​                                                          
 +      if not r is None:                                                         
 +        return r                                                                ​
 +      timeout -= 0.1                                                            ​
 +      sleep(0.1) ​                                                               ​
 +    return None                                                                 
 +                                                                                ​
 +  def wait_key(self,​ key, json, timeout): ​                                      
 +    while True:                                                                 
 +      r = self.wait_response(json,​ timeout) ​                                    
 +      if r is None:                                                             
 +        return None                                                             
 +      try:                                                                      ​
 +        if r['​key'​] == key:                                                     
 +          return r['​value'​] ​                                                    
 +      except: ​                                                                  
 +        pass                                                                    ​
 +                                                                                ​
 +  def get(self, key):                                                           
 +    json = TCPJSONClient('​127.0.0.1',​ 5700)                                     
 +    json.send({'​command':'​get',​ '​key':​key}) ​                                    
 +    r = self.wait_key(key,​ json, 10)                                            ​
 +    json.close() ​                                                               ​
 +    return r                                                                    ​
 +                                                                                ​
 +  def put(self, key, value): ​                                                   ​
 +    json = TCPJSONClient('​127.0.0.1',​ 5700)                                     
 +    json.send({'​command':'​put',​ '​key':​key,​ '​value':​value}) ​                     ​
 +    r = self.wait_key(key,​ json, 10)                                            ​
 +    json.close() ​                                                               ​
 +    return r 
 +</​code>​
bridgeclient.py.txt ยท Last modified: 2014/05/27 19:27 by zoza