Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include "serial.h"
  10. #define VERSION "LED-Cube Emu V1\n"
  11. #define OK 0x42
  12. #define ERROR 0x23
  13. int sendFrames(void);
  14. int recieveFrames(void);
  15. int deleteFrames(void);
  16. int serialWriteString(char *s);
  17. int serialWriteTry(char *data, size_t length);
  18. void intHandler(int dummy);
  19. volatile int keepRunning = 1;
  20. int main(int argc, char *argv[]) {
  21. char c;
  22. ssize_t size;
  23. char *slave;
  24. if ((slave = serialOpen()) == NULL) {
  25. printf("Could not open a pseudo Terminal!\n");
  26. return -1;
  27. }
  28. printf("Waiting for CubeControl on \"%s\"...\n", slave);
  29. signal(SIGINT, intHandler);
  30. signal(SIGQUIT, intHandler);
  31. printf("Stop with CTRL+C...\n");
  32. while(keepRunning) {
  33. size = serialRead(&c, 1);
  34. if (size == -1) {
  35. // Error while reading
  36. printf("Could not read from psuedo terminal!\n");
  37. return -1;
  38. }
  39. if (size == 1) {
  40. switch(c) {
  41. case OK:
  42. if (serialWriteTry(&c, 1)) {
  43. printf("Could not write to pseudo terminal\n");
  44. return -1;
  45. }
  46. printf("OK!\n");
  47. break;
  48. case 'h': case 'H':
  49. printf("Help\n");
  50. if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
  51. printf("Could not write to pseudo terminal\n");
  52. return -1;
  53. }
  54. break;
  55. case 'v': case 'V':
  56. printf("Version\n");
  57. if (serialWriteString(VERSION)) {
  58. printf("Could not write to pseudo terminal\n");
  59. return -1;
  60. }
  61. break;
  62. case 's': case 'S':
  63. printf("Recieving... ");
  64. if (recieveFrames()) {
  65. printf("Error while recieving frames!\n");
  66. return -1;
  67. }
  68. printf("Done!\n");
  69. break;
  70. case 'g': case 'G':
  71. printf("Sending... ");
  72. if (sendFrames()) {
  73. printf("Error while sending frames!\n");
  74. return -1;
  75. }
  76. printf("Done!\n");
  77. break;
  78. case 'd': case 'D':
  79. printf("Deleting... ");
  80. if (deleteFrames()) {
  81. printf("Error while deleting frames!\n");
  82. return -1;
  83. }
  84. printf("Done!\n");
  85. break;
  86. default:
  87. printf("Unknown. Error!\n");
  88. c = ERROR;
  89. if (serialWriteTry(&c, 1)) {
  90. printf("Could not write to pseudo terminal\n");
  91. return -1;
  92. }
  93. break;
  94. }
  95. }
  96. }
  97. serialClose();
  98. return 0;
  99. }
  100. int sendFrames() {
  101. char c = ERROR;
  102. printf("Not implemented!\n");
  103. if (serialWriteTry(&c, 1)) {
  104. printf("Could not write to pseudo terminal\n");
  105. }
  106. return -1;
  107. }
  108. int recieveFrames() {
  109. char c;
  110. ssize_t size;
  111. int animCount, a, frameCount, f, d;
  112. char data[65];
  113. clearMemory();
  114. // First send acknowledge
  115. c = OK;
  116. if (serialWriteTry(&c, 1)) {
  117. printf("Could not write to pseudo terminal\n");
  118. return -1;
  119. }
  120. printf("AnimationCount");
  121. while (keepRunning) {
  122. size = serialRead(&c, 1);
  123. if (size == 1) {
  124. break;
  125. } else if (size == -1) {
  126. printf("Could not read from psuedo terminal!\n");
  127. return -1;
  128. }
  129. }
  130. printf(" = %d\n", c);
  131. animCount = c;
  132. c = OK;
  133. if (serialWriteTry(&c, 1)) {
  134. printf("Could not write to pseudo terminal\n");
  135. return -1;
  136. }
  137. for (a = 0; a < animCount; a++) {
  138. printf("FrameCount");
  139. while (keepRunning) {
  140. size = serialRead(&c, 1);
  141. if (size == 1) {
  142. break;
  143. } else if (size == -1) {
  144. printf("Could not read from psuedo terminal!\n");
  145. return -1;
  146. }
  147. }
  148. printf(" = %d\n", c);
  149. frameCount = c;
  150. c = OK;
  151. if (serialWriteTry(&c, 1)) {
  152. printf("Could not write to pseudo terminal\n");
  153. return -1;
  154. }
  155. for (f = 0; f < frameCount; f++) {
  156. printf("Duration");
  157. while (keepRunning) {
  158. size = serialRead(&c, 1);
  159. if (size == 1) {
  160. break;
  161. } else if (size == -1) {
  162. printf("Could not read from psuedo terminal!\n");
  163. return -1;
  164. }
  165. }
  166. printf(" = %d\n", c);
  167. data[64] = c;
  168. // Acknowledge frame duration
  169. c = OK;
  170. if(serialWriteTry(&c, 1)){
  171. printf("Could not write to pseudo terminal\n");
  172. return -1;
  173. }
  174. printf("Data...");
  175. for (d = 0; d < 64; d++) {
  176. while (keepRunning) {
  177. size = serialRead(&c, 1);
  178. if (size == 1) {
  179. break; // We got our data byte
  180. } else if (size == -1) {
  181. printf("Could not read from psuedo terminal!\n");
  182. return -1;
  183. }
  184. }
  185. data[d] = c;
  186. }
  187. printf(" Done!\n");
  188. addFrame(data);
  189. // Acknowledge frame data
  190. c = OK;
  191. if(serialWriteTry(&c, 1)){
  192. printf("Could not write to pseudo terminal\n");
  193. return -1;
  194. }
  195. }
  196. }
  197. while (keepRunning) {
  198. size = serialRead(&c, 1);
  199. if (size == 1) {
  200. break;
  201. } else if (size == -1) {
  202. printf("Could not read from psuedo terminal!\n");
  203. return -1;
  204. }
  205. }
  206. while (keepRunning) {
  207. size = serialRead(&c, 1);
  208. if (size == 1) {
  209. break;
  210. } else if (size == -1) {
  211. printf("Could not read from psuedo terminal!\n");
  212. return -1;
  213. }
  214. }
  215. while (keepRunning) {
  216. size = serialRead(&c, 1);
  217. if (size == 1) {
  218. break;
  219. } else if (size == -1) {
  220. printf("Could not read from psuedo terminal!\n");
  221. return -1;
  222. }
  223. }
  224. while (keepRunning) {
  225. size = serialRead(&c, 1);
  226. if (size == 1) {
  227. break;
  228. } else if (size == -1) {
  229. printf("Could not read from psuedo terminal!\n");
  230. return -1;
  231. }
  232. }
  233. printf("Got 4 OKs!\n");
  234. c = OK;
  235. if (serialWriteTry(&c, 1)) {
  236. printf("Could not write to pseudo terminal\n");
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. int deleteFrames() {
  242. char c = OK;
  243. clearMemory();
  244. if (serialWriteTry(&c, 1)) {
  245. printf("Could not write to pseudo terminal\n");
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. int serialWriteString(char *s) {
  251. return serialWriteTry(s, strlen(s));
  252. }
  253. int serialWriteTry(char *data, size_t length) {
  254. int i = 0;
  255. int written = 0;
  256. int ret;
  257. while (keepRunning) {
  258. ret = serialWrite((data + written), (length - written));
  259. if (ret == -1) {
  260. i++;
  261. } else {
  262. written += ret;
  263. }
  264. if (i > 10) {
  265. return 1;
  266. }
  267. if (written == length) {
  268. break;
  269. }
  270. }
  271. return 0;
  272. }
  273. void intHandler(int dummy) {
  274. keepRunning = 0;
  275. printf("\nExiting...\n");
  276. }