Browse Source

Merge remote-tracking branch 'upstream/master'

Max Nuding 12 years ago
parent
commit
ec0f42c88d
5 changed files with 331 additions and 1 deletions
  1. 2
    1
      .gitignore
  2. 82
    0
      Cube Control/helper/unixSerial.c
  3. 101
    0
      Cube Control/helper/winSerial.c
  4. 21
    0
      Cube Control/makefile
  5. 125
    0
      Cube Control/serialHelper.c

+ 2
- 1
.gitignore View File

@@ -1,2 +1,3 @@
1 1
 ./.DS_Store
2
-Cube Control/.DS_Store
2
+Cube Control/.DS_Store
3
+Cube Control/helper/.DS_Store

+ 82
- 0
Cube Control/helper/unixSerial.c View File

@@ -0,0 +1,82 @@
1
+/*
2
+ *
3
+ * unixSerial.c
4
+ *
5
+ * POSIX compatible serial port library
6
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
7
+ *
8
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
9
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
10
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
11
+ *
12
+ * This file is part of LED-Cube.
13
+ *
14
+ * LED-Cube is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU General Public License as published by
16
+ * the Free Software Foundation, either version 3 of the License, or
17
+ * (at your option) any later version.
18
+ *
19
+ * LED-Cube is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License
25
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
26
+ */
27
+
28
+#include <stdio.h>
29
+#include <unistd.h>
30
+#include <fcntl.h>
31
+#include <termios.h>
32
+
33
+#define BAUD B19200
34
+
35
+int fd = -1;
36
+
37
+// Open the serial port
38
+int serialOpen(char *port) {
39
+	struct termios options;
40
+
41
+	if (fd != -1) {
42
+		close(fd);
43
+	}
44
+	fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
45
+	if (fd == -1) {
46
+		return -1;
47
+	}
48
+	
49
+	fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
50
+	tcgetattr(fd, &options);
51
+	cfsetispeed(&options, BAUD); // Set speed
52
+	cfsetospeed(&options, BAUD);
53
+	options.c_cflag |= (CLOCAL | CREAD);
54
+	
55
+	options.c_cflag &= ~PARENB; // 8N1
56
+	options.c_cflag &= ~CSTOPB;
57
+	options.c_cflag &= ~CSIZE;
58
+	options.c_cflag |= CS8;
59
+	
60
+	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
61
+	options.c_oflag &= ~OPOST; // Raw output
62
+	options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
63
+
64
+	tcsetattr(fd, TCSANOW, &options);
65
+
66
+	return 0;
67
+}
68
+
69
+// Write to port. Returns number of characters sent, -1 on error
70
+ssize_t serialWrite(char *data, size_t length) {
71
+	return write(fd, data, length);
72
+}
73
+
74
+// Read from port. Return number of characters read, 0 if none available, -1 on error
75
+ssize_t serialRead(char *data, size_t length) {
76
+	return read(fd, data, length);
77
+}
78
+
79
+// Close the serial Port
80
+void serialClose(void) {
81
+	close(fd);
82
+}

+ 101
- 0
Cube Control/helper/winSerial.c View File

@@ -0,0 +1,101 @@
1
+/*
2
+ *
3
+ * winSerial.c
4
+ *
5
+ * Windows 16 (& 32 & 64?) compatible serial port library
6
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
7
+ *
8
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
9
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
10
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
11
+ *
12
+ * This file is part of LED-Cube.
13
+ *
14
+ * LED-Cube is free software: you can redistribute it and/or modify
15
+ * it under the terms of the GNU General Public License as published by
16
+ * the Free Software Foundation, either version 3 of the License, or
17
+ * (at your option) any later version.
18
+ *
19
+ * LED-Cube is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License
25
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
26
+ */
27
+
28
+#include <stdio.h>
29
+#include <windows.h>
30
+
31
+#define BAUD CBR_19200;
32
+
33
+HANDLE hSerial = INVALID_HANDLE_VALUE;
34
+
35
+// Open the serial port
36
+int serialOpen(char *port) {
37
+	DCB dcbSerialParams = {0};
38
+	COMMTIMEOUTS timeouts = {0};
39
+
40
+	hSerial = CreateFile(port GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
41
+
42
+	if (hSerial == INVALID_HANDLE_VALUE) {
43
+		return -1;
44
+	}
45
+	
46
+
47
+	dcbSerial.DCBlength = sizeof(dcbSerialParams);
48
+	if (!GetCommState(hSerial, &dcbSerialParams)) {
49
+		CloseHandle(hSerial);
50
+		hSerial = INVALID_HANDLE_VALUE;
51
+		return -1;
52
+	}
53
+	dcbSerialParams.BaudRate = BAUD;
54
+	dcbSerialParams.ByteSize = 8;
55
+	dcbSerialParams.StopBits = ONESTOPBIT;
56
+	dcbSerialParams.Parity = NOPARITY;
57
+	if (!SetCommState(hSerial, &dcbSerialParams)) {
58
+		CloseHandle(hSerial);
59
+		hSerial = INVALID_HANDLE_VALUE;
60
+		return -1;
61
+	}
62
+	
63
+	timeouts.ReadIntervalTimeout = 50;
64
+	timeouts.ReadTotalTimeoutConstant = 50;
65
+	timeouts.ReadTotalTimeoutMultiplier = 10;
66
+	timeouts.WriteTotalTimeoutConstant = 50;
67
+	timeouts.WriteTotalTimeoutMultiplier = 10;
68
+
69
+	if (!SetCommTimeouts(hSerial, &timeouts)) {
70
+		CloseHandle(hSerial);
71
+		hSerial = INVALID_HANDLE_VALUE;
72
+		return -1;
73
+	}
74
+
75
+	return 0;
76
+}
77
+
78
+// Write to port. Returns number of characters sent, -1 on error
79
+ssize_t serialWrite(char *data, size_t length) {
80
+	DWORD dwBytesWritten = 0;
81
+	if (!WriteFile(hSerial, data, length, &dwBytesWritten, NULL)) {
82
+		return -1;
83
+	}
84
+	return dwBytesWritten;
85
+
86
+}
87
+
88
+// Read from port. Return number of characters read, 0 if none available, -1 on error
89
+ssize_t serialRead(char *data, size_t length) {
90
+	DWORD dwBytesRead = 0;
91
+	if (!ReadFile(hSerial, data, length, &dwBytesRead, NULL)) {
92
+		return -1;
93
+	}
94
+	return dwBytesRead;
95
+}
96
+
97
+// Close the serial Port
98
+void serialClose(void) {
99
+	CloseHandle(hSerial);
100
+	hSerial = INVALID_HANDLE_VALUE;
101
+}

+ 21
- 0
Cube Control/makefile View File

@@ -0,0 +1,21 @@
1
+JAVAC = javac
2
+CC = gcc
3
+TARGET = unix
4
+#TARGET = win
5
+
6
+
7
+all: java
8
+
9
+java: serialHelper
10
+	$(JAVAC) *.java
11
+
12
+serialHelper:
13
+ifeq ($(TARGET),win)
14
+	$(CC) -o serialHelper.exe -D winHelper serialHelper.c
15
+else
16
+	$(CC) -o serialHelper serialHelper.c
17
+endif
18
+
19
+clean:
20
+	rm -f *.class
21
+	rm -f serialHelper

+ 125
- 0
Cube Control/serialHelper.c View File

@@ -0,0 +1,125 @@
1
+/*
2
+ * unixHelper.c
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+#include <stdlib.h>
24
+#include <stdio.h>
25
+
26
+#ifdef winHelper
27
+#include "helper/winSerial.c"
28
+#else
29
+#include "helper/unixSerial.c"
30
+#endif
31
+
32
+char *fileData = NULL;
33
+
34
+void removeFromBeginning(size_t size, size_t remove);
35
+size_t readFile(char *path);
36
+size_t getFileSize(FILE *fp);
37
+
38
+/*
39
+Return values:
40
+0: Success
41
+1: Usage error
42
+2: Serial Port Error
43
+3: Data File Error
44
+*/
45
+int main(int argc, char *argv[]) {
46
+	size_t length, written;
47
+	
48
+	if (argc < 3) {
49
+#ifdef winHelper
50
+		printf("Usage:\n%s COM1 C:\\file\\to\\send.txt\n", argv[0]);
51
+#else
52
+		printf("Usage:\n%s /dev/SerialPort /file/to/send\n", argv[0]);
53
+#endif
54
+		return 1;
55
+	} else {
56
+		if (serialOpen(argv[1]) != 0) {
57
+			printf("Error: Could not open %s\n", argv[1]);
58
+			return 2;
59
+		}
60
+
61
+		length = readFile(argv[2]);
62
+		if (length == 0) {
63
+			printf("Error while reading %s\n", argv[2]);
64
+			return 3;
65
+		}
66
+
67
+		written = serialWrite(fileData, length);
68
+		while (written < length) {
69
+			removeFromBeginning(length, written);
70
+			length -= written;
71
+			written = serialWrite(fileData, length);
72
+		}
73
+
74
+		free(fileData);
75
+		fileData = NULL;
76
+		serialClose();
77
+		return 0;
78
+	}
79
+}
80
+
81
+void removeFromBeginning(size_t size, size_t remove) {
82
+	size_t i;
83
+	char *tmp = (char *)malloc((size - remove) * sizeof(char));
84
+
85
+	for (i = 0; i < (size - remove); i++) {
86
+		tmp[i] = fileData[i + remove];
87
+	}
88
+	free(fileData);
89
+	fileData = tmp;
90
+}
91
+
92
+size_t readFile(char *path) {
93
+	size_t size, i;
94
+	FILE *fp;
95
+
96
+	fp = fopen(path, "r");
97
+	if (!fp) {
98
+		return 0;
99
+	}
100
+
101
+	size = getFileSize(fp);
102
+	fileData = (char *)malloc(size * sizeof(char));
103
+	for (i = 0; i < size; i++) {
104
+		fileData[i] = fgetc(fp);
105
+	}
106
+
107
+	fclose(fp);
108
+	return size;
109
+}
110
+
111
+size_t getFileSize(FILE *fp) {
112
+	size_t size = 0;
113
+	int c;
114
+
115
+	fseek(fp, 0, 0); // Set file pointer to beginning
116
+	
117
+	do { // Calculate size
118
+		c = fgetc(fp);
119
+		size++;
120
+	} while (c != EOF);
121
+	
122
+	fseek(fp, 0, 0);
123
+
124
+	return size;
125
+}

Loading…
Cancel
Save