Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include "mem.h"
  11. #define VERSION "LED-Cube Emu V1\n"
  12. #define OK 0x42
  13. #define ERROR 0x23
  14. int sendFrames(void);
  15. int recieveFrames(void);
  16. int deleteFrames(void);
  17. int serialWriteString(char *s);
  18. int serialWriteTry(char *data, size_t length);
  19. void intHandler(int dummy);
  20. volatile int keepRunning = 1;
  21. #define printf if(keepRunning)printf
  22. int main(int argc, char *argv[]) {
  23. char c;
  24. ssize_t size;
  25. char *slave;
  26. if ((slave = serialOpen()) == NULL) {
  27. printf("Could not open a pseudo Terminal!\n");
  28. return -1;
  29. }
  30. printf("Waiting for CubeControl on \"%s\"...\n", slave);
  31. signal(SIGINT, intHandler);
  32. signal(SIGQUIT, intHandler);
  33. printf("Stop with CTRL+C...\n");
  34. while(keepRunning) {
  35. size = serialRead(&c, 1);
  36. if (size == -1) {
  37. // Error while reading
  38. printf("Could not read from psuedo terminal!\n");
  39. return -1;
  40. }
  41. if (size == 1) {
  42. switch(c) {
  43. case OK:
  44. if (serialWriteTry(&c, 1)) {
  45. printf("Could not write to pseudo terminal\n");
  46. return -1;
  47. }
  48. printf("OK!\n");
  49. break;
  50. case 'h': case 'H':
  51. printf("Help\n");
  52. if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
  53. printf("Could not write to pseudo terminal\n");
  54. return -1;
  55. }
  56. break;
  57. case 'v': case 'V':
  58. printf("Version\n");
  59. if (serialWriteString(VERSION)) {
  60. printf("Could not write to pseudo terminal\n");
  61. return -1;
  62. }
  63. break;
  64. case 's': case 'S':
  65. printf("Recieving... ");
  66. if (recieveFrames()) {
  67. printf("Error while recieving frames!\n");
  68. return -1;
  69. }
  70. printf("Done!\n");
  71. break;
  72. case 'g': case 'G':
  73. printf("Sending... ");
  74. if (sendFrames()) {
  75. printf("Error while sending frames!\n");
  76. return -1;
  77. }
  78. printf("Done!\n");
  79. break;
  80. case 'd': case 'D':
  81. printf("Deleting... ");
  82. if (deleteFrames()) {
  83. printf("Error while deleting frames!\n");
  84. return -1;
  85. }
  86. printf("Done!\n");
  87. break;
  88. default:
  89. printf("Unknown. Error!\n");
  90. c = ERROR;
  91. if (serialWriteTry(&c, 1)) {
  92. printf("Could not write to pseudo terminal\n");
  93. return -1;
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. serialClose();
  100. return 0;
  101. }
  102. int sendFrames() {
  103. char c;
  104. ssize_t size;
  105. int frameCount, f, d;
  106. char *data;
  107. // Acknowledge sending this
  108. c = OK;
  109. if(serialWriteTry(&c, 1)) {
  110. printf("Could not write to pseudo terminal\n");
  111. return -1;
  112. }
  113. // Send animation count. We dont store animations --> send 1
  114. printf("Sending animation count.\n");
  115. c = 1;
  116. if(serialWriteTry(&c, 1)) {
  117. printf("Could not write to pseudo terminal\n");
  118. return -1;
  119. }
  120. // Await OK from Host software
  121. while(keepRunning) {
  122. size = serialRead(&c, 1);
  123. if (size == 1) {
  124. break;
  125. } else if (size == -1) {
  126. printf("Couldn't read from pseudo terminal");
  127. return -1;
  128. }
  129. }
  130. if (c != OK) {
  131. printf("Invalid acknowledge from Host (%d)!", c);
  132. return -1;
  133. }
  134. // Send framecount
  135. frameCount = framesStored();
  136. printf("Sending frameCount = %d\n", frameCount);
  137. c = frameCount;
  138. if(serialWriteTry(&c, 1)) {
  139. printf("Could not write to pseudo terminal\n");
  140. return -1;
  141. }
  142. // Await OK from Host software
  143. while(keepRunning) {
  144. size = serialRead(&c, 1);
  145. if (size == 1) {
  146. break;
  147. } else if (size == -1) {
  148. printf("Couldn't read from pseudo terminal");
  149. return -1;
  150. }
  151. }
  152. if (c != OK) {
  153. printf("Invalid acknowledge from Host (%d)!", c);
  154. return -1;
  155. }
  156. for(f = 0; f < frameCount; f++){
  157. data = getFrame(f);
  158. printf("Sending duration = %d of frame %d\n", data[64], f);
  159. c = data[64]; // duration at end of frame data
  160. if(serialWriteTry(&c, 1)) {
  161. printf("Could not write to pseudo terminal\n");
  162. return -1;
  163. }
  164. // Await OK from Host software
  165. while(keepRunning) {
  166. size = serialRead(&c, 1);
  167. if (size == 1) {
  168. break;
  169. } else if (size == -1) {
  170. printf("Couldn't read from pseudo terminal");
  171. return -1;
  172. }
  173. }
  174. if (c != OK) {
  175. printf("Invalid acknowledge from Host (%d)!", c);
  176. return -1;
  177. }
  178. printf("Sending frame data...");
  179. for(d = 0; d < 64; d++) {
  180. c = data[d];
  181. if(serialWriteTry(&c, 1)) {
  182. printf("Could not write to pseudo terminal\n");
  183. return -1;
  184. }
  185. }
  186. printf(" %d successfully sent\n", f);
  187. // Await OK from Host software
  188. while(keepRunning) {
  189. size = serialRead(&c, 1);
  190. if (size == 1) {
  191. break;
  192. } else if (size == -1) {
  193. printf("Couldn't read from pseudo terminal");
  194. return -1;
  195. }
  196. }
  197. if (c != OK) {
  198. printf("Invalid acknowledge from Host (%d)!", c);
  199. return -1;
  200. }
  201. }
  202. printf("Done sending frames, start sending final sequence\n");
  203. c = OK;
  204. if(serialWriteTry(&c, 1)) {
  205. printf("Could not write to pseudo terminal\n");
  206. return -1;
  207. }
  208. if(serialWriteTry(&c, 1)) {
  209. printf("Could not write to pseudo terminal\n");
  210. return -1;
  211. }
  212. if(serialWriteTry(&c, 1)) {
  213. printf("Could not write to pseudo terminal\n");
  214. return -1;
  215. }
  216. if(serialWriteTry(&c, 1)) {
  217. printf("Could not write to pseudo terminal\n");
  218. return -1;
  219. }
  220. // Await OK from Host software
  221. while(keepRunning) {
  222. size = serialRead(&c, 1);
  223. if (size == 1) {
  224. break;
  225. } else if (size == -1) {
  226. printf("Couldn't read from pseudo terminal");
  227. return -1;
  228. }
  229. }
  230. if (c != OK) {
  231. printf("Invalid acknowledge from Host (%d)!", c);
  232. }
  233. return 0;
  234. }
  235. int recieveFrames() {
  236. char c;
  237. ssize_t size;
  238. int animCount, a, frameCount, f, d;
  239. char data[65];
  240. clearMemory();
  241. // First send acknowledge
  242. c = OK;
  243. if (serialWriteTry(&c, 1)) {
  244. printf("Could not write to pseudo terminal\n");
  245. return -1;
  246. }
  247. printf("AnimationCount");
  248. while (keepRunning) {
  249. size = serialRead(&c, 1);
  250. if (size == 1) {
  251. break;
  252. } else if (size == -1) {
  253. printf("Could not read from psuedo terminal!\n");
  254. return -1;
  255. }
  256. }
  257. printf(" = %d\n", c);
  258. animCount = c;
  259. c = OK;
  260. if (serialWriteTry(&c, 1)) {
  261. printf("Could not write to pseudo terminal\n");
  262. return -1;
  263. }
  264. for (a = 0; a < animCount; a++) {
  265. printf("FrameCount");
  266. while (keepRunning) {
  267. size = serialRead(&c, 1);
  268. if (size == 1) {
  269. break;
  270. } else if (size == -1) {
  271. printf("Could not read from psuedo terminal!\n");
  272. return -1;
  273. }
  274. }
  275. printf(" = %d\n", c);
  276. frameCount = c;
  277. c = OK;
  278. if (serialWriteTry(&c, 1)) {
  279. printf("Could not write to pseudo terminal\n");
  280. return -1;
  281. }
  282. for (f = 0; f < frameCount; f++) {
  283. printf("Duration");
  284. while (keepRunning) {
  285. size = serialRead(&c, 1);
  286. if (size == 1) {
  287. break;
  288. } else if (size == -1) {
  289. printf("Could not read from psuedo terminal!\n");
  290. return -1;
  291. }
  292. }
  293. printf(" = %d\n", c);
  294. data[64] = c;
  295. // Acknowledge frame duration
  296. c = OK;
  297. if(serialWriteTry(&c, 1)){
  298. printf("Could not write to pseudo terminal\n");
  299. return -1;
  300. }
  301. printf("Data...");
  302. for (d = 0; d < 64; d++) {
  303. while (keepRunning) {
  304. size = serialRead(&c, 1);
  305. if (size == 1) {
  306. break; // We got our data byte
  307. } else if (size == -1) {
  308. printf("Could not read from psuedo terminal!\n");
  309. return -1;
  310. }
  311. }
  312. data[d] = c;
  313. }
  314. printf(" Done!\n");
  315. addFrame(data);
  316. // Acknowledge frame data
  317. c = OK;
  318. if(serialWriteTry(&c, 1)){
  319. printf("Could not write to pseudo terminal\n");
  320. return -1;
  321. }
  322. }
  323. }
  324. while (keepRunning) {
  325. size = serialRead(&c, 1);
  326. if (size == 1) {
  327. break;
  328. } else if (size == -1) {
  329. printf("Could not read from psuedo terminal!\n");
  330. return -1;
  331. }
  332. }
  333. while (keepRunning) {
  334. size = serialRead(&c, 1);
  335. if (size == 1) {
  336. break;
  337. } else if (size == -1) {
  338. printf("Could not read from psuedo terminal!\n");
  339. return -1;
  340. }
  341. }
  342. while (keepRunning) {
  343. size = serialRead(&c, 1);
  344. if (size == 1) {
  345. break;
  346. } else if (size == -1) {
  347. printf("Could not read from psuedo terminal!\n");
  348. return -1;
  349. }
  350. }
  351. while (keepRunning) {
  352. size = serialRead(&c, 1);
  353. if (size == 1) {
  354. break;
  355. } else if (size == -1) {
  356. printf("Could not read from psuedo terminal!\n");
  357. return -1;
  358. }
  359. }
  360. printf("Got 4 OKs!\n");
  361. c = OK;
  362. if (serialWriteTry(&c, 1)) {
  363. printf("Could not write to pseudo terminal\n");
  364. return -1;
  365. }
  366. return 0;
  367. }
  368. int deleteFrames() {
  369. char c = OK;
  370. clearMemory();
  371. if (serialWriteTry(&c, 1)) {
  372. printf("Could not write to pseudo terminal\n");
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. int serialWriteString(char *s) {
  378. return serialWriteTry(s, strlen(s));
  379. }
  380. int serialWriteTry(char *data, size_t length) {
  381. int i = 0;
  382. int written = 0;
  383. int ret;
  384. while (keepRunning) {
  385. ret = serialWrite((data + written), (length - written));
  386. if (ret == -1) {
  387. i++;
  388. } else {
  389. written += ret;
  390. }
  391. if (i > 10) {
  392. return 1;
  393. }
  394. if (written == length) {
  395. break;
  396. }
  397. }
  398. return 0;
  399. }
  400. void intHandler(int dummy) {
  401. printf("\nExiting...\n");
  402. keepRunning = 0;
  403. serialClose();
  404. exit(0);
  405. }