📄 vidcat.c
字号:
/*
* vidcat.c
*
* Copyright (C) 1998 - 2001 Rasca, Berlin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h> /* gettimeofday() */
#include <fcntl.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/videodev.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
#define MYPORT 5555
#define BACKLOG 10
#include "v4l.h"
#include "bitmap.h"
static char *order[]={"temp","left","righ","upup","down","stop"};
typedef struct _SENDTEMP
{
char tmpbuf[5];
int temp;
}SENDTEMP;
char *basename (const char *s);
static int tcpflag=0;
/* globals
*/
static int verbose = 0;
/*
* write png image to stdout
*/
void put_image_png (FILE *out, char *image, int width, int height, int palette)
{
#ifdef HAVE_LIBPNG
int y, bpp;
char *p;
png_infop info_ptr;
png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
return;
info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
return;
png_init_io (png_ptr, out);
if (palette == VIDEO_PALETTE_GREY) {
png_set_IHDR (png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
bpp = 1;
} else {
png_set_IHDR (png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
bpp = 3;
}
png_set_bgr (png_ptr);
png_write_info (png_ptr, info_ptr);
p = image;
for (y = 0; y < height; y++) {
png_write_row (png_ptr, p);
p += width * bpp;
}
png_write_end (png_ptr, info_ptr);
#endif
}
/*
* write ppm image to stdout / file
*/
void put_image_ppm (FILE *out, char *image, int width, int height, int binary)
{
int x, y, ls=0;
unsigned char *p = (unsigned char *)image;
if (!binary) {
fprintf (out, "P3\n%d %d\n%d\n", width, height, 255);
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
fprintf (out, "%03d %03d %03d ", p[2], p[1], p[0]);
p += 3;
if (ls++ > 4) {
fprintf (out, "\n");
ls = 0;
}
}
}
fprintf (out, "\n");
} else {
unsigned char buff[3];
fprintf (out, "P6\n%d %d\n%d\n", width, height, 255);
for (x = 0; x < width * height; x++) {
buff[0] = p[2];
buff[1] = p[1];
buff[2] = p[0];
fwrite (buff, 1, 3, out);
p += 3;
}
}
fflush (out);
}
/*
* write pgm image to stdout / file
*/
void put_image_pgm (FILE *out, char *image, int width, int height, int binary)
{
int x, y, ls=0;
unsigned char *p = (unsigned char *)image;
if (!binary) {
fprintf (out, "P2\n%d %d\n%d\n", width, height, 255);
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
fprintf (out, "%03d ", p[0]);
p++;
if (ls++ > 4) {
fprintf (out, "\n");
ls = 0;
}
}
}
fprintf (out, "\n");
} else {
fprintf (out, "P5\n%d %d\n%d\n", width, height, 255);
for (x = 0; x < width * height; x++) {
fwrite (p, 1, 1, out);
p++;
}
}
fflush (out);
}
void ctlthread(void)
{
static int fd=-1;
static int adc_fd=-1;
static int led_fd=-1;
fd_set rdset;
char bufval[16];
int ret=-1;
int result;
int adc_value;
int address_len;
int i,j;
int adc_sum;
char buf[16];
int tempaddress=1;
SENDTEMP sendtemp;
struct timeval tv;
struct sockaddr_in address;
struct sockaddr_in client_address;
struct sockaddr_in temp_address;
socklen_t len = sizeof(client_address);
tv.tv_sec=0;
tv.tv_usec=500000;
adc_fd=open("/dev/adc",0);
if(adc_fd<0)
{
perror("openadc failed\n");
exit(1);
}
led_fd = open("/dev/leds", 0);
if (led_fd < 0) {
perror("open device leds");
exit(1);
}
fd = socket(AF_INET, SOCK_DGRAM, 0); //SOCK_DGRAM
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(5678);
address_len = sizeof(address);
ret=bind(fd, (struct sockaddr *)&address, address_len);
if(ret==-1)
{
printf("udp bind error!\n");
exit(1);
}
while(1)
{ if(tcpflag==1)
{break;}
FD_ZERO(&rdset);
FD_SET(fd,&rdset);
result=select(fd+1,&rdset,NULL,NULL,NULL);
if(result==1)
{
if(FD_ISSET(fd,&rdset))
ret=-1;
ret = recvfrom(fd, buf, 16, 0,
(struct sockaddr *)&client_address, &len);
if(ret==-1)
{ printf("udp receive error!\n");
exit(1);
}
if(tempaddress)
{ temp_address=client_address;
temp_address.sin_port = htons(5678);
tempaddress=0;
}
memcpy(&sendtemp,buf,sizeof(SENDTEMP));
// printf("server received %s %d:%s\n", inet_ntoa(client_address.sin_addr),ntohs(client_address.sin_port), buf);
j=-1;
for(i=0;order[i]!=NULL;i++)
{
if(strncmp(sendtemp.tmpbuf,order[i], 4)==0)
{j=i;break;}
else j=-1;
}
switch(j)
{
case -1:printf("received error message\n");
break;
case 0: printf("temp request\n");
adc_value=0;
adc_sum=0;
for(i=0;i<8;i++)
{
ret=-1;
ret=read(adc_fd,&adc_value,sizeof adc_value);
if(ret!=sizeof(adc_value)){
if(errno!=EAGAIN)perror("read adc\n");
}
else{
adc_sum+=adc_value;
}
}
adc_value=adc_sum/8;
printf("adc_value:%d\n",adc_value);
sendtemp.tmpbuf[0]='t';
sendtemp.tmpbuf[1]='e';
sendtemp.tmpbuf[2]='m';
sendtemp.tmpbuf[3]='p';
sendtemp.tmpbuf[4]='\0';
sendtemp.temp=adc_value;
memcpy(bufval,&sendtemp,sizeof(SENDTEMP));
ret=-1;
ret=sendto(fd, bufval, 16, 0, (struct sockaddr *)&temp_address, len);
if(ret==-1)
{
printf("udp sendtemp error\n");
exit(1);
}
break;
case 1: printf("control request left\n");
ioctl(led_fd,1,0);
ioctl(led_fd,0,1);
ioctl(led_fd,0,2);
ioctl(led_fd,0,3);
break;
case 2: printf("control request right\n");
ioctl(led_fd,0,0);
ioctl(led_fd,1,1);
ioctl(led_fd,0,2);
ioctl(led_fd,0,3);
break;
case 3: printf("control request up\n");
ioctl(led_fd,0,0);
ioctl(led_fd,0,1);
ioctl(led_fd,1,2);
ioctl(led_fd,0,3);
break;
case 4: printf("control request down\n");
ioctl(led_fd,0,0);
ioctl(led_fd,0,1);
ioctl(led_fd,0,2);
ioctl(led_fd,1,3);
break;
case 5:printf("control stop\n");
ioctl(led_fd,0,0);
ioctl(led_fd,0,1);
ioctl(led_fd,0,2);
ioctl(led_fd,0,3);
break;
}
} //result==1;
else if(result<0)
{printf("select error\n");break;}
} //while
if(adc_fd)
close(adc_fd);
if(led_fd)
close(led_fd);
if(fd)
close(fd);
printf("udp thread ended\n");
}
/*
* main()
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -