Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
Removed whitespaces and fixed long lines

Signed-off-by: Endrik Einberg <endrik@kooder.dev>
  • Loading branch information
enduity committed Feb 10, 2024
1 parent 764ce85 commit 0908f15
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
16 changes: 9 additions & 7 deletions klippy/extras/adxl345.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def __init__(self, config):
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,))

# Check if i2c or spi is used
if (config.get('i2c_bus', None) is None and
if (config.get('i2c_bus', None) is None and
config.get('i2c_software_scl_pin', None) is None):
self.bus_type = 'spi'
self.spi = bus.MCU_SPI_from_config(config,
3,
self.spi = bus.MCU_SPI_from_config(config,
3,
default_speed=5000000)
self.mcu = mcu = self.spi.get_mcu()
self.oid = oid = mcu.create_oid()
Expand All @@ -227,7 +227,7 @@ def __init__(self, config):
% (self.oid, self.i2c.get_oid()))
self.mcu.add_config_cmd("query_adxl345_i2c oid=%d rest_ticks=0"
% (self.oid,), on_restart=True)

# Setup mcu sensor_adxl345 bulk query code
self.query_adxl345_cmd = None
mcu.register_config_callback(self._build_config)
Expand All @@ -252,13 +252,15 @@ def _build_config(self):
self.query_adxl345_cmd = self.mcu.lookup_command(
"query_adxl345 oid=%c rest_ticks=%u", cq=cmdqueue)
self.clock_updater.setup_query_command(
self.mcu, "query_adxl345_status oid=%c", oid=self.oid, cq=cmdqueue)
self.mcu, "query_adxl345_status oid=%c",
oid=self.oid, cq=cmdqueue)
else:
cmdqueue = self.i2c.get_command_queue()
self.query_adxl345_cmd = self.mcu.lookup_command(
"query_adxl345_i2c oid=%c rest_ticks=%u", cq=cmdqueue)
self.clock_updater.setup_query_command(
self.mcu, "query_adxl345_i2c_status oid=%c", oid=self.oid, cq=cmdqueue)
self.mcu, "query_adxl345_i2c_status oid=%c",
oid=self.oid, cq=cmdqueue)
def read_reg(self, reg):
if self.bus_type == 'spi':
params = self.spi.spi_transfer([reg | REG_MOD_READ, 0x00])
Expand Down
30 changes: 19 additions & 11 deletions src/sensor_adxl345_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ static uint_fast8_t adxl345_i2c_event(struct timer *timer) {

// Initialize ADXL345 sensor configuration
void command_config_adxl345_i2c(uint32_t *args) {
struct adxl345_i2c *ax2 = oid_alloc(args[0], command_config_adxl345_i2c, sizeof(*ax2));
struct adxl345_i2c *ax2 = oid_alloc(args[0],
command_config_adxl345_i2c, sizeof(*ax2));
ax2->timer.func = adxl345_i2c_event;
ax2->i2c = i2cdev_oid_lookup(args[1]);
}
DECL_COMMAND(command_config_adxl345_i2c, "config_adxl345_i2c oid=%c i2c_oid=%c");
DECL_COMMAND(command_config_adxl345_i2c,
"config_adxl345_i2c oid=%c i2c_oid=%c");

// Reschedules the timer for the next data read
static void adxl_i2c_reschedule_timer(struct adxl345_i2c *ax2) {
Expand All @@ -67,17 +69,17 @@ static void adxl_i2c_query(struct adxl345_i2c *ax2, uint8_t oid) {
uint8_t reg = AR_DATAX0; // Start register for accelerometer data
uint8_t data[8]; // Buffer for reading data
i2c_read(ax2->i2c->i2c_config, 1, &reg, sizeof(data), data);
ax2->fifo_entries = data[7] & ~0x80; // Mask off trigger bit from FIFO status
ax2->fifo_entries = data[7] & ~0x80; // Mask off trigger bit

uint8_t *d = &ax2->sb.data[ax2->sb.data_count];

// Check for data errors before processing
if (((data[1] & 0xf0) && (data[1] & 0xf0) != 0xf0)
|| ((data[3] & 0xf0) && (data[3] & 0xf0) != 0xf0)
|| ((data[5] & 0xf0) && (data[5] & 0xf0) != 0xf0)
|| (data[6] != SET_FIFO_CTL) || (ax2->fifo_entries > 32)) {
// Fill with error code and reset FIFO entries
memset(d, 0xff, BYTES_PER_SAMPLE);
memset(d, 0xff, BYTES_PER_SAMPLE);
ax2->fifo_entries = 0;
} else {
// Process and store valid X, Y, Z readings
Expand All @@ -91,7 +93,7 @@ static void adxl_i2c_query(struct adxl345_i2c *ax2, uint8_t oid) {
ax2->sb.data_count += BYTES_PER_SAMPLE;
if (ax2->sb.data_count + BYTES_PER_SAMPLE > ARRAY_SIZE(ax2->sb.data))
sensor_bulk_report(&ax2->sb, oid);

// Manage FIFO data and reschedule as needed
if (ax2->fifo_entries >= 31)
ax2->sb.possible_overflows++;
Expand All @@ -117,7 +119,8 @@ void command_query_adxl345_i2c(uint32_t *args) {
sensor_bulk_reset(&ax2->sb);
adxl_i2c_reschedule_timer(ax2);
}
DECL_COMMAND(command_query_adxl345_i2c, "query_adxl345_i2c oid=%c rest_ticks=%u");
DECL_COMMAND(command_query_adxl345_i2c,
"query_adxl345_i2c oid=%c rest_ticks=%u");

// Query the status of the ADXL345 sensor
void command_query_adxl345_i2c_status(uint32_t *args) {
Expand All @@ -132,10 +135,15 @@ void command_query_adxl345_i2c_status(uint32_t *args) {
uint_fast8_t fifo_status = fifo_status_msg & ~0x80; // Mask off trigger bit
if (fifo_status > 32)
return; // Error in query, no response sent

sensor_bulk_status(&ax2->sb, args[0], time1, time2 - time1, fifo_status * BYTES_PER_SAMPLE);

sensor_bulk_status(&ax2->sb,
args[0],
time1,
time2 - time1,
fifo_status * BYTES_PER_SAMPLE);
}
DECL_COMMAND(command_query_adxl345_i2c_status, "query_adxl345_i2c_status oid=%c");
DECL_COMMAND(command_query_adxl345_i2c_status,
"query_adxl345_i2c_status oid=%c");

// Task to handle ADXL345 data queries
void adxl345_i2c_task(void) {
Expand All @@ -148,4 +156,4 @@ void adxl345_i2c_task(void) {
adxl_i2c_query(ax2, oid);
}
}
DECL_TASK(adxl345_i2c_task);
DECL_TASK(adxl345_i2c_task);

0 comments on commit 0908f15

Please sign in to comment.