From 47e647c56a1d9a40d06b5a19c1a41e055186385f Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Fri, 2 Nov 2018 07:07:36 -0600 Subject: [PATCH] examples/mlx90614/mlx90614_main.c: Include support to change device address on mlx90614 --- examples/mlx90614/mlx90614_main.c | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/examples/mlx90614/mlx90614_main.c b/examples/mlx90614/mlx90614_main.c index 81a42f874..249950408 100644 --- a/examples/mlx90614/mlx90614_main.c +++ b/examples/mlx90614/mlx90614_main.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -101,6 +102,42 @@ int mlx90614_main(int argc, char *argv[]) return -1; } + /* If user passed a new address */ + + if (argc == 2) + { + FAR char *buffer; + uint8_t newaddr; + + buffer = argv[1]; + + if (buffer[0] != '0' && buffer[1] != 'x') + { + fprintf(stderr, "You need to pass the I2C address in hexa: 0xNN\n"); + goto out; + } + + newaddr = strtol(buffer, NULL, 16); + + if (newaddr == 0 || newaddr > 0x7f) + { + fprintf(stderr, "Inform a valid I2C address: 0x01 to 0x7f\n"); + goto out; + } + + ret = ioctl(fd, SNIOC_CHANGE_SMBUSADDR, (unsigned long)((uintptr_t)&newaddr)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SNIOC_CHANGE_SMBUSADDR) failed: %d\n", errno); + goto out; + } + + printf("Address 0x%02x stored on your device!\n", newaddr); + printf("Please do a power cycle and update the I2C board address!\n"); + + goto out; + } + for (; ; ) { /* Read new temperature from sensor */ @@ -127,6 +164,8 @@ int mlx90614_main(int argc, char *argv[]) usleep(MILISEC_DELAY * 1000); } +out: + close(fd); return 0; }