You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Driver for the 4 channels LEDs.
  28. #ifndef STREAMS_DRIVERS_LEDS_H_
  29. #define STREAMS_DRIVERS_LEDS_H_
  30. #include "stmlib/stmlib.h"
  31. namespace streams {
  32. const uint8_t kNumLeds = 8;
  33. class Leds {
  34. public:
  35. Leds() { }
  36. ~Leds() { }
  37. void Init();
  38. void set(uint8_t led, uint8_t red, uint8_t green) {
  39. red_[led] = red;
  40. green_[led] = green;
  41. }
  42. void Write();
  43. void Clear();
  44. void PaintBar(int32_t start, int32_t direction, int32_t db) {
  45. if (db < 0) {
  46. return;
  47. }
  48. if (db > 32767) {
  49. db = 32767;
  50. }
  51. db <<= 1;
  52. if (db >= 49152) {
  53. set(start, (db - 49152) >> 6, 0);
  54. set(start + direction, 255, 255);
  55. set(start + 2 * direction, 0, 255);
  56. set(start + 3 * direction, 0, 255);
  57. } else if (db >= 32768) {
  58. set(start + direction, (db - 32768) >> 6, (db - 32768) >> 6);
  59. set(start + 2 * direction, 0, 255);
  60. set(start + 3 * direction, 0, 255);
  61. } else if (db >= 16384) {
  62. set(start + 2 * direction, 0, (db - 16384) >> 6);
  63. set(start + 3 * direction, 0, 255);
  64. } else {
  65. set(start + 3 * direction, 0, db >> 6);
  66. }
  67. }
  68. void PaintPositiveBar(uint8_t channel, int32_t db) {
  69. PaintBar(channel * 4, +1, db);
  70. }
  71. void PaintNegativeBar(uint8_t channel, int32_t db) {
  72. PaintBar(channel * 4 + 3, -1, -db);
  73. }
  74. void PaintCv(uint8_t channel, int32_t cv) {
  75. uint8_t bank = channel * 4;
  76. bool flip = false;
  77. if (cv < 0) {
  78. flip = true;
  79. cv = -cv;
  80. }
  81. if (cv < 1024) {
  82. cv = 0;
  83. }
  84. if (cv > 32767) {
  85. cv = 32767;
  86. }
  87. for (uint8_t i = 0; i < 4; ++i) {
  88. int16_t residual = (cv - (3 - i) * 8192);
  89. if (residual < 0) {
  90. residual = 0;
  91. } else if (residual > 8191) {
  92. residual = 8191;
  93. }
  94. if (flip) {
  95. set(bank + i, residual >> 5, 0);
  96. } else {
  97. set(bank + i, 0, residual >> 5);
  98. }
  99. }
  100. }
  101. private:
  102. void WriteMcp23s17(uint8_t address, uint8_t data);
  103. void ShiftOut(uint8_t byte);
  104. uint8_t red_[kNumLeds];
  105. uint8_t green_[kNumLeds];
  106. uint8_t pwm_counter_;
  107. DISALLOW_COPY_AND_ASSIGN(Leds);
  108. };
  109. } // namespace streams
  110. #endif // STREAMS_DRIVERS_LEDS_H_