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.
 
 
 
 
 

121 lines
3.1 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 digitalWriteEx(int GPIO, int GPIOIN, int mode) {
  37. digitalWrite(GPIO, mode);
  38. delayMicroseconds(5000);
  39. CheckGPIO(GPIO, GPIOIN, mode);
  40. }
  41. void pullUpDnControlEx (int GPIO, int GPIOIN, int mode) {
  42. pullUpDnControl (GPIO, mode);
  43. int out = mode==PUD_UP ? 1:0;
  44. printf("in = %4s ", mode==PUD_UP ? "up":"down");
  45. delayMicroseconds(5000);
  46. CheckGPIO(GPIO, GPIOIN, out);
  47. }
  48. void CheckSameText(const char* msg, const char* value, const char* expect) {
  49. if (!strcmp(value, expect)) {
  50. printf("%39s (%10s==%10s) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  51. } else {
  52. printf("%39s (%10s<>%10s) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  53. globalError=1;
  54. }
  55. }
  56. void CheckSame(const char* msg, int value, int expect) {
  57. if (value==expect) {
  58. printf("%39s (% 3d==% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  59. } else {
  60. printf("%39s (% 3d<>% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  61. globalError=1;
  62. }
  63. }
  64. void CheckNotSame(const char* msg, int value, int expect) {
  65. if (value!=expect) {
  66. printf("%39s (% 3d<>% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  67. } else {
  68. printf("%39s (% 3d==% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  69. globalError=1;
  70. }
  71. }
  72. void CheckSameFloat(const char* msg, float value, float expect) {
  73. if (fabs(value-expect)<0.08) {
  74. printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
  75. } else {
  76. printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
  77. globalError=1;
  78. }
  79. }
  80. int UnitTestState() {
  81. printf("\n\nUNIT TEST STATE: ");
  82. if (globalError) {
  83. printf(" %sFAILED%s\n\n", FINALCOLRED, COLORDEF);
  84. return EXIT_FAILURE;
  85. } else {
  86. printf(" %sPASSED%s\n\n", FINALCOLGRN, COLORDEF);
  87. return EXIT_SUCCESS;
  88. }
  89. }
  90. void FailAndExitWithErrno(const char* msg, int ret) {
  91. printf("%s (Return=%d, Err: %s) -> %sfailed%s \n" , msg, ret, strerror(errno), COLORRED, COLORDEF);
  92. globalError=1;
  93. exit(UnitTestState());
  94. }