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

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