Browse Source

Created Hardware Emulator.

Runs on Unix and creates a virtual serial port that can be opened by
CubeControl. Only responds to OK, (h)elp and (v)ersion right now.
Thomas Buck 12 years ago
parent
commit
07b9d556f5
4 changed files with 236 additions and 0 deletions
  1. 113
    0
      HardwareEmulator/main.c
  2. 19
    0
      HardwareEmulator/makefile
  3. 83
    0
      HardwareEmulator/serial.c
  4. 21
    0
      HardwareEmulator/serial.h

+ 113
- 0
HardwareEmulator/main.c View File

@@ -0,0 +1,113 @@
1
+/*
2
+ * LED-Cube Hardware Emulator.
3
+ * Creates a new pseudo terminal and emulates the LED Cube Hardware.
4
+ * Used for testing of CubeControl Software.
5
+ */
6
+#include <stdlib.h>
7
+#include <stdio.h>
8
+#include <strings.h>
9
+
10
+#include "serial.h"
11
+
12
+#define VERSION "LED-Cube Emu V1\n"
13
+
14
+#define OK 0x42
15
+#define ERROR 0x23
16
+
17
+int serialWriteString(char *s);
18
+int serialWriteTry(char *data, size_t length);
19
+void intHandler(int dummy);
20
+
21
+volatile int keepRunning = 1;
22
+
23
+int main(int argc, char *argv[]) {
24
+	char c;
25
+	ssize_t size;
26
+	char *slave;
27
+
28
+	if ((slave = serialOpen()) == NULL) {
29
+		printf("Could not open a pseudo Terminal!\n");
30
+		return -1;
31
+	}
32
+
33
+	printf("Waiting for CubeControl on \"%s\"...\n", slave);
34
+
35
+	signal(SIGINT, intHandler);
36
+	signal(SIGQUIT, intHandler);
37
+	printf("Stop with CTRL+C...\n");
38
+
39
+	while(keepRunning) {
40
+		size = serialRead(&c, 1);
41
+		if (size == -1) {
42
+			// Error while reading
43
+			printf("Could not read from psuedo terminal!\n");
44
+			return -1;
45
+		}
46
+		if (size == 1) {
47
+			switch(c) {
48
+				case OK:
49
+					if (serialWriteTry(&c, 1)) {
50
+						printf("Could not write to pseudo terminal");
51
+						return -1;
52
+					}
53
+					break;
54
+
55
+				case 'h': case 'H':
56
+					if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
57
+						printf("Could not write to pseudo terminal");
58
+						return -1;
59
+					}
60
+					break;
61
+
62
+				case 'v': case 'V':
63
+					if (serialWriteString(VERSION)) {
64
+						printf("Could not write to pseudo terminal");
65
+						return -1;
66
+					}
67
+					break;
68
+
69
+				default:
70
+					c = ERROR;
71
+					if (serialWriteTry(&c, 1)) {
72
+						printf("Could not write to pseudo terminal");
73
+						return -1;
74
+					}
75
+					break;
76
+			}
77
+		}
78
+	}
79
+
80
+	serialClose();
81
+
82
+	return 0;
83
+}
84
+
85
+int serialWriteString(char *s) {
86
+	return serialWriteTry(s, strlen(s));
87
+}
88
+
89
+int serialWriteTry(char *data, size_t length) {
90
+	int i = 0;
91
+	int written = 0;
92
+	int ret;
93
+	while (1) {
94
+		ret = serialWrite((data + written), (length - written));
95
+		if (ret == -1) {
96
+			i++;
97
+		} else {
98
+			written += ret;
99
+		}
100
+		if (i > 10) {
101
+			return 1;
102
+		}
103
+		if (written == length) {
104
+			break;
105
+		}
106
+	}
107
+	return 0;
108
+}
109
+
110
+void intHandler(int dummy) {
111
+	keepRunning = 0;
112
+	printf("\nExiting...\n");
113
+}

+ 19
- 0
HardwareEmulator/makefile View File

@@ -0,0 +1,19 @@
1
+CC = gcc
2
+SRC = main.c
3
+SRC += serial.c
4
+
5
+TARGET = cubeEmu
6
+
7
+OBJ = $(SRC:.c=.o)
8
+
9
+all: $(TARGET)
10
+
11
+$(TARGET): $(OBJ)
12
+	$(CC) -o $(TARGET) $(OBJ)
13
+
14
+%.o: %.c
15
+	$(CC) -x c -c $< -o $@
16
+
17
+clean:
18
+	$(RM) *.o
19
+	$(RM) $(TARGET)

+ 83
- 0
HardwareEmulator/serial.c View File

@@ -0,0 +1,83 @@
1
+/*
2
+ * POSIX compatible serial port library
3
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
4
+ * By: Thomas Buck <taucher.bodensee@gmail.com>
5
+ * Visit: www.xythobuz.org
6
+ */
7
+
8
+#include <stdio.h>
9
+#include <stdlib.h>
10
+#include <string.h>
11
+#include <unistd.h>
12
+#include <fcntl.h>
13
+#include <termios.h>
14
+#include <dirent.h>
15
+#include <errno.h>
16
+
17
+#include "serial.h"
18
+
19
+int fd = -1;
20
+
21
+// Open pseudo terminal
22
+// return name of slave side or NULL on error.
23
+char *serialOpen() {
24
+	struct termios options;
25
+
26
+	if (fd != -1) {
27
+		close(fd);
28
+	}
29
+
30
+	fd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
31
+	if (fd == -1) {
32
+		return NULL;
33
+	}
34
+
35
+	// Set rigths
36
+	if (grantpt(fd) != 0) {
37
+		return NULL;
38
+	}
39
+
40
+	// Unlock slave
41
+	if (unlockpt(fd) != 0) {
42
+		return NULL;
43
+	}
44
+	
45
+	fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
46
+	tcgetattr(fd, &options);
47
+	cfsetispeed(&options, BAUD); // Set speed
48
+	cfsetospeed(&options, BAUD);
49
+	options.c_cflag |= (CLOCAL | CREAD);
50
+	
51
+	options.c_cflag &= ~PARENB; // 8N1
52
+	options.c_cflag &= ~CSTOPB;
53
+	options.c_cflag &= ~CSIZE;
54
+	options.c_cflag |= CS8;
55
+	
56
+	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
57
+	options.c_oflag &= ~OPOST; // Raw output
58
+	options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
59
+
60
+	tcsetattr(fd, TCSANOW, &options);
61
+
62
+	return ptsname(fd);
63
+}
64
+
65
+// Write to port. Returns number of characters sent, -1 on error
66
+ssize_t serialWrite(char *data, size_t length) {
67
+	return write(fd, data, length);
68
+}
69
+
70
+// Read from port. Return number of characters read, 0 if none available, -1 on error
71
+ssize_t serialRead(char *data, size_t length) {
72
+	ssize_t temp = read(fd, data, length);
73
+	if ((temp == -1) && (errno == EAGAIN)) {
74
+		return 0;
75
+	} else {
76
+		return temp;
77
+	}
78
+}
79
+
80
+// Close the serial Port
81
+void serialClose(void) {
82
+	close(fd);
83
+}

+ 21
- 0
HardwareEmulator/serial.h View File

@@ -0,0 +1,21 @@
1
+/*
2
+ * POSIX compatible serial port library
3
+ * Uses 8 databits, no parity, 1 stop bit, no handshaking
4
+ * By: Thomas Buck <taucher.bodensee@gmail.com>
5
+ * Visit: www.xythobuz.org
6
+ */
7
+
8
+// Use POSIX Baud constants (B2400, B9600...)
9
+#define BAUD B38400
10
+
11
+// Open pseudo terminal. Return name of slave on success, NULL on error.
12
+char *serialOpen(void);
13
+
14
+// Write to port. Returns number of characters sent, -1 on error
15
+ssize_t serialWrite(char *data, size_t length);
16
+
17
+// Read from port. Return number of characters read, 0 if none available, -1 on error
18
+ssize_t serialRead(char *data, size_t length);
19
+
20
+// Close the serial Port
21
+void serialClose(void);

Loading…
Cancel
Save