Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.c 8.3KB

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