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.

mainclass.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "mainclass.hh"
  2. #include <iostream>
  3. #include <libgen.h>
  4. #include <sysexits.h>
  5. #include <climits>
  6. #include <getopt.h>
  7. #include <unistd.h>
  8. #include <pifacedigital.h>
  9. int MainClass::current_device_ = -1;
  10. uint8_t MainClass::pins_ = 0;
  11. MainClass::MainClass(int argc, char* argv[])
  12. : argc_(argc)
  13. , argv_(argv)
  14. {
  15. }
  16. int MainClass::usage()
  17. {
  18. std::cerr << "Usage: " << basename(argv_[0]) << std::endl <<
  19. " [--device DEVICE|--device=DEVICE|-e DEVICE]" << std::endl <<
  20. " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
  21. " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
  22. " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
  23. " [--help|-h]" << std::endl;
  24. std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
  25. << std::endl << std::endl;
  26. std::cerr << "--device \tSwitch to the provided device."
  27. << std::endl << "--up \t\tEnable the provided ouput pin."
  28. << std::endl << "--down \t\tDisable the provided output pin."
  29. << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
  30. << std::endl << "--help \t\tDisplay this message."
  31. << std::endl << std::endl;
  32. std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
  33. << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
  34. << UINT_MAX << "."
  35. << std::endl << "DEVICE \t: The device number. Default is 0. "
  36. "0 <= DEVICE <= 4." << std::endl;
  37. return EX_USAGE;
  38. }
  39. bool MainClass::bad_value_()
  40. {
  41. extern int optind;
  42. extern char* optarg;
  43. std::cerr << "Bad value " << optarg <<
  44. " for option " << argv_[optind - 2] << std::endl << std::endl;
  45. return false;
  46. }
  47. bool MainClass::build_actions_()
  48. {
  49. option opts[] = {
  50. {"device", 1, 0, 'e'},
  51. {"up", 1, 0, 'u'},
  52. {"down", 1, 0, 'd'},
  53. {"sleep", 1, 0, 's'},
  54. {"help", 0, 0, 'h'},
  55. {0, 0, 0, 0}
  56. };
  57. int opt;
  58. extern int optind;
  59. extern char* optarg;
  60. char* endptr;
  61. while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
  62. {
  63. if (!optarg)
  64. return false;
  65. long int arg = strtol(optarg, &endptr, 10);
  66. if (arg < 0)
  67. return bad_value_();
  68. if (opt == 'e')
  69. {
  70. if (arg > 4)
  71. return bad_value_();
  72. actions_.push_back([arg]() -> bool
  73. {
  74. if (get_current_device() != -1)
  75. pifacedigital_close(get_current_device());
  76. return pifacedigital_open(set_current_device(arg)) != -1;
  77. });
  78. }
  79. else if (opt == 'u')
  80. {
  81. if (arg > 7)
  82. return bad_value_();
  83. actions_.push_back([arg]() -> bool
  84. {
  85. pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
  86. return true;
  87. });
  88. }
  89. else if (opt == 'd')
  90. {
  91. if (arg > 7)
  92. return bad_value_();
  93. actions_.push_back([arg]() -> bool
  94. {
  95. pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
  96. return true;
  97. });
  98. }
  99. else if (opt == 's')
  100. {
  101. if (arg > UINT_MAX)
  102. return bad_value_();
  103. actions_.push_back([arg]() -> bool
  104. {
  105. usleep(arg * 1000);
  106. return true;
  107. });
  108. }
  109. else
  110. return false;
  111. }
  112. return optind == argc_;
  113. }
  114. int MainClass::execute()
  115. {
  116. if (argc_ <= 2 || !build_actions_())
  117. return usage();
  118. for (auto f : actions_)
  119. if (!f())
  120. return 1;
  121. return 0;
  122. }
  123. int MainClass::get_current_device()
  124. {
  125. return current_device_;
  126. }
  127. int MainClass::set_current_device(int c)
  128. {
  129. return (current_device_ = c);
  130. }
  131. uint8_t MainClass::get_pins()
  132. {
  133. return pins_;
  134. }
  135. uint8_t MainClass::up_pin(uint8_t pin)
  136. {
  137. return (pins_ |= (1 << pin));
  138. }
  139. uint8_t MainClass::down_pin(uint8_t pin)
  140. {
  141. return (pins_ &= ~(1 << pin));
  142. }