todbot / blink1-python

Official Python library for blink(1) USB RGB LED notification device
MIT License
34 stars 7 forks source link

Cannot initialize Blink1() twice in the same process #3

Open vejuhust opened 6 years ago

vejuhust commented 6 years ago

This issue is easy to reproduce --- with a little change to the demo usage like this:

import time
from blink1.blink1 import blink1

with blink1() as b1:
    b1.fade_to_color(100, 'navy')
    time.sleep(3)

with blink1() as b1:
    b1.fade_to_color(100, 'pink')
    time.sleep(3)

Following error will be thrown while executing the 2nd with:

Traceback (most recent call last):
  File "bug_blink2.py", line 9, in <module>
    b1.fade_to_color(100, 'pink')
  File "/Users/Vej/.virtualenvs/nb/lib/python3.5/site-packages/blink1/blink1.py", line 179, in fade_to_color
    return self.fade_to_rgb(fade_milliseconds, red, green, blue)
  File "/Users/Vej/.virtualenvs/nb/lib/python3.5/site-packages/blink1/blink1.py", line 152, in fade_to_rgb
    return self.fade_to_rgb_uncorrected(fade_milliseconds, r, g, b, led_number)
  File "/Users/Vej/.virtualenvs/nb/lib/python3.5/site-packages/blink1/blink1.py", line 148, in fade_to_rgb_uncorrected
    self.write( buf )
  File "/Users/Vej/.virtualenvs/nb/lib/python3.5/site-packages/blink1/blink1.py", line 127, in write
    self.dev.send_feature_report(buf)
AttributeError: 'NoneType' object has no attribute 'send_feature_report'

I also tried two various versions, both got the same error on the 2nd call:

Version1:

def light_on(color = 'navy'):
    with blink1() as b1:
        print("dev", b1)
        b1.fade_to_color(100, color)
        sleep(3)

light_on('red')
light_on('yellow')
light_on('blue')

Version2:

def light_on(color = 'navy'):
    b1 = Blink1()
    print("dev", b1.dev)
    b1.fade_to_color(100, color)
    sleep(3)
    b1.off()
    b1.close()

light_on('blue')
light_on('yellow')
light_on('red')

It seems that the close method of the first Blink1() didn't release the device which made the second Blink1() fail to find the blink(1) while initializing.

About my environment, I have one blink(1) mk connected to my Mac ---

Darwin VM.local 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64

Python Version ---

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Python packages (i.e. output of pip freeze)---

blink1==0.1.3
click==6.7
hidapi==0.7.99.post21
webcolors==1.8.1

Please fix this, thanks!