Python API reference
Complete API for the turbidvision Python client library.
The package exposes the TurbidVision client plus the Status, Snapshot, LatestReading, and TriggeredReading dataclasses and a TurbidVisionError exception. Read methods return nan or None when a value is unavailable; command methods raise TurbidVisionError on a transport failure. For the underlying register addresses, see the I²C interface reference.
Construction
TurbidVision(bus: int | SMBus = 1, address: int = 0x69)bus is either an I²C bus number (e.g. 1 for /dev/i2c-1) or an already-open smbus2.SMBus. When a number is given, this object owns the bus and opens/closes it on open()/close() or context entry/exit. A passed-in SMBus instance is used as-is and never closed for you.
Lifecycle and connection
| Method | Returns | Description |
|---|---|---|
| open() | TurbidVision | Open the bus when this object owns it. Returns self. Called automatically on context entry. |
| close() | None | Close the bus if this object owns it. A passed-in SMBus instance is never closed for you. |
| __enter__() / __exit__() | TurbidVision | Context-manager protocol: opens on entry, closes on exit. Use a 'with' block. |
| bus | SMBus | Property returning the underlying open bus. Raises TurbidVisionError if the bus is not open. |
| is_present() | bool | True if the sensor responds (status register reads OK). |
Measurement reads
Each returns a float in the column unit, or nan when the value is unavailable.
| Method | Register | Description |
|---|---|---|
| measured_reflectance() | 0x10 | Raw reflectance ratio (unitless). |
| normalized_reflectance() | 0x11 | Reflectance normalized to the initial reference (and blank, if set). |
| calibrated_density() | 0x12 | Unfiltered density in g/L from the calibration lookup table. |
| growth_rate() | 0x13 | Kalman filter specific growth rate in 1/h. |
| laser_power_mw() | 0x14 | Laser optical output power in mW. |
| filtered_calibrated_density() | 0x16 | Kalman-filtered density in g/L. |
| filtered_calibrated_growth_rate() | 0x17 | Filtered volumetric growth rate in g/L/h. |
| calibrated_absolute_growth_rate() | 0x18 | Unfiltered volumetric growth rate in g/L/h. |
Raw register reads
| Method | Returns | Description |
|---|---|---|
| read_float(reg) | float | Read a 32-bit little-endian float. Returns nan on a non-OK status. |
| read_uint32(reg) | int | None | Read a 32-bit little-endian unsigned int. Returns None on error. |
| read_status() | Status | Read and decode the live status byte (0x20). Raises TurbidVisionError on a non-OK status. |
| error_flags() | int | None | Read the system error/warning vector (0x21). Returns the bitmask (0 = no faults) or None on error. |
| reading_number() | int | None | Monotonic reading counter (0x30). None on error. |
| read_all() | Snapshot | Read every measurement register plus the status byte and error flags. |
Atomic host readings
These methods return normalized reflectance and the calibrated value in one coherent result with age, quality, diagnostic, calibration flag, and unit metadata. A one-shot is observational and does not change the normal reading counter, rolling average, Growth Model, datalog, or normal capture cadence.
| Method | Returns | Description |
|---|---|---|
| latest_reading() | LatestReading | Return one coherent latest normal-cadence sample from register 0x31, including its age, calibration flag, calibration unit, normalized reflectance, and calibrated value. |
| triggered_reading() | TriggeredReading | Read the pending or completed one-shot result latch at register 0x32 without starting a new capture. |
| trigger_reading(timeout=35.0, poll_interval=0.05) | TriggeredReading | Request a fresh observational capture and block until its new sequence completes. Raises TimeoutError on timeout and TurbidVisionError when the completed sample is invalid. |
Commands
Each returns None and raises TurbidVisionError on a transport failure. Job, blank, and calibration commands run asynchronously on the sensor; poll the status byte to confirm completion.
| Method | Description |
|---|---|
| start_job() | Start the OD job (control 0x01). Resets the register bank to NaN. |
| stop_job() | Stop the OD job and turn off the laser (control 0x02). |
| take_blank() | Capture a blank reference (control 0x03). Stop the job first. |
| take_cal_reading() | Capture a calibration reading against a known standard (control 0x04). |
| deactivate_laser() | Stop the laser power controller and turn the laser off (control 0x05). |
| erase_initial() | Erase the stored initial reference (control 0x06). |
| clear_errors() | Clear the latching error-class flags in register 0x21 (control 0x07). |
| set_laser_power_pct(pct) | Set the laser target as a percentage of maximum (0.0 to 100.0) and activate the controller. Writes register 0x50. |
Polling
| Method | Returns | Description |
|---|---|---|
| wait_for_status(mask, timeout=35.0, poll_interval=0.2, clear=False) | bool | Block until (status & mask) is set, or cleared when clear is True. Returns True on success, False on timeout. |
| wait_until(predicate, timeout=35.0, poll_interval=0.2) | bool | Block until predicate(status) is True. predicate takes a Status. Returns False on timeout. |
Data classes
Status
Decoded result of read_status(). Build one from a raw byte with the classmethod Status.from_byte(flags); str(status) gives a compact one-line summary.
| Field | Type | Description |
|---|---|---|
| raw | int | The raw status byte. |
| job_running | bool | OD job is active (bit 0). |
| has_blank | bool | A blank reference has been captured (bit 1). |
| has_initial | bool | Initial reference is complete (bit 2). |
| laser_pwrctl_running | bool | Laser power controller is active (bit 3). |
| laser_stable | bool | Laser output has stabilized (bit 4). |
| reflectance_stable | bool | Reflectance is stable (bit 5). |
| error | bool | Any error/warning flag is set; read error_flags() (bit 6). |
| calibrated | bool | A usable calibration lookup table is present (bit 7). |
Snapshot
Full snapshot returned by read_all(). Float fields are nan when unavailable.
| Field | Type | Description |
|---|---|---|
| reading_number | int | None | Monotonic reading counter, or None on read error. |
| status | Status | Decoded status byte. |
| error_flags | int | None | Error/warning vector, or None on error. |
| measured_reflectance | float | nan when unavailable. |
| normalized_reflectance | float | nan when unavailable. |
| calibrated_density | float | nan when unavailable. |
| growth_rate | float | nan when the Kalman filter is disabled. |
| laser_power_mw | float | nan when unavailable. |
| filtered_calibrated_density | float | nan when the Kalman filter is disabled. |
| filtered_calibrated_growth_rate | float | nan when the Kalman filter is disabled. |
| calibrated_absolute_growth_rate | float | nan when the Kalman filter is disabled. |
LatestReading
Atomic latest normal-cadence sample returned by latest_reading().
| Field | Type | Description |
|---|---|---|
| flags | int | Packed valid, calibrated, and job-running-at-capture flags. |
| quality | int | Reading quality code. |
| calibration_unit | int | None | 0 g/L, 1 OD600, 2 FNU, 3 NTU, 4 custom, or None. |
| diagnostic_code | int | Diagnostic code associated with the sample. |
| reading_number | int | Normal-cadence reading number. |
| age_ms | int | None | Milliseconds since capture, or None before a sample exists. |
| normalized_reflectance | float | Normalized reflectance, or nan when no initial reference is available. |
| calibrated_value | float | Value in calibration_unit, or nan when no usable calibration was applied. |
| valid / calibrated / job_running_at_capture | bool properties | Convenience properties decoded from flags. |
TriggeredReading
Latched one-shot state returned by triggered_reading() and the completed sample returned by trigger_reading().
| Field | Type | Description |
|---|---|---|
| request_sequence | int | Sequence assigned to the one-shot request. |
| phase | int | 0 idle, 1 pending, or 2 complete. |
| flags | int | Packed valid, calibrated, and job-running-at-capture flags. |
| quality | int | Reading quality code. |
| calibration_unit | int | None | 0 g/L, 1 OD600, 2 FNU, 3 NTU, 4 custom, or None. |
| diagnostic_code | int | Diagnostic code associated with the request or sample. |
| age_ms | int | None | Milliseconds since capture, or None before a sample exists. |
| normalized_reflectance | float | Normalized reflectance, or nan when no initial reference is available. |
| calibrated_value | float | Value in calibration_unit, or nan when no usable calibration was applied. |
| valid / calibrated / job_running_at_capture | bool properties | Convenience properties decoded from flags. |
TurbidVisionError
Subclass of IOError, raised on an I²C transport failure or a non-OK response status.
Constants
All constants are module-level (e.g. turbidvision.STATUS_HAS_INITIAL); the STATUS_* masks are also re-exported as class attributes (sensor.STATUS_HAS_INITIAL) for convenience.
Status bit masks
Use with read_status().raw or wait_for_status().
| Constant | Bit |
|---|---|
| STATUS_JOB_RUNNING | 0 |
| STATUS_HAS_BLANK | 1 |
| STATUS_HAS_INITIAL | 2 |
| STATUS_LASER_PWRCTL_RUNNING | 3 |
| STATUS_LASER_STABLE | 4 |
| STATUS_REFLECTANCE_STABLE | 5 |
| STATUS_ERROR | 6 |
| STATUS_CALIBRATED | 7 |
Error flag masks
Decode the result of error_flags() (register 0x21). Bits 0 to 4 are error-class (latching); bits 5 to 8 are warning-class.
| Constant | Bit |
|---|---|
| ERR_SAFETY_CRITICAL | 0 |
| ERR_SAFETY_WARNING | 1 |
| ERR_PI_FAILED | 2 |
| ERR_LOW_HEAP | 3 |
| ERR_STORAGE | 4 |
| WARN_NO_BLANK | 5 |
| WARN_NO_INITIAL | 6 |
| WARN_WIFI_DOWN | 7 |
| WARN_OTA | 8 |
Response status codes
| Constant | Value | Meaning |
|---|---|---|
| RESP_OK | 0x00 | Read succeeded; payload is valid. |
| RESP_UNKNOWN_REGISTER | 0x01 | Unknown register; no data follows. |
| RESP_WIRE_ERROR | 0xFF | Client-side wire-error sentinel; not emitted by the sensor. |
Register constants (REG_*) and command constants (CMD_*) map one-to-one to the addresses and command bytes in the I²C interface reference. For runnable scripts using this API, see the Python examples.

