Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void WS2812B::update()
- {
- uint32_t index = 0; //And index for creating the PWM value sequence
- uint8_t scaledRED;
- uint8_t scaledGREEN;
- uint8_t scaledBLUE;
- uint32_t data;
- for (uint32_t i = 0; i < pixelNum; i++) //For every pixel
- {
- if (brightness) //If brightness is not zero (So the brightness is not maximum)
- {
- //Scale the values with the provided brightness
- scaledGREEN = (pixelData[i][0] * brightness) >> 8;
- scaledRED = (pixelData[i][1] * brightness) >> 8;
- scaledBLUE = (pixelData[i][2] * brightness) >> 8;
- }
- else //If the brightness is maximum, just copy the values
- {
- //Set every color value to maximum brightness
- scaledGREEN = pixelData[i][0];
- scaledRED = pixelData[i][1];
- scaledBLUE = pixelData[i][2];
- }
- data = (scaledGREEN << 16) | (scaledRED << 8) | scaledBLUE; //We create a 32bit data variable with bit manipulation. This was data will be 0x00GGRRBB
- //For every bit in the data
- //The for has an inverted order because the WS2812B pixels need MSB first, but the DMA will send LSB first
- for (int32_t j = 23; j >= 0; j--)
- {
- if ((data & (1 << j))) //We check the given bit in the data variable
- {
- pwmData[index] = 33; //If it was 1, then the PWM pulse width will be 64% of the period. The counter period is 50-1. (40MHz/800kHz)
- }
- else
- {
- pwmData[index] = 17; //If it was 0, then the PWM pulse width will be 32% of the period. The counter period is 50-1. (40MHz/800kHz)
- }
- index++; //Increment the index to calculate the whole sequence
- }
- }
- for (int32_t i = 0; i < 50; i++) //50 times
- {
- pwmData[index] = 0; //Add a 0% duty cycle to the sequence. This was the reset time will be 50*1.25us, which is bigger than the minimum 50us
- index++; //Increment the index to write every last byte
- }
- HAL_TIM_PWM_Start_DMA(tim, timChannel, (uint32_t*) pwmData, pwmNum); //Send the calculated duty cycles on the selected timer and channel for updating the pixels
- while (dma->State == HAL_DMA_STATE_BUSY) //Wait for the data to be sent
- {
- }
- HAL_TIM_PWM_Stop_DMA(tim, timChannel); //Stop DMA transfer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement