Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5064020
  • 博文数量: 1200
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 14456
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
  • 认证徽章:
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1200)

文章存档

2019年(177)

2018年(81)

2017年(80)

2016年(70)

2015年(52)

2014年(41)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2015-12-09 14:43:26

linux平台整理的Makefile平台.


点击(此处)折叠或打开

  1. #!/bin/sh

  2. #
  3. # 为 xxxx 定做文件系统.
  4. #

  5. export XROOT=/home/w55fa92bsp-2.6.35/custom_rootfs/usr_data
  6. export PREFIX=${XROOT}
  7. export LD_LIBRARY_PATH=${PREFIX}/lib
  8. export PKG_CONFIG_PATH=${LD_LIBRARY_PATH}/pkgconfig

  9. export HOST=arm-linux
  10. export BUILD=i686-linux
  11. export TARGET=arm-unknown-linux-gnu

  12. export CROSS_COMPILE=/usr/local/arm/2010q1/bin/arm-none-linux-gnueabi-
  13. export CROSS_EXTOPT="-march=armv5te -mtune=arm9tdmi -msoft-float -mlittle-endian"

  14. export CC=${CROSS_COMPILE}gcc
  15. export CXX=${CROSS_COMPILE}g++
  16. export AR=${CROSS_COMPILE}ar
  17. export RANLIB=${CROSS_COMPILE}ranlib
  18. export NM=${CROSS_COMPILE}nm
  19. export AS=${CROSS_COMPILE}as
  20. export LD=${CROSS_COMPILE}ld

  21. export STRIP=${CROSS_COMPILE}strip
  22. export READELF=${CROSS_COMPILE}readelf
  23. export STRINGS=${CROSS_COMPILE}strings
  24. export SIZE=${CROSS_COMPILE}size
  25. export ADDR2LINE=${CROSS_COMPILE}addr2line

  26. export CFLAGS="-I${XROOT}/include ${CROSS_EXTOPT}"
  27. export CPPFLAGS=${CFLAGS}
  28. export CXXFLAGS=${CFLAGS}
  29. export LDFLAGS="-L${XROOT}/lib"
编译库


点击(此处)折叠或打开

  1. VERSION := 1
  2. PATCHLEVEL := 0
  3. SUBLEVEL := 0

  4. AIM_FILE := NDPeri
  5. SHAREDLIB = lib$(AIM_FILE).so
  6. SHAREDLIBV = lib$(AIM_FILE).so.$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
  7. SHAREDLIBM = lib$(AIM_FILE).so.$(VERSION)
  8. TARGET := $(SHAREDLIB)

  9. SrcDir= .
  10. ExtList= .c .cpp

  11. Source= $(foreach ext,$(ExtList), \
  12.     $(foreach sd, $(SrcDir), $(wildcard $(addprefix $(sd)/*,$(ext)))))

  13. Objs= $(foreach ext, $(ExtList), \
  14.     $(patsubst %$(ext), %.o, $(filter %$(ext),$(Source))))

  15. CFLAGS += -g -Wall
  16. CFLAGS += ${CROSS_EXTOPT}
  17. LDFlags +=
  18. INCFLAGS := -I .

  19. #do yourself

编译可执行程序

点击(此处)折叠或打开

  1. Target=demo_keypad

  2. SrcDir= .
  3. ExtList= .c .cpp

  4. Source= $(foreach ext,$(ExtList), \
  5.     $(foreach sd, $(SrcDir), $(wildcard $(addprefix $(sd)/*,$(ext)))))

  6. Objs= $(foreach ext, $(ExtList), \
  7.     $(patsubst %$(ext), %.o, $(filter %$(ext),$(Source))))

  8. CFLAGS += -g -Wall
  9. CFLAGS += ${CROSS_EXTOPT}
  10. LDFlags += -lNDPeri
  11. INCFLAGS := -I . -I ${XROOT}/include/nd_ext/

  12. %.o : %.c
  13.     $(CC) $(CFLAGS) -c $(@D)/$(<F) -o $(@D)/$(@F) $(INCFLAGS)
  14. %.o : %.cpp
  15.     $(CPP) $(CFLAGS) -c $(@D)/$(<F) -o $(@D)/$(@F) $(INCFLAGS)

  16. .PHONY : all objs clean cleanall rebuild install

  17. $(Target): $(Objs)
  18.     $(CC) $(CFLAGS) -o $(Target) $(Objs) $(LDFlags)

  19. install:$(Target)
  20.     cp -v $(Target) ${XROOT}/bin

  21. release:$(Target)
  22.     $(STRIP) $(Target)
  23.     @$(MAKE) install

  24. all: $(Target)

  25. objs: $(Objs)

  26. RM= rm -rf

  27. clean:
  28.     @$(RM) $(foreach sd, $(SrcDir), $(wildcard $(sd)/*.o))
  29.     @$(RM) $(Target) ${XROOT}/bin/$(Target)

  30. cleanall:clean

  31. rebuild: cleanall


C / C++中正确使用PRId64

nt64_t用来表示64位整数,在32位系统中是long
long int,在64位系统中是long int,所以打印int64_t的格式化方法是:

点击(此处)折叠或打开

  1. printf("%ld", value); // 64bit OS
  2. printf("%lld", value); // 32bit OS
当然有跨平台的方法:

点击(此处)折叠或打开

  1. #include <inttypes.h>
  2. printf("%" PRId64 "\n", value);
  3. // 相当于64位的:
  4. printf("%" "ld" "\n", value);
  5. // 或32位的:
  6. printf("%" "lld" "\n", value)
PRId64/32/16/8 是定义给C语音用的, 如果C++要用,  要在g++ 加入 -D__STDC_FORMAT_MACROS 开关.


V4L_DEMO : v4l version 1

点击(此处)折叠或打开

  1.     int i32VidFD;
  2.     ERRCODE err = ERR_V4L_SUCCESS;
  3.     struct video_capability sVidCap;
  4.     struct video_picture sPict;
  5.     int i32Width, i32Height;
  6.     i32VidFD = open(VID_DEVICE, O_RDWR);
  7.     ioctl(i32VidFD, VIDIOCGCAP, &sVidCap) ;
  8.     if (!(sVidCap.type & VID_TYPE_CAPTURE)) {
  9.         DEBUG_PRINT("Fatal: grab device does not handle capture\n");
  10.         err = ERR_V4L_VID_GRAB_DEV;
  11.         goto fail;
  12.     }
  13.     i32Width = 320;  i32Height = 240;
  14.     ioctl(i32VidFD, VIDIOCGPICT, &sPict);
  15.     sPict.palette = VIDEO_PALETTE_YUV422;
  16.     sPict.depth= 16;
  17.     if(ioctl(i32VidFD, VIDIOCSPICT, &sPict) < 0) {
  18.         DEBUG_PRINT("VIDIOCSPICT: %s\n", strerror(errno));
  19.         err = ERR_V4L_VID_SET_PICT;
  20.         goto fail;
  21.     }
  22.     if (ioctl(i32VidFD, VIDIOCGMBUF, &s_sVidData.sVidMBufs) < 0) {
  23.         /* try to use read based access */
  24.         int val;
  25.         s_sVidData.sVidWin.width = i32Width;
  26.         s_sVidData.sVidWin.height = i32Height;
  27.         s_sVidData.sVidWin.x = 0;
  28.         s_sVidData.sVidWin.y = 0;
  29.         s_sVidData.sVidWin.chromakey = -1;
  30.         s_sVidData.sVidWin.flags = 0;
  31.         if (ioctl(i32VidFD, VIDIOCSWIN, &s_sVidData.sVidWin) < 0) {
  32.             DEBUG_PRINT("VIDIOCSWIN: %s\n", strerror(errno));
  33.             err = ERR_V4L_VID_SET_WIN;
  34.             goto fail;
  35.         }
  36.         s_sVidData.i32FrameFmt = VIDEO_PALETTE_YUV420P;
  37.         val = 1;
  38.         if (ioctl(i32VidFD, VIDIOCCAPTURE, &val) < 0) {
  39.             DEBUG_PRINT("VIDIOCCAPTURE: %s\n", strerror(errno));
  40.             err = ERR_AVDEV_VID_CAPTURE;
  41.             goto fail;
  42.         }
  43.         s_sVidData.i32UseMMap = 0;
  44.     }
  45.     else {
  46.         s_sVidData.pu8VidBuf = mmap(0, s_sVidData.sVidMBufs.size, PROT_READ|PROT_WRITE, MAP_SHARED, i32VidFD, 0);
  47.         if ((unsigned char*)-1 == s_sVidData.pu8VidBuf) {
  48.             s_sVidData.pu8VidBuf = mmap(0, s_sVidData.sVidMBufs.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, i32VidFD, 0);
  49.             if ((unsigned char*)-1 == s_sVidData.pu8VidBuf) {
  50.                 DEBUG_PRINT("mmap: %s\n", strerror(errno));
  51.                 err = ERR_V4L_MMAP;
  52.                 goto fail;
  53.             }
  54.         }
  55.         s_sVidData.i32VidFrame = 0;
  56.         /* start to grab the first frame */
  57.         s_sVidData.sVidMMap.frame = s_sVidData.i32VidFrame % s_sVidData.sVidMBufs.frames;
  58.         s_sVidData.sVidMMap.height = i32Height;
  59.         s_sVidData.sVidMMap.width = i32Width;
  60.         s_sVidData.sVidMMap.format = VIDEO_PALETTE_YUV420P;
  61.         if (ioctl(i32VidFD, VIDIOCMCAPTURE, &s_sVidData.sVidMMap) < 0) {
  62.             if (errno != EAGAIN) {
  63.                 DEBUG_PRINT("VIDIOCMCAPTURE: %s\n", strerror(errno));
  64.             }
  65.             else {
  66.                 DEBUG_PRINT("Fatal: grab device does not receive any video signal\n");
  67.             }
  68.             err = ERR_V4L_VID_MCAPTURE;
  69.             goto fail;
  70.         }
  71.         ioctl(i32VidFD,VIDIOC_G_DIFF_OFFSET, &s_sVidData.i32DiffOffset);
  72.         ioctl(i32VidFD,VIDIOC_G_DIFF_SIZE, &s_sVidData.i32DiffSize);
  73.         s_sVidData.pu8MDDiffBuf = s_sVidData.pu8VidBuf + s_sVidData.i32DiffOffset;   
  74.         s_sVidData.i32FrameFmt = s_sVidData.sVidMMap.format;
  75.         s_sVidData.i32UseMMap = 1;
  76.     }
  77.     s_sVidData.i32FrameSize = s_sVidData.sVidMMap.width * s_sVidData.sVidMMap.height* 2;
  78.     s_sVidData.i32VidFD = i32VidFD;
  79.     return err;


V4L2_Demo

点击(此处)折叠或打开

  1. /*
  2.  * V4L2 video capture example
  3.  *
  4.  * This program can be used and distributed without restrictions.
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <getopt.h> /* getopt_long() */
  11. #include <fcntl.h> /* low-level i/o */
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <malloc.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/time.h>
  18. #include <sys/mman.h>
  19. #include <sys/ioctl.h>
  20. #include <asm/types.h> /* for videodev2.h */
  21. #include <linux/videodev2.h>
  22. #define CLEAR(x) memset (&(x), 0, sizeof (x))
  23. typedef enum {
  24.     IO_METHOD_READ,
  25.     IO_METHOD_MMAP,    
  26.     IO_METHOD_USERPTR,
  27. } io_method;
  28. struct buffer {
  29.         void * start;
  30.         size_t length;
  31. };
  32. static char * dev_name = NULL;
  33. static io_method    io        = IO_METHOD_MMAP;
  34. static int fd = -1;
  35. struct buffer * buffers = NULL;
  36. static unsigned int n_buffers = 0;
  37. static void
  38. errno_exit (const char * s)
  39. {
  40.         fprintf (stderr, "%s error %d, %s\n",
  41.                  s, errno, strerror (errno));
  42.         exit (EXIT_FAILURE);
  43. }
  44. static int
  45. xioctl (int fd,  int request, void * arg)
  46. {
  47. int r;
    do r = ioctl (fd, request, arg);
    while (-1 == r && EINTR == errno);
  48.         return r;
  49. }
  50. static void process_image (const void * p)
  51. {
  52.         fputc ('.', stdout);
  53.         fflush (stdout);
  54. }

  55. static int
  56. read_frame            (void)
  57. {
  58.         struct v4l2_buffer buf;
  59.     unsigned int i;

  60.     switch (io) {
  61.     case IO_METHOD_READ:
  62.             if (-1 == read (fd, buffers[0].start, buffers[0].length)) {
  63.                     switch (errno) {
  64.                     case EAGAIN:
  65.                             return 0;

  66.             case EIO:
  67.                 /* Could ignore EIO, see spec. */

  68.                 /* fall through */

  69.             default:
  70.                 errno_exit ("read");
  71.             }
  72.         }

  73.             process_image (buffers[0].start);

  74.         break;

  75.     case IO_METHOD_MMAP:
  76.         CLEAR (buf);

  77.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  78.                 buf.memory = V4L2_MEMORY_MMAP;

  79.             if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
  80.                     switch (errno) {
  81.                     case EAGAIN:
  82.                             return 0;

  83.             case EIO:
  84.                 /* Could ignore EIO, see spec. */

  85.                 /* fall through */

  86.             default:
  87.                 errno_exit ("VIDIOC_DQBUF");
  88.             }
  89.         }

  90.                 assert (buf.index < n_buffers);

  91.      process_image (buffers[buf.index].start);

  92.         if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  93.             errno_exit ("VIDIOC_QBUF");

  94.         break;

  95.     case IO_METHOD_USERPTR:
  96.         CLEAR (buf);

  97.             buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  98.             buf.memory = V4L2_MEMORY_USERPTR;

  99.         if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
  100.             switch (errno) {
  101.             case EAGAIN:
  102.                 return 0;

  103.             case EIO:
  104.                 /* Could ignore EIO, see spec. */

  105.                 /* fall through */

  106.             default:
  107.                 errno_exit ("VIDIOC_DQBUF");
  108.             }
  109.         }

  110.         for (i = 0; i < n_buffers; ++i)
  111.             if (buf.m.userptr == (unsigned long) buffers[i].start
  112.              && buf.length == buffers[i].length)
  113.                 break;

  114.         assert (i < n_buffers);

  115.             process_image ((void *) buf.m.userptr);

  116.         if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  117.             errno_exit ("VIDIOC_QBUF");

  118.         break;
  119.     }

  120.     return 1;
  121. }

  122. static void
  123. mainloop (void)
  124. {
  125.     unsigned int count;

  126.         count = 100;

  127.         while (count-- > 0) {
  128.                 for (;;) {
  129.                         fd_set fds;
  130.                         struct timeval tv;
  131.                         int r;

  132.                         FD_ZERO (&fds);
  133.                         FD_SET (fd, &fds);

  134.                         /* Timeout. */
  135.                         tv.tv_sec = 2;
  136.                         tv.tv_usec = 0;

  137.                         r = select (fd + 1, &fds, NULL, NULL, &tv);

  138.                         if (-1 == r) {
  139.                                 if (EINTR == errno)
  140.                                         continue;

  141.                                 errno_exit ("select");
  142.                         }

  143.                         if (0 == r) {
  144.                                 fprintf (stderr, "select timeout\n");
  145.                                 exit (EXIT_FAILURE);
  146.                         }

  147.             if (read_frame ())
  148.                             break;
  149.     
  150.             /* EAGAIN - continue select loop. */
  151.                 }
  152.         }
  153. }

  154. static void
  155. stop_capturing (void)
  156. {
  157.         enum v4l2_buf_type type;

  158.     switch (io) {
  159.     case IO_METHOD_READ:
  160.         /* Nothing to do. */
  161.         break;

  162.     case IO_METHOD_MMAP:
  163.     case IO_METHOD_USERPTR:
  164.         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  165.         if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
  166.             errno_exit ("VIDIOC_STREAMOFF");

  167.         break;
  168.     }
  169. }

  170. static void
  171. start_capturing (void)
  172. {
  173.         unsigned int i;
  174.         enum v4l2_buf_type type;

  175.     switch (io) {
  176.     case IO_METHOD_READ:
  177.         /* Nothing to do. */
  178.         break;

  179.     case IO_METHOD_MMAP:
  180.         for (i = 0; i < n_buffers; ++i) {
  181.                     struct v4l2_buffer buf;

  182.                 CLEAR (buf);

  183.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  184.                 buf.memory = V4L2_MEMORY_MMAP;
  185.                 buf.index = i;

  186.                 if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  187.                             errno_exit ("VIDIOC_QBUF");
  188.         }
  189.         
  190.         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  191.         if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
  192.             errno_exit ("VIDIOC_STREAMON");

  193.         break;

  194.     case IO_METHOD_USERPTR:
  195.         for (i = 0; i < n_buffers; ++i) {
  196.                     struct v4l2_buffer buf;

  197.                 CLEAR (buf);

  198.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  199.                 buf.memory = V4L2_MEMORY_USERPTR;
  200.             buf.index = i;
  201.             buf.m.userptr    = (unsigned long) buffers[i].start;
  202.             buf.length = buffers[i].length;

  203.             if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  204.                             errno_exit ("VIDIOC_QBUF");
  205.         }

  206.         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  207.         if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
  208.             errno_exit ("VIDIOC_STREAMON");

  209.         break;
  210.     }
  211. }

  212. static void
  213. uninit_device (void)
  214. {
  215.         unsigned int i;

  216.     switch (io) {
  217.     case IO_METHOD_READ:
  218.         free (buffers[0].start);
  219.         break;

  220.     case IO_METHOD_MMAP:
  221.         for (i = 0; i < n_buffers; ++i)
  222.             if (-1 == munmap (buffers[i].start, buffers[i].length))
  223.                 errno_exit ("munmap");
  224.         break;

  225.     case IO_METHOD_USERPTR:
  226.         for (i = 0; i < n_buffers; ++i)
  227.             free (buffers[i].start);
  228.         break;
  229.     }

  230.     free (buffers);
  231. }

  232. static void
  233. init_read            (unsigned int        buffer_size)
  234. {
  235.         buffers = calloc (1, sizeof (*buffers));

  236.         if (!buffers) {
  237.                 fprintf (stderr, "Out of memory\n");
  238.                 exit (EXIT_FAILURE);
  239.         }

  240.     buffers[0].length = buffer_size;
  241.     buffers[0].start = malloc (buffer_size);

  242.     if (!buffers[0].start) {
  243.             fprintf (stderr, "Out of memory\n");
  244.                 exit (EXIT_FAILURE);
  245.     }
  246. }

  247. static void
  248. init_mmap            (void)
  249. {
  250.     struct v4l2_requestbuffers req;

  251.         CLEAR (req);

  252.         req.count = 4;
  253.         req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  254.         req.memory = V4L2_MEMORY_MMAP;

  255.     if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
  256.                 if (EINVAL == errno) {
  257.                         fprintf (stderr, "%s does not support "
  258.                                  "memory mapping\n", dev_name);
  259.                         exit (EXIT_FAILURE);
  260.                 } else {
  261.                         errno_exit ("VIDIOC_REQBUFS");
  262.                 }
  263.         }

  264.         if (req.count < 2) {
  265.                 fprintf (stderr, "Insufficient buffer memory on %s\n",
  266.                          dev_name);
  267.                 exit (EXIT_FAILURE);
  268.         }

  269.         buffers = calloc (req.count, sizeof (*buffers));

  270.         if (!buffers) {
  271.                 fprintf (stderr, "Out of memory\n");
  272.                 exit (EXIT_FAILURE);
  273.         }

  274.         for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  275.                 struct v4l2_buffer buf;

  276.                 CLEAR (buf);

  277.                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  278.                 buf.memory = V4L2_MEMORY_MMAP;
  279.                 buf.index = n_buffers;

  280.                 if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
  281.                         errno_exit ("VIDIOC_QUERYBUF");

  282.                 buffers[n_buffers].length = buf.length;
  283.                 buffers[n_buffers].start =
  284.                         mmap (NULL /* start anywhere */,
  285.                               buf.length,
  286.                               PROT_READ | PROT_WRITE /* required */,
  287.                               MAP_SHARED /* recommended */,
  288.                               fd, buf.m.offset);

  289.                 if (MAP_FAILED == buffers[n_buffers].start)
  290.                         errno_exit ("mmap");
  291.         }
  292. }

  293. static void
  294. init_userp            (unsigned int        buffer_size)
  295. {
  296.     struct v4l2_requestbuffers req;
  297.         unsigned int page_size;

  298.         page_size = getpagesize ();
  299.         buffer_size = (buffer_size + page_size - 1) & ~(page_size - 1);

  300.         CLEAR (req);

  301.         req.count = 4;
  302.         req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  303.         req.memory = V4L2_MEMORY_USERPTR;

  304.         if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
  305.                 if (EINVAL == errno) {
  306.                         fprintf (stderr, "%s does not support "
  307.                                  "user pointer i/o\n", dev_name);
  308.                         exit (EXIT_FAILURE);
  309.                 } else {
  310.                         errno_exit ("VIDIOC_REQBUFS");
  311.                 }
  312.         }

  313.         buffers = calloc (4, sizeof (*buffers));

  314.         if (!buffers) {
  315.                 fprintf (stderr, "Out of memory\n");
  316.                 exit (EXIT_FAILURE);
  317.         }

  318.         for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
  319.                 buffers[n_buffers].length = buffer_size;
  320.                 buffers[n_buffers].start = memalign (/* boundary */ page_size,
  321.                                                      buffer_size);

  322.                 if (!buffers[n_buffers].start) {
  323.                 fprintf (stderr, "Out of memory\n");
  324.                     exit (EXIT_FAILURE);
  325.         }
  326.         }
  327. }

  328. static void
  329. init_device (void)
  330. {
  331.         struct v4l2_capability cap;
  332.         struct v4l2_cropcap cropcap;
  333.         struct v4l2_crop crop;
  334.         struct v4l2_format fmt;
  335.     unsigned int min;

  336.         if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
  337.                 if (EINVAL == errno) {
  338.                         fprintf (stderr, "%s is no V4L2 device\n",
  339.                                  dev_name);
  340.                         exit (EXIT_FAILURE);
  341.                 } else {
  342.                         errno_exit ("VIDIOC_QUERYCAP");
  343.                 }
  344.         }

  345.         if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  346.                 fprintf (stderr, "%s is no video capture device\n",
  347.                          dev_name);
  348.                 exit (EXIT_FAILURE);
  349.         }

  350.     switch (io) {
  351.     case IO_METHOD_READ:
  352.         if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
  353.             fprintf (stderr, "%s does not support read i/o\n",
  354.                  dev_name);
  355.             exit (EXIT_FAILURE);
  356.         }

  357.         break;

  358.     case IO_METHOD_MMAP:
  359.     case IO_METHOD_USERPTR:
  360.         if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  361.             fprintf (stderr, "%s does not support streaming i/o\n",
  362.                  dev_name);
  363.             exit (EXIT_FAILURE);
  364.         }

  365.         break;
  366.     }


  367.         /* Select video input, video standard and tune here. */


  368.     CLEAR (cropcap);

  369.         cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  370.         if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
  371.                 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  372.                 crop.c = cropcap.defrect; /* reset to default */

  373.                 if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) {
  374.                         switch (errno) {
  375.                         case EINVAL:
  376.                                 /* Cropping not supported. */
  377.                                 break;
  378.                         default:
  379.                                 /* Errors ignored. */
  380.                                 break;
  381.                         }
  382.                 }
  383.         } else {    
  384.                 /* Errors ignored. */
  385.         }


  386.         CLEAR (fmt);

  387.         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  388.         fmt.fmt.pix.width = 640;
  389.         fmt.fmt.pix.height = 480;
  390.         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  391.         fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;

  392.         if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
  393.                 errno_exit ("VIDIOC_S_FMT");

  394.         /* Note VIDIOC_S_FMT may change width and height. */

  395.     /* Buggy driver paranoia. */
  396.     min = fmt.fmt.pix.width * 2;
  397.     if (fmt.fmt.pix.bytesperline < min)
  398.         fmt.fmt.pix.bytesperline = min;
  399.     min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  400.     if (fmt.fmt.pix.sizeimage < min)
  401.         fmt.fmt.pix.sizeimage = min;

  402.     switch (io) {
  403.     case IO_METHOD_READ:
  404.         init_read (fmt.fmt.pix.sizeimage);
  405.         break;

  406.     case IO_METHOD_MMAP:
  407.         init_mmap ();
  408.         break;

  409.     case IO_METHOD_USERPTR:
  410.         init_userp (fmt.fmt.pix.sizeimage);
  411.         break;
  412.     }
  413. }

  414. static void
  415. close_device (void)
  416. {
  417.         if (-1 == close (fd))
  418.      errno_exit ("close");

  419.         fd = -1;
  420. }

  421. static void
  422. open_device (void)
  423. {
  424.         struct stat st;

  425.         if (-1 == stat (dev_name, &st)) {
  426.                 fprintf (stderr, "Cannot identify '%s': %d, %s\n",
  427.                          dev_name, errno, strerror (errno));
  428.                 exit (EXIT_FAILURE);
  429.         }

  430.         if (!S_ISCHR (st.st_mode)) {
  431.                 fprintf (stderr, "%s is no device\n", dev_name);
  432.                 exit (EXIT_FAILURE);
  433.         }

  434.         fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);

  435.         if (-1 == fd) {
  436.                 fprintf (stderr, "Cannot open '%s': %d, %s\n",
  437.                          dev_name, errno, strerror (errno));
  438.                 exit (EXIT_FAILURE);
  439.         }
  440. }

  441. static void
  442. usage (FILE * fp,
  443.                                  int argc,
  444.                                  char ** argv)
  445. {
  446.         fprintf (fp,
  447.                  "Usage: %s [options]\n\n"
  448.                  "Options:\n"
  449.                  "-d | --device name Video device name [/dev/video]\n"
  450.                  "-h | --help Print this message\n"
  451.                  "-m | --mmap Use memory mapped buffers\n"
  452.                  "-r | --read Use read() calls\n"
  453.                  "-u | --userp Use application allocated buffers\n"
  454.                  "",
  455.          argv[0]);
  456. }

  457. static const char short_options [] = "d:hmru";

  458. static const struct option
  459. long_options [] = {
  460.         { "device", required_argument, NULL, 'd' },
  461.         { "help", no_argument, NULL, 'h' },
  462.         { "mmap", no_argument, NULL, 'm' },
  463.         { "read", no_argument, NULL, 'r' },
  464.         { "userp", no_argument, NULL, 'u' },
  465.         { 0, 0, 0, 0 }
  466. };

  467. int
  468. main (int argc,
  469.                                  char ** argv)
  470. {
  471.         //dev_name = "/dev/video";
  472.         dev_name = "/dev/video0";
  473.         for (;;) {
  474.                 int index;
  475.                 int c;
  476.                 
  477.                 c = getopt_long (argc, argv,
  478.                                  short_options, long_options,
  479.                                  &index);

  480.                 if (-1 == c)
  481.                         break;

  482.                 switch (c) {
  483.                 case 0: /* getopt_long() flag */
  484.                         break;

  485.                 case 'd':
  486.                         dev_name = optarg;
  487.                         break;

  488.                 case 'h':
  489.                         usage (stdout, argc, argv);
  490.                         exit (EXIT_SUCCESS);

  491.                 case 'm':
  492.                         io = IO_METHOD_MMAP;
  493.             break;

  494.                 case 'r':
  495.                         io = IO_METHOD_READ;
  496.             break;

  497.                 case 'u':
  498.                         io = IO_METHOD_USERPTR;
  499.             break;

  500.                 default:
  501.                         usage (stderr, argc, argv);
  502.                         exit (EXIT_FAILURE);
  503.                 }
  504.         }

  505.         open_device ();

  506.         init_device ();

  507.         start_capturing ();

  508.         mainloop ();

  509.         stop_capturing ();

  510.         uninit_device ();

  511.         close_device ();

  512.         exit (EXIT_SUCCESS);

  513.         return 0;
  514. }



把frame buffer 映射到用户控件, 然后可以操作fb的用户控件的达到控制像素点的功能.

点击(此处)折叠或打开

  1. int InitFBDevice(
  2.     uint8_t **ppu8FBBuf,
  3.     uint32_t *pu32FBBufSize
  4. )
  5. {
  6.     int32_t i32FBFd;
  7.     uint8_t *pu8FBBuf;
  8.     char achFBDevice[] = "/dev/fb0";
  9.     static struct fb_var_screeninfo sVar;
  10.     *ppu8FBBuf = MAP_FAILED;
  11.     *pu32FBBufSize = 0;
  12.     i32FBFd = open(achFBDevice, O_RDWR);   printf("fb open successful\n");
  13.     ioctl(i32FBFd, FBIOGET_VSCREENINFO, &sVar) ;
  14.     s_i32FBWidth = sVar.xres;
  15.     s_i32FBHeight = sVar.yres;
  16.     pu8FBBuf = mmap( NULL, s_i32FBWidth*s_i32FBHeight*2 , PROT_READ|PROT_WRITE, MAP_SHARED, i32FBFd, 0 );
  17.     if ((unsigned char*)-1 == pu8FBBuf) {
  18.             close(i32FBFd);
  19.             return -1;
  20.     }
  21.     *pu32FBBufSize = s_i32FBWidth*s_i32FBHeight*2;
  22.     ioctl(i32FBFd, IOCTL_LCD_DISABLE_INT, 0);
  23.     ioctl(i32FBFd, VIDEO_FORMAT_CHANGE, DISPLAY_MODE_YCBYCR);
  24.     ioctl(i32FBFd, IOCTL_LCD_ENABLE_INT, 0);
  25.     *ppu8FBBuf = pu8FBBuf;
  26.     return i32FBFd;
  27. }




从各个设备节点点取出其名字. 对应判断是哪个设备. 如果从/dev/eventX中取出名字, 判断是键盘/鼠标/触屏等等.

点击(此处)折叠或打开

  1. char achDevName[256]; 
  2.     char achKeypad[] = "Keypad";  //想要找的设备名称, 对应 driver 里边的name.
  3.     char achKpdDevice[] = "/dev/input/event0";
  4.     int32_t i32KpdFd= -1;
  5.     uint32_t u32Result;
  6.     int32_t i;    
  7.     u32Result = 0;
  8.     for (i = 0; i < RETRY_NUM; i++) {
  9.         DEBUG_PRINT("trying to open %s ...\n", achKpdDevice);
  10.         if ((i32KpdFd=open(achKpdDevice, O_RDONLY | O_NONBLOCK)) < 0) {
  11.             break;
  12.         }
  13.         memset(achDevName, 0x0, sizeof(achDevName));
  14.         if (ioctl(i32KpdFd, EVIOCGNAME(sizeof(achDevName)), achDevName) < 0) {  //取出设备名.  等同于操作 /sys/class/input/input0/name
  15.             DEBUG_PRINT("%s evdev ioctl error!\n", achKpdDevice);
  16.         }
  17.         else{
  18.             if (strstr(achDevName, achKeypad) != NULL) { //比较名字.
  19.                 DEBUG_PRINT("Keypad device is %s\n", achKpdDevice);
  20.                 u32Result = 1;
  21.                 break;
  22.             }
  23.         }
  24.         close(i32KpdFd);
  25.         achKpdDevice[16]++;
  26.     }
  27.     if (u32Result == 0) {
  28.         DEBUG_PRINT("can not find any Keypad device!\n");
  29.         return -1;
  30.     }
  31.     return i32KpdFd;

阅读(1510) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册