TM1637Display.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (Off) to 8 (highest brightness)
  42. void setBrightness(uint8_t brightness);
  43. //! Sets the colon indicator mode on or off
  44. //! @param colon When true, lights up the colon on next use of showNumberDec
  45. void setColon(const bool colon);
  46. //! Display arbitrary data on the module
  47. //!
  48. //! This function receives raw segment values as input and displays them. The segment data
  49. //! is given as a byte array, each byte corresponding to a single digit. Within each byte,
  50. //! bit 0 is segment A, bit 1 is segment B etc.
  51. //! The function may either set the entire display or any desirable part on its own. The first
  52. //! digit is given by the @ref pos argument with 0 being the leftmost digit. The @ref length
  53. //! argument is the number of digits to be set. Other digits are not affected.
  54. //!
  55. //! @param segments An array of size @ref length containing the raw segment values
  56. //! @param length The number of digits to be modified
  57. //! @param pos The position from which to start the modification (0 - leftmost, 3 - rightmost)
  58. void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
  59. //! Displayes a decimal number
  60. //!
  61. //! Dispalyes the given argument as a decimal number
  62. //!
  63. //! @param num The number to be shown
  64. //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
  65. //! blank
  66. //! @param length The number of digits to set. The user must ensure that the number to be shown
  67. //! fits to the number of digits requested (for example, if two digits are to be displayed,
  68. //! the number must be between 0 to 99)
  69. //! @param pos The position least significant digit (0 - leftmost, 3 - rightmost)
  70. void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
  71. //! Translate a single digit into 7 segment code
  72. //!
  73. //! The method accepts a number between 0 - 15 and converts it to the
  74. //! code required to display the number on a 7 segment display.
  75. //! Numbers between 10-15 are converted to hexadecimal digits (A-F)
  76. //!
  77. //! @param digit A number between 0 to 15
  78. //! @return A code representing the 7 segment image of the digit (LSB - segment A;
  79. //! bit 6 - segment G; bit 7 - always zero)
  80. uint8_t encodeDigit(uint8_t digit);
  81. protected:
  82. void bitDelay();
  83. void start();
  84. void stop();
  85. bool writeByte(uint8_t b);
  86. private:
  87. uint8_t m_pinClk;
  88. uint8_t m_pinDIO;
  89. uint8_t m_brightness;
  90. bool m_colon;
  91. };
  92. #endif // __TM1637DISPLAY__