tm1650.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <SmingCore.h>
  2. #include "configuration.h"
  3. #include "tm1650.h"
  4. #include "Wire.h"
  5. /* Private Defines */
  6. #define TM1650_ADDR_SYS (uint8_t)(0x48 >> 1)
  7. #define TM1650_ADDR_BTN (uint8_t)(0x4F >> 1)
  8. #define TM1650_ADDR_DIG1 (uint8_t)(0x68 >> 1)
  9. #define TM1650_ADDR_DIG2 (uint8_t)(0x6A >> 1)
  10. #define TM1650_ADDR_DIG3 (uint8_t)(0x6C >> 1)
  11. #define TM1650_ADDR_DIG4 (uint8_t)(0x6E >> 1)
  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 bool isPresent;
  21. static uint8 Buffer[LED_NUM] = {0};
  22. /* Private Fuctions */
  23. static bool I2CWriteTo(uint8_t addr, uint8_t data);
  24. /**
  25. * @brief Initialization
  26. */
  27. void TM1650_Init(void) {
  28. if (I2CWriteTo(TM1650_ADDR_SYS, 0x71)) {
  29. Serial.println("TM1650 not respond!");
  30. isPresent = false;
  31. return;
  32. }
  33. isPresent = true;
  34. TM1650_Bright(LedBrightMiddl);
  35. }
  36. /**
  37. * @brief Output digit to all indicators
  38. *
  39. * @param i1 Value in range 0..F
  40. * @param i2 Value in range 0..F
  41. * @param i3 Value in range 0..F
  42. * @param i4 Value in range 0..F
  43. */
  44. void TM1650_Out(uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4) {
  45. if (!isPresent) { return; }
  46. Buffer[0] = Digits[i1];
  47. Buffer[1] = Digits[i2];
  48. Buffer[2] = Digits[i3];
  49. Buffer[3] = Digits[i4];
  50. I2CWriteTo(TM1650_ADDR_DIG1, Buffer[0]);
  51. I2CWriteTo(TM1650_ADDR_DIG2, Buffer[1]);
  52. I2CWriteTo(TM1650_ADDR_DIG3, Buffer[2]);
  53. I2CWriteTo(TM1650_ADDR_DIG4, Buffer[3]);
  54. }
  55. /**
  56. * @brief Output symbol to indicator 1.
  57. *
  58. * @param value Segment code.
  59. */
  60. void TM1650_Out1(tm1650_sym_t value) {
  61. if (!isPresent) { return; }
  62. Buffer[0] = value;
  63. I2CWriteTo(TM1650_ADDR_DIG1, value);
  64. }
  65. /**
  66. * @brief Output symbol to indicator 2.
  67. *
  68. * @param value Segment code.
  69. */
  70. void TM1650_Out2(tm1650_sym_t value) {
  71. if (!isPresent) { return; }
  72. Buffer[1] = value;
  73. I2CWriteTo(TM1650_ADDR_DIG2, value);
  74. }
  75. /**
  76. * @brief Output symbol to indicator 3.
  77. *
  78. * @param value Segment code.
  79. */
  80. void TM1650_Out3(tm1650_sym_t value) {
  81. if (!isPresent) { return; }
  82. Buffer[2] = value;
  83. I2CWriteTo(TM1650_ADDR_DIG3, value);
  84. }
  85. /**
  86. * @brief Output symbol to indicator 4.
  87. *
  88. * @param value Segment code.
  89. */
  90. void TM1650_Out4(tm1650_sym_t value) {
  91. if (!isPresent) { return; }
  92. Buffer[3] = value;
  93. I2CWriteTo(TM1650_ADDR_DIG4, value);
  94. }
  95. /**
  96. * @brief Set dot for selected led
  97. *
  98. * @param pos Led 0..3
  99. */
  100. void TM1650_DotSet(tm1650_pos_t pos) {
  101. if (!isPresent) { return; }
  102. if (pos > LED_NUM-1) {
  103. return;
  104. }
  105. I2CWriteTo(Addr[pos], (Buffer[pos] | Sym_Dot));
  106. }
  107. /**
  108. * @brief Reset dot for selected led
  109. *
  110. * @param pos Led 0..3
  111. */
  112. void TM1650_DotRes(tm1650_pos_t pos) {
  113. if (!isPresent) { return; }
  114. if (pos > LED_NUM-1) {
  115. return;
  116. }
  117. I2CWriteTo(Addr[pos], (Buffer[pos] & ~(Sym_Dot)));
  118. }
  119. /**
  120. * @brief Set LED bright
  121. *
  122. * @param bright Brgiht level 0..7
  123. */
  124. void TM1650_Bright(uint8_t bright) {
  125. if (!isPresent) { return; }
  126. if (bright > LedBrightMax) {
  127. bright = LedBrightMax;
  128. }
  129. bright <<= 4;
  130. bright |= 1; // Display On
  131. I2CWriteTo(TM1650_ADDR_SYS, bright);
  132. }
  133. static bool I2CWriteTo(uint8_t addr, uint8_t data) {
  134. Wire.beginTransmission(addr);
  135. Wire.write(data);
  136. return Wire.endTransmission();
  137. }