nuttx-apps/examples/fb/fb_main.c

168 lines
5.3 KiB
C

/****************************************************************************
* examples/fb/fb_main.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 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 NuttX 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <nuttx/video/fb.h>
/****************************************************************************
* Private Types
****************************************************************************/
struct fb_state_s
{
int fd;
struct fb_videoinfo_s vinfo;
struct fb_planeinfo_s pinfo;
FAR void *fbmem;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_default_fbdev[] = CONFIG_EXAMPLES_FB_DEFAULTFB;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* fb_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int fb_main(int argc, char *argv[])
#endif
{
struct fb_state_s state;
FAR const char *fbdev = g_default_fbdev;
int ret;
/* There is a single required argument: The path to the framebuffer
* driver.
*/
if (argc == 2)
{
fbdev = argv[1];
}
else if (argc != 1)
{
fprintf(stderr, "ERROR: Single argument required\n");
fprintf(stderr, "USAGE: %s [<fb-driver-path>]\n", argv[0]);
return EXIT_FAILURE;
}
/* Open the framebuffer driver */
state.fd = open(fbdev, O_RDWR);
if (state.fd < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n", fbdev, errcode);
return EXIT_FAILURE;
}
/* Get the characteristics of the framebuffer */
ret = ioctl(state.fd, FBIOGET_VIDEOINFO,
(unsigned long)((uintptr_t)&state.vinfo));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_VIDEOINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("VideoInfo:\n");
printf(" fmt: %u\n", state.vinfo.fmt);
printf(" xres: %u\n", state.vinfo.xres);
printf(" yres: %u\n", state.vinfo.yres);
printf(" nplanes: %u\n", state.vinfo.nplanes);
ret = ioctl(state.fd, FBIOGET_PLANEINFO,
(unsigned long)((uintptr_t)&state.pinfo));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("PlaneInfo (plane 0):\n");
printf(" fbmem: %p\n", state.pinfo.fbmem);
printf(" fblen: %lu\n", (unsigned long)state.pinfo.fblen);
printf(" stride: %u\n", state.pinfo.stride);
printf(" display: %u\n", state.pinfo.display);
printf(" bpp: %u\n", state.pinfo.bpp);
/* mmap() the framebuffer */
state.fbmem = mmap(NULL, state.pinfo.fblen, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FILE, state.fd, 0);
if (state.fbmem == MAP_FAILED)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("Mapped FB: %p\n", state.fbmem);
munmap(state.fd, state.fbmem);
close(state.fd);
return EXIT_SUCCESS;
}