You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2021/09/16 08:45:43 UTC

[incubator-nuttx-apps] branch master updated: examples/camera: Fix some issues and update license

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new d8757a6  examples/camera: Fix some issues and update license
d8757a6 is described below

commit d8757a6bcf92aea2c06f9015604952e9728aec15
Author: SPRESENSE <41...@users.noreply.github.com>
AuthorDate: Thu Sep 16 11:03:33 2021 +0900

    examples/camera: Fix some issues and update license
    
    Fix some issues and update license to apache.
    - Fails to open the video driver.
    - Change format from YUV422 to RGB565,
      and add captured image output to LCD.
    - An application cannot exit by infinite loop.
    - In video-only mode without capturing still images,
      do not allocate capture memory for still images to
      reduce the memory usage.
---
 examples/camera/Make.defs         |  41 ++---
 examples/camera/Makefile          |  45 ++---
 examples/camera/README.md         |  28 ++--
 examples/camera/camera_bkgd.c     | 339 ++++++++++++++++++++++++++++++++++++++
 examples/camera/camera_bkgd.h     |  38 +++++
 examples/camera/camera_fileutil.c |  41 ++---
 examples/camera/camera_fileutil.h |  41 ++---
 examples/camera/camera_main.c     | 306 ++++++++++++++++++++++++----------
 8 files changed, 663 insertions(+), 216 deletions(-)

diff --git a/examples/camera/Make.defs b/examples/camera/Make.defs
index 94ad598..d2507a9 100644
--- a/examples/camera/Make.defs
+++ b/examples/camera/Make.defs
@@ -1,35 +1,20 @@
 ############################################################################
-# examples/camera/Make.defs
+# apps/examples/camera/Make.defs
 #
-#   Copyright 2018 Sony Semiconductor Solutions Corporation
+# 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
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+#   http://www.apache.org/licenses/LICENSE-2.0
 #
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in
-#    the documentation and/or other materials provided with the
-#    distribution.
-# 3. Neither the name of Sony Semiconductor Solutions Corporation nor
-#    the names of its contributors may be used to endorse or promote
-#    products derived from this software without specific prior written
-#    permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# 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.
 #
 ############################################################################
 
diff --git a/examples/camera/Makefile b/examples/camera/Makefile
index a3110bd..946f304 100644
--- a/examples/camera/Makefile
+++ b/examples/camera/Makefile
@@ -1,35 +1,20 @@
 ############################################################################
-# examples/camera/Makefile
+# apps/examples/camera/Makefile
 #
-#   Copyright 2018, 2020 Sony Semiconductor Solutions Corporation
+# 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
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+#   http://www.apache.org/licenses/LICENSE-2.0
 #
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in
-#    the documentation and/or other materials provided with the
-#    distribution.
-# 3. Neither the name of Sony Semiconductor Solutions Corporation nor
-#    the names of its contributors may be used to endorse or promote
-#    products derived from this software without specific prior written
-#    permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# 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.
 #
 ############################################################################
 
@@ -44,6 +29,10 @@ MODULE    = $(CONFIG_EXAMPLES_CAMERA)
 
 # camera Example
 
+ifeq ($(CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD),y)
+CSRCS += camera_bkgd.c
+endif
+
 CSRCS += camera_fileutil.c
 MAINSRC = camera_main.c
 
diff --git a/examples/camera/README.md b/examples/camera/README.md
index 42909da..309f6cc 100644
--- a/examples/camera/README.md
+++ b/examples/camera/README.md
@@ -7,8 +7,8 @@ the command is as below.
 nsh> camera ([-jpg]) ([capture num])
 
   -jpg        : this option is set for storing JPEG file into a strage.
-              : If this option isn't set capturing raw YUV422 data in a file.
-              : raw YUV422 is default.
+              : If this option isn't set capturing raw RGB565 data in a file.
+              : raw RGB565 is default.
 
   capture num : this option instructs number of taking pictures.
               : 10 is default.
@@ -22,20 +22,20 @@ Execution example:
 nsh> camera
 nximage_listener: Connected
 nximage_initialize: Screen resolution (320,240)
-Take 10 pictures as YUV file in /mnt/sd0 after 5000 mili-seconds.
-After finishing taking pictures, this app will be finished after 10000 mili-seconds.
+Take 10 pictures as RGB file in /mnt/sd0 after 5 seconds.
+After finishing taking pictures, this app will be finished after 10 seconds.
 Expier time is pasted.
 Start capturing...
-FILENAME:/mnt/sd0/VIDEO001.YUV
-FILENAME:/mnt/sd0/VIDEO002.YUV
-FILENAME:/mnt/sd0/VIDEO003.YUV
-FILENAME:/mnt/sd0/VIDEO004.YUV
-FILENAME:/mnt/sd0/VIDEO005.YUV
-FILENAME:/mnt/sd0/VIDEO006.YUV
-FILENAME:/mnt/sd0/VIDEO007.YUV
-FILENAME:/mnt/sd0/VIDEO008.YUV
-FILENAME:/mnt/sd0/VIDEO009.YUV
-FILENAME:/mnt/sd0/VIDEO010.YUV
+FILENAME:/mnt/sd0/VIDEO001.RGB
+FILENAME:/mnt/sd0/VIDEO002.RGB
+FILENAME:/mnt/sd0/VIDEO003.RGB
+FILENAME:/mnt/sd0/VIDEO004.RGB
+FILENAME:/mnt/sd0/VIDEO005.RGB
+FILENAME:/mnt/sd0/VIDEO006.RGB
+FILENAME:/mnt/sd0/VIDEO007.RGB
+FILENAME:/mnt/sd0/VIDEO008.RGB
+FILENAME:/mnt/sd0/VIDEO009.RGB
+FILENAME:/mnt/sd0/VIDEO010.RGB
 Finished capturing...
 Expier time is pasted.
 nximage_listener: Lost server connection: 117
diff --git a/examples/camera/camera_bkgd.c b/examples/camera/camera_bkgd.c
new file mode 100644
index 0000000..3fa80f3
--- /dev/null
+++ b/examples/camera/camera_bkgd.c
@@ -0,0 +1,339 @@
+/****************************************************************************
+ * apps/examples/camera/camera_bkgd.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 <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <semaphore.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <arch/board/board.h>
+#include <nuttx/nx/nx.h>
+#include <nuttx/nx/nxglib.h>
+
+#include "camera_bkgd.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct nximage_data_s
+{
+  /* The NX handles */
+
+  NXHANDLE hnx;
+  NXHANDLE hbkgd;
+  bool     connected;
+
+  /* The screen resolution */
+
+  nxgl_coord_t xres;
+  nxgl_coord_t yres;
+
+  volatile bool havepos;
+  sem_t sem;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void nximage_redraw(NXWINDOW hwnd,
+                           FAR const struct nxgl_rect_s *rect,
+                           bool more,
+                           FAR void *arg);
+static void nximage_position(NXWINDOW hwnd,
+                             FAR const struct nxgl_size_s *size,
+                             FAR const struct nxgl_point_s *pos,
+                             FAR const struct nxgl_rect_s *bounds,
+                             FAR void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Background window call table */
+
+static const struct nx_callback_s g_nximagecb =
+{
+  nximage_redraw,   /* redraw */
+  nximage_position  /* position */
+#ifdef CONFIG_NX_XYINPUT
+  , NULL            /* mousein */
+#endif
+#ifdef CONFIG_NX_KBD
+  , NULL            /* my kbdin */
+#endif
+};
+
+/* To handle nx context, below variable is defined for this application. */
+
+static struct nximage_data_s g_nximage =
+{
+  NULL,          /* hnx */
+  NULL,          /* hbkgd */
+  false,         /* connected */
+  0,             /* xres */
+  0,             /* yres */
+  false,         /* havpos */
+  { 0 },         /* sem */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nximage_listener()
+ *
+ * Description:
+ *   NX event listener for an event from NX server.
+ ****************************************************************************/
+
+FAR void *nximage_listener(FAR void *arg)
+{
+  int ret;
+
+  /* Process events forever */
+
+  for (; ; )
+    {
+      /* Handle the next event.  If we were configured blocking, then
+       * we will stay right here until the next event is received.  Since
+       * we have dedicated a while thread to servicing events, it would
+       * be most natural to also select CONFIG_NX_BLOCKING -- if not, the
+       * following would be a tight infinite loop (unless we added addition
+       * logic with nx_eventnotify and sigwait to pace it).
+       */
+
+      ret = nx_eventhandler(g_nximage.hnx);
+      if (ret < 0)
+        {
+          /* An error occurred... assume that we have lost connection with
+           * the server.
+           */
+
+          printf("nximage_listener: Lost server connection: %d\n", errno);
+          exit(EXIT_FAILURE);
+        }
+
+      /* If we received a message, we must be connected */
+
+      if (!g_nximage.connected)
+        {
+          g_nximage.connected = true;
+          sem_post(&g_nximage.sem);
+          printf("nximage_listener: Connected\n");
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: nximage_redraw
+ *
+ * Description:
+ *   NX re-draw handler
+ *
+ ****************************************************************************/
+
+static void nximage_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
+                        bool more, FAR void *arg)
+{
+  ginfo("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
+         hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
+         more ? "true" : "false");
+}
+
+/****************************************************************************
+ * Name: nximage_position
+ *
+ * Description:
+ *   NX position change handler
+ *
+ ****************************************************************************/
+
+static void nximage_position(NXWINDOW hwnd,
+                             FAR const struct nxgl_size_s *size,
+                             FAR const struct nxgl_point_s *pos,
+                             FAR const struct nxgl_rect_s *bounds,
+                             FAR void *arg)
+{
+  /* Report the position */
+
+  ginfo("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
+        hwnd, size->w, size->h, pos->x, pos->y,
+        bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
+
+  /* Have we picked off the window bounds yet? */
+
+  if (!g_nximage.havepos)
+    {
+      /* Save the background window handle */
+
+      g_nximage.hbkgd = hwnd;
+
+      /* Save the window limits */
+
+      g_nximage.xres = bounds->pt2.x + 1;
+      g_nximage.yres = bounds->pt2.y + 1;
+
+      g_nximage.havepos = true;
+      sem_post(&g_nximage.sem);
+      ginfo("Have xres=%d yres=%d\n", g_nximage.xres, g_nximage.yres);
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nximage_initialize
+ *
+ * Description:
+ *   Initialize NX graphics subsystem.
+ *
+ ****************************************************************************/
+
+int nximage_initialize(void)
+{
+  nxgl_mxpixel_t color;
+  pthread_t thread;
+  int ret;
+
+  /* Start the NX server kernel thread */
+
+  ret = boardctl(BOARDIOC_NX_START, 0);
+  if (ret < 0)
+    {
+      printf("nximage_initialize: Failed to start the NX server: %d\n",
+             errno);
+      return ERROR;
+    }
+
+  /* Connect to the server */
+
+  g_nximage.hnx = nx_connect();
+  if (!g_nximage.hnx)
+    {
+      printf("nximage_initialize: nx_connect failed: %d\n", errno);
+      return ERROR;
+    }
+
+  /* Start a separate thread to listen for server events.
+   * For simplicity, use defaul thread attribute.
+   */
+
+  ret = pthread_create(&thread, NULL, nximage_listener, NULL);
+  if (ret != 0)
+    {
+      printf("nximage_initialize: pthread_create failed: %d\n", ret);
+      return ERROR;
+    }
+
+  /* Don't return until we are connected to the server */
+
+  while (!g_nximage.connected)
+    {
+      /* Wait for the listener thread to wake us up when we really
+       * are connected.
+       */
+
+      (void)sem_wait(&g_nximage.sem);
+    }
+
+  /* Set background color to black */
+
+  color = 0;
+  nx_setbgcolor(g_nximage.hnx, &color);
+  ret = nx_requestbkgd(g_nximage.hnx, &g_nximagecb, NULL);
+  if (ret < 0)
+    {
+      printf("nximage_initialize: nx_requestbkgd failed: %d\n", errno);
+      nx_disconnect(g_nximage.hnx);
+      return ERROR;
+    }
+
+  while (!g_nximage.havepos)
+    {
+      (void) sem_wait(&g_nximage.sem);
+    }
+
+  printf("nximage_initialize: Screen resolution (%d,%d)\n",
+         g_nximage.xres, g_nximage.yres);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: nximage_image
+ *
+ * Description:
+ *   Put the NuttX logo in the center of the display.
+ *
+ ****************************************************************************/
+
+void nximage_draw(FAR void *image, int w, int h)
+{
+  FAR struct nxgl_point_s origin;
+  FAR struct nxgl_rect_s dest;
+  FAR const void *src[CONFIG_NX_NPLANES];
+  int ret;
+
+  origin.x = 0;
+  origin.y = 0;
+
+  /* Set up the destination to whole LCD screen */
+
+  dest.pt1.x = 0;
+  dest.pt1.y = 0;
+  dest.pt2.x = g_nximage.xres - 1;
+  dest.pt2.y = g_nximage.yres - 1;
+
+  src[0] = image;
+
+  ret = nx_bitmap((NXWINDOW)g_nximage.hbkgd, &dest, src, &origin,
+                  g_nximage.xres * sizeof(nxgl_mxpixel_t));
+  if (ret < 0)
+    {
+      printf("nximage_image: nx_bitmapwindow failed: %d\n", errno);
+    }
+}
+
+/****************************************************************************
+ * Name: nximage_finalize()
+ *
+ * Description:
+ *   Finalize NX server.
+ ****************************************************************************/
+
+void nximage_finalize(void)
+{
+  nx_disconnect(g_nximage.hnx);
+}
+
diff --git a/examples/camera/camera_bkgd.h b/examples/camera/camera_bkgd.h
new file mode 100644
index 0000000..7d56aad
--- /dev/null
+++ b/examples/camera/camera_bkgd.h
@@ -0,0 +1,38 @@
+/****************************************************************************
+ * apps/examples/camera/camera_bkgd.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __APPS_EXAMPLES_CAMERA_CAMERA_BKGD_H
+#define __APPS_EXAMPLES_CAMERA_CAMERA_BKGD_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+int nximage_initialize(void);                      /* Initialize NX graphics subsystem */
+void nximage_draw(FAR void *image, int w, int h);  /* Draw image onto Background window */
+void nximage_finalize(void);                       /* Finalize NX graphics subsystem */
+
+#endif /* __APPS_EXAMPLES_CAMERA_CAMERA_BKGD_H */
diff --git a/examples/camera/camera_fileutil.c b/examples/camera/camera_fileutil.c
index 0698e2d..743b0c7 100644
--- a/examples/camera/camera_fileutil.c
+++ b/examples/camera/camera_fileutil.c
@@ -1,35 +1,20 @@
 /****************************************************************************
- * camera/camera_fileutil.c
+ * apps/examples/camera/camera_fileutil.c
  *
- *   Copyright 2020 Sony Semiconductor Solutions Corporation
+ * 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
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
- *    the names of its contributors may be used to endorse or promote
- *    products derived from this software without specific prior written
- *    permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * 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.
  *
  ****************************************************************************/
 
diff --git a/examples/camera/camera_fileutil.h b/examples/camera/camera_fileutil.h
index 8dd0293..0340f95 100644
--- a/examples/camera/camera_fileutil.h
+++ b/examples/camera/camera_fileutil.h
@@ -1,35 +1,20 @@
 /****************************************************************************
- * camera/camera_fileutil.h
+ * apps/examples/camera/camera_fileutil.h
  *
- *   Copyright 2020 Sony Semiconductor Solutions Corporation
+ * 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
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
- *    the names of its contributors may be used to endorse or promote
- *    products derived from this software without specific prior written
- *    permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * 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.
  *
  ****************************************************************************/
 
diff --git a/examples/camera/camera_main.c b/examples/camera/camera_main.c
index 0da36de..288f617 100644
--- a/examples/camera/camera_main.c
+++ b/examples/camera/camera_main.c
@@ -1,35 +1,20 @@
 /****************************************************************************
- * camera/camera_main.c
+ * apps/examples/camera/camera_main.c
  *
- *   Copyright 2018, 2020 Sony Semiconductor Solutions Corporation
+ * 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
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
- *    the names of its contributors may be used to endorse or promote
- *    products derived from this software without specific prior written
- *    permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * 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.
  *
  ****************************************************************************/
 
@@ -52,12 +37,18 @@
 
 #include "camera_fileutil.h"
 
+#ifdef CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD
+#include <nuttx/nx/nx.h>
+#include <nuttx/nx/nxglib.h>
+#include "camera_bkgd.h"
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
 #define IMAGE_JPG_SIZE     (512*1024)  /* 512kB for FullHD Jpeg file. */
-#define IMAGE_YUV_SIZE     (320*240*2) /* QVGA YUV422 */
+#define IMAGE_RGB_SIZE     (320*240*2) /* QVGA RGB565 */
 
 #define VIDEO_BUFNUM       (3)
 #define STILL_BUFNUM       (1)
@@ -65,16 +56,8 @@
 #define MAX_CAPTURE_NUM     (100)
 #define DEFAULT_CAPTURE_NUM (10)
 
-#define START_CAPTURE_TIME  (5000)   /* mili-seconds */
-#define KEEP_VIDEO_TIME     (10000)  /* mili-seconds */
-
-#define RESET_INITIAL_TIME(t) gettimeofday(&(t), NULL)
-#define UPDATE_DIFF_TIME(d, then, now) \
-    { \
-      gettimeofday(&(now), NULL); \
-      (d) = ((((now).tv_sec - (then).tv_sec) * 1000) \
-                   + (((now).tv_usec - (then).tv_usec) / 1000)); \
-    }
+#define START_CAPTURE_TIME  (5)   /* seconds */
+#define KEEP_VIDEO_TIME     (10)  /* seconds */
 
 #define APP_STATE_BEFORE_CAPTURE  (0)
 #define APP_STATE_UNDER_CAPTURE   (1)
@@ -463,8 +446,13 @@ int main(int argc, FAR char *argv[])
   enum v4l2_buf_type capture_type = V4L2_BUF_TYPE_STILL_CAPTURE;
   struct v4l2_buffer v4l2_buf;
   const char *save_dir;
+  int is_eternal;
+  int app_state;
 
-  struct timeval then;
+  struct timeval start;
+  struct timeval now;
+  struct timeval delta;
+  struct timeval wait;
 
   struct v_buffer *buffers_video = NULL;
   struct v_buffer *buffers_still = NULL;
@@ -480,10 +468,30 @@ int main(int argc, FAR char *argv[])
 
   /* =====  Initialization Code  ===== */
 
+  /* Initialize NX graphics subsystem to use LCD */
+
+#ifdef CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD
+  ret = nximage_initialize();
+  if (ret < 0)
+    {
+      printf("camera_main: Failed to get NX handle: %d\n", errno);
+      return ERROR;
+    }
+#endif
+
   /* Select storage to save image files */
 
   save_dir = futil_initialize();
 
+  /* Initialize video driver to create a device file */
+
+  ret = video_initialize("/dev/video");
+  if (ret != 0)
+    {
+      printf("ERROR: Failed to initialize video: errno = %d\n", errno);
+      goto exit_without_cleaning_videodriver;
+    }
+
   /* Open the device file. */
 
   v_fd = open("/dev/video", 0);
@@ -507,13 +515,16 @@ int main(int argc, FAR char *argv[])
    * And all allocated memorys are VIDIOC_QBUFed.
    */
 
-  ret = camera_prepare(v_fd, V4L2_BUF_TYPE_STILL_CAPTURE,
-                       V4L2_BUF_MODE_FIFO, V4L2_PIX_FMT_JPEG,
-                       VIDEO_HSIZE_FULLHD, VIDEO_VSIZE_FULLHD,
-                       &buffers_still, STILL_BUFNUM, IMAGE_JPG_SIZE);
-  if (ret != OK)
+  if (capture_num != 0)
     {
-      goto exit_this_app;
+      ret = camera_prepare(v_fd, V4L2_BUF_TYPE_STILL_CAPTURE,
+                           V4L2_BUF_MODE_FIFO, V4L2_PIX_FMT_JPEG,
+                           VIDEO_HSIZE_FULLHD, VIDEO_VSIZE_FULLHD,
+                           &buffers_still, STILL_BUFNUM, IMAGE_JPG_SIZE);
+      if (ret != OK)
+        {
+          goto exit_this_app;
+        }
     }
 
   /* Prepare for VIDEO_CAPTURE stream.
@@ -525,74 +536,182 @@ int main(int argc, FAR char *argv[])
    * order from the captured frame buffer and a new camera image is
    * recaptured.
    *
-   * Allocate freame buffers for QVGA YUV422 size (320x240x2=150KB).
+   * Allocate freame buffers for QVGA RGB565 size (320x240x2=150KB).
    * Number of frame buffers is defined as VIDEO_BUFNUM(3).
    * And all allocated memorys are VIDIOC_QBUFed.
    */
 
   ret = camera_prepare(v_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE,
-                       V4L2_BUF_MODE_RING, V4L2_PIX_FMT_UYVY,
+                       V4L2_BUF_MODE_RING, V4L2_PIX_FMT_RGB565,
                        VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA,
-                       &buffers_video, VIDEO_BUFNUM, IMAGE_YUV_SIZE);
+                       &buffers_video, VIDEO_BUFNUM, IMAGE_RGB_SIZE);
   if (ret != OK)
     {
       goto exit_this_app;
     }
 
-  printf("Take %d pictures as %s file in %s after %d mili-seconds.\n",
-         capture_num,
-         (capture_type == V4L2_BUF_TYPE_STILL_CAPTURE) ? "JPEG" : "YUV",
-         save_dir, START_CAPTURE_TIME);
-  printf(" After taking pictures, the app will be exit after %d ms.\n",
-      KEEP_VIDEO_TIME);
+  /* This application has 3 states.
+   *
+   * APP_STATE_BEFORE_CAPTURE:
+   *    This state waits 5 seconds (definded START_CAPTURE_TIME)
+   *    with displaying preview (VIDEO_CAPTURE stream image) on LCD.
+   *    After 5 seconds, state will be changed to APP_STATE_UNDER_CAPTURE.
+   *
+   * APP_STATE_UNDER_CAPTURE:
+   *    This state will start taking picture and store the image into files.
+   *    Number of taking pictures is set capture_num valiable.
+   *    It can be changed by command line argument.
+   *    After finishing taking pictures, the state will be changed to
+   *    APP_STATE_AFTER_CAPTURE.
+   *
+   * APP_STATE_AFTER_CAPTURE:
+   *    This state waits 10 seconds (definded KEEP_VIDEO_TIME)
+   *    with displaying preview (VIDEO_CAPTURE stream image) on LCD.
+   *    After 10 seconds, this application will be finished.
+   *
+   * Notice:
+   *    If capture_num is set '0', state will stay APP_STATE_BEFORE_CAPTURE.
+   */
 
-  RESET_INITIAL_TIME(then);
+  app_state = APP_STATE_BEFORE_CAPTURE;
 
-  /* =====  Main Loop  ===== */
+  /* Show this application behavior. */
 
-  while (1)
+  if (capture_num == 0)
     {
-      printf("Start capturing...\n");
-      ret = start_stillcapture(v_fd, capture_type);
-      if (ret != OK)
-        {
-          goto exit_this_app;
-        }
-
-      while (capture_num)
-        {
-          ret = get_camimage(v_fd, &v4l2_buf, capture_type);
-          if (ret != OK)
-            {
-              goto exit_this_app;
-            }
-
-          futil_writeimage((uint8_t *)v4l2_buf.m.userptr,
-           (size_t)v4l2_buf.bytesused,
-           (capture_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ? "YUV" : "JPG");
+      is_eternal = 1;
+      printf("Start video this mode is eternal."
+             " (Non stop, non save files.)\n");
+#ifndef CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD
+      printf("This mode should be run with LCD display\n");
+#endif
+    }
+  else
+    {
+      is_eternal = 0;
+      wait.tv_sec = START_CAPTURE_TIME;
+      wait.tv_usec = 0;
+      printf("Take %d pictures as %s file in %s after %d seconds.\n",
+             capture_num,
+              (capture_type == V4L2_BUF_TYPE_STILL_CAPTURE) ? "JPEG" : "RGB",
+             save_dir, START_CAPTURE_TIME);
+      printf(" After finishing taking pictures,"
+             " this app will be finished after %d seconds.\n",
+              KEEP_VIDEO_TIME);
+    }
 
-          ret = release_camimage(v_fd, &v4l2_buf);
-          if (ret != OK)
-            {
-              goto exit_this_app;
-            }
+  gettimeofday(&start, NULL);
 
-          capture_num--;
-        }
+  /* =====  Main Loop  ===== */
 
-      ret = stop_stillcapture(v_fd, capture_type);
-      if (ret != OK)
+  while (1)
+    {
+      switch (app_state)
         {
-          goto exit_this_app;
+          /* BEFORE_CAPTURE and AFTER_CAPTURE is waiting for expiring the
+           * time.
+           * In the meantime, Captureing VIDEO image to show pre-view on LCD.
+           */
+
+          case APP_STATE_BEFORE_CAPTURE:
+          case APP_STATE_AFTER_CAPTURE:
+            ret = get_camimage(v_fd, &v4l2_buf, V4L2_BUF_TYPE_VIDEO_CAPTURE);
+            if (ret != OK)
+              {
+                goto exit_this_app;
+              }
+
+#ifdef CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD
+            nximage_draw((void *)v4l2_buf.m.userptr,
+                         VIDEO_HSIZE_QVGA, VIDEO_VSIZE_QVGA);
+#endif
+
+            ret = release_camimage(v_fd, &v4l2_buf);
+            if (ret != OK)
+              {
+                goto exit_this_app;
+              }
+
+            if (!is_eternal)
+              {
+                gettimeofday(&now, NULL);
+                timersub(&now, &start, &delta);
+                if (timercmp(&delta, &wait, >))
+                  {
+                    printf("Expire time is pasted. GoTo next state.\n");
+                    if (app_state == APP_STATE_BEFORE_CAPTURE)
+                      {
+                        app_state = APP_STATE_UNDER_CAPTURE;
+                      }
+                    else
+                      {
+                        ret = OK;
+                        goto exit_this_app;
+                      }
+                  }
+              }
+
+            break; /* Finish APP_STATE_BEFORE_CAPTURE or APP_STATE_AFTER_CAPTURE */
+
+          /* UNDER_CAPTURE is taking pictures until number of capture_num
+           * value.
+           * This state stays until finishing all pictures.
+           */
+
+          case APP_STATE_UNDER_CAPTURE:
+            printf("Start captureing...\n");
+            ret = start_stillcapture(v_fd, capture_type);
+            if (ret != OK)
+              {
+                goto exit_this_app;
+              }
+
+            while (capture_num)
+              {
+                ret = get_camimage(v_fd, &v4l2_buf, capture_type);
+                if (ret != OK)
+                  {
+                    goto exit_this_app;
+                  }
+
+                futil_writeimage(
+                  (uint8_t *)v4l2_buf.m.userptr,
+                  (size_t)v4l2_buf.bytesused,
+                  (capture_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
+                  "RGB" : "JPG");
+
+                ret = release_camimage(v_fd, &v4l2_buf);
+                if (ret != OK)
+                  {
+                    goto exit_this_app;
+                  }
+
+                capture_num--;
+              }
+
+            ret = stop_stillcapture(v_fd, capture_type);
+            if (ret != OK)
+              {
+                goto exit_this_app;
+              }
+
+            app_state = APP_STATE_AFTER_CAPTURE;
+            wait.tv_sec = KEEP_VIDEO_TIME;
+            wait.tv_usec = 0;
+            gettimeofday(&start, NULL);
+            printf("Finished captureing...\n");
+            break; /* Finish APP_STATE_UNDER_CAPTURE */
+
+          default:
+            printf("Unknown error is occured.. state=%d\n", app_state);
+            goto exit_this_app;
+            break;
         }
-
-      RESET_INITIAL_TIME(then);
-      printf("Finished capturing...\n");
     }
 
 exit_this_app:
 
-  /* Close video device file makes dequqe all buffers */
+  /* Close video device file makes dequeue all buffers */
 
   close(v_fd);
 
@@ -603,5 +722,12 @@ exit_without_cleaning_buffer:
 
   video_uninitialize();
 
+exit_without_cleaning_videodriver:
+
+#ifdef CONFIG_EXAMPLES_CAMERA_OUTPUT_LCD
+  nximage_finalize();
+#endif
+
   return ret;
 }
+