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.

171 lines
4.0KB

  1. // Copyright 2013 Emilie Gillet.
  2. //
  3. // Author: Emilie Gillet (emilie.o.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. #pragma once
  29. #include <stmlib/stmlib.h>
  30. namespace streams
  31. {
  32. const uint8_t kNumLeds = 8;
  33. class LedsEmulator
  34. {
  35. public:
  36. LedsEmulator() { }
  37. ~LedsEmulator() { }
  38. void Init()
  39. {
  40. Clear();
  41. }
  42. void set(uint8_t led, uint8_t red, uint8_t green)
  43. {
  44. red_[led] = red;
  45. green_[led] = green;
  46. }
  47. float intensity_red(uint8_t led)
  48. {
  49. return red_[led] / 255.f;
  50. }
  51. float intensity_green(uint8_t led)
  52. {
  53. return green_[led] / 255.f;
  54. }
  55. void Clear()
  56. {
  57. std::fill(&red_[0], &red_[kNumLeds], 0);
  58. std::fill(&green_[0], &green_[kNumLeds], 0);
  59. }
  60. void PaintBar(int32_t start, int32_t direction, int32_t db)
  61. {
  62. if (db < 0)
  63. {
  64. return;
  65. }
  66. if (db > 32767)
  67. {
  68. db = 32767;
  69. }
  70. db <<= 1;
  71. if (db >= 49152)
  72. {
  73. set(start, (db - 49152) >> 6, 0);
  74. set(start + direction, 255, 255);
  75. set(start + 2 * direction, 0, 255);
  76. set(start + 3 * direction, 0, 255);
  77. }
  78. else if (db >= 32768)
  79. {
  80. set(start + direction, (db - 32768) >> 6, (db - 32768) >> 6);
  81. set(start + 2 * direction, 0, 255);
  82. set(start + 3 * direction, 0, 255);
  83. }
  84. else if (db >= 16384)
  85. {
  86. set(start + 2 * direction, 0, (db - 16384) >> 6);
  87. set(start + 3 * direction, 0, 255);
  88. }
  89. else
  90. {
  91. set(start + 3 * direction, 0, db >> 6);
  92. }
  93. }
  94. void PaintPositiveBar(uint8_t channel, int32_t db)
  95. {
  96. PaintBar(channel * 4, +1, db);
  97. }
  98. void PaintNegativeBar(uint8_t channel, int32_t db)
  99. {
  100. PaintBar(channel * 4 + 3, -1, -db);
  101. }
  102. void PaintCv(uint8_t channel, int32_t cv)
  103. {
  104. uint8_t bank = channel * 4;
  105. bool flip = false;
  106. if (cv < 0)
  107. {
  108. flip = true;
  109. cv = -cv;
  110. }
  111. if (cv < 1024)
  112. {
  113. cv = 0;
  114. }
  115. if (cv > 32767)
  116. {
  117. cv = 32767;
  118. }
  119. for (uint8_t i = 0; i < 4; ++i)
  120. {
  121. int16_t residual = (cv - (3 - i) * 8192);
  122. if (residual < 0)
  123. {
  124. residual = 0;
  125. }
  126. else if (residual > 8191)
  127. {
  128. residual = 8191;
  129. }
  130. if (flip)
  131. {
  132. set(bank + i, residual >> 5, 0);
  133. }
  134. else
  135. {
  136. set(bank + i, 0, residual >> 5);
  137. }
  138. }
  139. }
  140. private:
  141. uint8_t red_[kNumLeds];
  142. uint8_t green_[kNumLeds];
  143. DISALLOW_COPY_AND_ASSIGN(LedsEmulator);
  144. };
  145. } // namespace streams