RmtDriver5.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /********************************************************************************
  2. * https://github.com/RoboticsBrno/SmartLeds
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2017 RoboticsBrno (RobotikaBrno)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *******************************************************************************/
  26. #pragma once
  27. #include "RmtDriver.h"
  28. #if SMARTLEDS_NEW_RMT_DRIVER
  29. #include <driver/rmt_tx.h>
  30. #include <freertos/FreeRTOS.h>
  31. #include <freertos/semphr.h>
  32. #include <type_traits>
  33. #include "Color.h"
  34. #if !defined(CONFIG_RMT_ISR_IRAM_SAFE) && !defined(SMARTLEDS_DISABLE_IRAM_WARNING)
  35. #warning "Please enable CONFIG_RMT_ISR_IRAM_SAFE IDF option." \
  36. "without it, the IDF driver is not able to supply data fast enough."
  37. #endif
  38. namespace detail {
  39. constexpr const int CHANNEL_COUNT = SOC_RMT_GROUPS * SOC_RMT_CHANNELS_PER_GROUP;
  40. class RmtDriver;
  41. // This is ridiculous
  42. struct RmtEncoderWrapper {
  43. struct rmt_encoder_t base;
  44. struct rmt_encoder_t* bytes_encoder;
  45. struct rmt_encoder_t* copy_encoder;
  46. RmtDriver* driver;
  47. rmt_symbol_word_t reset_code;
  48. uint8_t buffer[SOC_RMT_MEM_WORDS_PER_CHANNEL / 8];
  49. rmt_encode_state_t last_state;
  50. size_t frame_idx;
  51. uint8_t component_idx;
  52. uint8_t buffer_len;
  53. };
  54. static_assert(std::is_standard_layout<RmtEncoderWrapper>::value == true);
  55. class RmtDriver {
  56. public:
  57. RmtDriver(const LedType& timing, int count, int pin, int channel_num, SemaphoreHandle_t finishedFlag);
  58. RmtDriver(const RmtDriver&) = delete;
  59. esp_err_t init();
  60. esp_err_t registerIsr(bool isFirstRegisteredChannel);
  61. esp_err_t unregisterIsr();
  62. esp_err_t transmit(const Rgb* buffer);
  63. private:
  64. static bool IRAM_ATTR txDoneCallback(
  65. rmt_channel_handle_t tx_chan, const rmt_tx_done_event_data_t* edata, void* user_ctx);
  66. const LedType& _timing;
  67. int _count;
  68. int _pin;
  69. SemaphoreHandle_t _finishedFlag;
  70. rmt_channel_handle_t _channel;
  71. RmtEncoderWrapper _encoder;
  72. };
  73. };
  74. #endif // !SMARTLEDS_NEW_RMT_DRIVER