# HX711/HX717 Support # # Copyright (C) 2024 Gareth Farrington # # This file may be distributed under the terms of the GNU GPLv3 license. import logging from . import bulk_sensor # # Constants # UPDATE_INTERVAL = 0.10 SAMPLE_ERROR_DESYNC = -0x80000000 SAMPLE_ERROR_LONG_READ = 0x40000000 # Implementation of HX711 and HX717 class HX71xBase: def __init__( self, config, sensor_type, sample_rate_options, default_sample_rate, gain_options, default_gain, ): self.printer = printer = config.get_printer() self.name = config.get_name().split()[-1] self.last_error_count = 0 self.consecutive_fails = 0 self.sensor_type = sensor_type # Chip options dout_pin_name = config.get("dout_pin") sclk_pin_name = config.get("sclk_pin") ppins = printer.lookup_object("pins") dout_ppin = ppins.lookup_pin(dout_pin_name) sclk_ppin = ppins.lookup_pin(sclk_pin_name) self.mcu = mcu = dout_ppin["chip"] self.oid = mcu.create_oid() if sclk_ppin["chip"] is not mcu: raise config.error( "%s config error: All pins must be " "connected to the same MCU" % (self.name,) ) self.dout_pin = dout_ppin["pin"] self.sclk_pin = sclk_ppin["pin"] # Samples per second choices self.sps = config.getchoice( "sample_rate", sample_rate_options, default=default_sample_rate ) # gain/channel choices self.gain_channel = int( config.getchoice("gain", gain_options, default=default_gain) ) ## Bulk Sensor Setup self.bulk_queue = bulk_sensor.BulkDataQueue(mcu, oid=self.oid) # Clock tracking chip_smooth = self.sps * UPDATE_INTERVAL * 2 self.ffreader = bulk_sensor.FixedFreqReader(mcu, chip_smooth, " 0: logging.error("%s: Forced sensor restart due to error", self.name) self._finish_measurements() self._start_measurements() elif overflows > 0: self.consecutive_fails += 1 if self.consecutive_fails > 4: logging.error("%s: Forced sensor restart due to overflows", self.name) self._finish_measurements() self._start_measurements() else: self.consecutive_fails = 0 return { "data": samples, "errors": self.last_error_count, "overflows": self.ffreader.get_last_overflows(), } def HX711(config): return HX71xBase( config, "hx711", # HX711 sps options {80: 80, 10: 10}, 80, # HX711 gain/channel options {"A-128": 1, "B-32": 2, "A-64": 3}, "A-128", ) def HX717(config): return HX71xBase( config, "hx717", # HX717 sps options {320: 320, 80: 80, 20: 20, 10: 10}, 320, # HX717 gain/channel options {"A-128": 1, "B-64": 2, "A-64": 3, "B-8": 4}, "A-128", ) HX71X_SENSOR_TYPES = {"hx711": HX711, "hx717": HX717}