BrickUp API Service for Docker version.

Grove_LED_Bar.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. LED bar library V2.0
  3. 2010 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Original Author: LG
  5. Modify: Loovee, 2014-2-26
  6. User can choose which Io to be used.
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "Grove_LED_Bar.h"
  20. Grove_LED_Bar::Grove_LED_Bar(unsigned char pinClock, unsigned char pinData, bool greenToRed)
  21. {
  22. __pinClock = pinClock;
  23. __pinData = pinData;
  24. __greenToRed = greenToRed; // ascending or decending
  25. for (byte i = 0; i < 10; i++)
  26. __state[i] = 0x00; // persist state so individual leds can be toggled
  27. pinMode(__pinClock, OUTPUT);
  28. pinMode(__pinData, OUTPUT);
  29. }
  30. // Send the latch command
  31. void Grove_LED_Bar::latchData()
  32. {
  33. digitalWrite(__pinData, LOW);
  34. delayMicroseconds(10);
  35. for (unsigned char i = 0; i < 4; i++)
  36. {
  37. digitalWrite(__pinData, HIGH);
  38. digitalWrite(__pinData, LOW);
  39. }
  40. }
  41. // Send 16 bits of data
  42. void Grove_LED_Bar::sendData(unsigned int data)
  43. {
  44. for (unsigned char i = 0; i < 16; i++)
  45. {
  46. unsigned int state = (data & 0x8000) ? HIGH : LOW;
  47. digitalWrite(__pinData, state);
  48. state = digitalRead(__pinClock) ? LOW : HIGH;
  49. digitalWrite(__pinClock, state);
  50. data <<= 1;
  51. }
  52. }
  53. // Change the orientation
  54. // Green to red, or red to green
  55. void Grove_LED_Bar::setGreenToRed(bool greenToRed)
  56. {
  57. __greenToRed = greenToRed;
  58. setData(__state);
  59. }
  60. // Set level (0-10)
  61. // Level 0 means all leds off
  62. // Level 10 means all leds on
  63. // Level 4.5 means 4 LEDs on and the 5th LED's half on
  64. void Grove_LED_Bar::setLevel(float level)
  65. {
  66. level = max(0, min(10, level));
  67. level *= 8; // there are 8 (noticable) levels of brightness on each segment
  68. // Place number of 'level' of 1-bits on __state
  69. for (byte i = 0; i < 10; i++) {
  70. __state[i] = (level > 8) ? ~0 :
  71. (level > 0) ? ~(~0 << byte(level)) : 0;
  72. level -= 8;
  73. };
  74. setData(__state);
  75. }
  76. // Set a single led
  77. // led (1-10)
  78. // brightness (0-1)
  79. void Grove_LED_Bar::setLed(unsigned char led, float brightness)
  80. {
  81. led = max(1, min(10, led));
  82. brightness = max(0, min(brightness, 1));
  83. // Zero based index 0-9 for bitwise operations
  84. led--;
  85. // 8 (noticable) levels of brightness
  86. // 00000000 darkest
  87. // 00000011 brighter
  88. // ........
  89. // 11111111 brightest
  90. __state[led] = ~(~0 << (unsigned char) (brightness*8));
  91. setData(__state);
  92. }
  93. // Toggle a single led
  94. // led (1-10)
  95. void Grove_LED_Bar::toggleLed(unsigned char led)
  96. {
  97. led = max(1, min(10, led));
  98. // Zero based index 0-9 for bitwise operations
  99. led--;
  100. __state[led] = __state[led] ? 0 : ~0;
  101. setData(__state);
  102. }
  103. // each element in the state will hold the brightness level
  104. // 00000000 darkest
  105. // 00000011 brighter
  106. // ........
  107. // 11111111 brightest
  108. void Grove_LED_Bar::setData(unsigned char __state[])
  109. {
  110. sendData(GLB_CMDMODE);
  111. for (unsigned char i = 0; i < 10; i++)
  112. {
  113. if (__greenToRed)
  114. {
  115. // Go backward on __state
  116. sendData(__state[10-i-1]);
  117. }
  118. else
  119. {
  120. // Go forward on __state
  121. sendData(__state[i]);
  122. }
  123. }
  124. // Two extra empty bits for padding the command to the correct length
  125. sendData(0x00);
  126. sendData(0x00);
  127. latchData();
  128. }
  129. void Grove_LED_Bar::setBits(unsigned int bits)
  130. {
  131. for (unsigned char i = 0; i < 10; i++)
  132. {
  133. if ((bits % 2) == 1)
  134. __state[i] = 0xFF;
  135. else
  136. __state[i] = 0x00;
  137. bits /= 2;
  138. }
  139. setData(__state);
  140. }
  141. // Return the current bits
  142. unsigned int const Grove_LED_Bar::getBits()
  143. {
  144. unsigned int __bits = 0x00;
  145. for (unsigned char i = 0; i < 10; i++)
  146. {
  147. if (__state[i] != 0x0)
  148. __bits |= (0x1 << i);
  149. }
  150. return __bits;
  151. }