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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 serialWriteString(char *s);
  14. int serialWriteTry(char *data, size_t length);
  15. void intHandler(int dummy);
  16. volatile int keepRunning = 1;
  17. int main(int argc, char *argv[]) {
  18. char c;
  19. ssize_t size;
  20. char *slave;
  21. if ((slave = serialOpen()) == NULL) {
  22. printf("Could not open a pseudo Terminal!\n");
  23. return -1;
  24. }
  25. printf("Waiting for CubeControl on \"%s\"...\n", slave);
  26. signal(SIGINT, intHandler);
  27. signal(SIGQUIT, intHandler);
  28. printf("Stop with CTRL+C...\n");
  29. while(keepRunning) {
  30. size = serialRead(&c, 1);
  31. if (size == -1) {
  32. // Error while reading
  33. printf("Could not read from psuedo terminal!\n");
  34. return -1;
  35. }
  36. if (size == 1) {
  37. switch(c) {
  38. case OK:
  39. if (serialWriteTry(&c, 1)) {
  40. printf("Could not write to pseudo terminal");
  41. return -1;
  42. }
  43. break;
  44. case 'h': case 'H':
  45. if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
  46. printf("Could not write to pseudo terminal");
  47. return -1;
  48. }
  49. break;
  50. case 'v': case 'V':
  51. if (serialWriteString(VERSION)) {
  52. printf("Could not write to pseudo terminal");
  53. return -1;
  54. }
  55. break;
  56. default:
  57. c = ERROR;
  58. if (serialWriteTry(&c, 1)) {
  59. printf("Could not write to pseudo terminal");
  60. return -1;
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. serialClose();
  67. return 0;
  68. }
  69. int serialWriteString(char *s) {
  70. return serialWriteTry(s, strlen(s));
  71. }
  72. int serialWriteTry(char *data, size_t length) {
  73. int i = 0;
  74. int written = 0;
  75. int ret;
  76. while (1) {
  77. ret = serialWrite((data + written), (length - written));
  78. if (ret == -1) {
  79. i++;
  80. } else {
  81. written += ret;
  82. }
  83. if (i > 10) {
  84. return 1;
  85. }
  86. if (written == length) {
  87. break;
  88. }
  89. }
  90. return 0;
  91. }
  92. void intHandler(int dummy) {
  93. keepRunning = 0;
  94. printf("\nExiting...\n");
  95. }