ser2neo: a serial port to NeoPixel ring bridge.

neopixel.h 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // NeoPixel ring driver.
  2. //
  3. // Copyright 2015 Google Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #pragma once
  18. #include <stdint.h>
  19. /// NeoPixel ring driver.
  20. ///
  21. /// Implemented as a bit-banged loop.
  22. ///
  23. class NeoPixel {
  24. public:
  25. NeoPixel() { clear(); }
  26. void init();
  27. void clear();
  28. void append(uint8_t b);
  29. void write();
  30. private:
  31. static const int MaxLEDs = 16;
  32. static const int BitsPerColour = 8;
  33. static const int ColoursPerLED = 3;
  34. static const int Tail = 2;
  35. static const uint8_t Reload = F_CPU / 800000;
  36. static const uint8_t Low = Reload * 2 / 3;
  37. static const uint8_t High = Reload - Low;
  38. static const uint8_t Stop = 0xFF;
  39. uint8_t* p_;
  40. uint8_t bits_[MaxLEDs * ColoursPerLED * BitsPerColour + Tail];
  41. };