SoftSerial.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include "AP_RCProtocol.h"
  17. class SoftSerial {
  18. public:
  19. enum serial_config {
  20. SERIAL_CONFIG_8N1, // DSM, SRXL etc, 8 bit, no parity, 1 stop bit
  21. SERIAL_CONFIG_8E2I, // SBUS, 8 bit, even parity, 2 stop bits, inverted
  22. };
  23. SoftSerial(uint32_t baudrate, enum serial_config config);
  24. bool process_pulse(uint32_t width_s0, uint32_t width_s1, uint8_t &b);
  25. // get timestamp of the last byte
  26. uint32_t get_byte_timestamp_us(void) const {
  27. return byte_timestamp_us;
  28. }
  29. private:
  30. const uint32_t baudrate;
  31. const uint8_t half_bit; // width of half a bit in microseconds
  32. const enum serial_config config;
  33. uint8_t data_width;
  34. uint8_t byte_width;
  35. uint16_t stop_mask;
  36. uint32_t timestamp_us;
  37. uint32_t byte_timestamp_us;
  38. struct {
  39. uint32_t byte;
  40. uint16_t bit_ofs;
  41. } state;
  42. };