summaryrefslogtreecommitdiff
path: root/content/demo-programme/ppraider.c
blob: 20288c7f2a6c3de89ad18057520bed9d1bc608c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
  alle datenpins der reihe nach setzen via parport
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>

#include <linux/parport.h>
#include <linux/ppdev.h>


#define DEVICE   "/dev/parport0"

static char *RCSID __attribute__ ((unused))  =
    "$Id: ppraider.c,v 1.1 2006/03/19 15:33:54 walter Exp walter $";


#define SHOW(st,bits)    printf(#bits" %s\n", ( st & bits) ?"on":"off")


int xopenport(char *name, int fdmode)
{
	static int fd;
	int mode;


	mode = O_WRONLY | O_NOCTTY;
	fd = open(name, mode);

	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}



	if (ioctl(fd, PPCLAIM)) {
		perror("PPCLAIM");
		close(fd);
		exit(EXIT_FAILURE);
	}
	mode = IEEE1284_MODE_COMPAT;

	if (ioctl(fd, PPNEGOT, &mode)) {
		perror("PPNEGOT");
		close(fd);
		exit(EXIT_FAILURE);
	}
	return fd;
}

 /* release port close(fd), ignore errors */
void xcloseport(int fd)
{
	ioctl(fd, PPRELEASE);
	close(fd);
}


void xset_data(int fd, int data)
{
	int ret;
	ret = ioctl(fd, PPWDATA, &data);
	if (!ret)
		return;
	fprintf(stderr, "%s failed:%s\n", __func__, strerror(errno));
	exit(EXIT_FAILURE);
}


 /* get status info */
 /* clean up later */
void xget_status(int fd, unsigned char *foo)
{
	int ret;
	unsigned char status;
	ret = ioctl(fd, PPRSTATUS, &status);
	if (!ret) {
		*foo = status;
		return;

	}
	fprintf(stderr, "%s failed:%s\n", __func__, strerror(errno));
	exit(EXIT_FAILURE);
}


int main()
{
  int i,fd;
	fd = xopenport(DEVICE, 0);


	for (i = 0; i < 8; i++) {
		xset_data(fd, 1 << i);
		usleep(500000);
	}

	xset_data(fd, 0);
	xcloseport(fd);

	return 0;
}