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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. " [--version|-v]" << std::endl;
  25. std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
  26. << std::endl << std::endl;
  27. std::cerr << "--device \tSwitch to the provided device."
  28. << std::endl << "--up \t\tEnable the provided ouput pin."
  29. << std::endl << "--down \t\tDisable the provided output pin."
  30. << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
  31. << std::endl << "--help \t\tDisplay this message."
  32. << std::endl << "--version \tDisplay version."
  33. << std::endl << std::endl;
  34. std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
  35. << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
  36. << UINT_MAX << "."
  37. << std::endl << "DEVICE \t: The device number. Default is 0. "
  38. "0 <= DEVICE <= 4." << std::endl;
  39. return EX_USAGE;
  40. }
  41. bool MainClass::bad_value_()
  42. {
  43. extern int optind;
  44. extern char* optarg;
  45. std::cerr << "Bad value " << optarg <<
  46. " for option " << argv_[optind - 2] << std::endl << std::endl;
  47. return false;
  48. }
  49. bool MainClass::build_actions_()
  50. {
  51. option opts[] = {
  52. {"device", 1, 0, 'e'},
  53. {"up", 1, 0, 'u'},
  54. {"down", 1, 0, 'd'},
  55. {"sleep", 1, 0, 's'},
  56. {"help", 0, 0, 'h'},
  57. {"version", 0, 0, 'v'},
  58. {0, 0, 0, 0}
  59. };
  60. int opt;
  61. extern int optind;
  62. extern char* optarg;
  63. char* endptr;
  64. while ((opt = getopt_long(argc_, argv_, "e:u:d:s:hv", opts, 0)) != -1)
  65. {
  66. long int arg = optarg ? strtol(optarg, &endptr, 10) : 0;
  67. if (arg < 0)
  68. return bad_value_();
  69. if (opt == 'e')
  70. {
  71. if (arg > 4)
  72. return bad_value_();
  73. actions_.push_back([arg]() -> bool
  74. {
  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. if (get_current_device() == -1 &&
  86. pifacedigital_open(set_current_device(arg)) == -1)
  87. return false;
  88. pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
  89. return true;
  90. });
  91. }
  92. else if (opt == 'd')
  93. {
  94. if (arg > 7)
  95. return bad_value_();
  96. actions_.push_back([arg]() -> bool
  97. {
  98. if (get_current_device() == -1 &&
  99. pifacedigital_open(set_current_device(arg)) == -1)
  100. return false;
  101. pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
  102. return true;
  103. });
  104. }
  105. else if (opt == 's')
  106. {
  107. if (arg > UINT_MAX)
  108. return bad_value_();
  109. actions_.push_back([arg]() -> bool
  110. {
  111. usleep(arg * 1000);
  112. return true;
  113. });
  114. }
  115. else if (opt == 'v')
  116. {
  117. std::cout << "camotion-piface 1.0" << std::endl;
  118. actions_.clear();
  119. return true;
  120. }
  121. else if (opt == 'h')
  122. {
  123. usage();
  124. actions_.clear();
  125. return true;
  126. }
  127. else
  128. return false;
  129. }
  130. return optind == argc_;
  131. }
  132. int MainClass::execute()
  133. {
  134. if (argc_ == 1 || !build_actions_())
  135. return usage();
  136. for (auto f : actions_)
  137. if (!f())
  138. return 1;
  139. return 0;
  140. }
  141. int MainClass::get_current_device()
  142. {
  143. return current_device_;
  144. }
  145. int MainClass::set_current_device(int c)
  146. {
  147. return (current_device_ = c);
  148. }
  149. uint8_t MainClass::get_pins()
  150. {
  151. return pins_;
  152. }
  153. uint8_t MainClass::up_pin(uint8_t pin)
  154. {
  155. return (pins_ |= (1 << pin));
  156. }
  157. uint8_t MainClass::down_pin(uint8_t pin)
  158. {
  159. return (pins_ &= ~(1 << pin));
  160. }