scale

Functions

get_scale(address, min_threshold, max_threshold)

Factory function to initialize the Scale.

Classes

Scale(address, min_threshold, max_threshold)

Interface for an I2C scale sensor.

class Scale(address: str, min_threshold: float, max_threshold: float)[source]

Bases: NullScale

Interface for an I2C scale sensor.

Parameters:
  • address (str) – The I2C address of the scale as a hex string (e.g., “0x2A”).

  • min_threshold (float) – The minimum weight threshold for valid measurements.

  • max_threshold (float) – The maximum weight threshold for valid measurements.

I2C_ADDR

The I2C address of the scale.

Type:

int

REG_DATA_GET_RAM_DATA

Register address for getting raw data.

Type:

int

bus

The I2C bus number.

Type:

int

calibration

Calibration factor.

Type:

float

offset

Tare offset.

Type:

float

i2cbus

The I2C bus connection.

Type:

smbus2.SMBus

error

Error message if any.

Type:

str

error_message_timer

Timer to limit error logging frequency.

Type:

time_utils.Timer

alarm_timer

Timer for non-responsive alarms.

Type:

time_utils.Timer

min

Minimum weight threshold.

Type:

float

max

Maximum weight threshold.

Type:

float

tare() None[source]

Tares the scale (sets the current weight as zero).

calibrate(weight: float) None[source]

Calibrates the scale with a known weight.

Parameters:

weight (float) – The known weight in grams used for calibration.

get_value(samples: int = 5, interval_s: float = 0.005) int[source]

Reads raw values from the scale.

Parameters:
  • samples (int) – Number of samples to read.

  • interval_s (float) – Delay between samples in seconds.

Returns:

The mean of the raw values.

Return type:

int

get_weight() float[source]

Gets the current weight in grams.

Returns:

The weight in grams.

Return type:

float

real_weight_inference() tuple[bool, float][source]

Determines if a sequence of weight measurements represents a stable weight.

Conditions to call it a real weight: - standard deviation of the last 5 measurements is

smaller than 10% of the minimum threshold

Returns:

(True, median_weight) if stable, else (False, 0.0).

Return type:

tuple[bool, float]

get_scale(address: str, min_threshold: float, max_threshold: float) Scale | NullScale[source]

Factory function to initialize the Scale.

Parameters:

address (str) – The I2C address of the scale.

Returns:

An initialized Scale instance or a null scale on failure.

Return type:

Scale | NullScale