1. Jaroslav Beran
  2. PlotterController

Commits

Jaroslav Beran  committed e617b97

Port to FreeBSD

  • Participants
  • Parent commits 745eed6
  • Branches default

Comments (0)

Files changed (7)

File Makefile

View file
 # PlotterController (C) Jaroslav Beran, 2014
 # 
 
-plotter_controller: src/main.o src/graph.o src/printer.o src/parport.o src/text.o src/hpgl.o
-	$(CC) -Wall -ansi -pedantic -o $@ src/main.o src/graph.o src/printer.o src/parport.o src/text.o src/hpgl.o -lm
+CFLAGS = -O2 -Wall -c -o $@
+objects = src/main.o src/graph.o src/printer.o src/parport.o src/text.o src/hpgl.o
+
+plotter_controller: ${objects}
+	$(CC) -Wall -o $@ ${objects} -lm
 	cp var/font1.fnt ./font1.fnt
 
-src/main.o: src/main.c src/main.h src/graph.h src/printer.h src/text.h src/hpgl.h
-src/printer.o: src/printer.c src/printer.h src/parport.h
-src/graph.o: src/graph.c src/graph.h src/printer.h
-src/parport.o: src/parport.c src/parport.h
-src/text.o: src/text.c src/text.h src/graph.h src/printer.h
-src/hpgl.o: src/hpgl.c src/hpgl.h src/graph.h src/printer.h
+main.o: src/main.c src/main.h src/graph.h src/printer.h src/text.h src/hpgl.h
+printer.o: src/printer.c src/printer.h src/parport.h
+graph.o: src/graph.c src/graph.h src/printer.h
+parport.o: src/parport.c src/parport.h
+text.o: src/text.c src/text.h src/graph.h src/printer.h
+hpgl.o: src/hpgl.c src/hpgl.h src/graph.h src/printer.h
 
 clean:
-	rm -f src/*.o src/*.bak plotter_controller plotter_controller.exe *.bak *.fnt
+	rm -f ${objects} src/*.bak plotter_controller plotter_controller.exe *.bak *.fnt
 

File README

View file
 
 2.1 Currently supported hardware
 --------------------------------
-* Linux -> /dev/parportx - > XY41xx
+* Linux -> /dev/parportx -> XY41xx
 This configuration is tested very well. You only need to make sure you have
 rights for reading from and writing to /dev/parportx device.
 
-* MS Windows -> LPTx - > XY41xx
+* FreeBSD -> /dev/ppix -> XY41xx
+This configuration is tested very well. You only need to make sure you have
+rights for reading from and writing to /dev/ppix device.
+
+* MS Windows -> LPTx -> XY41xx
 This configuration is not tested well. In some versions of Winows OS you need 
 additional software (such as userport or porttalk22) for enabling direct access 
 to LPTx ports. See following link:

File src/hpgl.c

View file
 		if ((c == ';') || (strlen(cmd) == MAX_CMD_LEN)) {
 			process_cmd(p, cmd);
 			*cmd = '\0';
-			int len = 0;
+			len = 0;
 		} else {
 			strncat(cmd, &c, 1);
 			len++;

File src/main.c

View file
 #include "hpgl.h"
 #include "main.h"
 
-#define DEVICE "/dev/parport0" 
+#ifdef __linux__
+#define DEVICE "/dev/parport0"
+#endif
+
+#ifdef __FreeBSD__
+#define DEVICE "/dev/ppi0" 
+#endif
+
+#ifdef _WIN32
+#define DEVICE "0x378" 
+#endif
+
 
 
 static int show_menu(PRINTER *prn);
 
 static int show_menu(PRINTER *prn) {
 	printf("PlotterController\n");
-	printf("Build 20140220\n");
+	printf("Build 20140311\n");
 	printf("---------------------------\n\n");
 
 	printf("1) Test page\n");

File src/parport.c

View file
 *	parport.c
 *	10.1.2014
 *	Jaroslav Beran (jaroslav.beran@gmail.com)
-*	(http://mockmoon-cybernetics.ch/computer/linux/programming/parport.html)
 */
 
 
 #include <stdio.h> 
 #include "parport.h"
 
+/*
+*	Linux implementation
+*	(http://mockmoon-cybernetics.ch/computer/linux/programming/parport.html)
+*/
 #ifdef __linux__
 #include <fcntl.h>
+#include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <asm/ioctl.h> 
 #include <linux/parport.h> 
 #include <linux/ppdev.h> 
-#endif
-
-#ifdef _WIN32
-#include <windows.h>
-#include <conio.h>
-#endif
-
-
-#ifdef __linux__
 
 int open_parport(char *device) { 
-	struct ppdev_frob_struct frob; 
-	int fd; int mode; 
+	int fd; 
 	
 	if ((fd=open(device, O_RDWR)) < 0) { 
-		fprintf(stderr, "can not open %s\n", device); 
-		fprintf(stderr, "Probably you don't have permission access %s\n", device);
+		perror(device);
 		return 0; 
 	} 
 	
 }
 
 int read_data(int fd, unsigned char *data) { 
-	int mode, res; 
+	int mode; 
 
 	mode = IEEE1284_MODE_ECP; 
-	res=ioctl(fd, PPSETMODE, &mode);	/* ready to read ? */ 
-	mode=255; 
-	res=ioctl(fd, PPDATADIR, &mode); 	/* switch output driver off */ 
+	ioctl(fd, PPSETMODE, &mode);		/* ready to read */ 
+	mode = 255; 
+	ioctl(fd, PPDATADIR, &mode); 		/* read on */ 
 	usleep(1000); 
-	res=ioctl(fd, PPRDATA, data); 		/* now fetch the data! */ 
+	ioctl(fd, PPRDATA, data); 		/* fetch the data */ 
 	usleep(1000); 
 	mode=0;
-	res=ioctl(fd, PPDATADIR, &mode); 
+	ioctl(fd, PPDATADIR, &mode); 		/* write on */
 	return 0; 
 }
 
 #endif
 
+
+/*
+*	FreeBSD implementation
+*	(http://excamera.com/articles/21/parallel.html)
+*/
+#ifdef __FreeBSD__
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <dev/ppbus/ppi.h>
+#include <dev/ppbus/ppbconf.h>
+
+int open_parport(char *device) { 
+	int fd = open(device, O_RDWR);
+	if(fd < 0) {
+		perror(device);
+		return -1;
+	}
+
+	return fd;
+}
+
+int close_parport(int fd) {
+	close(fd); 
+	return 0; 
+}
+
+int write_data(int fd, unsigned char data) { 
+	if (ioctl(fd, PPISDATA, &data) < 0) {
+		perror("ioctl PPISDATA");
+		return -1;
+	}
+	return 0;
+}
+
+int read_data(int fd, unsigned char *data) { 
+	uint8_t val;
+	ioctl(fd, PPIGCTRL, &val);		/* get ctrl register  */
+	val |= PCD;
+	ioctl(fd, PPISCTRL, &val);		/* read on */
+	usleep(1000);
+	if (ioctl(fd, PPIGDATA, data) < 0) {	/* fetch the data */
+		perror("ioctl PPIGDATA");
+		return -1;
+	}
+	val &= ~PCD;
+	ioctl(fd, PPISCTRL, &val);		/* write on */
+	usleep(1000);
+	return 0;
+}
+
+#endif
+
+
+/*
+*	Windows implementation
+*	
+*/
 #ifdef _WIN32
-/*
-*	Access to parallel port doesn't work under MS Windows
-*	Needs to be fixed !!!
-*/
+#include <windows.h>
+#include <conio.h>
 
 static BOOL bPrivException = FALSE;
 
 	_inp(0x378);  // Try to access the given port address
 
 	if (bPrivException) {
-    	fprintf(stderr, "Privileged instruction exception has occured!\n\n"
-						"To use this program under Windows NT or Windows 2000\n"
-						"you need to install the driver 'UserPort.SYS' and grant\n"
-						"access to the ports used by this program.\n");
-		return 0;											 
-    }
+	fprintf(stderr,	"Privileged instruction exception has occured!\n\n"
+			"To use this program under Windows NT or Windows 2000\n"
+			"you need to install the driver 'UserPort.SYS' and grant\n"
+			"access to the ports used by this program.\n");
+		return 0;
+}
 	
 
 	/* To enable Bidirectional data transfer just set the "Bidirectional" bit (bit 5) in control register. */
-	write_data(0x37A, 32);
 
 	/* Parallel port address */
 	return 0x378;
 }
 
 int read_data(int fd, unsigned char *data) { 
-	*data = _inp(fd); 
+	write_data(0x37A, 32);		/* read on */
+	usleep_win(1000);
+	*data = _inp(fd); 		/* fetch the data */
+	write_data(0x37A, 0);		/* write on */
+	usleep_win(1000);
 	return 0;
 }
 

File src/printer.c

View file
 static void switch_direction(PRINTER *p, int value);
 static int check_cross_limits(PRINTER *p);
 static void sync_virt_curr_position(PRINTER *p);
-static void print_port_status(PRINTER *p);
 static void set_bit(DATA *data, int pos, int value);
 
 
 
 
 /*
-*	Print status
-*/
-static void print_port_status(PRINTER *p) {
-	DATA data = p->data;
-
-	printf("--status--\n");
-	int i;
-	for (i = 0; i < 8; i++) {
-		printf("%d  ", i);
-	}
-	printf("\n");
-	for (i = 0; i < 8; i++) {
-		printf("%u  ", (check_bit(data, i) == 0) ? 0 : 1);
-	}
-	printf("\n");
-	printf("----------\n");
-}
-
-
-/*
 *	Set bit
 */
 static void set_bit(DATA *data, int pos, int value) {

File src/text.c

View file
 static char **font = NULL;
 static int char_size = 10;		/* in mm */
 static int char_h_dist = 2;		/* in mm */
-static int char_v_dist = 3;		/* in mm */
 static double text_angle = 0;
 
 
 static void draw_char(PRINTER *p, unsigned char c);
-static POSITION transform_position(int x, int y, double angle);
 static char * get_first_command(char *s, int p[], int size);
 static int load_font(char *font_definition_file);
 static void free_font(void);
 	int i = 0;
 	while (fgets(line, sizeof(line), fr) != NULL) {
 
-		if ((strlen > 0) && strncmp(line, "#", 1) && strncmp(line, "\n", 1)) {
+		if ((strlen(line) > 0) && strncmp(line, "#", 1) && strncmp(line, "\n", 1)) {
 
 			if (i == FONT_TABLE_SIZE) {
 				printf("Error: Font definition file is longer than %d\n", FONT_TABLE_SIZE);