TM1637Display.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Author: avishorp@gmail.com
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU Lesser General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public
  14. // License along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. extern "C" {
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <inttypes.h>
  20. }
  21. #include <TM1637Display.h>
  22. #include <Arduino.h>
  23. #define TM1637_I2C_COMM1 0x40
  24. #define TM1637_I2C_COMM2 0xC0
  25. #define TM1637_I2C_COMM3 0x80
  26. //
  27. // A
  28. // ---
  29. // F | | B
  30. // -G-
  31. // E | | C
  32. // ---
  33. // D
  34. const uint8_t digitToSegment[] = {
  35. // XGFEDCBA
  36. 0b00111111, // 0
  37. 0b00000110, // 1
  38. 0b01011011, // 2
  39. 0b01001111, // 3
  40. 0b01100110, // 4
  41. 0b01101101, // 5
  42. 0b01111101, // 6
  43. 0b00000111, // 7
  44. 0b01111111, // 8
  45. 0b01101111, // 9
  46. 0b01110111, // A
  47. 0b01111100, // b
  48. 0b00111001, // C
  49. 0b01011110, // d
  50. 0b01111001, // E
  51. 0b01110001 // F
  52. };
  53. TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO)
  54. {
  55. // Copy the pin numbers
  56. m_pinClk = pinClk;
  57. m_pinDIO = pinDIO;
  58. // Set the pin direction and default value.
  59. // Both pins are set as inputs, allowing the pull-up resistors to pull them up
  60. pinMode(m_pinClk, INPUT);
  61. pinMode(m_pinDIO,INPUT);
  62. digitalWrite(m_pinClk, LOW);
  63. digitalWrite(m_pinDIO, LOW);
  64. }
  65. void TM1637Display::setBrightness(uint8_t brightness)
  66. {
  67. if(brightness > 0){
  68. m_brightness = ((brightness - 1) & 0x07) | 0x08;
  69. } else {
  70. m_brightness = 0x00;
  71. }
  72. }
  73. void TM1637Display::setColon(bool colon)
  74. {
  75. m_colon = colon;
  76. }
  77. void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
  78. {
  79. // Write COMM1
  80. start();
  81. writeByte(TM1637_I2C_COMM1);
  82. stop();
  83. // Write COMM2 + first digit address
  84. start();
  85. writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
  86. // Write the data bytes
  87. uint8_t currentByte = 0x00;
  88. for (uint8_t k=0; k < length; k++){
  89. currentByte = segments[k] & 0x7f;
  90. if(k == COLON_POSITION){
  91. if(m_colon){
  92. currentByte |= 0x80;
  93. }
  94. }
  95. writeByte(currentByte);
  96. }
  97. stop();
  98. // Write COMM3 + brightness
  99. start();
  100. writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
  101. stop();
  102. }
  103. void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
  104. {
  105. uint8_t digits[4];
  106. const static int divisors[] = { 1, 10, 100, 1000 };
  107. bool leading = true;
  108. for(int8_t k = 0; k < 4; k++) {
  109. int divisor = divisors[4 - 1 - k];
  110. int d = num / divisor;
  111. if (d == 0) {
  112. if (leading_zero || !leading || (k == 3))
  113. digits[k] = encodeDigit(d);
  114. else
  115. digits[k] = 0;
  116. }
  117. else {
  118. digits[k] = encodeDigit(d);
  119. num -= d * divisor;
  120. leading = false;
  121. }
  122. }
  123. setSegments(digits + (4 - length), length, pos);
  124. }
  125. void TM1637Display::bitDelay()
  126. {
  127. delayMicroseconds(50);
  128. }
  129. void TM1637Display::start()
  130. {
  131. pinMode(m_pinDIO, OUTPUT);
  132. bitDelay();
  133. }
  134. void TM1637Display::stop()
  135. {
  136. pinMode(m_pinDIO, OUTPUT);
  137. bitDelay();
  138. pinMode(m_pinClk, INPUT);
  139. bitDelay();
  140. pinMode(m_pinDIO, INPUT);
  141. bitDelay();
  142. }
  143. bool TM1637Display::writeByte(uint8_t b)
  144. {
  145. uint8_t data = b;
  146. // 8 Data Bits
  147. for(uint8_t i = 0; i < 8; i++) {
  148. // CLK low
  149. pinMode(m_pinClk, OUTPUT);
  150. bitDelay();
  151. // Set data bit
  152. if (data & 0x01)
  153. pinMode(m_pinDIO, INPUT);
  154. else
  155. pinMode(m_pinDIO, OUTPUT);
  156. bitDelay();
  157. // CLK high
  158. pinMode(m_pinClk, INPUT);
  159. bitDelay();
  160. data = data >> 1;
  161. }
  162. // Wait for acknowledge
  163. // CLK to zero
  164. pinMode(m_pinClk, OUTPUT);
  165. pinMode(m_pinDIO, INPUT);
  166. bitDelay();
  167. // CLK to high
  168. pinMode(m_pinClk, INPUT);
  169. bitDelay();
  170. uint8_t ack = digitalRead(m_pinDIO);
  171. if (ack == 0)
  172. pinMode(m_pinDIO, OUTPUT);
  173. bitDelay();
  174. pinMode(m_pinClk, OUTPUT);
  175. bitDelay();
  176. return ack;
  177. }
  178. uint8_t TM1637Display::encodeDigit(uint8_t digit)
  179. {
  180. return digitToSegment[digit & 0x0f];
  181. }