BrickUp API Service for Docker version.

TM1637.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * TM1637.cpp
  3. * A library for the 4 digit display
  4. *
  5. * Copyright (c) 2012 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : Frankie.Chu
  8. * Create Time: 9 April,2012
  9. * Change Log :
  10. *
  11. * The MIT License (MIT)
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #include "TM1637.h"
  32. #include <Arduino.h>
  33. static int8_t TubeTab[] = {0x3f,0x06,0x5b,0x4f,
  34. 0x66,0x6d,0x7d,0x07,
  35. 0x7f,0x6f,0x77,0x7c,
  36. 0x39,0x5e,0x79,0x71};//0~9,A,b,C,d,E,F
  37. TM1637::TM1637(uint8_t Clk, uint8_t Data)
  38. {
  39. Clkpin = Clk;
  40. Datapin = Data;
  41. pinMode(Clkpin,OUTPUT);
  42. pinMode(Datapin,OUTPUT);
  43. }
  44. void TM1637::init(void)
  45. {
  46. clearDisplay();
  47. }
  48. int TM1637::writeByte(int8_t wr_data)
  49. {
  50. uint8_t i,count1;
  51. for(i=0;i<8;i++) //sent 8bit data
  52. {
  53. digitalWrite(Clkpin,LOW);
  54. if(wr_data & 0x01)digitalWrite(Datapin,HIGH);//LSB first
  55. else digitalWrite(Datapin,LOW);
  56. wr_data >>= 1;
  57. digitalWrite(Clkpin,HIGH);
  58. }
  59. digitalWrite(Clkpin,LOW); //wait for the ACK
  60. digitalWrite(Datapin,HIGH);
  61. digitalWrite(Clkpin,HIGH);
  62. pinMode(Datapin,INPUT);
  63. bitDelay();
  64. uint8_t ack = digitalRead(Datapin);
  65. if (ack == 0)
  66. {
  67. pinMode(Datapin,OUTPUT);
  68. digitalWrite(Datapin,LOW);
  69. }
  70. bitDelay();
  71. pinMode(Datapin,OUTPUT);
  72. bitDelay();
  73. return ack;
  74. }
  75. //send start signal to TM1637
  76. void TM1637::start(void)
  77. {
  78. digitalWrite(Clkpin,HIGH);//send start signal to TM1637
  79. digitalWrite(Datapin,HIGH);
  80. digitalWrite(Datapin,LOW);
  81. digitalWrite(Clkpin,LOW);
  82. }
  83. //End of transmission
  84. void TM1637::stop(void)
  85. {
  86. digitalWrite(Clkpin,LOW);
  87. digitalWrite(Datapin,LOW);
  88. digitalWrite(Clkpin,HIGH);
  89. digitalWrite(Datapin,HIGH);
  90. }
  91. //display function.Write to full-screen.
  92. void TM1637::display(int8_t DispData[])
  93. {
  94. int8_t SegData[4];
  95. uint8_t i;
  96. for(i = 0;i < 4;i ++)
  97. {
  98. SegData[i] = DispData[i];
  99. }
  100. coding(SegData);
  101. start(); //start signal sent to TM1637 from MCU
  102. writeByte(ADDR_AUTO);//
  103. stop(); //
  104. start(); //
  105. writeByte(Cmd_SetAddr);//
  106. for(i=0;i < 4;i ++)
  107. {
  108. writeByte(SegData[i]); //
  109. }
  110. stop(); //
  111. start(); //
  112. writeByte(Cmd_DispCtrl);//
  113. stop(); //
  114. }
  115. //******************************************
  116. void TM1637::display(uint8_t BitAddr,int8_t DispData)
  117. {
  118. int8_t SegData;
  119. SegData = coding(DispData);
  120. start(); //start signal sent to TM1637 from MCU
  121. writeByte(ADDR_FIXED);//
  122. stop(); //
  123. start(); //
  124. writeByte(BitAddr|0xc0);//
  125. writeByte(SegData);//
  126. stop(); //
  127. start(); //
  128. writeByte(Cmd_DispCtrl);//
  129. stop(); //
  130. }
  131. void TM1637::clearDisplay(void)
  132. {
  133. display(0x00,0x7f);
  134. display(0x01,0x7f);
  135. display(0x02,0x7f);
  136. display(0x03,0x7f);
  137. }
  138. //To take effect the next time it displays.
  139. void TM1637::set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr)
  140. {
  141. Cmd_SetData = SetData;
  142. Cmd_SetAddr = SetAddr;
  143. Cmd_DispCtrl = 0x88 + brightness;//Set the brightness and it takes effect the next time it displays.
  144. }
  145. //Whether to light the clock point ":".
  146. //To take effect the next time it displays.
  147. void TM1637::point(boolean PointFlag)
  148. {
  149. _PointFlag = PointFlag;
  150. }
  151. void TM1637::coding(int8_t DispData[])
  152. {
  153. uint8_t PointData;
  154. if(_PointFlag == POINT_ON)PointData = 0x80;
  155. else PointData = 0;
  156. for(uint8_t i = 0;i < 4;i ++)
  157. {
  158. if(DispData[i] == 0x7f)DispData[i] = 0x00;
  159. else DispData[i] = TubeTab[DispData[i]] + PointData;
  160. }
  161. }
  162. int8_t TM1637::coding(int8_t DispData)
  163. {
  164. uint8_t PointData;
  165. if(_PointFlag == POINT_ON)PointData = 0x80;
  166. else PointData = 0;
  167. if(DispData == 0x7f) DispData = 0x00 + PointData;//The bit digital tube off
  168. else DispData = TubeTab[DispData] + PointData;
  169. return DispData;
  170. }
  171. void TM1637::bitDelay(void)
  172. {
  173. delayMicroseconds(50);
  174. }