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.
 
 
 
 
 

139 lines
3.7 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, float epsilon) {
  76. if (fabs(value-expect)<epsilon) {
  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 CheckSameFloatX(const char* msg, float value, float expect) {
  84. return CheckSameFloat(msg, value, expect, 0.08f);
  85. }
  86. void CheckSameDouble(const char* msg, double value, double expect, double epsilon) {
  87. if (fabs(value-expect)<epsilon) {
  88. printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
  89. } else {
  90. printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
  91. globalError=1;
  92. }
  93. }
  94. int UnitTestState() {
  95. printf("\n\nUNIT TEST STATE: ");
  96. if (globalError) {
  97. printf(" %sFAILED%s\n\n", FINALCOLRED, COLORDEF);
  98. return EXIT_FAILURE;
  99. } else {
  100. printf(" %sPASSED%s\n\n", FINALCOLGRN, COLORDEF);
  101. return EXIT_SUCCESS;
  102. }
  103. }
  104. void FailAndExitWithErrno(const char* msg, int ret) {
  105. printf("%s (Return=%d, Err: %s) -> %sfailed%s \n" , msg, ret, strerror(errno), COLORRED, COLORDEF);
  106. globalError=1;
  107. exit(UnitTestState());
  108. }