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

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