From fc8ff7ae702eb7cfae42f097a2cd29ec92c553cd Mon Sep 17 00:00:00 2001 From: RoboSchmied Date: Wed, 27 Mar 2024 03:46:12 +0100 Subject: [PATCH] refactoring i2cdetect makes it 2 times faster (on goke) before: time -p ./ipctool i2cdetect real 0.14 user 0.02 sys 0.08 after: time -p ./ipctool i2cdetect real 0.07 user 0.00 sys 0.05 Signed-off-by: RoboSchmied --- src/i2cspi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/i2cspi.c b/src/i2cspi.c index d935331..a17164d 100644 --- a/src/i2cspi.c +++ b/src/i2cspi.c @@ -200,10 +200,10 @@ static int i2cdetect(int argc, char **argv, bool script_mode) { unsigned char i2c_addr; printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); + int fd = prepare_i2c_sensor(0x00); i2c_addr = 0xff; // will be 0x00 after first increment do { ++i2c_addr; - int fd = prepare_i2c_sensor(i2c_addr); int res = i2c_read_register(fd, i2c_addr, 0, SELECT_WIDE(0), 1); if (i2c_addr % 16 == 0) @@ -221,10 +221,10 @@ static int i2cdetect(int argc, char **argv, bool script_mode) { printf("| \n"); } - close_sensor_fd(fd); - hal_cleanup(); } while (i2c_addr != 0xff); + close_sensor_fd(fd); + hal_cleanup(); printf("\n"); return EXIT_SUCCESS;