Browse Source

sensorhub: added cli command

master
Franz Levin 3 months ago
parent
commit
219b0cead8
1 changed files with 86 additions and 11 deletions
  1. +86
    -11
      project/sensorhub.c

+ 86
- 11
project/sensorhub.c View File

@@ -10,6 +10,7 @@
#include "usb_uart.h"
#include "tasks.h"
#include "utils.h"
#include "cli.h"

#include "drivers/drivers.h"

@@ -27,8 +28,12 @@ static driver_t driver_sensors[] = {
.params.bmp280.sdo_high = false,
.params.bmp280.temp_oversampling = 1,
.params.bmp280.pressure_oversampling = 2, },
{ .bus = &driver_buses[0], .sensor = &sensor_bmp280,
.params.bmp280.sdo_high = true,
.params.bmp280.temp_oversampling = 1,
.params.bmp280.pressure_oversampling = 2, },
#endif
#if 1
#if 0
{ .bus = &driver_buses[0], .sensor = &sensor_htu21, },
#endif
};
@@ -38,12 +43,16 @@ static struct {
TaskHandle_t handle;
StackType_t stack[TASK_STACK_SENSORHUB];
StaticTask_t task;
StaticSemaphore_t mutex;
} task;
bool initialized;
} sensorhub_data;

static bool print_value(char* data, uint16_t* index, int i, const char* string, void* value, int size)
{
usb_uart_send_data((unsigned char*)string, strlen(string));
usb_uart_send_data((unsigned char*)".", 1);
usb_uart_send_data((unsigned char*)&"0123456789"[i], 1);
usb_uart_send_data((unsigned char*)": ", 2);
switch (size)
{
@@ -59,6 +68,8 @@ static bool print_value(char* data, uint16_t* index, int i, const char* string,
static bool print_string(char* data, uint16_t* index, int i, const char* string, const char* value)
{
usb_uart_send_data((unsigned char*)string, strlen(string));
usb_uart_send_data((unsigned char*)".", 1);
usb_uart_send_data((unsigned char*)&"0123456789"[i], 1);
usb_uart_send_data((unsigned char*)": ", 2);
usb_uart_send_data((unsigned char*)value, strlen(value));
usb_uart_send_data((unsigned char*)"\r\n", 2);
@@ -69,13 +80,6 @@ static void sensorhub_task(void* data)
{
int i;

driver_print_data_t print_data = {
.data = NULL,
.index = 0,
.print_value = print_value,
.print_string = print_string,
};

for (i = 0; i < ARRAYSIZE(driver_buses); i++)
{
driver_buses[i].bus->init(&driver_buses[i].params);
@@ -88,20 +92,91 @@ static void sensorhub_task(void* data)

while (true)
{
vTaskDelay(2000 / portTICK_PERIOD_MS);
usb_uart_send_data((unsigned char*)"sensorhub\r\n", 11);
BaseType_t res;

res = xSemaphoreTake(&sensorhub_data.task.mutex, portMAX_DELAY);
if (res == pdTRUE)
{
for (i = 0; i < ARRAYSIZE(driver_sensors); i++)
{
driver_sensors[i].sensor->read(&driver_sensors[i].params);
}
}
xSemaphoreGive(&sensorhub_data.task.mutex);

vTaskDelay(30000 / portTICK_PERIOD_MS);
}
}

static int sensor_command(int argc, char* const argv[], cli_print_t* print)
{
if (argc < 2)
{
print->printline("Too few arguments: ");
print->printline(argv[0]);
print->printline(" get|status");
print->printline(print->newline);
return 1;
}
else if (argc == 2 && strcmp(argv[1], "help") == 0)
{
print->printline("Get sensor status");
print->printline(print->newline);
}
else if (argc == 2 && strcmp(argv[1], "get") == 0)
{
int i;
BaseType_t res;

driver_print_data_t print_data = {
.data = NULL,
.index = 0,
.print_value = print_value,
.print_string = print_string,
};

res = xSemaphoreTake(&sensorhub_data.task.mutex, portMAX_DELAY);
if (res != pdTRUE)
{
print->printline("Failed to get mutex");
print->printline(print->newline);
return 1;
}

for (i = 0; i < ARRAYSIZE(driver_sensors); i++)
{
driver_sensors[i].sensor->read(&driver_sensors[i].params);
print_data.i = i;
driver_sensors[i].sensor->print(&driver_sensors[i].params, &print_data);
}

xSemaphoreGive(&sensorhub_data.task.mutex);
}

return 0;
}

int sensorhub_init(void)
{
SemaphoreHandle_t mutex;

static cli_command_t command_sensor = {
.name = "sensor",
.help = "get sensor state",
.function = sensor_command,
};

mutex = xSemaphoreCreateMutexStatic(&sensorhub_data.task.mutex);
if (mutex == NULL)
{
return 1;
}
xSemaphoreGive(&sensorhub_data.task.mutex);

cli_register_command(&command_sensor);

sensorhub_data.task.handle = xTaskCreateStatic(sensorhub_task, TASK_NAME_SENSORHUB, ARRAYSIZE(sensorhub_data.task.stack)/4, NULL, TASK_PRIO_SENSORHUB, sensorhub_data.task.stack, &sensorhub_data.task.task);

sensorhub_data.initialized = true;

return 0;
}

Loading…
Cancel
Save