RCInput.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <AP_HAL/AP_HAL.h>
  2. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  3. #include "RCInput.h"
  4. #include <SITL/SITL.h>
  5. #include <AP_RCProtocol/AP_RCProtocol.h>
  6. using namespace HALSITL;
  7. extern const AP_HAL::HAL& hal;
  8. void RCInput::init()
  9. {
  10. AP::RC().init();
  11. }
  12. bool RCInput::new_input()
  13. {
  14. if (!using_rc_protocol) {
  15. if (AP::RC().new_input()) {
  16. using_rc_protocol = true;
  17. }
  18. }
  19. if (using_rc_protocol) {
  20. return AP::RC().new_input();
  21. }
  22. if (_sitlState->new_rc_input) {
  23. _sitlState->new_rc_input = false;
  24. return true;
  25. }
  26. return false;
  27. }
  28. uint16_t RCInput::read(uint8_t ch)
  29. {
  30. if (using_rc_protocol) {
  31. return AP::RC().read(ch);
  32. }
  33. if (ch >= num_channels()) {
  34. return 0;
  35. }
  36. return _sitlState->pwm_input[ch];
  37. }
  38. uint8_t RCInput::read(uint16_t* periods, uint8_t len)
  39. {
  40. if (len > num_channels()) {
  41. len = num_channels();
  42. }
  43. for (uint8_t i=0; i < len; i++) {
  44. periods[i] = read(i);
  45. }
  46. return len;
  47. }
  48. uint8_t RCInput::num_channels()
  49. {
  50. if (using_rc_protocol) {
  51. return AP::RC().num_channels();
  52. }
  53. SITL::SITL *_sitl = AP::sitl();
  54. if (_sitl) {
  55. return MIN(_sitl->rc_chancount.get(), SITL_RC_INPUT_CHANNELS);
  56. }
  57. return SITL_RC_INPUT_CHANNELS;
  58. }
  59. #endif