Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. break;
  47. case 'h': case 'H':
  48. if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
  49. printf("Could not write to pseudo terminal\n");
  50. return -1;
  51. }
  52. break;
  53. case 'v': case 'V':
  54. if (serialWriteString(VERSION)) {
  55. printf("Could not write to pseudo terminal\n");
  56. return -1;
  57. }
  58. break;
  59. case 's': case 'S':
  60. if (recieveFrames()) {
  61. printf("Error while recieving frames!\n");
  62. return -1;
  63. }
  64. break;
  65. case 'g': case 'G':
  66. if (sendFrames()) {
  67. printf("Error while sending frames!\n");
  68. return -1;
  69. }
  70. break;
  71. case 'd': case 'D':
  72. if (deleteFrames()) {
  73. printf("Error while deleting frames!\n");
  74. return -1;
  75. }
  76. break;
  77. default:
  78. c = ERROR;
  79. if (serialWriteTry(&c, 1)) {
  80. printf("Could not write to pseudo terminal\n");
  81. return -1;
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. serialClose();
  88. return 0;
  89. }
  90. int sendFrames() {
  91. }
  92. int recieveFrames() {
  93. }
  94. int deleteFrames() {
  95. }
  96. int serialWriteString(char *s) {
  97. return serialWriteTry(s, strlen(s));
  98. }
  99. int serialWriteTry(char *data, size_t length) {
  100. int i = 0;
  101. int written = 0;
  102. int ret;
  103. while (1) {
  104. ret = serialWrite((data + written), (length - written));
  105. if (ret == -1) {
  106. i++;
  107. } else {
  108. written += ret;
  109. }
  110. if (i > 10) {
  111. return 1;
  112. }
  113. if (written == length) {
  114. break;
  115. }
  116. }
  117. return 0;
  118. }
  119. void intHandler(int dummy) {
  120. keepRunning = 0;
  121. printf("\nExiting...\n");
  122. }