MYNEWT-665 SensorAPI: Add TCS34725 Driver - reading from ATIME, control for AGAIN and ENABLE for state of the sensor instead of using varibales to store state.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/fd20bc93 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/fd20bc93 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/fd20bc93 Branch: refs/heads/nrf_cputime Commit: fd20bc93e84b81cd9e4ff164f8ac976350c5bc50 Parents: 20ffb7e Author: Vipul Rahane <vipulrah...@apache.org> Authored: Tue Mar 14 16:24:03 2017 -0700 Committer: Vipul Rahane <vipulrah...@apache.org> Committed: Tue Mar 14 16:24:03 2017 -0700 ---------------------------------------------------------------------- hw/drivers/sensors/tcs34725/src/tcs34725.c | 78 +++++++++++++------- hw/drivers/sensors/tcs34725/src/tcs34725_priv.h | 21 +++--- .../sensors/tcs34725/src/tcs34725_shell.c | 29 +++++--- 3 files changed, 84 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fd20bc93/hw/drivers/sensors/tcs34725/src/tcs34725.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tcs34725/src/tcs34725.c b/hw/drivers/sensors/tcs34725/src/tcs34725.c index e20ecde..e535512 100644 --- a/hw/drivers/sensors/tcs34725/src/tcs34725.c +++ b/hw/drivers/sensors/tcs34725/src/tcs34725.c @@ -91,10 +91,6 @@ static const struct sensor_driver g_tcs34725_sensor_driver = { tcs34725_sensor_get_config }; -uint8_t g_tcs34725_gain; -uint8_t g_tcs34725_integration_time; -uint8_t g_tcs34725_enabled; - /** * Writes a single byte to the specified register * @@ -332,8 +328,6 @@ tcs34725_enable(uint8_t enable) } } - g_tcs34725_enabled = enable; - return 0; err: return rc; @@ -399,12 +393,25 @@ err: /** * Indicates whether the sensor is enabled or not * - * @return 1 if enabled, 0 if disabled + * @param ptr to is_enabled variable + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_enable (void) +int +tcs34725_get_enable (uint8_t *is_enabled) { - return g_tcs34725_enabled; + int rc; + uint8_t tmp; + + rc = tcs34725_read8(TCS34725_REG_ENABLE, &tmp); + if (rc) { + goto err; + } + + *is_enabled = tmp; + + return 0; +err: + return rc; } /** @@ -418,14 +425,12 @@ tcs34725_set_integration_time(uint8_t int_time) { int rc; - rc = tcs34725_write8(TCS34725_REG_ATIME, - int_time | g_tcs34725_gain); + rc = tcs34725_write8(TCS34725_REG_ATIME, int_time); if (rc) { goto err; } - g_tcs34725_integration_time = int_time; - + return 0; err: return rc; } @@ -433,12 +438,23 @@ err: /** * Gets integration time set earlier * - * @return integration time + * @param ptr to integration time + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_integration_time(void) +int +tcs34725_get_integration_time(uint8_t *int_time) { - return g_tcs34725_integration_time; + int rc; + uint8_t tmp; + + rc = tcs34725_read8(TCS34725_REG_ATIME, &tmp); + if (rc) { + goto err; + } + + return 0; +err: + return rc; } /** @@ -458,14 +474,11 @@ tcs34725_set_gain(uint8_t gain) goto err; } - rc = tcs34725_write8(TCS34725_REG_CONTROL, - g_tcs34725_integration_time | gain); + rc = tcs34725_write8(TCS34725_REG_CONTROL, gain); if (rc) { goto err; } - g_tcs34725_gain = gain; - err: return rc; } @@ -473,12 +486,25 @@ err: /** * Get gain of the sensor * - * @return gain + * @param ptr to gain + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_gain(void) +int +tcs34725_get_gain(uint8_t *gain) { - return g_tcs34725_gain; + int rc; + uint8_t tmp; + + rc = tcs34725_read8(TCS34725_REG_CONTROL, &tmp); + if (rc) { + goto err; + } + + *gain = tmp; + + return 0; +err: + return rc; } /** http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fd20bc93/hw/drivers/sensors/tcs34725/src/tcs34725_priv.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tcs34725/src/tcs34725_priv.h b/hw/drivers/sensors/tcs34725/src/tcs34725_priv.h index 5d5448c..1dcfb13 100644 --- a/hw/drivers/sensors/tcs34725/src/tcs34725_priv.h +++ b/hw/drivers/sensors/tcs34725/src/tcs34725_priv.h @@ -179,10 +179,11 @@ tcs34725_get_chip_id(uint8_t *id); /** * Get gain of the sensor * - * @return gain + * @param ptr to gain + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_gain(void); +int +tcs34725_get_gain(uint8_t *gain); /** * Set gain of the sensor @@ -235,10 +236,11 @@ tcs34725_set_integration_time(uint8_t int_time); /** * Gets integration time set earlier * - * @return integration time + * @param ptr to integration time + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_integration_time(void); +int +tcs34725_get_integration_time(uint8_t *int_time); /** * @@ -253,10 +255,11 @@ tcs34725_enable(uint8_t enable); /** * Indicates whether the sensor is enabled or not * - * @return 1 if enabled, 0 if disabled + * @param ptr to is_enabled variable + * @return 0 on success, non-zero on failure */ -uint8_t -tcs34725_get_enable (void); +int +tcs34725_get_enable (uint8_t *is_enabled); /** * Reads the raw red, green, blue and clear channel values http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fd20bc93/hw/drivers/sensors/tcs34725/src/tcs34725_shell.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tcs34725/src/tcs34725_shell.c b/hw/drivers/sensors/tcs34725/src/tcs34725_shell.c index e15b4fc..d231d5f 100644 --- a/hw/drivers/sensors/tcs34725/src/tcs34725_shell.c +++ b/hw/drivers/sensors/tcs34725/src/tcs34725_shell.c @@ -27,9 +27,6 @@ #include "defs/error.h" #if MYNEWT_VAL(TCS34725_CLI) -extern uint8_t g_tcs34725_integration_time; -extern uint8_t g_tcs34725_gain; - static int tcs34725_shell_cmd(int argc, char **argv); static struct shell_cmd tcs34725_shell_cmd_struct = { @@ -121,9 +118,6 @@ tcs34725_shell_cmd_read(int argc, char **argv) while(samples--) { - tcs34725.cfg.gain = g_tcs34725_gain; - tcs34725.cfg.integration_time = g_tcs34725_integration_time; - rc = tcs34725_get_rawdata(&r, &g, &b, &c, &tcs34725); if (rc) { console_printf("Read failed: %d\n", rc); @@ -150,7 +144,10 @@ tcs34725_shell_cmd_gain(int argc, char **argv) /* Display the gain */ if (argc == 2) { - gain = tcs34725_get_gain(); + rc = tcs34725_get_gain(&gain); + if (rc) { + goto err; + } console_printf("\tgain [0: 1|1: 4|2: 16|3: 60]\n"); console_printf("%u\n", gain); } @@ -189,7 +186,11 @@ tcs34725_shell_cmd_time(int argc, char **argv) /* Display the integration time */ if (argc == 2) { - time = tcs34725_get_integration_time(); + rc = tcs34725_get_integration_time(&time); + if (rc) { + goto err; + } + switch (time) { case TCS34725_INTEGRATIONTIME_2_4MS: console_printf("2.4\n"); @@ -338,14 +339,22 @@ tcs34725_shell_cmd_en(int argc, char **argv) { char *endptr; long lval; + int rc; + uint8_t is_enabled; + rc = 0; if (argc > 3) { return tcs34725_shell_err_too_many_args(argv[1]); } /* Display current enable state */ if (argc == 2) { - console_printf("%u\n", tcs34725_get_enable()); + rc = tcs34725_get_enable(&is_enabled); + if (rc) { + console_printf("Cannot get enable state of the sensor\n"); + goto err; + } + console_printf("%u\n", is_enabled); } /* Update the enable state */ @@ -360,6 +369,8 @@ tcs34725_shell_cmd_en(int argc, char **argv) } return 0; +err: + return rc; } static int