PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

PulsePosition.h 3.1KB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* PulsePosition Library for Teensy 3.x, LC, and 4.0
  2. * High resolution input and output of PPM encoded signals
  3. * http://www.pjrc.com/teensy/td_libs_PulsePosition.html
  4. * Copyright (c) 2019, Paul Stoffregen, paul@pjrc.com
  5. *
  6. * Development of this library was funded by PJRC.COM, LLC by sales of Teensy
  7. * boards. Please support PJRC's efforts to develop open source software by
  8. * purchasing Teensy or other PJRC products.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice, development funding notice, and this permission
  18. * notice shall be included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <Arduino.h>
  29. #ifdef __AVR__
  30. #error "Sorry, PulsePosition does not work on Teensy 2.0 and other AVR-based boards"
  31. #elif defined(__IMXRT1062__)
  32. #include "PulsePositionIMXRT.h"
  33. #else
  34. #define PULSEPOSITION_MAXCHANNELS 16
  35. struct ftm_channel_struct {
  36. uint32_t csc;
  37. uint32_t cv;
  38. };
  39. class PulsePositionOutput
  40. {
  41. public:
  42. PulsePositionOutput(void);
  43. PulsePositionOutput(int polarity);
  44. bool begin(uint8_t txPin); // txPin can be 5,6,9,10,20,21,22,23
  45. bool begin(uint8_t txPin, uint8_t framePin);
  46. bool write(uint8_t channel, float microseconds);
  47. friend void ftm0_isr(void);
  48. private:
  49. void isr(void);
  50. uint32_t pulse_width[PULSEPOSITION_MAXCHANNELS+1];
  51. uint32_t pulse_buffer[PULSEPOSITION_MAXCHANNELS+1];
  52. uint32_t pulse_remaining;
  53. volatile uint8_t *framePinReg;
  54. volatile uint8_t framePinMask;
  55. struct ftm_channel_struct *ftm;
  56. uint8_t state;
  57. uint8_t current_channel;
  58. uint8_t total_channels;
  59. uint8_t total_channels_buffer;
  60. uint8_t cscSet;
  61. uint8_t cscClear;
  62. static PulsePositionOutput *list[8];
  63. static uint8_t channelmask;
  64. };
  65. class PulsePositionInput
  66. {
  67. public:
  68. PulsePositionInput(void);
  69. PulsePositionInput(int polarity);
  70. bool begin(uint8_t rxPin); // rxPin can be 5,6,9,10,20,21,22,23
  71. int available(void);
  72. float read(uint8_t channel);
  73. friend void ftm0_isr(void);
  74. private:
  75. void isr(void);
  76. struct ftm_channel_struct *ftm;
  77. uint32_t pulse_width[PULSEPOSITION_MAXCHANNELS];
  78. uint32_t pulse_buffer[PULSEPOSITION_MAXCHANNELS];
  79. uint32_t prev;
  80. uint8_t write_index;
  81. uint8_t total_channels;
  82. uint8_t cscEdge;
  83. bool available_flag;
  84. static bool overflow_inc;
  85. static uint16_t overflow_count;
  86. static PulsePositionInput *list[8];
  87. static uint8_t channelmask;
  88. };
  89. #endif