Added light sensor
This commit is contained in:
@@ -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
|
||||
```
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "mode.h"
|
||||
#include "led.h"
|
||||
#include "imu.h"
|
||||
#include "light.h"
|
||||
|
||||
#include <zephyr/shell/shell.h>
|
||||
#include <stdlib.h>
|
||||
@@ -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
|
||||
);
|
||||
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
#include "light.h"
|
||||
#include "zbus_channels.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
|
||||
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]);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
struct light_data_t {
|
||||
uint16_t value;
|
||||
};
|
||||
|
||||
int light_init(void);
|
||||
void light_read(struct light_data_t *buf);
|
||||
|
||||
#endif // LIGHT_H
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user