lightbar.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <Adafruit_NeoPixel.h>
  2. #define LIGHTBAR_PIN A3
  3. #define LIGHTBAR_NUM_LEDS 4
  4. #define LIGHTBAR_NUM_COMPONENTS 3
  5. // TODO: fix this so that the fade works nicely
  6. #define LIGHTBAR_MAX_NUM_STEPS 16 // number of steps between start and target color
  7. // #define LIGHTBAR_MAX_NUM_STEPS 5
  8. #define LIGHTBAR_DEFAULT_BRIGHTNESS 16 // out of 255
  9. // lightbar led values
  10. // target value
  11. uint32_t lightBarTarget[LIGHTBAR_NUM_LEDS];
  12. // starting value
  13. uint32_t lightBarStart[LIGHTBAR_NUM_LEDS];
  14. // difference between starting and target color (for each led, each color component)
  15. float colorStep[LIGHTBAR_NUM_LEDS][LIGHTBAR_NUM_COMPONENTS];
  16. // counter for progress to target color
  17. int lightBarCounter;
  18. // total number of incremental steps between starting and target color
  19. int lightBarNumSteps;
  20. // lightbar control
  21. Adafruit_NeoPixel lightBar = Adafruit_NeoPixel(LIGHTBAR_NUM_LEDS, LIGHTBAR_PIN, NEO_GRB + NEO_KHZ800);
  22. void lightBarInit () {
  23. lightBarCounter = LIGHTBAR_MAX_NUM_STEPS;
  24. lightBarNumSteps = LIGHTBAR_MAX_NUM_STEPS;
  25. lightBar.begin();
  26. lightBar.setBrightness(LIGHTBAR_DEFAULT_BRIGHTNESS);
  27. lightBar.show();
  28. }
  29. // helper function to return a byte component of a color
  30. // ex: input = 0x123456
  31. // compNumber return value
  32. // 0 0x56
  33. // 1 0x34
  34. // 2 0x12
  35. uint32_t isolateComponent(uint32_t input, int compNumber) {
  36. return (uint32_t)((input >> (compNumber*8))&0xff);
  37. }
  38. // helper function to return intermediate progress color
  39. // note: difference between start and target color is computed every iteration
  40. // (instead of just at the beginning - this way the LEDs are completely independent of each other)
  41. uint32_t calculateColorProgress(uint32_t startColor, uint32_t targetColor, int stepNumber, int numSteps) {
  42. float stepSize;
  43. int32_t output = startColor;
  44. for (int i=0; i<LIGHTBAR_NUM_COMPONENTS; i++){
  45. stepSize = (float)((float)isolateComponent(targetColor, i) - (float)isolateComponent(startColor, i)) / (float)numSteps ;
  46. output += (int32_t)((int32_t)(stepSize*(float)stepNumber))<<(i*8);
  47. }
  48. return (uint32_t)output;
  49. }
  50. // helper function that does all the work setting an led to change to a specific color
  51. void setLedTargetColor (int index, uint32_t color) {
  52. // set the starting point based on the current status of the light
  53. lightBarStart[index] = calculateColorProgress(lightBarStart[index], lightBarTarget[index], lightBarCounter+1, lightBarNumSteps);
  54. // set the new target
  55. lightBarTarget[index] = color;
  56. }
  57. void lightBarSetColor (uint32_t color) {
  58. for (int i=0; i<LIGHTBAR_NUM_LEDS; i++){
  59. setLedTargetColor(i, color);
  60. }
  61. // reset the counter to start the fade
  62. lightBarCounter = 0;
  63. }
  64. void lightBarSetButton (int buttonIndex, uint32_t color) {
  65. if (buttonIndex >= 0 && buttonIndex < LIGHTBAR_NUM_LEDS) {
  66. setLedTargetColor(buttonIndex, color);
  67. // reset the counter to start the fade
  68. lightBarCounter = 0;
  69. }
  70. }
  71. void lightBarFade () {
  72. if (lightBarCounter < lightBarNumSteps) {
  73. for (int i=0; i<LIGHTBAR_NUM_LEDS; i++){
  74. lightBar.setPixelColor(i, calculateColorProgress(lightBarStart[i], lightBarTarget[i], lightBarCounter+1, lightBarNumSteps));
  75. // if (i==0){
  76. // Serial.print("For step ");
  77. // Serial.print(lightBarCounter);
  78. // Serial.print(" color is ");
  79. // Serial.println(calculateColorProgress(lightBarStart[i], colorStep[i], lightBarCounter), HEX);
  80. // }
  81. }
  82. lightBar.show();
  83. lightBarCounter++;
  84. // when the target color is reached, update the start value to be the same as the target
  85. // this is required so that leds can be independent of each other
  86. if (lightBarCounter >= lightBarNumSteps) {
  87. // Serial.println("Reached target - Setting start colors to target value");
  88. for (int i=0; i<LIGHTBAR_NUM_LEDS; i++){
  89. lightBarStart[i] = lightBarTarget[i];
  90. }
  91. }
  92. }
  93. }