Advertisement
Matqux

update

May 25th, 2022
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. void WS2812B::update()
  2. {
  3.     uint32_t index = 0; //And index for creating the PWM value sequence
  4.  
  5.     uint8_t scaledRED;
  6.     uint8_t scaledGREEN;
  7.     uint8_t scaledBLUE;
  8.     uint32_t data;
  9.  
  10.     for (uint32_t i = 0; i < pixelNum; i++) //For every pixel
  11.     {
  12.  
  13.         if (brightness) //If brightness is not zero (So the brightness is not maximum)
  14.         {
  15.             //Scale the values with the provided brightness
  16.             scaledGREEN = (pixelData[i][0] * brightness) >> 8;
  17.             scaledRED = (pixelData[i][1] * brightness) >> 8;
  18.             scaledBLUE = (pixelData[i][2] * brightness) >> 8;
  19.         }
  20.         else    //If the brightness is maximum, just copy the values
  21.         {
  22.             //Set every color value to maximum brightness
  23.             scaledGREEN = pixelData[i][0];
  24.             scaledRED = pixelData[i][1];
  25.             scaledBLUE = pixelData[i][2];
  26.         }
  27.  
  28.         data = (scaledGREEN << 16) | (scaledRED << 8) | scaledBLUE; //We create a 32bit data variable with bit manipulation. This was data will be 0x00GGRRBB
  29.  
  30.         //For every bit in the data
  31.         //The for has an inverted order because the WS2812B pixels need MSB first, but the DMA will send LSB first
  32.         for (int32_t j = 23; j >= 0; j--)
  33.         {
  34.             if ((data & (1 << j)))  //We check the given bit in the data variable
  35.             {
  36.                 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)
  37.             }
  38.             else
  39.             {
  40.                 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)
  41.             }
  42.             index++;    //Increment the index to calculate the whole sequence
  43.         }
  44.     }
  45.  
  46.     for (int32_t i = 0; i < 50; i++)    //50 times
  47.     {
  48.         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
  49.         index++;    //Increment the index to write every last byte
  50.     }
  51.  
  52.     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
  53.  
  54.     while (dma->State == HAL_DMA_STATE_BUSY) //Wait for the data to be sent
  55.     {
  56.     }
  57.  
  58.     HAL_TIM_PWM_Stop_DMA(tim, timChannel);  //Stop DMA transfer
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement