linux平台整理的Makefile平台.
-
#!/bin/sh
-
-
#
-
# 为 xxxx 定做文件系统.
-
#
-
-
export XROOT=/home/w55fa92bsp-2.6.35/custom_rootfs/usr_data
-
export PREFIX=${XROOT}
-
export LD_LIBRARY_PATH=${PREFIX}/lib
-
export PKG_CONFIG_PATH=${LD_LIBRARY_PATH}/pkgconfig
-
-
export HOST=arm-linux
-
export BUILD=i686-linux
-
export TARGET=arm-unknown-linux-gnu
-
-
export CROSS_COMPILE=/usr/local/arm/2010q1/bin/arm-none-linux-gnueabi-
-
export CROSS_EXTOPT="-march=armv5te -mtune=arm9tdmi -msoft-float -mlittle-endian"
-
-
export CC=${CROSS_COMPILE}gcc
-
export CXX=${CROSS_COMPILE}g++
-
export AR=${CROSS_COMPILE}ar
-
export RANLIB=${CROSS_COMPILE}ranlib
-
export NM=${CROSS_COMPILE}nm
-
export AS=${CROSS_COMPILE}as
-
export LD=${CROSS_COMPILE}ld
-
-
export STRIP=${CROSS_COMPILE}strip
-
export READELF=${CROSS_COMPILE}readelf
-
export STRINGS=${CROSS_COMPILE}strings
-
export SIZE=${CROSS_COMPILE}size
-
export ADDR2LINE=${CROSS_COMPILE}addr2line
-
-
export CFLAGS="-I${XROOT}/include ${CROSS_EXTOPT}"
-
export CPPFLAGS=${CFLAGS}
-
export CXXFLAGS=${CFLAGS}
-
export LDFLAGS="-L${XROOT}/lib"
编译库
-
VERSION := 1
-
PATCHLEVEL := 0
-
SUBLEVEL := 0
-
-
AIM_FILE := NDPeri
-
SHAREDLIB = lib$(AIM_FILE).so
-
SHAREDLIBV = lib$(AIM_FILE).so.$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
-
SHAREDLIBM = lib$(AIM_FILE).so.$(VERSION)
-
TARGET := $(SHAREDLIB)
-
-
SrcDir= .
-
ExtList= .c .cpp
-
-
Source= $(foreach ext,$(ExtList), \
-
$(foreach sd, $(SrcDir), $(wildcard $(addprefix $(sd)/*,$(ext)))))
-
-
Objs= $(foreach ext, $(ExtList), \
-
$(patsubst %$(ext), %.o, $(filter %$(ext),$(Source))))
-
-
CFLAGS += -g -Wall
-
CFLAGS += ${CROSS_EXTOPT}
-
LDFlags +=
-
INCFLAGS := -I .
-
-
#do yourself
编译可执行程序
-
Target=demo_keypad
-
-
SrcDir= .
-
ExtList= .c .cpp
-
-
Source= $(foreach ext,$(ExtList), \
-
$(foreach sd, $(SrcDir), $(wildcard $(addprefix $(sd)/*,$(ext)))))
-
-
Objs= $(foreach ext, $(ExtList), \
-
$(patsubst %$(ext), %.o, $(filter %$(ext),$(Source))))
-
-
CFLAGS += -g -Wall
-
CFLAGS += ${CROSS_EXTOPT}
-
LDFlags += -lNDPeri
-
INCFLAGS := -I . -I ${XROOT}/include/nd_ext/
-
-
%.o : %.c
-
$(CC) $(CFLAGS) -c $(@D)/$(<F) -o $(@D)/$(@F) $(INCFLAGS)
-
%.o : %.cpp
-
$(CPP) $(CFLAGS) -c $(@D)/$(<F) -o $(@D)/$(@F) $(INCFLAGS)
-
-
.PHONY : all objs clean cleanall rebuild install
-
-
$(Target): $(Objs)
-
$(CC) $(CFLAGS) -o $(Target) $(Objs) $(LDFlags)
-
-
install:$(Target)
-
cp -v $(Target) ${XROOT}/bin
-
-
release:$(Target)
-
$(STRIP) $(Target)
-
@$(MAKE) install
-
-
all: $(Target)
-
-
objs: $(Objs)
-
-
RM= rm -rf
-
-
clean:
-
@$(RM) $(foreach sd, $(SrcDir), $(wildcard $(sd)/*.o))
-
@$(RM) $(Target) ${XROOT}/bin/$(Target)
-
-
cleanall:clean
-
-
rebuild: cleanall
C / C++中正确使用PRId64
nt64_t用来表示64位整数,在32位系统中是long
long int,在64位系统中是long int,所以打印int64_t的格式化方法是:
-
printf("%ld", value); // 64bit OS
-
printf("%lld", value); // 32bit OS
当然有跨平台的方法:
-
#include <inttypes.h>
-
printf("%" PRId64 "\n", value);
-
// 相当于64位的:
-
printf("%" "ld" "\n", value);
-
// 或32位的:
-
printf("%" "lld" "\n", value)
但
PRId64/32/16/8 是定义给C语音用的, 如果C++要用, 要在g++ 加入 -D__STDC_FORMAT_MACROS 开关.
V4L_DEMO : v4l version 1
-
int i32VidFD;
-
ERRCODE err = ERR_V4L_SUCCESS;
-
struct video_capability sVidCap;
-
struct video_picture sPict;
-
int i32Width, i32Height;
-
i32VidFD = open(VID_DEVICE, O_RDWR);
-
ioctl(i32VidFD, VIDIOCGCAP, &sVidCap) ;
-
if (!(sVidCap.type & VID_TYPE_CAPTURE)) {
-
DEBUG_PRINT("Fatal: grab device does not handle capture\n");
-
err = ERR_V4L_VID_GRAB_DEV;
-
goto fail;
-
}
-
i32Width = 320; i32Height = 240;
-
ioctl(i32VidFD, VIDIOCGPICT, &sPict);
-
sPict.palette = VIDEO_PALETTE_YUV422;
-
sPict.depth= 16;
-
if(ioctl(i32VidFD, VIDIOCSPICT, &sPict) < 0) {
-
DEBUG_PRINT("VIDIOCSPICT: %s\n", strerror(errno));
-
err = ERR_V4L_VID_SET_PICT;
-
goto fail;
-
}
-
if (ioctl(i32VidFD, VIDIOCGMBUF, &s_sVidData.sVidMBufs) < 0) {
-
/* try to use read based access */
-
int val;
-
s_sVidData.sVidWin.width = i32Width;
-
s_sVidData.sVidWin.height = i32Height;
-
s_sVidData.sVidWin.x = 0;
-
s_sVidData.sVidWin.y = 0;
-
s_sVidData.sVidWin.chromakey = -1;
-
s_sVidData.sVidWin.flags = 0;
-
if (ioctl(i32VidFD, VIDIOCSWIN, &s_sVidData.sVidWin) < 0) {
-
DEBUG_PRINT("VIDIOCSWIN: %s\n", strerror(errno));
-
err = ERR_V4L_VID_SET_WIN;
-
goto fail;
-
}
-
s_sVidData.i32FrameFmt = VIDEO_PALETTE_YUV420P;
-
val = 1;
-
if (ioctl(i32VidFD, VIDIOCCAPTURE, &val) < 0) {
-
DEBUG_PRINT("VIDIOCCAPTURE: %s\n", strerror(errno));
-
err = ERR_AVDEV_VID_CAPTURE;
-
goto fail;
-
}
-
s_sVidData.i32UseMMap = 0;
-
}
-
else {
-
s_sVidData.pu8VidBuf = mmap(0, s_sVidData.sVidMBufs.size, PROT_READ|PROT_WRITE, MAP_SHARED, i32VidFD, 0);
-
if ((unsigned char*)-1 == s_sVidData.pu8VidBuf) {
-
s_sVidData.pu8VidBuf = mmap(0, s_sVidData.sVidMBufs.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, i32VidFD, 0);
-
if ((unsigned char*)-1 == s_sVidData.pu8VidBuf) {
-
DEBUG_PRINT("mmap: %s\n", strerror(errno));
-
err = ERR_V4L_MMAP;
-
goto fail;
-
}
-
}
-
s_sVidData.i32VidFrame = 0;
-
/* start to grab the first frame */
-
s_sVidData.sVidMMap.frame = s_sVidData.i32VidFrame % s_sVidData.sVidMBufs.frames;
-
s_sVidData.sVidMMap.height = i32Height;
-
s_sVidData.sVidMMap.width = i32Width;
-
s_sVidData.sVidMMap.format = VIDEO_PALETTE_YUV420P;
-
if (ioctl(i32VidFD, VIDIOCMCAPTURE, &s_sVidData.sVidMMap) < 0) {
-
if (errno != EAGAIN) {
-
DEBUG_PRINT("VIDIOCMCAPTURE: %s\n", strerror(errno));
-
}
-
else {
-
DEBUG_PRINT("Fatal: grab device does not receive any video signal\n");
-
}
-
err = ERR_V4L_VID_MCAPTURE;
-
goto fail;
-
}
-
ioctl(i32VidFD,VIDIOC_G_DIFF_OFFSET, &s_sVidData.i32DiffOffset);
-
ioctl(i32VidFD,VIDIOC_G_DIFF_SIZE, &s_sVidData.i32DiffSize);
-
s_sVidData.pu8MDDiffBuf = s_sVidData.pu8VidBuf + s_sVidData.i32DiffOffset;
-
s_sVidData.i32FrameFmt = s_sVidData.sVidMMap.format;
-
s_sVidData.i32UseMMap = 1;
-
}
-
s_sVidData.i32FrameSize = s_sVidData.sVidMMap.width * s_sVidData.sVidMMap.height* 2;
-
s_sVidData.i32VidFD = i32VidFD;
-
return err;
V4L2_Demo
-
/*
-
* V4L2 video capture example
-
*
-
* This program can be used and distributed without restrictions.
-
*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <assert.h>
-
#include <getopt.h> /* getopt_long() */
-
#include <fcntl.h> /* low-level i/o */
-
#include <unistd.h>
-
#include <errno.h>
-
#include <malloc.h>
-
#include <sys/stat.h>
-
#include <sys/types.h>
-
#include <sys/time.h>
-
#include <sys/mman.h>
-
#include <sys/ioctl.h>
-
#include <asm/types.h> /* for videodev2.h */
-
#include <linux/videodev2.h>
-
#define CLEAR(x) memset (&(x), 0, sizeof (x))
-
typedef enum {
-
IO_METHOD_READ,
-
IO_METHOD_MMAP,
-
IO_METHOD_USERPTR,
-
} io_method;
-
struct buffer {
-
void * start;
-
size_t length;
-
};
-
static char * dev_name = NULL;
-
static io_method io = IO_METHOD_MMAP;
-
static int fd = -1;
-
struct buffer * buffers = NULL;
-
static unsigned int n_buffers = 0;
-
static void
-
errno_exit (const char * s)
-
{
-
fprintf (stderr, "%s error %d, %s\n",
-
s, errno, strerror (errno));
-
exit (EXIT_FAILURE);
-
}
-
static int
-
xioctl (int fd, int request, void * arg)
-
{
-
int r;
do r = ioctl (fd, request, arg);
while (-1 == r && EINTR == errno);
-
return r;
-
}
-
static void process_image (const void * p)
-
{
-
fputc ('.', stdout);
-
fflush (stdout);
-
}
-
-
static int
-
read_frame (void)
-
{
-
struct v4l2_buffer buf;
-
unsigned int i;
-
-
switch (io) {
-
case IO_METHOD_READ:
-
if (-1 == read (fd, buffers[0].start, buffers[0].length)) {
-
switch (errno) {
-
case EAGAIN:
-
return 0;
-
-
case EIO:
-
/* Could ignore EIO, see spec. */
-
-
/* fall through */
-
-
default:
-
errno_exit ("read");
-
}
-
}
-
-
process_image (buffers[0].start);
-
-
break;
-
-
case IO_METHOD_MMAP:
-
CLEAR (buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
-
if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
-
switch (errno) {
-
case EAGAIN:
-
return 0;
-
-
case EIO:
-
/* Could ignore EIO, see spec. */
-
-
/* fall through */
-
-
default:
-
errno_exit ("VIDIOC_DQBUF");
-
}
-
}
-
-
assert (buf.index < n_buffers);
-
-
process_image (buffers[buf.index].start);
-
-
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
-
errno_exit ("VIDIOC_QBUF");
-
-
break;
-
-
case IO_METHOD_USERPTR:
-
CLEAR (buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_USERPTR;
-
-
if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
-
switch (errno) {
-
case EAGAIN:
-
return 0;
-
-
case EIO:
-
/* Could ignore EIO, see spec. */
-
-
/* fall through */
-
-
default:
-
errno_exit ("VIDIOC_DQBUF");
-
}
-
}
-
-
for (i = 0; i < n_buffers; ++i)
-
if (buf.m.userptr == (unsigned long) buffers[i].start
-
&& buf.length == buffers[i].length)
-
break;
-
-
assert (i < n_buffers);
-
-
process_image ((void *) buf.m.userptr);
-
-
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
-
errno_exit ("VIDIOC_QBUF");
-
-
break;
-
}
-
-
return 1;
-
}
-
-
static void
-
mainloop (void)
-
{
-
unsigned int count;
-
-
count = 100;
-
-
while (count-- > 0) {
-
for (;;) {
-
fd_set fds;
-
struct timeval tv;
-
int r;
-
-
FD_ZERO (&fds);
-
FD_SET (fd, &fds);
-
-
/* Timeout. */
-
tv.tv_sec = 2;
-
tv.tv_usec = 0;
-
-
r = select (fd + 1, &fds, NULL, NULL, &tv);
-
-
if (-1 == r) {
-
if (EINTR == errno)
-
continue;
-
-
errno_exit ("select");
-
}
-
-
if (0 == r) {
-
fprintf (stderr, "select timeout\n");
-
exit (EXIT_FAILURE);
-
}
-
-
if (read_frame ())
-
break;
-
-
/* EAGAIN - continue select loop. */
-
}
-
}
-
}
-
-
static void
-
stop_capturing (void)
-
{
-
enum v4l2_buf_type type;
-
-
switch (io) {
-
case IO_METHOD_READ:
-
/* Nothing to do. */
-
break;
-
-
case IO_METHOD_MMAP:
-
case IO_METHOD_USERPTR:
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-
if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
-
errno_exit ("VIDIOC_STREAMOFF");
-
-
break;
-
}
-
}
-
-
static void
-
start_capturing (void)
-
{
-
unsigned int i;
-
enum v4l2_buf_type type;
-
-
switch (io) {
-
case IO_METHOD_READ:
-
/* Nothing to do. */
-
break;
-
-
case IO_METHOD_MMAP:
-
for (i = 0; i < n_buffers; ++i) {
-
struct v4l2_buffer buf;
-
-
CLEAR (buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
buf.index = i;
-
-
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
-
errno_exit ("VIDIOC_QBUF");
-
}
-
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-
if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
-
errno_exit ("VIDIOC_STREAMON");
-
-
break;
-
-
case IO_METHOD_USERPTR:
-
for (i = 0; i < n_buffers; ++i) {
-
struct v4l2_buffer buf;
-
-
CLEAR (buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_USERPTR;
-
buf.index = i;
-
buf.m.userptr = (unsigned long) buffers[i].start;
-
buf.length = buffers[i].length;
-
-
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
-
errno_exit ("VIDIOC_QBUF");
-
}
-
-
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-
if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
-
errno_exit ("VIDIOC_STREAMON");
-
-
break;
-
}
-
}
-
-
static void
-
uninit_device (void)
-
{
-
unsigned int i;
-
-
switch (io) {
-
case IO_METHOD_READ:
-
free (buffers[0].start);
-
break;
-
-
case IO_METHOD_MMAP:
-
for (i = 0; i < n_buffers; ++i)
-
if (-1 == munmap (buffers[i].start, buffers[i].length))
-
errno_exit ("munmap");
-
break;
-
-
case IO_METHOD_USERPTR:
-
for (i = 0; i < n_buffers; ++i)
-
free (buffers[i].start);
-
break;
-
}
-
-
free (buffers);
-
}
-
-
static void
-
init_read (unsigned int buffer_size)
-
{
-
buffers = calloc (1, sizeof (*buffers));
-
-
if (!buffers) {
-
fprintf (stderr, "Out of memory\n");
-
exit (EXIT_FAILURE);
-
}
-
-
buffers[0].length = buffer_size;
-
buffers[0].start = malloc (buffer_size);
-
-
if (!buffers[0].start) {
-
fprintf (stderr, "Out of memory\n");
-
exit (EXIT_FAILURE);
-
}
-
}
-
-
static void
-
init_mmap (void)
-
{
-
struct v4l2_requestbuffers req;
-
-
CLEAR (req);
-
-
req.count = 4;
-
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
req.memory = V4L2_MEMORY_MMAP;
-
-
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
-
if (EINVAL == errno) {
-
fprintf (stderr, "%s does not support "
-
"memory mapping\n", dev_name);
-
exit (EXIT_FAILURE);
-
} else {
-
errno_exit ("VIDIOC_REQBUFS");
-
}
-
}
-
-
if (req.count < 2) {
-
fprintf (stderr, "Insufficient buffer memory on %s\n",
-
dev_name);
-
exit (EXIT_FAILURE);
-
}
-
-
buffers = calloc (req.count, sizeof (*buffers));
-
-
if (!buffers) {
-
fprintf (stderr, "Out of memory\n");
-
exit (EXIT_FAILURE);
-
}
-
-
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
-
struct v4l2_buffer buf;
-
-
CLEAR (buf);
-
-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
buf.memory = V4L2_MEMORY_MMAP;
-
buf.index = n_buffers;
-
-
if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
-
errno_exit ("VIDIOC_QUERYBUF");
-
-
buffers[n_buffers].length = buf.length;
-
buffers[n_buffers].start =
-
mmap (NULL /* start anywhere */,
-
buf.length,
-
PROT_READ | PROT_WRITE /* required */,
-
MAP_SHARED /* recommended */,
-
fd, buf.m.offset);
-
-
if (MAP_FAILED == buffers[n_buffers].start)
-
errno_exit ("mmap");
-
}
-
}
-
-
static void
-
init_userp (unsigned int buffer_size)
-
{
-
struct v4l2_requestbuffers req;
-
unsigned int page_size;
-
-
page_size = getpagesize ();
-
buffer_size = (buffer_size + page_size - 1) & ~(page_size - 1);
-
-
CLEAR (req);
-
-
req.count = 4;
-
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
req.memory = V4L2_MEMORY_USERPTR;
-
-
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
-
if (EINVAL == errno) {
-
fprintf (stderr, "%s does not support "
-
"user pointer i/o\n", dev_name);
-
exit (EXIT_FAILURE);
-
} else {
-
errno_exit ("VIDIOC_REQBUFS");
-
}
-
}
-
-
buffers = calloc (4, sizeof (*buffers));
-
-
if (!buffers) {
-
fprintf (stderr, "Out of memory\n");
-
exit (EXIT_FAILURE);
-
}
-
-
for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
-
buffers[n_buffers].length = buffer_size;
-
buffers[n_buffers].start = memalign (/* boundary */ page_size,
-
buffer_size);
-
-
if (!buffers[n_buffers].start) {
-
fprintf (stderr, "Out of memory\n");
-
exit (EXIT_FAILURE);
-
}
-
}
-
}
-
-
static void
-
init_device (void)
-
{
-
struct v4l2_capability cap;
-
struct v4l2_cropcap cropcap;
-
struct v4l2_crop crop;
-
struct v4l2_format fmt;
-
unsigned int min;
-
-
if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
-
if (EINVAL == errno) {
-
fprintf (stderr, "%s is no V4L2 device\n",
-
dev_name);
-
exit (EXIT_FAILURE);
-
} else {
-
errno_exit ("VIDIOC_QUERYCAP");
-
}
-
}
-
-
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
-
fprintf (stderr, "%s is no video capture device\n",
-
dev_name);
-
exit (EXIT_FAILURE);
-
}
-
-
switch (io) {
-
case IO_METHOD_READ:
-
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
-
fprintf (stderr, "%s does not support read i/o\n",
-
dev_name);
-
exit (EXIT_FAILURE);
-
}
-
-
break;
-
-
case IO_METHOD_MMAP:
-
case IO_METHOD_USERPTR:
-
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
-
fprintf (stderr, "%s does not support streaming i/o\n",
-
dev_name);
-
exit (EXIT_FAILURE);
-
}
-
-
break;
-
}
-
-
-
/* Select video input, video standard and tune here. */
-
-
-
CLEAR (cropcap);
-
-
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-
if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
-
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
crop.c = cropcap.defrect; /* reset to default */
-
-
if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) {
-
switch (errno) {
-
case EINVAL:
-
/* Cropping not supported. */
-
break;
-
default:
-
/* Errors ignored. */
-
break;
-
}
-
}
-
} else {
-
/* Errors ignored. */
-
}
-
-
-
CLEAR (fmt);
-
-
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
fmt.fmt.pix.width = 640;
-
fmt.fmt.pix.height = 480;
-
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
-
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
-
-
if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
-
errno_exit ("VIDIOC_S_FMT");
-
-
/* Note VIDIOC_S_FMT may change width and height. */
-
-
/* Buggy driver paranoia. */
-
min = fmt.fmt.pix.width * 2;
-
if (fmt.fmt.pix.bytesperline < min)
-
fmt.fmt.pix.bytesperline = min;
-
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
-
if (fmt.fmt.pix.sizeimage < min)
-
fmt.fmt.pix.sizeimage = min;
-
-
switch (io) {
-
case IO_METHOD_READ:
-
init_read (fmt.fmt.pix.sizeimage);
-
break;
-
-
case IO_METHOD_MMAP:
-
init_mmap ();
-
break;
-
-
case IO_METHOD_USERPTR:
-
init_userp (fmt.fmt.pix.sizeimage);
-
break;
-
}
-
}
-
-
static void
-
close_device (void)
-
{
-
if (-1 == close (fd))
-
errno_exit ("close");
-
-
fd = -1;
-
}
-
-
static void
-
open_device (void)
-
{
-
struct stat st;
-
-
if (-1 == stat (dev_name, &st)) {
-
fprintf (stderr, "Cannot identify '%s': %d, %s\n",
-
dev_name, errno, strerror (errno));
-
exit (EXIT_FAILURE);
-
}
-
-
if (!S_ISCHR (st.st_mode)) {
-
fprintf (stderr, "%s is no device\n", dev_name);
-
exit (EXIT_FAILURE);
-
}
-
-
fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
-
-
if (-1 == fd) {
-
fprintf (stderr, "Cannot open '%s': %d, %s\n",
-
dev_name, errno, strerror (errno));
-
exit (EXIT_FAILURE);
-
}
-
}
-
-
static void
-
usage (FILE * fp,
-
int argc,
-
char ** argv)
-
{
-
fprintf (fp,
-
"Usage: %s [options]\n\n"
-
"Options:\n"
-
"-d | --device name Video device name [/dev/video]\n"
-
"-h | --help Print this message\n"
-
"-m | --mmap Use memory mapped buffers\n"
-
"-r | --read Use read() calls\n"
-
"-u | --userp Use application allocated buffers\n"
-
"",
-
argv[0]);
-
}
-
-
static const char short_options [] = "d:hmru";
-
-
static const struct option
-
long_options [] = {
-
{ "device", required_argument, NULL, 'd' },
-
{ "help", no_argument, NULL, 'h' },
-
{ "mmap", no_argument, NULL, 'm' },
-
{ "read", no_argument, NULL, 'r' },
-
{ "userp", no_argument, NULL, 'u' },
-
{ 0, 0, 0, 0 }
-
};
-
-
int
-
main (int argc,
-
char ** argv)
-
{
-
//dev_name = "/dev/video";
-
dev_name = "/dev/video0";
-
for (;;) {
-
int index;
-
int c;
-
-
c = getopt_long (argc, argv,
-
short_options, long_options,
-
&index);
-
-
if (-1 == c)
-
break;
-
-
switch (c) {
-
case 0: /* getopt_long() flag */
-
break;
-
-
case 'd':
-
dev_name = optarg;
-
break;
-
-
case 'h':
-
usage (stdout, argc, argv);
-
exit (EXIT_SUCCESS);
-
-
case 'm':
-
io = IO_METHOD_MMAP;
-
break;
-
-
case 'r':
-
io = IO_METHOD_READ;
-
break;
-
-
case 'u':
-
io = IO_METHOD_USERPTR;
-
break;
-
-
default:
-
usage (stderr, argc, argv);
-
exit (EXIT_FAILURE);
-
}
-
}
-
-
open_device ();
-
-
init_device ();
-
-
start_capturing ();
-
-
mainloop ();
-
-
stop_capturing ();
-
-
uninit_device ();
-
-
close_device ();
-
-
exit (EXIT_SUCCESS);
-
-
return 0;
-
}
把frame buffer 映射到用户控件, 然后可以操作fb的用户控件的达到控制像素点的功能.
-
int InitFBDevice(
-
uint8_t **ppu8FBBuf,
-
uint32_t *pu32FBBufSize
-
)
-
{
-
int32_t i32FBFd;
-
uint8_t *pu8FBBuf;
-
char achFBDevice[] = "/dev/fb0";
-
static struct fb_var_screeninfo sVar;
-
*ppu8FBBuf = MAP_FAILED;
-
*pu32FBBufSize = 0;
-
i32FBFd = open(achFBDevice, O_RDWR); printf("fb open successful\n");
-
ioctl(i32FBFd, FBIOGET_VSCREENINFO, &sVar) ;
-
s_i32FBWidth = sVar.xres;
-
s_i32FBHeight = sVar.yres;
-
pu8FBBuf = mmap( NULL, s_i32FBWidth*s_i32FBHeight*2 , PROT_READ|PROT_WRITE, MAP_SHARED, i32FBFd, 0 );
-
if ((unsigned char*)-1 == pu8FBBuf) {
-
close(i32FBFd);
-
return -1;
-
}
-
*pu32FBBufSize = s_i32FBWidth*s_i32FBHeight*2;
-
ioctl(i32FBFd, IOCTL_LCD_DISABLE_INT, 0);
-
ioctl(i32FBFd, VIDEO_FORMAT_CHANGE, DISPLAY_MODE_YCBYCR);
-
ioctl(i32FBFd, IOCTL_LCD_ENABLE_INT, 0);
-
*ppu8FBBuf = pu8FBBuf;
-
return i32FBFd;
-
}
从各个设备节点点取出其名字. 对应判断是哪个设备. 如果从/dev/eventX中取出名字, 判断是键盘/鼠标/触屏等等.
-
char achDevName[256];
-
char achKeypad[] = "Keypad"; //想要找的设备名称, 对应 driver 里边的name.
-
char achKpdDevice[] = "/dev/input/event0";
-
int32_t i32KpdFd= -1;
-
uint32_t u32Result;
-
int32_t i;
-
u32Result = 0;
-
for (i = 0; i < RETRY_NUM; i++) {
-
DEBUG_PRINT("trying to open %s ...\n", achKpdDevice);
-
if ((i32KpdFd=open(achKpdDevice, O_RDONLY | O_NONBLOCK)) < 0) {
-
break;
-
}
-
memset(achDevName, 0x0, sizeof(achDevName));
-
if (ioctl(i32KpdFd, EVIOCGNAME(sizeof(achDevName)), achDevName) < 0) { //取出设备名. 等同于操作 /sys/class/input/input0/name
-
DEBUG_PRINT("%s evdev ioctl error!\n", achKpdDevice);
-
}
-
else{
-
if (strstr(achDevName, achKeypad) != NULL) { //比较名字.
-
DEBUG_PRINT("Keypad device is %s\n", achKpdDevice);
-
u32Result = 1;
-
break;
-
}
-
}
-
close(i32KpdFd);
-
achKpdDevice[16]++;
-
}
-
if (u32Result == 0) {
-
DEBUG_PRINT("can not find any Keypad device!\n");
-
return -1;
-
}
-
return i32KpdFd;
阅读(3394) | 评论(0) | 转发(0) |