SHARE
TWEET

Untitled

a guest Dec 15th, 2018 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Neopixel::Neopixel(int size)
  2. :values(size),
  3.  mode(Mode::UP),
  4.  LED_data{0}
  5. {
  6.     values.shrink_to_fit();
  7.  
  8.     Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 9, IOCON_FUNC1); /* PIO0_9 connected to MOSI0 */
  9.     Chip_SSP_Init(LPC_SSP0);
  10.     Chip_SSP_SetMaster(LPC_SSP0, 1);
  11.     Chip_SSP_SetClockRate(LPC_SSP0, 0, 6);
  12.     Chip_SSP_SetFormat(LPC_SSP0, SSP_BITS_15, SSP_FRAMEFORMAT_SPI, SSP_CLOCK_CPHA0_CPOL0);
  13.     Chip_SSP_Enable(LPC_SSP0);
  14. }
  15.  
  16. Neopixel::~Neopixel()
  17. {
  18.     Chip_SSP_Disable(LPC_SSP0);
  19. }
  20.  
  21. void Neopixel::send()
  22. {
  23.     for (auto& val : values) {
  24.         for(int i{23}; i >= 0; --i) {
  25.             LED_data = get_bit(val, i) ? CODE_1 : CODE_0;
  26.             while(!Chip_SSP_GetStatus(LPC_SSP0, SSP_STAT_TNF)); //TX FIFO not FULL
  27.             Chip_SSP_SendFrame(LPC_SSP0, LED_data);
  28.         }
  29.     }
  30. }
  31.  
  32. void Neopixel::shine_everyone(Color val)
  33. {
  34.     std::fill(std::begin(values), std::end(values), val);
  35.     send();
  36. }
  37.  
  38. void Neopixel::rotate_left(std::vector<Color> colors)
  39. {
  40.     static uint32_t cnt{0};
  41.     values[cnt % values.size()] = colors[cnt % colors.size()];
  42.     send();
  43.  
  44.     if (values[values.size() - 1] != 0) {
  45.         std::fill(std::begin(values), std::end(values), Color{});
  46.         mode = Mode::DOWN;
  47.     }
  48.     ++cnt;
  49. }
  50.  
  51. void Neopixel::rotate_right(std::vector<Color> colors)
  52. {
  53.     static uint32_t cnt{values.size() - 1};
  54.     values[cnt] = colors[cnt % colors.size()];
  55.     send();
  56.  
  57.     if (values[0] != 0) {
  58.         std::fill(std::begin(values), std::end(values), Color{});
  59.         mode = Mode::UP;
  60.         cnt = values.size();
  61.     }
  62.     --cnt;
  63. }
  64.  
  65. void Neopixel::run_n_back(std::vector<Color> colors)
  66. {
  67.     mode == Mode::UP ? rotate_left(colors) : rotate_right(colors);
  68. }
  69.  
  70. auto Neopixel::create_vec_levels(Color color, double bright)
  71. {
  72.     auto col{color};
  73.     bool flag{true};
  74.  
  75.     std::vector<Color> to_zeros{col};
  76.  
  77.     while (flag) {
  78.         auto col_array = col.RGB_vec();
  79.         std::transform(std::begin(col_array), std::end(col_array), std::begin(col_array),
  80.                 [=](auto comp) {return comp * bright;});
  81.  
  82.         if (std::all_of(std::begin(col_array), std::end(col_array), [](int i){return i == 0;})) {
  83.             col = {};
  84.             flag = false;
  85.         }
  86.         else {
  87.             col = {col_array[0], col_array[1], col_array[2]};
  88.         }
  89.         to_zeros.push_back(col);
  90.     }
  91.  
  92.     return to_zeros;
  93. }
  94.  
  95. void Neopixel::darkness(Color color, double bright)
  96. {
  97.     static auto vec_colors = create_vec_levels(color, bright);
  98.     static uint32_t cnt{0};
  99.     std::fill(std::begin(values), std::end(values), vec_colors[cnt % vec_colors.size()]);
  100.     send();
  101.     ++cnt;
  102.  
  103.     for (volatile int i{0}; i < 1000000; ++i);
  104. }
  105.  
  106. void Neopixel::brightness(Color color, double bright)
  107. {
  108.     static auto vec_colors = create_vec_levels(color, bright);
  109.     static auto cnt{vec_colors.size() - 1};
  110.     std::fill(std::begin(values), std::end(values), vec_colors[cnt]);
  111.     send();
  112.     cnt = cnt > 0 ? cnt - 1 : vec_colors.size() - 1;
  113.  
  114.     for (volatile int i{0}; i < 1000000; ++i);
  115. }
  116.  
  117. void Neopixel::every_second(Color color)
  118. {
  119.     for (uint32_t i{0}; i < values.size(); i += 2)
  120.         values[i] = color;
  121.     send();
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top