|
31 | 31 | #endif |
32 | 32 |
|
33 | 33 |
|
34 | | - |
35 | 34 | #ifdef HAS_ESP_IDF_5 |
36 | 35 |
|
| 36 | +static SemaphoreHandle_t show_mutex = NULL; |
| 37 | + |
37 | 38 | void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) { |
38 | | - rmt_data_t led_data[numBytes * 8]; |
| 39 | + // Note: Because rmtPin is shared between all instances, we will |
| 40 | + // end up releasing/initializing the RMT channels each time we |
| 41 | + // invoke on different pins. This is probably ok, just not |
| 42 | + // efficient. led_data is shared between all instances but will |
| 43 | + // be allocated with enough space for the largest instance; data |
| 44 | + // is not used beyond the mutex lock so this should be fine. |
| 45 | + |
| 46 | +#define SEMAPHORE_TIMEOUT_MS 50 |
| 47 | + |
| 48 | + static rmt_data_t *led_data = NULL; |
| 49 | + static uint32_t led_data_size = 0; |
| 50 | + static int rmtPin = -1; |
| 51 | + |
| 52 | + if (show_mutex && xSemaphoreTake(show_mutex, SEMAPHORE_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) { |
| 53 | + uint32_t requiredSize = numBytes * 8; |
| 54 | + if (requiredSize > led_data_size) { |
| 55 | + free(led_data); |
| 56 | + if (led_data = (rmt_data_t *)malloc(requiredSize * sizeof(rmt_data_t))) { |
| 57 | + led_data_size = requiredSize; |
| 58 | + } else { |
| 59 | + led_data_size = 0; |
| 60 | + } |
| 61 | + } else if (requiredSize == 0) { |
| 62 | + // To release RMT resources (RMT channels and led_data), call |
| 63 | + // .updateLength(0) to set number of pixels/bytes to zero, |
| 64 | + // then call .show() to invoke this code and free resources. |
| 65 | + free(led_data); |
| 66 | + led_data = NULL; |
| 67 | + if (rmtPin >= 0) { |
| 68 | + rmtDeinit(rmtPin); |
| 69 | + rmtPin = -1; |
| 70 | + } |
| 71 | + led_data_size = 0; |
| 72 | + } |
39 | 73 |
|
40 | | - if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { |
41 | | - log_e("Failed to init RMT TX mode on pin %d", pin); |
42 | | - return; |
43 | | - } |
| 74 | + if (led_data_size > 0 && requiredSize <= led_data_size) { |
| 75 | + if (pin != rmtPin) { |
| 76 | + if (rmtPin >= 0) { |
| 77 | + rmtDeinit(rmtPin); |
| 78 | + rmtPin = -1; |
| 79 | + } |
| 80 | + if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { |
| 81 | + log_e("Failed to init RMT TX mode on pin %d", pin); |
| 82 | + return; |
| 83 | + } |
| 84 | + rmtPin = pin; |
| 85 | + } |
44 | 86 |
|
45 | | - int i=0; |
46 | | - for (int b=0; b < numBytes; b++) { |
47 | | - for (int bit=0; bit<8; bit++){ |
48 | | - if ( pixels[b] & (1<<(7-bit)) ) { |
49 | | - led_data[i].level0 = 1; |
50 | | - led_data[i].duration0 = 8; |
51 | | - led_data[i].level1 = 0; |
52 | | - led_data[i].duration1 = 4; |
53 | | - } else { |
54 | | - led_data[i].level0 = 1; |
55 | | - led_data[i].duration0 = 4; |
56 | | - led_data[i].level1 = 0; |
57 | | - led_data[i].duration1 = 8; |
| 87 | + if (rmtPin >= 0) { |
| 88 | + int i=0; |
| 89 | + for (int b=0; b < numBytes; b++) { |
| 90 | + for (int bit=0; bit<8; bit++){ |
| 91 | + if ( pixels[b] & (1<<(7-bit)) ) { |
| 92 | + led_data[i].level0 = 1; |
| 93 | + led_data[i].duration0 = 8; |
| 94 | + led_data[i].level1 = 0; |
| 95 | + led_data[i].duration1 = 4; |
| 96 | + } else { |
| 97 | + led_data[i].level0 = 1; |
| 98 | + led_data[i].duration0 = 4; |
| 99 | + led_data[i].level1 = 0; |
| 100 | + led_data[i].duration1 = 8; |
| 101 | + } |
| 102 | + i++; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + rmtWrite(pin, led_data, numBytes * 8, RMT_WAIT_FOR_EVER); |
58 | 107 | } |
59 | | - i++; |
60 | 108 | } |
61 | | - } |
62 | 109 |
|
63 | | - //pinMode(pin, OUTPUT); // don't do this, will cause the rmt to disable! |
64 | | - rmtWrite(pin, led_data, numBytes * 8, RMT_WAIT_FOR_EVER); |
| 110 | + xSemaphoreGive(show_mutex); |
| 111 | + } |
65 | 112 | } |
66 | 113 |
|
67 | | - |
| 114 | +// To avoid race condition initializing the mutex, all instances of |
| 115 | +// Adafruit_NeoPixel must be constructed before launching and child threads |
| 116 | +void espInit() { |
| 117 | + if (!show_mutex) { |
| 118 | + show_mutex = xSemaphoreCreateMutex(); |
| 119 | + } |
| 120 | +} |
68 | 121 |
|
69 | 122 | #else |
70 | 123 |
|
@@ -219,6 +272,6 @@ void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) |
219 | 272 | } |
220 | 273 |
|
221 | 274 | #endif // ifndef IDF5 |
222 | | - |
| 275 | + |
223 | 276 |
|
224 | 277 | #endif // ifdef(ESP32) |
0 commit comments