Skip to content

Commit

Permalink
Update Sensors to report to load_cell_endstop
Browse files Browse the repository at this point in the history
  • Loading branch information
garethky committed Jun 17, 2024
1 parent 74a0678 commit c92d841
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions klippy/extras/ads1220.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, config):
self.name,
{'header': ('time', 'counts')})
# Command Configuration
self.config_endstop_cmd = None
mcu.add_config_cmd(
"config_ads1220 oid=%d spi_oid=%d data_ready_pin=%s"
% (self.oid, self.spi.get_oid(), self.data_ready_pin))
Expand All @@ -84,6 +85,8 @@ def __init__(self, config):

def _build_config(self):
cmdqueue = self.spi.get_command_queue()
self.query_ads1220_cmd = self.mcu.lookup_command(
"query_ads1220 oid=%c rest_ticks=%u", cq=cmdqueue)
self.query_ads1220_cmd = self.mcu.lookup_command(
"query_ads1220 oid=%c rest_ticks=%u", cq=cmdqueue)
self.ffreader.setup_query_command("query_ads1220_status oid=%c",
Expand All @@ -104,6 +107,9 @@ def get_range(self):
def add_client(self, callback):
self.batch_bulk.add_client(callback)

def attach_endstop(self, endstop_oid):
self.config_endstop_cmd.send_wait_ack([self.oid, endstop_oid])

# Measurement decoding
def _convert_samples(self, samples):
adc_factor = 1. / (1 << 23)
Expand Down
7 changes: 7 additions & 0 deletions klippy/extras/hx71x.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, config, sensor_type,
{'header': ('time', 'counts')})
# Command Configuration
self.query_hx71x_cmd = None
self.config_endstop_cmd = None
mcu.add_config_cmd(
"config_hx71x oid=%d gain_channel=%d dout_pin=%s sclk_pin=%s"
% (self.oid, self.gain_channel, self.dout_pin, self.sclk_pin))
Expand All @@ -68,10 +69,13 @@ def __init__(self, config, sensor_type,
def _build_config(self):
self.query_hx71x_cmd = self.mcu.lookup_command(
"query_hx71x oid=%c rest_ticks=%u")
self.config_endstop_cmd = self.mcu.lookup_command(
"attach_endstop_hx71x oid=%c load_cell_endstop_oid=%c")
self.ffreader.setup_query_command("query_hx71x_status oid=%c",
oid=self.oid,
cq=self.mcu.alloc_command_queue())


def get_mcu(self):
return self.mcu

Expand All @@ -87,6 +91,9 @@ def get_range(self):
def add_client(self, callback):
self.batch_bulk.add_client(callback)

def attach_endstop(self, endstop_oid):
self.config_endstop_cmd.send_wait_ack([self.oid, endstop_oid])

# Measurement decoding
def _convert_samples(self, samples):
adc_factor = 1. / (1 << 23)
Expand Down
16 changes: 16 additions & 0 deletions src/sensor_ads1220.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "command.h" // DECL_COMMAND
#include "sched.h" // sched_add_timer
#include "sensor_bulk.h" // sensor_bulk_report
#include "load_cell_endstop.h" // load_cell_endstop_report_sample
#include "spicmds.h" // spidev_transfer
#include <stdint.h>

Expand All @@ -22,6 +23,7 @@ struct ads1220_adc {
struct spidev_s *spi;
uint8_t pending_flag, data_count;
struct sensor_bulk sb;
struct load_cell_endstop *lce;
};

// Flag types
Expand Down Expand Up @@ -106,6 +108,11 @@ ads1220_read_adc(struct ads1220_adc *ads1220, uint8_t oid)
}

add_sample(ads1220, oid, counts);

// endstop is optional, report if enabled and no errors
if (ads1220->last_error == 0 && ads1220->lce) {
load_cell_endstop_report_sample(ads1220->lce, counts, start);
}
}

// Create an ads1220 sensor
Expand All @@ -122,6 +129,15 @@ command_config_ads1220(uint32_t *args)
DECL_COMMAND(command_config_ads1220, "config_ads1220 oid=%c"
" spi_oid=%c data_ready_pin=%u");

void
command_attach_endstop_ads1220(uint32_t *args) {
uint8_t oid = args[0];
struct ads1220_adc *ads1220 = oid_lookup(oid, command_config_ads1220);
ads1220->lce = load_cell_endstop_oid_lookup(args[1]);
}
DECL_COMMAND(command_attach_endstop_ads1220, "attach_endstop_ads1220 oid=%c"
" load_cell_endstop_oid=%c");

// start/stop capturing ADC data
void
command_query_ads1220(uint32_t *args)
Expand Down
16 changes: 16 additions & 0 deletions src/sensor_hx71x.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "command.h" // DECL_COMMAND
#include "sched.h" // sched_add_timer
#include "sensor_bulk.h" // sensor_bulk_report
#include "load_cell_endstop.h" // load_cell_endstop_report_sample
#include <stdbool.h>
#include <stdint.h>

Expand All @@ -24,6 +25,7 @@ struct hx71x_adc {
struct gpio_in dout; // pin used to receive data from the hx71x
struct gpio_out sclk; // pin used to generate clock for the hx71x
struct sensor_bulk sb;
struct load_cell_endstop *lce;
};

#define BYTES_PER_SAMPLE 4
Expand Down Expand Up @@ -168,6 +170,11 @@ hx71x_read_adc(struct hx71x_adc *hx71x, uint8_t oid)

// Add measurement to buffer
add_sample(hx71x, oid, counts, false);

// endstop is optional, report if enabled
if (hx71x->last_error == 0 && hx71x->lce) {
load_cell_endstop_report_sample(hx71x->lce, counts, start);
}
}

// Create a hx71x sensor
Expand All @@ -190,6 +197,15 @@ command_config_hx71x(uint32_t *args)
DECL_COMMAND(command_config_hx71x, "config_hx71x oid=%c gain_channel=%c"
" dout_pin=%u sclk_pin=%u");

void
command_attach_endstop_hx71x(uint32_t *args) {
uint8_t oid = args[0];
struct hx71x_adc *hx71x = oid_lookup(oid, command_config_hx71x);
hx71x->lce = load_cell_endstop_oid_lookup(args[1]);
}
DECL_COMMAND(command_attach_endstop_hx71x, "attach_endstop_hx71x oid=%c"
" load_cell_endstop_oid=%c");

// start/stop capturing ADC data
void
command_query_hx71x(uint32_t *args)
Expand Down

0 comments on commit c92d841

Please sign in to comment.