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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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;
  103. ssize_t size;
  104. int frameCount, f, d;
  105. char *data;
  106. // Acknowledge sending this
  107. c = OK;
  108. if(serialWriteTry(&c, 1)) {
  109. printf("Could not write to pseudo terminal\n");
  110. return -1;
  111. }
  112. // Send animation count. We dont store animations --> send 1
  113. printf("Sending animation count.\n");
  114. c = 1;
  115. if(serialWriteTry(&c, 1)) {
  116. printf("Could not write to pseudo terminal\n");
  117. return -1;
  118. }
  119. // Await OK from Host software
  120. while(keepRunning) {
  121. size = serialRead(&c, 1);
  122. if (size == 1) {
  123. break;
  124. } else if (size == -1) {
  125. printf("Couldn't read from pseudo terminal");
  126. return -1;
  127. }
  128. }
  129. if (c != OK) {
  130. printf("Invalid acknowledge from Host (%d)!", c);
  131. return -1;
  132. }
  133. // Send framecount
  134. frameCount = framesStored();
  135. printf("Sending frameCount = %d\n", frameCount);
  136. c = frameCount;
  137. if(serialWriteTry(&c, 1)) {
  138. printf("Could not write to pseudo terminal\n");
  139. return -1;
  140. }
  141. // Await OK from Host software
  142. while(keepRunning) {
  143. size = serialRead(&c, 1);
  144. if (size == 1) {
  145. break;
  146. } else if (size == -1) {
  147. printf("Couldn't read from pseudo terminal");
  148. return -1;
  149. }
  150. }
  151. if (c != OK) {
  152. printf("Invalid acknowledge from Host (%d)!", c);
  153. return -1;
  154. }
  155. for(f = 0; f < frameCount; f++){
  156. data = getFrame(f);
  157. printf("Sending duration = %d of frame %d\n", data[64], f);
  158. c = data[64]; // duration at end of frame data
  159. if(serialWriteTry(&c, 1)) {
  160. printf("Could not write to pseudo terminal\n");
  161. return -1;
  162. }
  163. // Await OK from Host software
  164. while(keepRunning) {
  165. size = serialRead(&c, 1);
  166. if (size == 1) {
  167. break;
  168. } else if (size == -1) {
  169. printf("Couldn't read from pseudo terminal");
  170. return -1;
  171. }
  172. }
  173. if (c != OK) {
  174. printf("Invalid acknowledge from Host (%d)!", c);
  175. return -1;
  176. }
  177. printf("Sending frame data...");
  178. for(d = 0; d < 64; d++) {
  179. c = data[d];
  180. if(serialWriteTry(&c, 1)) {
  181. printf("Could not write to pseudo terminal\n");
  182. return -1;
  183. }
  184. }
  185. printf(" %d successfully sent\n", f);
  186. // Await OK from Host software
  187. while(keepRunning) {
  188. size = serialRead(&c, 1);
  189. if (size == 1) {
  190. break;
  191. } else if (size == -1) {
  192. printf("Couldn't read from pseudo terminal");
  193. return -1;
  194. }
  195. }
  196. if (c != OK) {
  197. printf("Invalid acknowledge from Host (%d)!", c);
  198. return -1;
  199. }
  200. }
  201. printf("Done sending frames, start sending final sequence\n");
  202. c = OK;
  203. if(serialWriteTry(&c, 1)) {
  204. printf("Could not write to pseudo terminal\n");
  205. return -1;
  206. }
  207. if(serialWriteTry(&c, 1)) {
  208. printf("Could not write to pseudo terminal\n");
  209. return -1;
  210. }
  211. if(serialWriteTry(&c, 1)) {
  212. printf("Could not write to pseudo terminal\n");
  213. return -1;
  214. }
  215. if(serialWriteTry(&c, 1)) {
  216. printf("Could not write to pseudo terminal\n");
  217. return -1;
  218. }
  219. // Await OK from Host software
  220. while(keepRunning) {
  221. size = serialRead(&c, 1);
  222. if (size == 1) {
  223. break;
  224. } else if (size == -1) {
  225. printf("Couldn't read from pseudo terminal");
  226. return -1;
  227. }
  228. }
  229. if (c != OK) {
  230. printf("Invalid acknowledge from Host (%d)!", c);
  231. }
  232. return 0;
  233. }
  234. int recieveFrames() {
  235. char c;
  236. ssize_t size;
  237. int animCount, a, frameCount, f, d;
  238. char data[65];
  239. clearMemory();
  240. // First send acknowledge
  241. c = OK;
  242. if (serialWriteTry(&c, 1)) {
  243. printf("Could not write to pseudo terminal\n");
  244. return -1;
  245. }
  246. printf("AnimationCount");
  247. while (keepRunning) {
  248. size = serialRead(&c, 1);
  249. if (size == 1) {
  250. break;
  251. } else if (size == -1) {
  252. printf("Could not read from psuedo terminal!\n");
  253. return -1;
  254. }
  255. }
  256. printf(" = %d\n", c);
  257. animCount = c;
  258. c = OK;
  259. if (serialWriteTry(&c, 1)) {
  260. printf("Could not write to pseudo terminal\n");
  261. return -1;
  262. }
  263. for (a = 0; a < animCount; a++) {
  264. printf("FrameCount");
  265. while (keepRunning) {
  266. size = serialRead(&c, 1);
  267. if (size == 1) {
  268. break;
  269. } else if (size == -1) {
  270. printf("Could not read from psuedo terminal!\n");
  271. return -1;
  272. }
  273. }
  274. printf(" = %d\n", c);
  275. frameCount = c;
  276. c = OK;
  277. if (serialWriteTry(&c, 1)) {
  278. printf("Could not write to pseudo terminal\n");
  279. return -1;
  280. }
  281. for (f = 0; f < frameCount; f++) {
  282. printf("Duration");
  283. while (keepRunning) {
  284. size = serialRead(&c, 1);
  285. if (size == 1) {
  286. break;
  287. } else if (size == -1) {
  288. printf("Could not read from psuedo terminal!\n");
  289. return -1;
  290. }
  291. }
  292. printf(" = %d\n", c);
  293. data[64] = c;
  294. // Acknowledge frame duration
  295. c = OK;
  296. if(serialWriteTry(&c, 1)){
  297. printf("Could not write to pseudo terminal\n");
  298. return -1;
  299. }
  300. printf("Data...");
  301. for (d = 0; d < 64; d++) {
  302. while (keepRunning) {
  303. size = serialRead(&c, 1);
  304. if (size == 1) {
  305. break; // We got our data byte
  306. } else if (size == -1) {
  307. printf("Could not read from psuedo terminal!\n");
  308. return -1;
  309. }
  310. }
  311. data[d] = c;
  312. }
  313. printf(" Done!\n");
  314. addFrame(data);
  315. // Acknowledge frame data
  316. c = OK;
  317. if(serialWriteTry(&c, 1)){
  318. printf("Could not write to pseudo terminal\n");
  319. return -1;
  320. }
  321. }
  322. }
  323. while (keepRunning) {
  324. size = serialRead(&c, 1);
  325. if (size == 1) {
  326. break;
  327. } else if (size == -1) {
  328. printf("Could not read from psuedo terminal!\n");
  329. return -1;
  330. }
  331. }
  332. while (keepRunning) {
  333. size = serialRead(&c, 1);
  334. if (size == 1) {
  335. break;
  336. } else if (size == -1) {
  337. printf("Could not read from psuedo terminal!\n");
  338. return -1;
  339. }
  340. }
  341. while (keepRunning) {
  342. size = serialRead(&c, 1);
  343. if (size == 1) {
  344. break;
  345. } else if (size == -1) {
  346. printf("Could not read from psuedo terminal!\n");
  347. return -1;
  348. }
  349. }
  350. while (keepRunning) {
  351. size = serialRead(&c, 1);
  352. if (size == 1) {
  353. break;
  354. } else if (size == -1) {
  355. printf("Could not read from psuedo terminal!\n");
  356. return -1;
  357. }
  358. }
  359. printf("Got 4 OKs!\n");
  360. c = OK;
  361. if (serialWriteTry(&c, 1)) {
  362. printf("Could not write to pseudo terminal\n");
  363. return -1;
  364. }
  365. return 0;
  366. }
  367. int deleteFrames() {
  368. char c = OK;
  369. clearMemory();
  370. if (serialWriteTry(&c, 1)) {
  371. printf("Could not write to pseudo terminal\n");
  372. return -1;
  373. }
  374. return 0;
  375. }
  376. int serialWriteString(char *s) {
  377. return serialWriteTry(s, strlen(s));
  378. }
  379. int serialWriteTry(char *data, size_t length) {
  380. int i = 0;
  381. int written = 0;
  382. int ret;
  383. while (keepRunning) {
  384. ret = serialWrite((data + written), (length - written));
  385. if (ret == -1) {
  386. i++;
  387. } else {
  388. written += ret;
  389. }
  390. if (i > 10) {
  391. return 1;
  392. }
  393. if (written == length) {
  394. break;
  395. }
  396. }
  397. return 0;
  398. }
  399. void intHandler(int dummy) {
  400. keepRunning = 0;
  401. printf("\nExiting...\n");
  402. }