Skip to content

(#9) change __getitem__ to use self._n instead of self._buf // 4 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2018

Conversation

margaret
Copy link
Contributor

@margaret margaret commented May 14, 2018

@margaret
Copy link
Contributor Author

margaret commented May 14, 2018

More context
setup for self._buf is like this

:param int n: The number of dotstars in the chain
...
self._n = n
self.start_header_size = 4
# Supply one extra clock cycle for each two pixels in the strip.
self.end_header_size = n // 16
if n % 16 != 0:
    self.end_header_size += 1
self._buf = bytearray(n * 4 + self.start_header_size + self.end_header_size)

so if you had one dotstar, length of self._buf would be 1*4 + 4 + 0 and self._buf // 4 would be 2 instead of the expected 1 in

    def __getitem__(self, index):
        if isinstance(index, slice):
            out = []
            for in_i in range(*index.indices(self._n)): # used to be self._buf // 4
                out.append(
                    tuple(self._buf[in_i * 4 + (3 - i) + self.start_header_size] for i in range(3)))
            return out

@margaret
Copy link
Contributor Author

margaret commented May 14, 2018

Tested this manually with the pycon Gemma M0 ✅

(Test with current master of this library) code.py (modified from the default pycon open session file) is
# Gemma IO demo
# Welcome to CircuitPython 2.2.4 :)

from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn, AnalogOut
from touchio import TouchIn
import adafruit_dotstar as dotstar
import microcontroller
import board
import time

# One pixel connected internally!
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)

tests = [
  '[0]',
  '[1]',
  '[-1]',
  '[:1]',
  '[1:]',
  '[0:]'
]
for test_idx in tests:
  teststr = 'dot'+test_idx
  print(teststr + ' ' + '-'*80)
  try:
    print(eval(teststr))
  except IndexError as e:
    print('Error:')
    print(e)
  print()

This prints

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
dot[0] --------------------------------------------------------------------------------
(0, 0, 0)

dot[1] --------------------------------------------------------------------------------
Error:


dot[-1] --------------------------------------------------------------------------------
(0, 0, 0)

dot[:1] --------------------------------------------------------------------------------
[(0, 0, 0)]

dot[1:] --------------------------------------------------------------------------------
Error:
bytearray index out of range

dot[0:] --------------------------------------------------------------------------------
Error:
bytearray index out of range

Compared to this from CPython
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

>>> dot
[(0, 0, 0)]
>>> tests = [
...   '[0]',
...   '[1]',
...   '[-1]',
...   '[:1]',
...   '[1:]',
...   '[0:]'
... ]
>>> for test_idx in tests:
...   teststr = 'dot'+test_idx
...   print(teststr + ' ' + '-'*80)
...   try:
...     print(eval(teststr))
...   except IndexError as e:
...     print('Error:')
...     print(e)
...   print()
... 
dot[0] --------------------------------------------------------------------------------
(0, 0, 0)

dot[1] --------------------------------------------------------------------------------
Error:
list index out of range

dot[-1] --------------------------------------------------------------------------------
(0, 0, 0)

dot[:1] --------------------------------------------------------------------------------
[(0, 0, 0)]

dot[1:] --------------------------------------------------------------------------------
[]

dot[0:] --------------------------------------------------------------------------------
[(0, 0, 0)]

With this PR's changes

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
dot[0] --------------------------------------------------------------------------------
(0, 0, 0)

dot[1] --------------------------------------------------------------------------------
Error:


dot[-1] --------------------------------------------------------------------------------
(0, 0, 0)

dot[:1] --------------------------------------------------------------------------------
[(0, 0, 0)]

dot[1:] --------------------------------------------------------------------------------
[]

dot[0:] --------------------------------------------------------------------------------
[(0, 0, 0)]

This matches the behavior of standard slicing, so I think this change is ✅

Note that the IndexError description doesn't seem to get bubbled up in circuitpython, but I think that should be a separate issue.

@margaret
Copy link
Contributor Author

@tannewt Ready for review

@margaret margaret changed the title (#9) __getitem__ uses self._n instead of self._buf // 4 (#9) change __getitem__ to use self._n instead of self._buf // 4 May 14, 2018
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks much simpler!

@tannewt tannewt merged commit 144bb07 into adafruit:master May 14, 2018
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 16, 2018
Updating https://github.com/adafruit/Adafruit_CircuitPython_DotStar to 1.2.0 from 1.1.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#20 from mcscope/patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#19 from mcscope/advanced_dotstar
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#18 from mcscope/color_orders
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#17 from margaret/consistent_get_set
  > Merge pull request adafruit/Adafruit_CircuitPython_DotStar#14 from sommersoft/new_docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants