Adding tsl2561 shell back
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/3559c73a Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/3559c73a Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/3559c73a Branch: refs/heads/develop Commit: 3559c73ac1a0de7fd34f9802e401fc8c6c48bfc2 Parents: 9f59c4f Author: Vipul Rahane <vipulrah...@apache.org> Authored: Fri Feb 3 15:25:53 2017 -0800 Committer: Vipul Rahane <vipulrah...@apache.org> Committed: Fri Feb 3 15:25:53 2017 -0800 ---------------------------------------------------------------------- apps/slinky/src/main.c | 7 ++ .../sensors/tsl2561/include/tsl2561/tsl2561.h | 5 ++ hw/drivers/sensors/tsl2561/src/tsl2561.c | 84 ++++++++++++++++++++ hw/drivers/sensors/tsl2561/src/tsl2561_priv.h | 5 -- hw/drivers/sensors/tsl2561/src/tsl2561_shell.c | 24 ++++-- 5 files changed, 112 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3559c73a/apps/slinky/src/main.c ---------------------------------------------------------------------- diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c index e87aa0e..718d690 100755 --- a/apps/slinky/src/main.c +++ b/apps/slinky/src/main.c @@ -47,6 +47,9 @@ #include <reboot/log_reboot.h> #include <os/os_time.h> #include <id/id.h> +#if MYNEWT_VAL(TSL2561_CLI) +#include <tsl2561/tsl2561.h> +#endif #ifdef ARCH_sim #include <mcu/mcu_sim.h> @@ -377,6 +380,10 @@ main(int argc, char **argv) } #endif +#if MYNEWT_VAL(TSL2561_CLI) + tsl2561_shell_init(); +#endif + config_sensor(); /* http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3559c73a/hw/drivers/sensors/tsl2561/include/tsl2561/tsl2561.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tsl2561/include/tsl2561/tsl2561.h b/hw/drivers/sensors/tsl2561/include/tsl2561/tsl2561.h index 2f093be..046967f 100644 --- a/hw/drivers/sensors/tsl2561/include/tsl2561/tsl2561.h +++ b/hw/drivers/sensors/tsl2561/include/tsl2561/tsl2561.h @@ -189,6 +189,11 @@ int tsl2561_clear_interrupt(void); int tsl2561_config(struct tsl2561 *, struct tsl2561_cfg *); +#if MYNEWT_VAL(TSL2561_CLI) +int tsl2561_shell_init(void); +#endif + + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3559c73a/hw/drivers/sensors/tsl2561/src/tsl2561.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tsl2561/src/tsl2561.c b/hw/drivers/sensors/tsl2561/src/tsl2561.c index 8f90718..c9f7531 100644 --- a/hw/drivers/sensors/tsl2561/src/tsl2561.c +++ b/hw/drivers/sensors/tsl2561/src/tsl2561.c @@ -58,6 +58,13 @@ /* ToDo: Add timer based polling in bg and data ready callback (os_event?) */ /* ToDo: Move values to struct incl. address to allow multiple instances */ +#if MYNEWT_VAL(TSL2561_CLI) +uint8_t g_tsl2561_gain; +uint8_t g_tsl2561_integration_time; +uint8_t g_tsl2561_enabled; +#endif + + #if MYNEWT_VAL(TSL2561_STATS) /* Define the stats section and records */ STATS_SECT_START(tsl2561_stat_section) @@ -217,6 +224,83 @@ err: return rc; } +#if MYNEWT_VAL(TSL2561_CLI) +int +tsl2561_enable(uint8_t state) +{ + int rc; + + /* Enable the device by setting the control bit to 0x03 */ + rc = tsl2561_write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, + state ? TSL2561_CONTROL_POWERON : + TSL2561_CONTROL_POWEROFF); + if (!rc) { + g_tsl2561_enabled = state ? 1 : 0; + } + + return rc; +} + +uint8_t +tsl2561_get_enable (void) +{ + return g_tsl2561_enabled; +} + +int +tsl2561_set_integration_time(uint8_t int_time) +{ + int rc; + + rc = tsl2561_write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, + g_tsl2561_integration_time | g_tsl2561_gain); + if (rc) { + goto error; + } + + g_tsl2561_integration_time = int_time; + +error: + return rc; +} + +uint8_t +tsl2561_get_integration_time(void) +{ + return g_tsl2561_integration_time; +} + +int +tsl2561_set_gain(uint8_t gain) +{ + int rc; + + if ((gain != TSL2561_LIGHT_GAIN_1X) && (gain != TSL2561_LIGHT_GAIN_16X)) { + TSL2561_ERR("Invalid gain value\n"); + rc = EINVAL; + goto error; + } + + rc = tsl2561_write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, + g_tsl2561_integration_time | gain); + if (rc) { + goto error; + } + + g_tsl2561_gain = gain; + +error: + return rc; +} + +uint8_t +tsl2561_get_gain(void) +{ + return g_tsl2561_gain; +} +#endif + + int tsl2561_get_data(uint16_t *broadband, uint16_t *ir, struct tsl2561 *tsl2561) { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3559c73a/hw/drivers/sensors/tsl2561/src/tsl2561_priv.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tsl2561/src/tsl2561_priv.h b/hw/drivers/sensors/tsl2561/src/tsl2561_priv.h index 7d92d5f..7cbe66d 100644 --- a/hw/drivers/sensors/tsl2561/src/tsl2561_priv.h +++ b/hw/drivers/sensors/tsl2561/src/tsl2561_priv.h @@ -138,11 +138,6 @@ int tsl2561_write16(uint8_t reg, uint16_t value); int tsl2561_read8(uint8_t reg, uint8_t *value); int tsl2561_read16(uint8_t reg, uint16_t *value); -/* tsl2561_shell.c */ -#if MYNEWT_VAL(TSL2561_CLI) -int tsl2561_shell_init(void); -#endif - #ifdef __cplusplus } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3559c73a/hw/drivers/sensors/tsl2561/src/tsl2561_shell.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/tsl2561/src/tsl2561_shell.c b/hw/drivers/sensors/tsl2561/src/tsl2561_shell.c index c485261..9d6720e 100644 --- a/hw/drivers/sensors/tsl2561/src/tsl2561_shell.c +++ b/hw/drivers/sensors/tsl2561/src/tsl2561_shell.c @@ -44,6 +44,9 @@ #include "tsl2561_priv.h" #if MYNEWT_VAL(TSL2561_CLI) +extern uint8_t g_tsl2561_integration_time; +extern uint8_t g_tsl2561_gain; + static int tsl2561_shell_cmd(int argc, char **argv); static struct shell_cmd tsl2561_shell_cmd_struct = { @@ -127,6 +130,7 @@ tsl2561_shell_cmd_read(int argc, char **argv) uint16_t samples = 1; long val; int rc; + struct tsl2561 tsl2561; if (argc > 3) { return tsl2561_shell_err_too_many_args(argv[1]); @@ -141,7 +145,11 @@ tsl2561_shell_cmd_read(int argc, char **argv) } while(samples--) { - rc = tsl2561_get_data(&full, &ir); + + tsl2561.cfg.gain = g_tsl2561_gain; + tsl2561.cfg.integration_time = g_tsl2561_integration_time; + + rc = tsl2561_get_data(&full, &ir, &tsl2561); if (rc != 0) { console_printf("Read failed: %d\n", rc); return rc; @@ -177,7 +185,7 @@ tsl2561_shell_cmd_gain(int argc, char **argv) if ((val != 1) && (val != 16)) { return tsl2561_shell_err_invalid_arg(argv[2]); } - tsl2561_set_gain(val ? TSL2561_GAIN_16X : TSL2561_GAIN_1X); + tsl2561_set_gain(val ? TSL2561_LIGHT_GAIN_16X : TSL2561_LIGHT_GAIN_1X); } return 0; @@ -197,13 +205,13 @@ tsl2561_shell_cmd_time(int argc, char **argv) if (argc == 2) { time = tsl2561_get_integration_time(); switch (time) { - case TSL2561_INTEGRATIONTIME_13MS: + case TSL2561_LIGHT_ITIME_13MS: console_printf("13\n"); break; - case TSL2561_INTEGRATIONTIME_101MS: + case TSL2561_LIGHT_ITIME_101MS: console_printf("101\n"); break; - case TSL2561_INTEGRATIONTIME_402MS: + case TSL2561_LIGHT_ITIME_402MS: console_printf("402\n"); break; } @@ -220,13 +228,13 @@ tsl2561_shell_cmd_time(int argc, char **argv) } switch(val) { case 13: - tsl2561_set_integration_time(TSL2561_INTEGRATIONTIME_13MS); + tsl2561_set_integration_time(TSL2561_LIGHT_ITIME_13MS); break; case 101: - tsl2561_set_integration_time(TSL2561_INTEGRATIONTIME_101MS); + tsl2561_set_integration_time(TSL2561_LIGHT_ITIME_101MS); break; case 402: - tsl2561_set_integration_time(TSL2561_INTEGRATIONTIME_402MS); + tsl2561_set_integration_time(TSL2561_LIGHT_ITIME_402MS); break; } }