Skip to content

Automatic byte count for spectral read #8

@samiebee43

Description

@samiebee43

It would make sense to know the correct number of bytes to read during spectral acquisition using the get line length function.

We do not want to call that from within get_spectrum for two reasons. (1) It's against one of the design principles of this module to have driver functions call each other (2) when get_spectrum is called repeatedly that would correspond to reading the EEPROM repeatedly, which may be slow or reduce the lifespan of the EEPROM (not sure).

TinyWP/TinyWP/TinyWP.py

Lines 129 to 130 in 03b0ecd

# TODO do something smart wrt msg_bytes and get_line_length, do not overcook EEPROM
def get_spectrum(device, msg_bytes=1024):

TinyWP/TinyWP/TinyWP.py

Lines 117 to 124 in 03b0ecd

def get_line_length(device):
"get number of pixels reported by device"
# TODO check if EEPROM is involved, if so: mention in docstring
seq = device.ctrl_transfer(DEVICE_TO_HOST, 0xff, 0x03, 0, 64)
return seq[1]<<8 + seq[0]

One option which fits into the rationale of this module is to leave that as a responsibility of the caller. So they would write a client program something along the lines of the following. Here it is the caller who keeps track of the EEPROM data and makes the decision to call it only once at the beginning. That keeps the module from being stateful. We can simplify the caller's work slightly be adding a get_line_bytes function which includes the factor of two.

# ...
bytes_to_read = 2*get_line_length(device) # OR bytes_to_read = get_line_bytes(device)

while True:
    get_spectrum(device, bytes_to_read)
    # ...

It's also possible to use caching on eeprom reading operations so then statefulness is tucked into the language. With caching in place, it could be acceptable to call get_line_length from within get_spectrum.

In summary the options are:

  1. Responsibility of caller: add a get_line_bytes function to simplify the client program determining and storing that data
  2. Automatic Caching: let get_spectrum be an exception to the rule that driver functions do not call each other. Use an annotation for caching
  3. Manual Caching: implement the contents of get_line_length within get_spectrum (keeping the functions independent). Use a small amount of state to cache the byte_count value so that the USB command that determines the line length only need to be done on the first call of get_spectrum

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions