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.
 
 
 
 
 

136 regels
3.6 KiB

  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #define COLORDEF "\x1B[0m"
  8. #define COLORRED "\x1B[31m"
  9. #define COLORGRN "\x1B[32m"
  10. #define BOLD "\x1B[1m"
  11. #define FINALCOLRED "\x1B[7;49;91m"
  12. #define FINALCOLGRN "\x1B[7;49;32m"
  13. unsigned int globalError = 0;
  14. void CheckGPIO(int GPIO, int GPIOIN, int out) {
  15. int in = out;
  16. if (GPIOIN>=0) {
  17. in = digitalRead(GPIOIN);
  18. }
  19. int readback = digitalRead(GPIO);
  20. int pass = 0;
  21. if (out==readback && in==out) {
  22. pass = 1;
  23. }
  24. if (GPIOIN>=0) {
  25. printf("set GPIO%02d = %d (readback %d), in GPIO%02d = %d ", GPIO, out, readback, GPIOIN, in);
  26. } else {
  27. printf("set GPIO%02d = %d (readback %d) ", GPIO, out, readback);
  28. }
  29. if (pass) {
  30. printf("-> %spassed%s\n", COLORGRN, COLORDEF );
  31. } else {
  32. globalError=1;
  33. printf("-> %sfailed%s\n", COLORRED, COLORDEF );
  34. }
  35. }
  36. void CheckInversGPIO(int GPIO, int GPIOIN, int out) {
  37. CheckGPIO(GPIO, GPIOIN, out==HIGH ? LOW : HIGH);
  38. }
  39. void digitalWriteEx(int GPIO, int GPIOIN, int mode) {
  40. digitalWrite(GPIO, mode);
  41. delayMicroseconds(5000);
  42. CheckGPIO(GPIO, GPIOIN, mode);
  43. }
  44. void pullUpDnControlEx (int GPIO, int GPIOIN, int mode) {
  45. pullUpDnControl (GPIO, mode);
  46. int out = mode==PUD_UP ? 1:0;
  47. printf("in = %4s ", mode==PUD_UP ? "up":"down");
  48. delayMicroseconds(5000);
  49. CheckGPIO(GPIO, GPIOIN, out);
  50. }
  51. void CheckSameText(const char* msg, const char* value, const char* expect) {
  52. if (!strcmp(value, expect)) {
  53. printf("%39s (%10s==%10s) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  54. } else {
  55. printf("%39s (%10s<>%10s) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  56. globalError=1;
  57. }
  58. }
  59. void CheckSame(const char* msg, int value, int expect) {
  60. if (value==expect) {
  61. printf("%39s (% 3d==% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  62. } else {
  63. printf("%39s (% 3d<>% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  64. globalError=1;
  65. }
  66. }
  67. void CheckNotSame(const char* msg, int value, int expect) {
  68. if (value!=expect) {
  69. printf("%39s (% 3d<>% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  70. } else {
  71. printf("%39s (% 3d==% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  72. globalError=1;
  73. }
  74. }
  75. void CheckSameFloat(const char* msg, float value, float expect) {
  76. if (fabs(value-expect)<0.08) {
  77. printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
  78. } else {
  79. printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
  80. globalError=1;
  81. }
  82. }
  83. void CheckSameDouble(const char* msg, double value, double expect, double epsilon) {
  84. if (fabs(value-expect)<epsilon) {
  85. printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
  86. } else {
  87. printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
  88. globalError=1;
  89. }
  90. }
  91. int UnitTestState() {
  92. printf("\n\nUNIT TEST STATE: ");
  93. if (globalError) {
  94. printf(" %sFAILED%s\n\n", FINALCOLRED, COLORDEF);
  95. return EXIT_FAILURE;
  96. } else {
  97. printf(" %sPASSED%s\n\n", FINALCOLGRN, COLORDEF);
  98. return EXIT_SUCCESS;
  99. }
  100. }
  101. void FailAndExitWithErrno(const char* msg, int ret) {
  102. printf("%s (Return=%d, Err: %s) -> %sfailed%s \n" , msg, ret, strerror(errno), COLORRED, COLORDEF);
  103. globalError=1;
  104. exit(UnitTestState());
  105. }