tm1650.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <SmingCore.h>
  2. #include "configuration.h"
  3. #include "tm1650.h"
  4. #include "Wire.h"
  5. /* Private Defines */
  6. #define TM1650_ADDR_SYS 0x48
  7. #define TM1650_ADDR_BTN 0x4F
  8. #define TM1650_ADDR_DIG1 0x68
  9. #define TM1650_ADDR_DIG2 0x6A
  10. #define TM1650_ADDR_DIG3 0x6C
  11. #define TM1650_ADDR_DIG4 0x6E
  12. /* Private Variables */
  13. static const uint8_t Digits[16] = {
  14. Sym_0, Sym_1, Sym_2, Sym_3, Sym_4, Sym_5, Sym_6, Sym_7,
  15. Sym_8, Sym_9, Sym_A, Sym_B, Sym_C, Sym_D, Sym_E, Sym_F
  16. };
  17. static const uint8_t Addr[LED_NUM] = {
  18. TM1650_ADDR_DIG1, TM1650_ADDR_DIG2, TM1650_ADDR_DIG3, TM1650_ADDR_DIG4
  19. };
  20. static uint8 Buffer[LED_NUM] = {0};
  21. /* Private Fuctions */
  22. bool I2CWriteTo(uint8_t addr, uint8_t data);
  23. /**
  24. * @brief Initialization
  25. */
  26. void TM1650_Init(void) {
  27. I2CWriteTo(TM1650_ADDR_SYS, 0x79);
  28. TM1650_Out(Sym_Minus, Sym_Minus, Sym_Minus, Sym_Minus);
  29. }
  30. /**
  31. * @brief Output digit to all indicators
  32. *
  33. * @param i1 Value in range 0..F
  34. * @param i2 Value in range 0..F
  35. * @param i3 Value in range 0..F
  36. * @param i4 Value in range 0..F
  37. */
  38. void TM1650_Out(uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4) {
  39. Buffer[0] = Digits[i1];
  40. Buffer[1] = Digits[i2];
  41. Buffer[2] = Digits[i3];
  42. Buffer[3] = Digits[i4];
  43. I2CWriteTo(TM1650_ADDR_DIG1, Buffer[0]);
  44. I2CWriteTo(TM1650_ADDR_DIG2, Buffer[1]);
  45. I2CWriteTo(TM1650_ADDR_DIG3, Buffer[2]);
  46. I2CWriteTo(TM1650_ADDR_DIG4, Buffer[3]);
  47. }
  48. /**
  49. * @brief Output symbol to indicator 1.
  50. *
  51. * @param value Segment code.
  52. */
  53. void TM1650_Out1(tm1650_sym_t value) {
  54. Buffer[0] = value;
  55. I2CWriteTo(TM1650_ADDR_DIG1, value);
  56. }
  57. /**
  58. * @brief Output symbol to indicator 2.
  59. *
  60. * @param value Segment code.
  61. */
  62. void TM1650_Out2(tm1650_sym_t value) {
  63. Buffer[1] = value;
  64. I2CWriteTo(TM1650_ADDR_DIG2, value);
  65. }
  66. /**
  67. * @brief Output symbol to indicator 3.
  68. *
  69. * @param value Segment code.
  70. */
  71. void TM1650_Out3(tm1650_sym_t value) {
  72. Buffer[2] = value;
  73. I2CWriteTo(TM1650_ADDR_DIG3, value);
  74. }
  75. /**
  76. * @brief Output symbol to indicator 4.
  77. *
  78. * @param value Segment code.
  79. */
  80. void TM1650_Out4(tm1650_sym_t value) {
  81. Buffer[3] = value;
  82. I2CWriteTo(TM1650_ADDR_DIG4, value);
  83. }
  84. /**
  85. * @brief Set dot for selected led
  86. *
  87. * @param pos Led 0..3
  88. */
  89. void TM1650_DotSet(tm1650_pos_t pos) {
  90. if (pos > LED_NUM-1) {
  91. return;
  92. }
  93. I2CWriteTo(Addr[pos], (Buffer[pos] | Sym_Dot));
  94. }
  95. /**
  96. * @brief Reset dot for selected led
  97. *
  98. * @param pos Led 0..3
  99. */
  100. void TM1650_DotRes(tm1650_pos_t pos) {
  101. if (pos > LED_NUM-1) {
  102. return;
  103. }
  104. I2CWriteTo(Addr[pos], (Buffer[pos] & ~(Sym_Dot)));
  105. }
  106. /**
  107. * @brief Set LED bright
  108. *
  109. * @param bright Brgiht level 0..7
  110. */
  111. void TM1650_Bright(uint8_t bright) {
  112. if (bright > LedBrightMax) {
  113. bright = LedBrightMax;
  114. }
  115. bright <<= 4;
  116. bright |= 1; // Display On
  117. I2CWriteTo(TM1650_ADDR_SYS, bright);
  118. }
  119. bool I2CWriteTo(uint8_t addr, uint8_t data) {
  120. Wire.beginTransmission(addr);
  121. Wire.write(data);
  122. return Wire.endTransmission();
  123. }