123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /*
- * LED-Cube Hardware Emulator.
- * Creates a new pseudo terminal and emulates the LED Cube Hardware.
- * Used for testing of CubeControl Software.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <strings.h>
-
- #include "serial.h"
-
- #define VERSION "LED-Cube Emu V1\n"
-
- #define OK 0x42
- #define ERROR 0x23
-
- int sendFrames(void);
- int recieveFrames(void);
- int deleteFrames(void);
- int serialWriteString(char *s);
- int serialWriteTry(char *data, size_t length);
- void intHandler(int dummy);
-
- volatile int keepRunning = 1;
-
- int main(int argc, char *argv[]) {
- char c;
- ssize_t size;
- char *slave;
-
- if ((slave = serialOpen()) == NULL) {
- printf("Could not open a pseudo Terminal!\n");
- return -1;
- }
-
- printf("Waiting for CubeControl on \"%s\"...\n", slave);
-
- signal(SIGINT, intHandler);
- signal(SIGQUIT, intHandler);
- printf("Stop with CTRL+C...\n");
-
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == -1) {
- // Error while reading
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- if (size == 1) {
- switch(c) {
- case OK:
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
-
- case 'h': case 'H':
- if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
-
- case 'v': case 'V':
- if (serialWriteString(VERSION)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
-
- case 's': case 'S':
- if (recieveFrames()) {
- printf("Error while recieving frames!\n");
- return -1;
- }
- break;
-
- case 'g': case 'G':
- if (sendFrames()) {
- printf("Error while sending frames!\n");
- return -1;
- }
- break;
-
- case 'd': case 'D':
- if (deleteFrames()) {
- printf("Error while deleting frames!\n");
- return -1;
- }
- break;
-
- default:
- c = ERROR;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
- }
- }
- }
-
- serialClose();
-
- return 0;
- }
-
- int sendFrames() {
-
- }
-
- int recieveFrames() {
-
- }
-
- int deleteFrames() {
-
- }
-
- int serialWriteString(char *s) {
- return serialWriteTry(s, strlen(s));
- }
-
- int serialWriteTry(char *data, size_t length) {
- int i = 0;
- int written = 0;
- int ret;
- while (1) {
- ret = serialWrite((data + written), (length - written));
- if (ret == -1) {
- i++;
- } else {
- written += ret;
- }
- if (i > 10) {
- return 1;
- }
- if (written == length) {
- break;
- }
- }
- return 0;
- }
-
- void intHandler(int dummy) {
- keepRunning = 0;
- printf("\nExiting...\n");
- }
|