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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. char c;
  108. ssize_t size;
  109. int a, frameCount, f, d;
  110. char data[65];
  111. c = OK;
  112. if (serialWriteTry(&c, 1)) {
  113. printf("Could not write to pseudo terminal\n");
  114. return -1;
  115. }
  116. frameCount = framesStored();
  117. printf("FrameCount = %d\n", frameCount);
  118. }
  119. int recieveFrames() {
  120. char c;
  121. ssize_t size;
  122. int animCount, a, frameCount, f, d;
  123. char data[65];
  124. clearMemory();
  125. // First send acknowledge
  126. c = OK;
  127. if (serialWriteTry(&c, 1)) {
  128. printf("Could not write to pseudo terminal\n");
  129. return -1;
  130. }
  131. printf("AnimationCount");
  132. while (keepRunning) {
  133. size = serialRead(&c, 1);
  134. if (size == 1) {
  135. break;
  136. } else if (size == -1) {
  137. printf("Could not read from psuedo terminal!\n");
  138. return -1;
  139. }
  140. }
  141. printf(" = %d\n", c);
  142. animCount = c;
  143. c = OK;
  144. if (serialWriteTry(&c, 1)) {
  145. printf("Could not write to pseudo terminal\n");
  146. return -1;
  147. }
  148. for (a = 0; a < animCount; a++) {
  149. printf("FrameCount");
  150. while (keepRunning) {
  151. size = serialRead(&c, 1);
  152. if (size == 1) {
  153. break;
  154. } else if (size == -1) {
  155. printf("Could not read from psuedo terminal!\n");
  156. return -1;
  157. }
  158. }
  159. printf(" = %d\n", c);
  160. frameCount = c;
  161. c = OK;
  162. if (serialWriteTry(&c, 1)) {
  163. printf("Could not write to pseudo terminal\n");
  164. return -1;
  165. }
  166. for (f = 0; f < frameCount; f++) {
  167. printf("Duration");
  168. while (keepRunning) {
  169. size = serialRead(&c, 1);
  170. if (size == 1) {
  171. break;
  172. } else if (size == -1) {
  173. printf("Could not read from psuedo terminal!\n");
  174. return -1;
  175. }
  176. }
  177. printf(" = %d\n", c);
  178. data[64] = c;
  179. // Acknowledge frame duration
  180. c = OK;
  181. if(serialWriteTry(&c, 1)){
  182. printf("Could not write to pseudo terminal\n");
  183. return -1;
  184. }
  185. printf("Data...");
  186. for (d = 0; d < 64; d++) {
  187. while (keepRunning) {
  188. size = serialRead(&c, 1);
  189. if (size == 1) {
  190. break; // We got our data byte
  191. } else if (size == -1) {
  192. printf("Could not read from psuedo terminal!\n");
  193. return -1;
  194. }
  195. }
  196. data[d] = c;
  197. }
  198. printf(" Done!\n");
  199. addFrame(data);
  200. // Acknowledge frame data
  201. c = OK;
  202. if(serialWriteTry(&c, 1)){
  203. printf("Could not write to pseudo terminal\n");
  204. return -1;
  205. }
  206. }
  207. }
  208. while (keepRunning) {
  209. size = serialRead(&c, 1);
  210. if (size == 1) {
  211. break;
  212. } else if (size == -1) {
  213. printf("Could not read from psuedo terminal!\n");
  214. return -1;
  215. }
  216. }
  217. while (keepRunning) {
  218. size = serialRead(&c, 1);
  219. if (size == 1) {
  220. break;
  221. } else if (size == -1) {
  222. printf("Could not read from psuedo terminal!\n");
  223. return -1;
  224. }
  225. }
  226. while (keepRunning) {
  227. size = serialRead(&c, 1);
  228. if (size == 1) {
  229. break;
  230. } else if (size == -1) {
  231. printf("Could not read from psuedo terminal!\n");
  232. return -1;
  233. }
  234. }
  235. while (keepRunning) {
  236. size = serialRead(&c, 1);
  237. if (size == 1) {
  238. break;
  239. } else if (size == -1) {
  240. printf("Could not read from psuedo terminal!\n");
  241. return -1;
  242. }
  243. }
  244. printf("Got 4 OKs!\n");
  245. c = OK;
  246. if (serialWriteTry(&c, 1)) {
  247. printf("Could not write to pseudo terminal\n");
  248. return -1;
  249. }
  250. return 0;
  251. }
  252. int deleteFrames() {
  253. char c = OK;
  254. clearMemory();
  255. if (serialWriteTry(&c, 1)) {
  256. printf("Could not write to pseudo terminal\n");
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. int serialWriteString(char *s) {
  262. return serialWriteTry(s, strlen(s));
  263. }
  264. int serialWriteTry(char *data, size_t length) {
  265. int i = 0;
  266. int written = 0;
  267. int ret;
  268. while (keepRunning) {
  269. ret = serialWrite((data + written), (length - written));
  270. if (ret == -1) {
  271. i++;
  272. } else {
  273. written += ret;
  274. }
  275. if (i > 10) {
  276. return 1;
  277. }
  278. if (written == length) {
  279. break;
  280. }
  281. }
  282. return 0;
  283. }
  284. void intHandler(int dummy) {
  285. keepRunning = 0;
  286. printf("\nExiting...\n");
  287. }