bulk_sensor: Rework APIDumpHelper() to BatchBulkHelper()

The APIDumpHelper class is mainly intended to help process messages in
batches.  Rework the class methods to make that more clear.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor
2023-12-16 23:26:42 -05:00
parent 95c753292d
commit 3370134593
6 changed files with 112 additions and 124 deletions

View File

@@ -187,7 +187,7 @@ MIN_MSG_TIME = 0.100
BYTES_PER_SAMPLE = 5
SAMPLES_PER_BLOCK = 10
API_UPDATES = 0.100
BATCH_UPDATES = 0.100
# Printer class that controls ADXL345 chip
class ADXL345:
@@ -211,18 +211,19 @@ class ADXL345:
mcu.register_config_callback(self._build_config)
self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, "adxl345_data", oid)
# Clock tracking
chip_smooth = self.data_rate * API_UPDATES * 2
chip_smooth = self.data_rate * BATCH_UPDATES * 2
self.clock_sync = bulk_sensor.ClockSyncRegression(mcu, chip_smooth)
self.clock_updater = bulk_sensor.ChipClockUpdater(self.clock_sync,
BYTES_PER_SAMPLE)
self.last_error_count = 0
# API server endpoints
self.api_dump = bulk_sensor.APIDumpHelper(
self.printer, self._api_update, self._api_startstop, API_UPDATES)
# Process messages in batches
self.batch_bulk = bulk_sensor.BatchBulkHelper(
self.printer, self._process_batch,
self._start_measurements, self._finish_measurements, BATCH_UPDATES)
self.name = config.get_name().split()[-1]
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
self.api_dump.add_mux_endpoint("adxl345/dump_adxl345", "sensor",
self.name, {'header': hdr})
self.batch_bulk.add_mux_endpoint("adxl345/dump_adxl345", "sensor",
self.name, {'header': hdr})
def _build_config(self):
cmdqueue = self.spi.get_command_queue()
self.query_adxl345_cmd = self.mcu.lookup_command(
@@ -248,7 +249,10 @@ class ADXL345:
"This is generally indicative of connection problems "
"(e.g. faulty wiring) or a faulty adxl345 chip." % (
reg, val, stored_val))
# Measurement collection
def start_internal_client(self):
cconn = self.batch_bulk.add_internal_client()
return AccelQueryHelper(self.printer, cconn)
# Measurement decoding
def _extract_samples(self, raw_samples):
# Load variables to optimize inner loop below
(x_pos, x_scale), (y_pos, y_scale), (z_pos, z_scale) = self.axes_map
@@ -294,6 +298,7 @@ class ADXL345:
else:
raise self.printer.command_error("Unable to query adxl345 fifo")
self.clock_updater.update_clock(params)
# Start, stop, and process message batches
def _start_measurements(self):
# In case of miswiring, testing ADXL345 device ID prevents treating
# noise or wrong signal as a correctly initialized device
@@ -329,8 +334,7 @@ class ADXL345:
params = self.query_adxl345_end_cmd.send([self.oid, 0, 0])
self.bulk_queue.clear_samples()
logging.info("ADXL345 finished '%s' measurements", self.name)
# API interface
def _api_update(self, eventtime):
def _process_batch(self, eventtime):
self._update_clock()
raw_samples = self.bulk_queue.pull_samples()
if not raw_samples:
@@ -340,14 +344,6 @@ class ADXL345:
return {}
return {'data': samples, 'errors': self.last_error_count,
'overflows': self.clock_updater.get_last_limit_count()}
def _api_startstop(self, is_start):
if is_start:
self._start_measurements()
else:
self._finish_measurements()
def start_internal_client(self):
cconn = self.api_dump.add_internal_client()
return AccelQueryHelper(self.printer, cconn)
def load_config(config):
return ADXL345(config)