Skip to content

Bug: Incorrect bit operation #9

@simlu

Description

@simlu

The spad map calculation is incorrect.

        spads_enabled = 0
        for i in range(48):
            if i < 12 and is_aperture or spads_enabled >= spad_count:
                spad_map[i // 8] &= ~(1 << (i >> 2))
            elif spad_map[i // 8] & (1 << (i >> 2)):
                spads_enabled += 1

https://github.com/uceeatz/VL53L0X/blob/master/VL53L0X.py#L206

Why? It's easy to see:

When i = 0, i >> 2 is 0
When i = 1, i >> 2 is 0
When i = 2, i >> 2 is 0
When i = 3, i >> 2 is 0
When i = 4, i >> 2 is 1

Corrected version should be something like this:

  first_spad_to_enable = 12 if spad_is_aperture else 0
  spads_enabled = 0
  for i in range(48):
    if i < first_spad_to_enable or spads_enabled == spad_count:
      ref_spad_map[(i // 8)] &= ~(1 << (i % 8)) # zero this bit.
    elif (ref_spad_map[(i // 8)] >> (i % 8)) & 0x1 > 0:
      spads_enabled += 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions