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

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