TM1637Display.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef __TM1637DISPLAY__
  17. #define __TM1637DISPLAY__
  18. #include <inttypes.h>
  19. #define SEG_A 0b00000001
  20. #define SEG_B 0b00000010
  21. #define SEG_C 0b00000100
  22. #define SEG_D 0b00001000
  23. #define SEG_E 0b00010000
  24. #define SEG_F 0b00100000
  25. #define SEG_G 0b01000000
  26. // Defines which element's highest bit is the colon's control bit
  27. #define COLON_POSITION 1
  28. class TM1637Display {
  29. public:
  30. //! Initialize a TM1637Display object, setting the clock and
  31. //! data pins.
  32. //!
  33. //! @param pinClk - The number of the digital pin connected to the clock pin of the module
  34. //! @param pinDIO - The number of the digital pin connected to the DIO pin of the module
  35. TM1637Display(uint8_t pinClk, uint8_t pinDIO);
  36. //! Sets the brightness of the display.
  37. //!
  38. //! The setting takes effect when a command is given to change the data being
  39. //! displayed.
  40. //!
  41. //! @param brightness A number from 0 (lowes brightness) to 7 (highest brightness)
  42. //! @param on Turn display on or off
  43. void setBrightness(uint8_t brightness, bool on = true);
  44. //! Sets the colon indicator mode on or off
  45. //! @param colon When true, lights up the colon on next use of showNumberDec
  46. void setColon(const bool colon);
  47. //! Display arbitrary data on the module
  48. //!
  49. //! This function receives raw segment values as input and displays them. The segment data
  50. //! is given as a byte array, each byte corresponding to a single digit. Within each byte,
  51. //! bit 0 is segment A, bit 1 is segment B etc.
  52. //! The function may either set the entire display or any desirable part on its own. The first
  53. //! digit is given by the @ref pos argument with 0 being the leftmost digit. The @ref length
  54. //! argument is the number of digits to be set. Other digits are not affected.
  55. //!
  56. //! @param segments An array of size @ref length containing the raw segment values
  57. //! @param length The number of digits to be modified
  58. //! @param pos The position from which to start the modification (0 - leftmost, 3 - rightmost)
  59. void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
  60. //! Displayes a decimal number
  61. //!
  62. //! Dispalyes the given argument as a decimal number
  63. //!
  64. //! @param num The number to be shown
  65. //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
  66. //! blank
  67. //! @param length The number of digits to set. The user must ensure that the number to be shown
  68. //! fits to the number of digits requested (for example, if two digits are to be displayed,
  69. //! the number must be between 0 to 99)
  70. //! @param pos The position most significant digit (0 - leftmost, 3 - rightmost)
  71. void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
  72. //! Displayes a decimal number, with dot control
  73. //!
  74. //! Dispalyes the given argument as a decimal number. The dots between the digits (or colon)
  75. //! can be individually controlled
  76. //!
  77. //! @param num The number to be shown
  78. //! @param dots Dot enable. The argument is a bitmask, with each bit corresponding to a dot
  79. //! between the digits (or colon mark, as implemented by each module). The MSB is the
  80. //! leftmost dot of the digit being update. For example, if pos is 1, the MSB of dots
  81. //! will correspond the dot near digit no. 2 from the left. Dots are updated only on
  82. //! those digits actually being update (that is, no more than len digits)
  83. //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
  84. //! blank
  85. //! @param length The number of digits to set. The user must ensure that the number to be shown
  86. //! fits to the number of digits requested (for example, if two digits are to be displayed,
  87. //! the number must be between 0 to 99)
  88. //! @param pos The position least significant digit (0 - leftmost, 3 - rightmost)
  89. void showNumberDecEx(int num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
  90. //! Translate a single digit into 7 segment code
  91. //!
  92. //! The method accepts a number between 0 - 15 and converts it to the
  93. //! code required to display the number on a 7 segment display.
  94. //! Numbers between 10-15 are converted to hexadecimal digits (A-F)
  95. //!
  96. //! @param digit A number between 0 to 15
  97. //! @return A code representing the 7 segment image of the digit (LSB - segment A;
  98. //! bit 6 - segment G; bit 7 - always zero)
  99. uint8_t encodeDigit(uint8_t digit);
  100. protected:
  101. void bitDelay();
  102. void start();
  103. void stop();
  104. bool writeByte(uint8_t b);
  105. private:
  106. uint8_t m_pinClk;
  107. uint8_t m_pinDIO;
  108. uint8_t m_brightness;
  109. bool m_colon;
  110. };
  111. #endif // __TM1637DISPLAY__