xsns_28_tm1638.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. xsns_28_tm1638.ino - TM1638 8 switch, led and 7 segment unit support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_TM1638
  16. /*********************************************************************************************\
  17. * TM1638 8 switch, led and 7 segment
  18. *
  19. * Uses GPIO TM16 DIO, TM16 CLK and TM16 STB
  20. \*********************************************************************************************/
  21. #define XSNS_28 28
  22. #define TM1638_COLOR_NONE 0
  23. #define TM1638_COLOR_RED 1
  24. #define TM1638_COLOR_GREEN 2
  25. #define TM1638_CLOCK_DELAY 1 // uSec
  26. uint8_t tm1638_type = 1;
  27. uint8_t tm1638_clock_pin = 0;
  28. uint8_t tm1638_data_pin = 0;
  29. uint8_t tm1638_strobe_pin = 0;
  30. uint8_t tm1638_displays = 8;
  31. uint8_t tm1638_active_display = 1;
  32. uint8_t tm1638_intensity = 0;
  33. uint8_t tm1638_state = 0;
  34. /*********************************************************************************************\
  35. * Pieces from library https://github.com/rjbatista/tm1638-library
  36. * and from library https://github.com/MartyMacGyver/TM1638-demos-and-examples
  37. \*********************************************************************************************/
  38. void Tm16XXSend(byte data)
  39. {
  40. for (uint8_t i = 0; i < 8; i++) {
  41. digitalWrite(tm1638_data_pin, !!(data & (1 << i)));
  42. digitalWrite(tm1638_clock_pin, LOW);
  43. delayMicroseconds(TM1638_CLOCK_DELAY);
  44. digitalWrite(tm1638_clock_pin, HIGH);
  45. }
  46. }
  47. void Tm16XXSendCommand(byte cmd)
  48. {
  49. digitalWrite(tm1638_strobe_pin, LOW);
  50. Tm16XXSend(cmd);
  51. digitalWrite(tm1638_strobe_pin, HIGH);
  52. }
  53. void TM16XXSendData(byte address, byte data)
  54. {
  55. Tm16XXSendCommand(0x44);
  56. digitalWrite(tm1638_strobe_pin, LOW);
  57. Tm16XXSend(0xC0 | address);
  58. Tm16XXSend(data);
  59. digitalWrite(tm1638_strobe_pin, HIGH);
  60. }
  61. byte Tm16XXReceive(void)
  62. {
  63. byte temp = 0;
  64. // Pull-up on
  65. pinMode(tm1638_data_pin, INPUT);
  66. digitalWrite(tm1638_data_pin, HIGH);
  67. for (uint8_t i = 0; i < 8; ++i) {
  68. digitalWrite(tm1638_clock_pin, LOW);
  69. delayMicroseconds(TM1638_CLOCK_DELAY);
  70. temp |= digitalRead(tm1638_data_pin) << i;
  71. digitalWrite(tm1638_clock_pin, HIGH);
  72. }
  73. // Pull-up off
  74. pinMode(tm1638_data_pin, OUTPUT);
  75. digitalWrite(tm1638_data_pin, LOW);
  76. return temp;
  77. }
  78. /*********************************************************************************************/
  79. void Tm16XXClearDisplay(void)
  80. {
  81. for (int i = 0; i < tm1638_displays; i++) {
  82. TM16XXSendData(i << 1, 0);
  83. }
  84. }
  85. void Tm1638SetLED(byte color, byte pos)
  86. {
  87. TM16XXSendData((pos << 1) + 1, color);
  88. }
  89. void Tm1638SetLEDs(word leds)
  90. {
  91. for (int i = 0; i < tm1638_displays; i++) {
  92. byte color = 0;
  93. if ((leds & (1 << i)) != 0) {
  94. color |= TM1638_COLOR_RED;
  95. }
  96. if ((leds & (1 << (i + 8))) != 0) {
  97. color |= TM1638_COLOR_GREEN;
  98. }
  99. Tm1638SetLED(color, i);
  100. }
  101. }
  102. byte Tm1638GetButtons(void)
  103. {
  104. byte keys = 0;
  105. digitalWrite(tm1638_strobe_pin, LOW);
  106. Tm16XXSend(0x42);
  107. for (int i = 0; i < 4; i++) {
  108. keys |= Tm16XXReceive() << i;
  109. }
  110. digitalWrite(tm1638_strobe_pin, HIGH);
  111. return keys;
  112. }
  113. /*********************************************************************************************/
  114. void TmInit(void)
  115. {
  116. tm1638_type = 0;
  117. if ((pin[GPIO_TM16CLK] < 99) && (pin[GPIO_TM16DIO] < 99) && (pin[GPIO_TM16STB] < 99)) {
  118. tm1638_clock_pin = pin[GPIO_TM16CLK];
  119. tm1638_data_pin = pin[GPIO_TM16DIO];
  120. tm1638_strobe_pin = pin[GPIO_TM16STB];
  121. pinMode(tm1638_data_pin, OUTPUT);
  122. pinMode(tm1638_clock_pin, OUTPUT);
  123. pinMode(tm1638_strobe_pin, OUTPUT);
  124. digitalWrite(tm1638_strobe_pin, HIGH);
  125. digitalWrite(tm1638_clock_pin, HIGH);
  126. Tm16XXSendCommand(0x40);
  127. Tm16XXSendCommand(0x80 | (tm1638_active_display ? 8 : 0) | tmin(7, tm1638_intensity));
  128. digitalWrite(tm1638_strobe_pin, LOW);
  129. Tm16XXSend(0xC0);
  130. for (int i = 0; i < 16; i++) {
  131. Tm16XXSend(0x00);
  132. }
  133. digitalWrite(tm1638_strobe_pin, HIGH);
  134. tm1638_type = 1;
  135. tm1638_state = 1;
  136. }
  137. }
  138. void TmLoop(void)
  139. {
  140. if (tm1638_state) {
  141. byte buttons = Tm1638GetButtons();
  142. for (byte i = 0; i < MAX_SWITCHES; i++) {
  143. virtualswitch[i] = (buttons &1) ^1;
  144. byte color = (virtualswitch[i]) ? TM1638_COLOR_NONE : TM1638_COLOR_RED;
  145. Tm1638SetLED(color, i);
  146. buttons >>= 1;
  147. }
  148. SwitchHandler(1);
  149. }
  150. }
  151. /*
  152. void TmShow(boolean json)
  153. {
  154. if (tm1638_type) {
  155. }
  156. }
  157. */
  158. /*********************************************************************************************\
  159. * Interface
  160. \*********************************************************************************************/
  161. boolean Xsns28(byte function)
  162. {
  163. boolean result = false;
  164. if (tm1638_type) {
  165. switch (function) {
  166. case FUNC_INIT:
  167. TmInit();
  168. break;
  169. case FUNC_EVERY_50_MSECOND:
  170. TmLoop();
  171. break;
  172. /*
  173. case FUNC_JSON_APPEND:
  174. TmShow(1);
  175. break;
  176. #ifdef USE_WEBSERVER
  177. case FUNC_WEB_APPEND:
  178. TmShow(0);
  179. break;
  180. #endif // USE_WEBSERVER
  181. */
  182. }
  183. }
  184. return result;
  185. }
  186. #endif // USE_TM1638