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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. MainClass::MainClass(int argc, char* argv[])
  11. : argc_(argc)
  12. , argv_(argv)
  13. {
  14. }
  15. int MainClass::usage()
  16. {
  17. std::cerr << "Usage: " << basename(argv_[0]) << std::endl <<
  18. " [--device DEVICE|--device=DEVICE|-e DEVICE]" << std::endl <<
  19. " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
  20. " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
  21. " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
  22. " [--help|-h]" << std::endl <<
  23. " [--version|-v]" << 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 << "--version \tDisplay version."
  32. << std::endl << std::endl;
  33. std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
  34. << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
  35. << UINT_MAX << "."
  36. << std::endl << "DEVICE \t: The device number. Default is 0. "
  37. "0 <= DEVICE <= 4." << std::endl;
  38. return EX_USAGE;
  39. }
  40. bool MainClass::bad_value_()
  41. {
  42. extern int optind;
  43. extern char* optarg;
  44. std::cerr << "Bad value " << optarg <<
  45. " for option " << argv_[optind - 2] << std::endl << std::endl;
  46. return false;
  47. }
  48. bool MainClass::build_actions_()
  49. {
  50. option opts[] = {
  51. {"device", 1, 0, 'e'},
  52. {"up", 1, 0, 'u'},
  53. {"down", 1, 0, 'd'},
  54. {"sleep", 1, 0, 's'},
  55. {"help", 0, 0, 'h'},
  56. {"version", 0, 0, 'v'},
  57. {0, 0, 0, 0}
  58. };
  59. int opt;
  60. extern int optind;
  61. extern char* optarg;
  62. char* endptr;
  63. while ((opt = getopt_long(argc_, argv_, "e:u:d:s:hv", opts, 0)) != -1)
  64. {
  65. long int arg = optarg ? strtol(optarg, &endptr, 10) : 0;
  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. pifacedigital_close(get_current_device());
  75. return set_current_device(arg);
  76. });
  77. }
  78. else if (opt == 'u' || opt == 'd')
  79. {
  80. if (arg > 7)
  81. return bad_value_();
  82. actions_.push_back([arg, opt]() -> bool
  83. {
  84. if (get_current_device() == -1 &&
  85. !set_current_device(0))
  86. return false;
  87. std::cout << "Setting pin " << arg << " " <<
  88. (opt == 'u' ? "up" : "down") << std::endl;
  89. pifacedigital_write_bit(opt == 'u', arg, OUTPUT,
  90. get_current_device());
  91. return true;
  92. });
  93. }
  94. else if (opt == 's')
  95. {
  96. if (arg > INT_MAX)
  97. return bad_value_();
  98. actions_.push_back([arg]() -> bool
  99. {
  100. std::cout << "Sleeping " << (arg * 1000) << " ms" << std::endl;
  101. usleep(arg * 1000);
  102. return true;
  103. });
  104. }
  105. else if (opt == 'v')
  106. {
  107. std::cout << "camotion-piface 1.0" << std::endl;
  108. actions_.clear();
  109. return true;
  110. }
  111. else if (opt == 'h')
  112. {
  113. usage();
  114. actions_.clear();
  115. return true;
  116. }
  117. else
  118. return false;
  119. }
  120. return optind == argc_;
  121. }
  122. int MainClass::execute()
  123. {
  124. if (argc_ == 1 || !build_actions_())
  125. return usage();
  126. for (auto f : actions_)
  127. if (!f())
  128. return 1;
  129. return 0;
  130. }
  131. int MainClass::get_current_device()
  132. {
  133. return current_device_;
  134. }
  135. bool MainClass::set_current_device(int c)
  136. {
  137. std::cout << "Switching to device " << c << std::endl;
  138. if (pifacedigital_open((current_device_ = c)) == -1)
  139. {
  140. std::cerr << std::endl;
  141. return false;
  142. }
  143. return true;
  144. }