diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2023-12-16 11:30:51 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2023-12-26 11:47:21 -0500 |
commit | e67cbbe5c12bae1deb4651e2b3aa12c3c77c3439 (patch) | |
tree | e59bfdac4e65409b1073a91f021a22ff1c8842ea /klippy/extras/adxl345.py | |
parent | 978c294741bafac136081f40d066ad2884b1ffce (diff) | |
download | kutter-e67cbbe5c12bae1deb4651e2b3aa12c3c77c3439.tar.gz kutter-e67cbbe5c12bae1deb4651e2b3aa12c3c77c3439.tar.xz kutter-e67cbbe5c12bae1deb4651e2b3aa12c3c77c3439.zip |
bulk_sensor: Add new BulkDataQueue class
Move the bulk sample queue collection to a new helper class in
bulk_sensor.py.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras/adxl345.py')
-rw-r--r-- | klippy/extras/adxl345.py | 21 |
1 files changed, 5 insertions, 16 deletions
diff --git a/klippy/extras/adxl345.py b/klippy/extras/adxl345.py index 36dc8071..1cac6143 100644 --- a/klippy/extras/adxl345.py +++ b/klippy/extras/adxl345.py @@ -3,7 +3,7 @@ # Copyright (C) 2020-2021 Kevin O'Connor <kevin@koconnor.net> # # This file may be distributed under the terms of the GNU GPLv3 license. -import logging, time, collections, threading, multiprocessing, os +import logging, time, collections, multiprocessing, os from . import bus, motion_report, bulk_sensor # ADXL345 registers @@ -195,9 +195,6 @@ class ADXL345: self.data_rate = config.getint('rate', 3200) if self.data_rate not in QUERY_RATES: raise config.error("Invalid rate parameter: %d" % (self.data_rate,)) - # Measurement storage (accessed from background thread) - self.lock = threading.Lock() - self.raw_samples = [] # Setup mcu sensor_adxl345 bulk query code self.spi = bus.MCU_SPI_from_config(config, 3, default_speed=5000000) self.mcu = mcu = self.spi.get_mcu() @@ -209,7 +206,7 @@ class ADXL345: mcu.add_config_cmd("query_adxl345 oid=%d clock=0 rest_ticks=0" % (oid,), on_restart=True) mcu.register_config_callback(self._build_config) - mcu.register_response(self._handle_adxl345_data, "adxl345_data", oid) + self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, "adxl345_data", oid) # Clock tracking self.last_sequence = self.max_query_duration = 0 self.last_limit_count = self.last_error_count = 0 @@ -250,9 +247,6 @@ class ADXL345: # Measurement collection def is_measuring(self): return self.query_rate > 0 - def _handle_adxl345_data(self, params): - with self.lock: - self.raw_samples.append(params) 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 @@ -335,10 +329,8 @@ class ADXL345: self.set_reg(REG_FIFO_CTL, 0x00) self.set_reg(REG_BW_RATE, QUERY_RATES[self.data_rate]) self.set_reg(REG_FIFO_CTL, SET_FIFO_CTL) - # Setup samples - with self.lock: - self.raw_samples = [] # Start bulk reading + self.bulk_queue.clear_samples() systime = self.printer.get_reactor().monotonic() print_time = self.mcu.estimated_print_time(systime) + MIN_MSG_TIME reqclock = self.mcu.print_time_to_clock(print_time) @@ -360,15 +352,12 @@ class ADXL345: # Halt bulk reading params = self.query_adxl345_end_cmd.send([self.oid, 0, 0]) self.query_rate = 0 - with self.lock: - self.raw_samples = [] + self.bulk_queue.clear_samples() logging.info("ADXL345 finished '%s' measurements", self.name) # API interface def _api_update(self, eventtime): self._update_clock() - with self.lock: - raw_samples = self.raw_samples - self.raw_samples = [] + raw_samples = self.bulk_queue.pull_samples() if not raw_samples: return {} samples = self._extract_samples(raw_samples) |