diff --git a/NOTES.md b/NOTES.md index 49fbf67..f69e4ad 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,9 +1,11 @@ -## serial +## client +Prefer socat to support arrow keys ``` nc 192.168.1.154 7777 +socat -d -d -,raw,echo=0 TCP:192.168.1.154:7777 ``` -## socat +## server ``` socat -d -d /dev/ttyACM0,b115200,raw,echo=0 tcp-listen:7777,reuseaddr ``` diff --git a/prj.conf b/prj.conf index 3607be5..45a9875 100644 --- a/prj.conf +++ b/prj.conf @@ -20,6 +20,7 @@ CONFIG_UDC_BUF_POOL_SIZE=4096 # Shell CONFIG_SHELL=y CONFIG_USBD_SHELL=y +CONFIG_SHELL_HISTORY=y # Storage CONFIG_NVS=y diff --git a/src/debug_shell.c b/src/debug_shell.c index 30725d3..081b032 100644 --- a/src/debug_shell.c +++ b/src/debug_shell.c @@ -4,6 +4,7 @@ #include "mode.h" #include "led.h" #include "imu.h" +#include "light.h" #include #include @@ -110,6 +111,21 @@ SHELL_STATIC_SUBCMD_SET_CREATE(debug_imu_cmds, SHELL_SUBCMD_SET_END ); +/* --- debug light sensor --- */ + +static int cmd_dbg_light_read(const struct shell *sh, size_t argc, char **argv) +{ + struct light_data_t data; + light_read(&data); + shell_print(sh, "value=%d", data.value); + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(debug_light_cmds, + SHELL_CMD(read, NULL, "Read ambient light sensor value.", cmd_dbg_light_read), + SHELL_SUBCMD_SET_END +); + /* --- top-level debug command --- */ SHELL_STATIC_SUBCMD_SET_CREATE(debug_cmds, @@ -117,6 +133,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(debug_cmds, SHELL_CMD(led, &debug_led_cmds, "PWM LED bar control.", NULL), SHELL_CMD(leds, &debug_leds_cmds, "Addressable LED control.", NULL), SHELL_CMD(imu, &debug_imu_cmds, "IMU read.", NULL), + SHELL_CMD(light, &debug_light_cmds, "Ambient light sensor read.", NULL), SHELL_SUBCMD_SET_END ); diff --git a/src/light.c b/src/light.c new file mode 100644 index 0000000..6ab1058 --- /dev/null +++ b/src/light.c @@ -0,0 +1,46 @@ +#include "light.h" +#include "zbus_channels.h" + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(light, CONFIG_LOG_MAX_LEVEL); + +#define ALS_DATA_L 0x1E +#define DATA_STATUS 0x17 // 0b10000000 mask for data_ready + +static const uint8_t i2c_address = 0x38; + +static const struct device *const i2c_dev = DEVICE_DT_GET(DT_ALIAS(i2c_1)); + +int light_init(void) +{ + if (!device_is_ready(i2c_dev)) { + LOG_ERR("I2C device %s is not ready", i2c_dev->name); + return -ENODEV; + } + + const uint8_t RESET[2] = {0x00, 0b00000001}; // reset + const uint8_t ENABLE_ALS[2] = {0x00, 0b00000001}; // enable ALS + + i2c_write(i2c_dev, RESET, sizeof(RESET), i2c_address); k_sleep(K_MSEC(100)); + i2c_write(i2c_dev, ENABLE_ALS, sizeof(ENABLE_ALS), i2c_address); k_sleep(K_MSEC(100)); + + return 0; +} + +void light_read(struct light_data_t *buf) +{ + uint8_t raw[2]; + uint16_t reg = ALS_DATA_L; + + int ret = i2c_write_read(i2c_dev, i2c_address, ®, sizeof(reg), raw, sizeof(raw)); + if (ret) { + LOG_ERR("I2C burst read failed (%d)", ret); + return; + } + + buf->value = (int16_t)((raw[1] << 8) | raw[0]); +} \ No newline at end of file diff --git a/src/light.h b/src/light.h new file mode 100644 index 0000000..8098295 --- /dev/null +++ b/src/light.h @@ -0,0 +1,13 @@ +#ifndef LIGHT_H +#define LIGHT_H + +#include + +struct light_data_t { + uint16_t value; +}; + +int light_init(void); +void light_read(struct light_data_t *buf); + +#endif // LIGHT_H \ No newline at end of file diff --git a/src/main.c b/src/main.c index 665cb40..eba1c8c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "config.h" #include "imu.h" #include "mode.h" +#include "light.h" LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); @@ -37,6 +38,12 @@ int main(void) return ret; } + ret = light_init(); + if (ret != 0) { + LOG_ERR("Failed to init ambient light sensor: %d", ret); + return ret; + } + mode_init(); mode_start();