123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- /*
- * LED-Cube Hardware Emulator.
- * Creates a new pseudo terminal and emulates the LED Cube Hardware.
- * Used for testing of CubeControl Software.
- *
- * main.c
- *
- * Copyright 2012 Thomas Buck <xythobuz@me.com>
- *
- * This file is part of LED-Cube.
- *
- * LED-Cube is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * LED-Cube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <strings.h>
-
- #include "serial.h"
- #include "mem.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;
-
- #define printf if(keepRunning)printf
-
- 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;
- }
- printf("OK!\n");
- break;
-
- case 'h': case 'H':
- printf("Help\n");
- 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':
- printf("Version\n");
- if (serialWriteString(VERSION)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
-
- case 's': case 'S':
- printf("Recieving... ");
- if (recieveFrames()) {
- printf("Error while recieving frames!\n");
- return -1;
- }
- printf("Done!\n");
- break;
-
- case 'g': case 'G':
- printf("Sending... ");
- if (sendFrames()) {
- printf("Error while sending frames!\n");
- return -1;
- }
- printf("Done!\n");
- break;
-
- case 'd': case 'D':
- printf("Deleting... ");
- if (deleteFrames()) {
- printf("Error while deleting frames!\n");
- return -1;
- }
- printf("Done!\n");
- break;
-
- default:
- printf("Unknown. Error!\n");
- c = ERROR;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- break;
- }
- }
- }
-
- serialClose();
-
- return 0;
- }
-
- int sendFrames() {
- char c;
- ssize_t size;
- int frameCount, f, d;
- char *data;
-
- // Acknowledge sending this
- c = OK;
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- // Send animation count. We dont store animations --> send 1
- printf("Sending animation count.\n");
- c = 1;
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- // Await OK from Host software
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Couldn't read from pseudo terminal");
- return -1;
- }
- }
- if (c != OK) {
- printf("Invalid acknowledge from Host (%d)!", c);
- return -1;
- }
-
- // Send framecount
- frameCount = framesStored();
- printf("Sending frameCount = %d\n", frameCount);
- c = frameCount;
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- // Await OK from Host software
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Couldn't read from pseudo terminal");
- return -1;
- }
- }
- if (c != OK) {
- printf("Invalid acknowledge from Host (%d)!", c);
- return -1;
- }
-
- for(f = 0; f < frameCount; f++){
- data = getFrame(f);
-
- printf("Sending duration = %d of frame %d\n", data[64], f);
- c = data[64]; // duration at end of frame data
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- // Await OK from Host software
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Couldn't read from pseudo terminal");
- return -1;
- }
- }
- if (c != OK) {
- printf("Invalid acknowledge from Host (%d)!", c);
- return -1;
- }
-
- printf("Sending frame data...");
- for(d = 0; d < 64; d++) {
- c = data[d];
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- }
- printf(" %d successfully sent\n", f);
-
- // Await OK from Host software
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Couldn't read from pseudo terminal");
- return -1;
- }
- }
- if (c != OK) {
- printf("Invalid acknowledge from Host (%d)!", c);
- return -1;
- }
- }
-
- printf("Done sending frames, start sending final sequence\n");
- c = OK;
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- if(serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- // Await OK from Host software
- while(keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Couldn't read from pseudo terminal");
- return -1;
- }
- }
- if (c != OK) {
- printf("Invalid acknowledge from Host (%d)!", c);
- }
-
- return 0;
- }
-
- int recieveFrames() {
- char c;
- ssize_t size;
- int animCount, a, frameCount, f, d;
- char data[65];
-
- clearMemory();
-
- // First send acknowledge
- c = OK;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- printf("AnimationCount");
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- printf(" = %d\n", c);
- animCount = c;
-
- c = OK;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- for (a = 0; a < animCount; a++) {
- printf("FrameCount");
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- printf(" = %d\n", c);
- frameCount = c;
-
- c = OK;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- for (f = 0; f < frameCount; f++) {
- printf("Duration");
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- printf(" = %d\n", c);
- data[64] = c;
-
- // Acknowledge frame duration
- c = OK;
- if(serialWriteTry(&c, 1)){
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- printf("Data...");
- for (d = 0; d < 64; d++) {
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break; // We got our data byte
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- data[d] = c;
- }
- printf(" Done!\n");
-
- addFrame(data);
-
- // Acknowledge frame data
- c = OK;
- if(serialWriteTry(&c, 1)){
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- }
- }
-
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
- while (keepRunning) {
- size = serialRead(&c, 1);
- if (size == 1) {
- break;
- } else if (size == -1) {
- printf("Could not read from psuedo terminal!\n");
- return -1;
- }
- }
-
- printf("Got 4 OKs!\n");
-
- c = OK;
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
-
- return 0;
- }
-
- int deleteFrames() {
- char c = OK;
- clearMemory();
- if (serialWriteTry(&c, 1)) {
- printf("Could not write to pseudo terminal\n");
- return -1;
- }
- return 0;
- }
-
- 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 (keepRunning) {
- 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) {
- printf("\nExiting...\n");
- keepRunning = 0;
- serialClose();
- exit(0);
- }
|