Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <time.h>
  5. #include "serial.h"
  6. #define OK 0x42
  7. #define ERROR 0x23
  8. void readAck(void);
  9. int serialWriteString(char *s);
  10. int serialReadTry(char *data, size_t length);
  11. int serialWriteTry(char *data, size_t length);
  12. void intHandler(int dummy);
  13. volatile int keepRunning = 1;
  14. #define suicide intHandler(0)
  15. int main(int argc, char *argv[]) {
  16. char c;
  17. int i, f;
  18. int animationCount = 1;
  19. int frameCount = 2;
  20. int duration = 3;
  21. int data = -1;
  22. char command = -1;
  23. if (argc < 2) {
  24. printf("Usage:\n%s /dev/port [-d 0xff]\n", argv[0]);
  25. return 0;
  26. }
  27. if (serialOpen(argv[1]) != 0) {
  28. printf("Could not open port: %s\n", argv[1]);
  29. return 0;
  30. }
  31. if ((argc >= 4) && (strcmp(argv[2], "-d") == 0)) {
  32. sscanf(argv[3], "%x", &data);
  33. frameCount = 1;
  34. }
  35. if (argc == 3) {
  36. command = argv[2][0];
  37. }
  38. signal(SIGINT, intHandler);
  39. signal(SIGQUIT, intHandler);
  40. printf("Port opened. Sending test data:\n");
  41. printf("\tSending command 's'...");
  42. if (command != -1) {
  43. c = command;
  44. } else {
  45. c = 's';
  46. }
  47. if (serialWriteTry(&c, 1) != 0) {
  48. printf(" Could not send it!\n");
  49. suicide;
  50. }
  51. printf(" Success!\n");
  52. if (command != -1) {
  53. suicide;
  54. }
  55. readAck();
  56. printf("\tSending anim count (1)...");
  57. c = (char)animationCount;
  58. if (serialWriteTry(&c, 1) != 0) {
  59. printf(" Could not send it!\n");
  60. suicide;
  61. }
  62. printf(" Success!\n");
  63. readAck();
  64. printf("\tSending frame count (2)...");
  65. c = (char)frameCount;
  66. if (serialWriteTry(&c, 1) != 0) {
  67. printf(" Could not send it!\n");
  68. suicide;
  69. }
  70. printf(" Success!\n");
  71. readAck();
  72. for (f = 0; f < frameCount; f++) {
  73. printf("\tSending duration (3)...");
  74. c = (char)duration;
  75. if (serialWriteTry(&c, 1) != 0) {
  76. printf(" Could not send it!\n");
  77. suicide;
  78. }
  79. printf(" Success!\n");
  80. readAck();
  81. printf("\tSending data");
  82. for(i = 0; i < 64; i++) {
  83. if (data == -1) {
  84. if (f == 0) {
  85. c = (char)i; // Some test data
  86. } else {
  87. c = (char)(63 - i);
  88. }
  89. } else {
  90. c = (char)data;
  91. }
  92. if (serialWriteTry(&c, 1) != 0) {
  93. printf(" Error while sending!\n");
  94. suicide;
  95. } else {
  96. printf(".");
  97. }
  98. }
  99. printf(" Success!\n");
  100. readAck();
  101. }
  102. printf("\tSending end sequence");
  103. for (i = 0; i < 4; i++) {
  104. // Send 4 OKs
  105. c = OK;
  106. if (serialWriteTry(&c, 1) != 0) {
  107. printf(" Error while sending!\n");
  108. suicide;
  109. } else {
  110. printf(".");
  111. }
  112. }
  113. printf(" Success!\n");
  114. readAck();
  115. printf("Finished. Success!\n");
  116. serialClose();
  117. return 0;
  118. }
  119. void readAck() {
  120. char c;
  121. printf("Awaiting acknowledge...");
  122. if (serialReadTry(&c, 1) != 0) {
  123. printf(" Could not read!\n");
  124. suicide;
  125. }
  126. if (c != OK) {
  127. printf(" Was not OK!\n");
  128. suicide;
  129. }
  130. printf(" OK\n");
  131. }
  132. int serialWriteString(char *s) {
  133. return serialWriteTry(s, strlen(s));
  134. }
  135. // 1 on error, 0 on success
  136. int serialReadTry(char *data, size_t length) {
  137. int i = 0;
  138. int written = 0;
  139. int ret;
  140. time_t start, end;
  141. start = time(NULL);
  142. while (keepRunning) {
  143. ret = serialRead((data + written), (length - written));
  144. if (ret == -1) {
  145. i++;
  146. } else {
  147. written += ret;
  148. }
  149. if (i > 10) {
  150. return 1;
  151. }
  152. if (written == length) {
  153. break;
  154. }
  155. end = time(NULL);
  156. if (difftime(start, end) > 2) {
  157. printf("Timeout went by. Exit!");
  158. suicide;
  159. }
  160. }
  161. return 0;
  162. }
  163. // 1 on error, 0 on success
  164. int serialWriteTry(char *data, size_t length) {
  165. int i = 0;
  166. int written = 0;
  167. int ret;
  168. while (keepRunning) {
  169. ret = serialWrite((data + written), (length - written));
  170. if (ret == -1) {
  171. i++;
  172. } else {
  173. written += ret;
  174. }
  175. if (i > 10) {
  176. return 1;
  177. }
  178. if (written == length) {
  179. break;
  180. }
  181. }
  182. return 0;
  183. }
  184. void intHandler(int dummy) {
  185. keepRunning = 0;
  186. serialClose();
  187. exit(0);
  188. }