PeterBee97 commented on code in PR #1449: URL: https://github.com/apache/nuttx-apps/pull/1449#discussion_r1041756391
########## system/nxcamera/nxcamera_main.c: ########## @@ -0,0 +1,564 @@ +/**************************************************************************** + * apps/system/nxcamera/nxcamera_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/video/video.h> + +#include <sys/types.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <assert.h> + +#include "system/readline.h" +#include "system/nxcamera.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NXCAMERA_VER "1.00" + +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP +# define NXCAMERA_HELP_TEXT(x) x +#else +# define NXCAMERA_HELP_TEXT(x) +#endif + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +typedef int (*nxcamera_func)(FAR struct nxcamera_s *plooper, char *pargs); + +struct mp_cmd_s +{ + FAR const char *cmd; /* The command text */ + FAR const char *arghelp; /* Text describing the args */ + nxcamera_func pfunc; /* Pointer to command handler */ + FAR const char *help; /* The help text */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int nxcamera_cmd_quit(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_stream(FAR struct nxcamera_s *plooper, char *parg); + +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE +static int nxcamera_cmd_input(FAR struct nxcamera_s *plooper, char *parg); +#endif + +static int nxcamera_cmd_output(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_stop(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_set(FAR struct nxcamera_s *plooper, char *parg); + +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP +static int nxcamera_cmd_help(FAR struct nxcamera_s *plooper, char *parg); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct mp_cmd_s g_nxcamera_cmds[] = +{ +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE + { + "input", + "videodev", + nxcamera_cmd_input, + NXCAMERA_HELP_TEXT("Specify a preferred capture device") + }, +#endif + { + "output", + "fbdev|filename", + nxcamera_cmd_output, + NXCAMERA_HELP_TEXT("Specify a display device or filename") + }, +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP + { + "h", + "", + nxcamera_cmd_help, + NXCAMERA_HELP_TEXT("Display help for commands") + }, + { + "help", + "", + nxcamera_cmd_help, + NXCAMERA_HELP_TEXT("Display help for commands") + }, +#endif + { + "stream", + "width height framerate format", + nxcamera_cmd_stream, + NXCAMERA_HELP_TEXT("Video stream test (format is fourcc)") + }, + { + "stop", + "", + nxcamera_cmd_stop, + NXCAMERA_HELP_TEXT("Stop stream") + }, + { + "q", + "", + nxcamera_cmd_quit, + NXCAMERA_HELP_TEXT("Exit NxCamera") + }, + { + "quit", + "", + nxcamera_cmd_quit, + NXCAMERA_HELP_TEXT("Exit NxCamera") + }, + { + "set", + "parameter d%", + nxcamera_cmd_set, + NXCAMERA_HELP_TEXT("Set camera control parameters") + } +}; + +static const char *controls[] = +{ + "brightness", + "contrast", + "saturation", + "hue", + "while_balance_automatic", + "exposure", + "gain_automatic", + "gain", + "horizontal_flip", + "vertical_flip", + "power_line_frequency", + "sharpness", + "auto_exposure" +}; + +static const int g_nxcamera_cmd_count = sizeof(g_nxcamera_cmds) / + sizeof(struct mp_cmd_s); + +/**************************************************************************** + * Name: nxcamera_cmd_stream + * + * nxcamera_cmd_loop() play and record the raw data file using the nxcamera + * context. + * + ****************************************************************************/ + +static int nxcamera_cmd_stream(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + uint16_t width = 0; + uint16_t height = 0; + uint32_t framerate = 0; + uint32_t format = 0; + char cc[4] = + { + 0 + }; + + sscanf(parg, "%hd %hd %d %s", &width, &height, &framerate, cc); + format = v4l2_fourcc(cc[0], cc[1], cc[2], cc[3]); + + /* Try to stream raw data with settings specified */ + + ret = nxcamera_stream(plooper, width, height, framerate, format); + + /* nxcamera_stream returned values: + * + * OK Stream is being run + * -EBUSY The media device is busy + * -ENOSYS The video format is not unsupported + * -ENODEV No video device suitable to capture media + */ + + switch (-ret) + { + case OK: + break; + + case ENODEV: + printf("No suitable Video Device found\n"); + break; + + case EBUSY: + printf("Video device busy\n"); + break; + + case ENOSYS: + printf("Unknown video format\n"); + break; + + default: + printf("Error stream test: %d\n", -ret); + break; + } + + return ret; +} + +/**************************************************************************** + * Name: nxcamera_cmd_stop + * + * nxcamera_cmd_stop() stops stream context. + * + ****************************************************************************/ + +static int nxcamera_cmd_stop(FAR struct nxcamera_s *plooper, char *parg) +{ + /* Stop the stream */ + + return nxcamera_stop(plooper); +} + +/**************************************************************************** + * Name: nxcamera_cmd_set + * + * nxcamera_cmd_set() sets camera controls. + * + ****************************************************************************/ + +static int nxcamera_cmd_set(FAR struct nxcamera_s *plooper, char *parg) +{ + /* Resume the stream */ + + return nxcamera_set(plooper); +} + +/**************************************************************************** + * Name: nxcamera_cmd_input + * + * nxcamera_cmd_input() sets the preferred capture device for stream. + * + ****************************************************************************/ + +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE +static int nxcamera_cmd_input(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + char path[PATH_MAX]; + + /* First try to open the file directly */ + + ret = nxcamera_setdevice(plooper, parg); + if (ret == -ENOENT) + { + /* Append the /dev path and try again */ + + snprintf(path, sizeof(path), "/dev/%s", parg); + ret = nxcamera_setdevice(plooper, path); + } + + /* Test if the device file exists */ + + if (ret == -ENOENT) + { + /* Device doesn't exit. Report error */ + + printf("Device %s not found\n", parg); + return ret; + } + + /* Test if is is an video device */ + + if (ret == -ENODEV) + { + printf("Device %s is not an video device\n", parg); + return ret; + } + + if (ret < 0) + { + return ret; + } + + /* Device set successfully */ + + return OK; +} +#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */ + +/**************************************************************************** + * Name: nxcamera_cmd_output + * + * nxcamera_cmd_device() sets the output device/file for display/save. + * + ****************************************************************************/ + +static int nxcamera_cmd_output(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + char path[PATH_MAX]; + char *ext; + bool isimage; + + /* First try to open the device directly */ + + ret = nxcamera_setfb(plooper, parg); + if (ret == -ENOENT) + { + /* Append the /dev path and try again */ + + snprintf(path, sizeof(path), "/dev/%s", parg); + ret = nxcamera_setfb(plooper, path); + } + + /* Device doesn't exist or is not a video device. Treat as file */ + + if ((ret == -ENOENT) || (ret == -ENODEV)) + { + if (ret == -ENODEV) + { + printf("Device %s is not an video device\n", parg); + return ret; + } + + ext = strrchr(parg, '.'); + if (ext && (ext != parg)) + { + ext++; + isimage = (strncmp(ext, "jpg", sizeof("jpg")) == 0) + || (strncmp(ext, "jpeg", sizeof("jpeg")) == 0); + } + + ret = nxcamera_setfile(plooper, parg, isimage); + } + + if (ret < 0) + { + /* Create file error. Report error */ + + printf("Error outputting to %s\n", parg); + return ret; + } + + /* Output device or file set successfully */ + + return OK; +} + +/**************************************************************************** + * Name: nxcamera_cmd_quit + * + * nxcamera_cmd_quit() terminates the application. + * + ****************************************************************************/ + +static int nxcamera_cmd_quit(FAR struct nxcamera_s *plooper, char *parg) Review Comment: done ########## system/nxcamera/nxcamera_main.c: ########## @@ -0,0 +1,564 @@ +/**************************************************************************** + * apps/system/nxcamera/nxcamera_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/video/video.h> + +#include <sys/types.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <assert.h> + +#include "system/readline.h" +#include "system/nxcamera.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NXCAMERA_VER "1.00" + +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP +# define NXCAMERA_HELP_TEXT(x) x +#else +# define NXCAMERA_HELP_TEXT(x) +#endif + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +typedef int (*nxcamera_func)(FAR struct nxcamera_s *plooper, char *pargs); + +struct mp_cmd_s +{ + FAR const char *cmd; /* The command text */ + FAR const char *arghelp; /* Text describing the args */ + nxcamera_func pfunc; /* Pointer to command handler */ + FAR const char *help; /* The help text */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int nxcamera_cmd_quit(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_stream(FAR struct nxcamera_s *plooper, char *parg); + +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE +static int nxcamera_cmd_input(FAR struct nxcamera_s *plooper, char *parg); +#endif + +static int nxcamera_cmd_output(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_stop(FAR struct nxcamera_s *plooper, char *parg); +static int nxcamera_cmd_set(FAR struct nxcamera_s *plooper, char *parg); + +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP +static int nxcamera_cmd_help(FAR struct nxcamera_s *plooper, char *parg); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct mp_cmd_s g_nxcamera_cmds[] = +{ +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE + { + "input", + "videodev", + nxcamera_cmd_input, + NXCAMERA_HELP_TEXT("Specify a preferred capture device") + }, +#endif + { + "output", + "fbdev|filename", + nxcamera_cmd_output, + NXCAMERA_HELP_TEXT("Specify a display device or filename") + }, +#ifdef CONFIG_NXCAMERA_INCLUDE_HELP + { + "h", + "", + nxcamera_cmd_help, + NXCAMERA_HELP_TEXT("Display help for commands") + }, + { + "help", + "", + nxcamera_cmd_help, + NXCAMERA_HELP_TEXT("Display help for commands") + }, +#endif + { + "stream", + "width height framerate format", + nxcamera_cmd_stream, + NXCAMERA_HELP_TEXT("Video stream test (format is fourcc)") + }, + { + "stop", + "", + nxcamera_cmd_stop, + NXCAMERA_HELP_TEXT("Stop stream") + }, + { + "q", + "", + nxcamera_cmd_quit, + NXCAMERA_HELP_TEXT("Exit NxCamera") + }, + { + "quit", + "", + nxcamera_cmd_quit, + NXCAMERA_HELP_TEXT("Exit NxCamera") + }, + { + "set", + "parameter d%", + nxcamera_cmd_set, + NXCAMERA_HELP_TEXT("Set camera control parameters") + } +}; + +static const char *controls[] = +{ + "brightness", + "contrast", + "saturation", + "hue", + "while_balance_automatic", + "exposure", + "gain_automatic", + "gain", + "horizontal_flip", + "vertical_flip", + "power_line_frequency", + "sharpness", + "auto_exposure" +}; + +static const int g_nxcamera_cmd_count = sizeof(g_nxcamera_cmds) / + sizeof(struct mp_cmd_s); + +/**************************************************************************** + * Name: nxcamera_cmd_stream + * + * nxcamera_cmd_loop() play and record the raw data file using the nxcamera + * context. + * + ****************************************************************************/ + +static int nxcamera_cmd_stream(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + uint16_t width = 0; + uint16_t height = 0; + uint32_t framerate = 0; + uint32_t format = 0; + char cc[4] = + { + 0 + }; + + sscanf(parg, "%hd %hd %d %s", &width, &height, &framerate, cc); + format = v4l2_fourcc(cc[0], cc[1], cc[2], cc[3]); + + /* Try to stream raw data with settings specified */ + + ret = nxcamera_stream(plooper, width, height, framerate, format); + + /* nxcamera_stream returned values: + * + * OK Stream is being run + * -EBUSY The media device is busy + * -ENOSYS The video format is not unsupported + * -ENODEV No video device suitable to capture media + */ + + switch (-ret) + { + case OK: + break; + + case ENODEV: + printf("No suitable Video Device found\n"); + break; + + case EBUSY: + printf("Video device busy\n"); + break; + + case ENOSYS: + printf("Unknown video format\n"); + break; + + default: + printf("Error stream test: %d\n", -ret); + break; + } + + return ret; +} + +/**************************************************************************** + * Name: nxcamera_cmd_stop + * + * nxcamera_cmd_stop() stops stream context. + * + ****************************************************************************/ + +static int nxcamera_cmd_stop(FAR struct nxcamera_s *plooper, char *parg) +{ + /* Stop the stream */ + + return nxcamera_stop(plooper); +} + +/**************************************************************************** + * Name: nxcamera_cmd_set + * + * nxcamera_cmd_set() sets camera controls. + * + ****************************************************************************/ + +static int nxcamera_cmd_set(FAR struct nxcamera_s *plooper, char *parg) +{ + /* Resume the stream */ + + return nxcamera_set(plooper); +} + +/**************************************************************************** + * Name: nxcamera_cmd_input + * + * nxcamera_cmd_input() sets the preferred capture device for stream. + * + ****************************************************************************/ + +#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE +static int nxcamera_cmd_input(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + char path[PATH_MAX]; + + /* First try to open the file directly */ + + ret = nxcamera_setdevice(plooper, parg); + if (ret == -ENOENT) + { + /* Append the /dev path and try again */ + + snprintf(path, sizeof(path), "/dev/%s", parg); + ret = nxcamera_setdevice(plooper, path); + } + + /* Test if the device file exists */ + + if (ret == -ENOENT) + { + /* Device doesn't exit. Report error */ + + printf("Device %s not found\n", parg); + return ret; + } + + /* Test if is is an video device */ + + if (ret == -ENODEV) + { + printf("Device %s is not an video device\n", parg); + return ret; + } + + if (ret < 0) + { + return ret; + } + + /* Device set successfully */ + + return OK; +} +#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */ + +/**************************************************************************** + * Name: nxcamera_cmd_output + * + * nxcamera_cmd_device() sets the output device/file for display/save. + * + ****************************************************************************/ + +static int nxcamera_cmd_output(FAR struct nxcamera_s *plooper, char *parg) +{ + int ret; + char path[PATH_MAX]; + char *ext; + bool isimage; + + /* First try to open the device directly */ + + ret = nxcamera_setfb(plooper, parg); + if (ret == -ENOENT) + { + /* Append the /dev path and try again */ + + snprintf(path, sizeof(path), "/dev/%s", parg); + ret = nxcamera_setfb(plooper, path); + } + + /* Device doesn't exist or is not a video device. Treat as file */ + + if ((ret == -ENOENT) || (ret == -ENODEV)) + { + if (ret == -ENODEV) + { + printf("Device %s is not an video device\n", parg); + return ret; + } + + ext = strrchr(parg, '.'); + if (ext && (ext != parg)) + { + ext++; + isimage = (strncmp(ext, "jpg", sizeof("jpg")) == 0) + || (strncmp(ext, "jpeg", sizeof("jpeg")) == 0); + } + + ret = nxcamera_setfile(plooper, parg, isimage); + } + + if (ret < 0) + { + /* Create file error. Report error */ + + printf("Error outputting to %s\n", parg); + return ret; + } + + /* Output device or file set successfully */ + + return OK; +} + +/**************************************************************************** + * Name: nxcamera_cmd_quit + * + * nxcamera_cmd_quit() terminates the application. + * + ****************************************************************************/ + +static int nxcamera_cmd_quit(FAR struct nxcamera_s *plooper, char *parg) +{ + /* Stop the stream if any */ + +#ifndef CONFIG_VIDEO_EXCLUDE_STOP + return nxcamera_stop(plooper); +#endif + + return OK; Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org