選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

101 行
2.5 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 CheckSame(const char* msg, int value, int expect) {
  49. if (value==expect) {
  50. printf("%39s (% 3d==% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
  51. } else {
  52. printf("%39s (% 3d<>% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
  53. globalError=1;
  54. }
  55. }
  56. void CheckSameFloat(const char* msg, float value, float expect) {
  57. if (fabs(value-expect)<0.08) {
  58. printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
  59. } else {
  60. printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
  61. globalError=1;
  62. }
  63. }
  64. int UnitTestState() {
  65. printf("\n\nUNIT TEST STATE: ");
  66. if (globalError) {
  67. printf(" %sFAILED%s\n\n", FINALCOLRED, COLORDEF);
  68. return EXIT_FAILURE;
  69. } else {
  70. printf(" %sPASSED%s\n\n", FINALCOLGRN, COLORDEF);
  71. return EXIT_SUCCESS;
  72. }
  73. }
  74. void FailAndExitWithErrno(const char* msg, int ret) {
  75. printf("%s (Return=%d, Err: %s) -> %sfailed%s \n" , msg, ret, strerror(errno), COLORRED, COLORDEF);
  76. globalError=1;
  77. exit(UnitTestState());
  78. }