dzhu / myo-raw

MIT License
218 stars 115 forks source link

Prevent Timeout #8

Open garphi opened 9 years ago

garphi commented 9 years ago

Hi, thank you for this great lib. It works great, but if you leave the myo alone, it will automatically disconnect and never will come back again. Myo Firmware Version: 1.1.755.2 Therefore I decided to modify your lib.

In the myo_raw.py I've replaced in function Main (line 463):

        if m.run(1) is None:
          print("Connection lost, try to reconnect")
          m.connect()
        else:

...and changed function run (line 194) to:

def run(self, timeout=None):
    return self.bt.recv_packet(timeout) 

In the classify_myo.py I've replaced in function Main (line 48):

ltime = time.time() #Set initial lasttime for further timeout
timeout = 1 #Timeout in seconds

try:
    while True:
      if time.time() > (ltime + timeout):
        print("Connection lost, try to reconnect")
        m.connect()
        ltime = time.time() #Set lasttime for further timeout
      else:
        ltime = time.time() #Set lasttime for further timeout
        m.run()

The same for the function main in myo.py (line 113)

ltime = time.time() #Set initial lasttime for further timeout
timeout = 1 #Timeout in seconds

while True:
  if time.time() > (ltime + timeout):
    print("Connection lost, try to reconnect")
    m.connect()
    ltime = time.time() #Set lasttime for further timeout
  else:
    ltime = time.time() #Set lasttime for further timeout
    m.run()

With this modifications myo will reconnect after a few seconds. Don't know how to uploads Commits.

Best regards garphi