-
Notifications
You must be signed in to change notification settings - Fork 221
Implementing WS2812b signal transmission in SPI plugin. #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 17 commits
05e72b1
fdfdeb9
7a7b040
c17d0d0
960ee4b
8e3ccf9
c94f8f1
58f0ed4
9205b56
791c585
9c22386
585d41f
826061f
7e3f8af
9f0f5be
c511698
0c99492
c17ef87
2629090
d510fec
7d0a5f2
d90feca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -87,11 +87,13 @@ const uint16_t SPIOutput::LPD8806_SLOTS_PER_PIXEL = 3; | |||||
| const uint16_t SPIOutput::P9813_SLOTS_PER_PIXEL = 3; | ||||||
| const uint16_t SPIOutput::APA102_SLOTS_PER_PIXEL = 3; | ||||||
| const uint16_t SPIOutput::APA102_PB_SLOTS_PER_PIXEL = 4; | ||||||
| const uint16_t SPIOutput::WS2812B_SLOTS_PER_PIXEL = 3; | ||||||
|
|
||||||
| // Number of bytes that each pixel uses on the SPI wires | ||||||
| // (if it differs from 1:1 with colors) | ||||||
| const uint16_t SPIOutput::P9813_SPI_BYTES_PER_PIXEL = 4; | ||||||
| const uint16_t SPIOutput::APA102_SPI_BYTES_PER_PIXEL = 4; | ||||||
| const uint16_t SPIOutput::WS2812B_SPI_BYTES_PER_PIXEL = 9; | ||||||
|
|
||||||
| const uint16_t SPIOutput::APA102_START_FRAME_BYTES = 4; | ||||||
| const uint8_t SPIOutput::APA102_LEDFRAME_START_MARK = 0xE0; | ||||||
|
|
@@ -260,6 +262,17 @@ SPIOutput::SPIOutput(const UID &uid, SPIBackendInterface *backend, | |||||
| "APA102 Pixel Brightness Combined", | ||||||
| sdc_irgb_combined)); | ||||||
|
|
||||||
| personalities.insert( | ||||||
| personalities.begin() + PERS_WS2812B_INDIVIDUAL - 1, | ||||||
| Personality(m_pixel_count * WS2812B_SLOTS_PER_PIXEL, | ||||||
| "WS2812b Individual Control")); | ||||||
|
|
||||||
| personalities.insert( | ||||||
| personalities.begin() + PERS_WS2812B_COMBINED - 1, | ||||||
| Personality(WS2812B_SLOTS_PER_PIXEL, | ||||||
| "WS2812b Combined Control", | ||||||
| sdc_rgb_combined)); | ||||||
|
|
||||||
| m_personality_collection.reset(new PersonalityCollection(personalities)); | ||||||
| m_personality_manager.reset(new PersonalityManager( | ||||||
| m_personality_collection.get())); | ||||||
|
|
@@ -386,6 +399,12 @@ bool SPIOutput::InternalWriteDMX(const DmxBuffer &buffer) { | |||||
| case PERS_APA102_PB_COMBINED: | ||||||
| CombinedAPA102ControlPixelBrightness(buffer); | ||||||
| break; | ||||||
| case PERS_WS2812B_INDIVIDUAL: | ||||||
| IndividualWS2812bControl(buffer); | ||||||
| break; | ||||||
| case PERS_WS2812B_COMBINED: | ||||||
| CombinedWS2812bControl(buffer); | ||||||
| break; | ||||||
| default: | ||||||
| break; | ||||||
| } | ||||||
|
|
@@ -871,6 +890,133 @@ uint8_t SPIOutput::CalculateAPA102PixelBrightness(uint8_t brightness) { | |||||
| return (brightness >> 3); | ||||||
| } | ||||||
|
|
||||||
| void SPIOutput::IndividualWS2812bControl(const DmxBuffer &buffer) { | ||||||
| const unsigned int first_slot = m_start_address - 1; // 0 offset | ||||||
| if (buffer.Size() - first_slot < WS2812B_SLOTS_PER_PIXEL) { | ||||||
| OLA_INFO << "Insufficient DMX data, required " << WS2812B_SLOTS_PER_PIXEL | ||||||
| << ", got " << buffer.Size() - first_slot; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // We always check out the entire string length, even if we only have data | ||||||
| // for part of it | ||||||
| const unsigned int output_length = m_pixel_count | ||||||
| * WS2812B_SPI_BYTES_PER_PIXEL; | ||||||
| uint8_t *output = m_backend->Checkout(m_output_number, output_length); | ||||||
| if (!output) { | ||||||
| OLA_INFO << "Unable to create output buffer of required length: " | ||||||
| << output_length; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const unsigned int length = m_pixel_count; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to keep the min to not over-read the buffer, otherwise if the DMX buffer is say 10 slots long in total, and you try to read data for four pixels, g and b of pixel 4 won't be in the buffer!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I check for that within the loop. This is done the way it is to initialise the pixels in case the first DMX signal is not a full set of data, as is the case in the first test.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you say initialise, do you mean to send the o444444 out to all of them? |
||||||
|
|
||||||
| for (unsigned int i = 0; i < length; i++) { | ||||||
| // Convert RGB to GRB | ||||||
| unsigned int offset = first_slot + i * WS2812B_SLOTS_PER_PIXEL; | ||||||
|
|
||||||
| //Get DMX data | ||||||
| uint8_t r = 0; | ||||||
| uint8_t g = 0; | ||||||
| uint8_t b = 0; | ||||||
| if(offset < buffer.Size() - 2) { | ||||||
| r = buffer.Get(offset); | ||||||
| g = buffer.Get(offset + 1); | ||||||
| b = buffer.Get(offset + 2); | ||||||
| } // fill further pixel data only if the pixel data is empty | ||||||
|
||||||
| else if(output[i * WS2812B_SPI_BYTES_PER_PIXEL] != 0) | ||||||
| break; | ||||||
| uint8_t low = 0, mid = 0, high = 0; | ||||||
|
|
||||||
| WS2812bByteMapper(g, &low, &mid, &high); | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL] = high; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 1] = mid; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 2] = low; | ||||||
|
|
||||||
| WS2812bByteMapper(r, &low, &mid, &high); | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 3] = high; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 4] = mid; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 5] = low; | ||||||
|
|
||||||
| WS2812bByteMapper(b, &low, &mid, &high); | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 6] = high; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 7] = mid; | ||||||
| output[i * WS2812B_SPI_BYTES_PER_PIXEL + 8] = low; | ||||||
| } | ||||||
|
|
||||||
| // write output back... | ||||||
| m_backend->Commit(m_output_number); | ||||||
| } | ||||||
|
|
||||||
| void SPIOutput::CombinedWS2812bControl(const DmxBuffer &buffer) { | ||||||
| const unsigned int first_slot = m_start_address - 1; // 0 offset | ||||||
| if (buffer.Size() - first_slot < WS2812B_SLOTS_PER_PIXEL) { | ||||||
| OLA_INFO << "Insufficient DMX data, required " << WS2812B_SLOTS_PER_PIXEL | ||||||
| << ", got " << buffer.Size() - first_slot; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // We always check out the entire string length, even if we only have data | ||||||
| // for part of it | ||||||
| const unsigned int output_length = m_pixel_count | ||||||
| * WS2812B_SPI_BYTES_PER_PIXEL; | ||||||
| uint8_t *output = m_backend->Checkout(m_output_number, output_length); | ||||||
| if (!output) { | ||||||
| OLA_INFO << "Unable to create output buffer of required length: " | ||||||
| << output_length; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Grab RGB data for conversion to GBR | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a typo based on the other use and the code.
Suggested change
|
||||||
| uint8_t r = buffer.Get(first_slot); | ||||||
| uint8_t g = buffer.Get(first_slot + 1); | ||||||
| uint8_t b = buffer.Get(first_slot + 2); | ||||||
| uint8_t low = 0, mid = 0, high = 0; | ||||||
|
|
||||||
| // create Pixel Data | ||||||
| uint8_t pixel_data[WS2812B_SPI_BYTES_PER_PIXEL]; | ||||||
|
|
||||||
| WS2812bByteMapper(g, &low, &mid, &high); | ||||||
| pixel_data[0] = high; | ||||||
| pixel_data[1] = mid; | ||||||
| pixel_data[2] = low; | ||||||
|
|
||||||
| WS2812bByteMapper(r, &low, &mid, &high); | ||||||
| pixel_data[3] = high; | ||||||
| pixel_data[4] = mid; | ||||||
| pixel_data[5] = low; | ||||||
|
|
||||||
| WS2812bByteMapper(b, &low, &mid, &high); | ||||||
| pixel_data[6] = high; | ||||||
| pixel_data[7] = mid; | ||||||
| pixel_data[8] = low; | ||||||
|
|
||||||
| // set all pixel to same value | ||||||
| for (uint16_t i = 0; i < m_pixel_count; i++) { | ||||||
| memcpy(&output[i * WS2812B_SPI_BYTES_PER_PIXEL], pixel_data, | ||||||
| WS2812B_SPI_BYTES_PER_PIXEL); | ||||||
| } | ||||||
|
|
||||||
| // write output back... | ||||||
| m_backend->Commit(m_output_number); | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * Converting to WS2811/12b format. | ||||||
| * | ||||||
| * The format sends each bit with a leading 1 and a trailing 0. | ||||||
| * This function spaces out the bits of a byte and inserts them into a | ||||||
| * hexadecimal version (0x924924) of the octal 44444444, ending up with | ||||||
| * three bytes of information per byte input. | ||||||
| */ | ||||||
| void SPIOutput::WS2812bByteMapper(uint8_t input, | ||||||
| uint8_t *low, uint8_t *mid, uint8_t *high) { | ||||||
| *low = 0x24 | ((input & 0x1) << 1) | ((input & 0x2) << 3) | ||||||
| | ((input & 0x4) << 5); | ||||||
| *mid = 0x49 | ((input & 0x8) >> 1) | ((input & 0x10) << 1); | ||||||
| *high = 0x92 | ((input & 0x20) >> 5) | ((input & 0x40) >> 3) | ||||||
| | ((input & 0x80) >> 1); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| RDMResponse *SPIOutput::GetDeviceInfo(const RDMRequest *request) { | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.