NTP clock made with ESP-12 (ESP8266) and 16-char TM1640 (JY-LM1640)
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

  1. // lib for JY-LM1640 - inspired by: https://github.com/avishorp/TM1637
  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 __LM1640DISPLAY__
  17. #define __LM1640DISPLAY__
  18. #include <inttypes.h>
  19. // A
  20. // -----
  21. // | |
  22. // F | | B
  23. // | G |
  24. // -----
  25. // | |
  26. // E | | C
  27. // | |
  28. // ----- * DP
  29. // D
  30. //
  31. #define S7_A 0x0001
  32. #define S7_B 0x0002
  33. #define S7_C 0x0004
  34. #define S7_D 0x0008
  35. #define S7_E 0x0010
  36. #define S7_F 0x0020
  37. #define S7_G 0x0040
  38. #define S7_DP 0x0080
  39. // Segment bit positions for 7 Segment display using the CATALEX and ROBOTDYN mapping for TM1637
  40. // Modify this table for different 'bit-to-segment' mappings. The ASCII character defines and the FONT_7S const table below
  41. // will be adapted automatically according to the bit-to-segment mapping. Obviously this will only work when the segment
  42. // mapping is identical for every digit position. This will be the case unless the hardware designer really hates software developers.
  43. //
  44. // A
  45. // -----
  46. // | |
  47. // F | | B
  48. // | G |
  49. // -----
  50. // | |
  51. // E | | C
  52. // | |
  53. // ----- * DP
  54. // D
  55. //
  56. #define S7_A 0x0001
  57. #define S7_B 0x0002
  58. #define S7_C 0x0004
  59. #define S7_D 0x0008
  60. #define S7_E 0x0010
  61. #define S7_F 0x0020
  62. #define S7_G 0x0040
  63. #define S7_DP 0x0080
  64. #define SIZE 16
  65. //Mask for blending out and setting 7 segments digits
  66. #define MASK_7S_ALL = (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G}
  67. //The CATALAX mapping between Digit positions or Columns (Left to Right) and Grids (ie memory address) are:
  68. //The column numbers are:
  69. // 0 1 2 3
  70. //The Grids are:
  71. // GR1 GR2 GR3 GR4
  72. //The memory addresses are:
  73. // 0 1 2 3
  74. //The ROBOTDYN mapping between Digit positions or columns (Left to Right) and Grids (ie memory address) are:
  75. //The column numbers are:
  76. // 0 1 2 3 4 5
  77. //The Grids are:
  78. // GR3 GR2 GR1 GR6 GR5 GR4
  79. //The memory addresses are:
  80. // 2 1 0 5 4 3
  81. //The EYEWINK mapping between Digit positions or columns (Left to Right) and Grids (ie memory address) are:
  82. //The column numbers are:
  83. // 0 1 2 3 4 5
  84. //The Grids are:
  85. // GR1 GR2 GR3 GR4 GR5 GR6
  86. //The memory addresses are:
  87. // 0 1 2 3 4 5
  88. //Icons Grid 1
  89. #define S7_DP1 0x0080
  90. #define S7_ICON_GR1 (0x0000)
  91. //#define S7_ICON_GR1 (S7_DP1)
  92. //Icons Grid 2
  93. #define S7_DP2 0x0080
  94. #define S7_COL2 0x0080
  95. #define S7_ICON_GR2 (0x0000)
  96. //#define S7_ICON_GR2 (S7_DP2)
  97. //Icons Grid 3
  98. #define S7_DP3 0x0080
  99. #define S7_ICON_GR3 (0x0000)
  100. //#define S7_ICON_GR3 (S7_DP3)
  101. //Icons Grid 4
  102. #define S7_DP4 0x0080
  103. #define S7_ICON_GR4 (0x0000)
  104. //#define S7_ICON_GR4 (S7_DP4)
  105. //Icons Grid 5
  106. #define S7_DP5 0x0080
  107. #define S7_ICON_GR5 (0x0000)
  108. //#define S7_ICON_GR5 (S7_DP5)
  109. //Icons Grid 6
  110. #define S7_DP6 0x0080
  111. #define S7_ICON_GR6 (0x0000)
  112. //#define S7_ICON_GR6 (S7_DP6)
  113. //Mask for blending out and restoring Icons
  114. extern const char MASK_ICON_GRID[];
  115. // ASCII Font definitions for segments in each character
  116. //
  117. //32 0x20 Symbols
  118. #define C7_SPC (0x0000)
  119. #define C7_EXC (S7_B | S7_C) //!
  120. #define C7_QTE (S7_B | S7_F) //"
  121. #define C7_HSH (S7_C | S7_D | S7_E | S7_G) //#
  122. #define C7_DLR (S7_A | S7_C | S7_D | S7_F | S7_G) //$
  123. #define C7_PCT (S7_C | S7_F) //%
  124. #define C7_AMP (S7_A | S7_C | S7_D | S7_E | S7_F | S7_G) //&
  125. #define C7_ACC (S7_B) //'
  126. #define C7_LBR (S7_A | S7_D | S7_E | S7_F) //(
  127. #define C7_RBR (S7_A | S7_B | S7_C | S7_D) //)
  128. #define C7_MLT (S7_B | S7_C | S7_E | S7_F | S7_G) //*
  129. #define C7_PLS (S7_B | S7_C | S7_G) //+
  130. #define C7_CMA (S7_DP)
  131. #define C7_MIN (S7_G)
  132. #define C7_DPT (S7_DP)
  133. #define C7_RS (S7_B | S7_E | S7_G) // /
  134. //48 0x30 Digits
  135. #define C7_0 (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
  136. #define C7_1 (S7_B | S7_C)
  137. #define C7_2 (S7_A | S7_B | S7_D | S7_E | S7_G)
  138. #define C7_3 (S7_A | S7_B | S7_C | S7_D | S7_G)
  139. #define C7_4 (S7_B | S7_C | S7_F | S7_G)
  140. #define C7_5 (S7_A | S7_C | S7_D | S7_F | S7_G)
  141. #define C7_6 (S7_A | S7_C | S7_D | S7_E | S7_F | S7_G)
  142. #define C7_7 (S7_A | S7_B | S7_C)
  143. #define C7_8 (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F | S7_G)
  144. #define C7_9 (S7_A | S7_B | S7_C | S7_D | S7_F | S7_G)
  145. //58 0x3A
  146. #define C7_COL (S7_D | S7_G) // :
  147. #define C7_SCL (S7_D | S7_G) // ;
  148. #define C7_LT (S7_D | S7_E | S7_G) // <
  149. #define C7_EQ (S7_D | S7_G) // =
  150. #define C7_GT (S7_C | S7_D | S7_G) // >
  151. #define C7_QM (S7_A | S7_B | S7_E | S7_G) // ?
  152. #define C7_AT (S7_A | S7_B | S7_C | S7_D | S7_E | S7_G) // @
  153. //65 0x41 Upper case alphabet
  154. #define C7_A (S7_A | S7_B | S7_C | S7_E | S7_F | S7_G )
  155. #define C7_B (S7_C | S7_D | S7_E | S7_F | S7_G)
  156. #define C7_C (S7_A | S7_D | S7_E | S7_F)
  157. #define C7_D (S7_B | S7_C | S7_D | S7_E | S7_G)
  158. #define C7_E (S7_A | S7_D | S7_E | S7_F | S7_G)
  159. #define C7_F (S7_A | S7_E | S7_F | S7_G)
  160. #define C7_G (S7_A | S7_C | S7_D | S7_E | S7_F)
  161. #define C7_H (S7_B | S7_C | S7_E | S7_F | S7_G)
  162. #define C7_I (S7_B | S7_C)
  163. #define C7_J (S7_B | S7_C | S7_D | S7_E)
  164. #define C7_K (S7_B | S7_C | S7_E | S7_F | S7_G)
  165. #define C7_L (S7_D | S7_E | S7_F)
  166. #define C7_M (S7_A | S7_C | S7_E)
  167. #define C7_N (S7_A | S7_B | S7_C | S7_E | S7_F)
  168. #define C7_O (S7_A | S7_B | S7_C | S7_D | S7_E | S7_F)
  169. #define C7_P (S7_A | S7_B | S7_E | S7_F | S7_G)
  170. #define C7_Q (S7_A | S7_B | S7_C | S7_F | S7_G)
  171. #define C7_R (S7_E | S7_G )
  172. #define C7_S (S7_A | S7_C | S7_D | S7_F | S7_G)
  173. #define C7_T (S7_D | S7_E | S7_F | S7_G)
  174. #define C7_U (S7_B | S7_C | S7_D | S7_E | S7_F)
  175. #define C7_V (S7_B | S7_C | S7_D | S7_E | S7_F)
  176. #define C7_W (S7_B | S7_D | S7_F)
  177. #define C7_X (S7_B | S7_C | S7_E | S7_F | S7_G)
  178. #define C7_Y (S7_B | S7_C | S7_D | S7_F | S7_G)
  179. #define C7_Z (S7_A | S7_B | S7_D | S7_E | S7_G)
  180. //91 0x5B
  181. #define C7_SBL (S7_A | S7_D | S7_E | S7_F) // [
  182. #define C7_LS (S7_C | S7_F | S7_G) // left slash
  183. #define C7_SBR (S7_A | S7_B | S7_C | S7_D) // ]
  184. #define C7_PWR (S7_A | S7_B | S7_F) // ^
  185. #define C7_UDS (S7_D) // _
  186. #define C7_DSH (S7_F) // `
  187. //97 0x61 Lower case alphabet
  188. #define C7_a C7_A
  189. #define C7_b C7_B
  190. #define C7_c C7_C
  191. #define C7_d C7_D
  192. #define C7_e C7_E
  193. #define C7_f C7_H
  194. #define C7_g C7_G
  195. #define C7_h C7_H
  196. #define C7_i C7_I
  197. #define C7_j C7_J
  198. #define C7_k C7_K
  199. #define C7_l C7_L
  200. #define C7_m C7_M
  201. //#define C7_n C7_N
  202. #define C7_n (S7_C | S7_E | S7_G)
  203. //#define C7_o C7_O
  204. #define C7_o (S7_C | S7_D | S7_E | S7_G)
  205. #define C7_p C7_P
  206. #define C7_q C7_Q
  207. //#define C7_r C7_R
  208. #define C7_r (S7_E | S7_G)
  209. #define C7_s C7_S
  210. #define C7_t C7_T
  211. #define C7_u C7_U
  212. #define C7_v C7_V
  213. #define C7_w C7_W
  214. #define C7_x C7_X
  215. #define C7_y C7_Y
  216. #define C7_z C7_Z
  217. //123 0x7B
  218. #define C7_CBL (S7_A | S7_D | S7_E | S7_F) // {
  219. #define C7_OR (S7_B | S7_C) // |
  220. #define C7_CBR (S7_A | S7_B | S7_C | S7_D) // }
  221. #define C7_TLD (S7_B | S7_E | S7_G ) // ~
  222. #define C7_DEL (0x0000)
  223. //User Defined Characters (some examples)
  224. #define C7_DGR (S7_A | S7_B | S7_F | S7_G) //Degrees
  225. // Font data selection for transmission to TM1637 memory
  226. #define LO(x) ( x & 0xFF)
  227. #define HI(x) ((x >> 8) & 0xFF)
  228. // ASCII Font definition table
  229. //
  230. #define FONT_7S_START 0x20
  231. #define FONT_7S_END 0x7F
  232. //#define FONT_7S_NR_CHARS (FONT_7S_END - FONT_7S_START + 1)
  233. class LM1640Display {
  234. public:
  235. //! Initialize a LM1640Display object, setting the clock and
  236. //! data pins.
  237. //!
  238. //! @param pinClk - The number of the digital pin connected to the clock pin of the module
  239. //! @param pinDIO - The number of the digital pin connected to the DIO pin of the module
  240. LM1640Display(uint8_t pinClk, uint8_t pinDIO);
  241. //! Sets the brightness of the display.
  242. //!
  243. //! The setting takes effect when a command is given to change the data being
  244. //! displayed.
  245. //!
  246. //! @param brightness A number from 0 (lowes brightness) to 7 (highest brightness)
  247. //! @param on Turn display on or off
  248. void setBrightness(uint8_t brightness, bool on = true);
  249. //! Display arbitrary data on the module
  250. //!
  251. //! This function receives raw segment values as input and displays them. The segment data
  252. //! is given as a byte array, each byte corresponding to a single digit. Within each byte,
  253. //! bit 0 is segment A, bit 1 is segment B etc.
  254. //! The function may either set the entire display or any desirable part on its own. The first
  255. //! digit is given by the @ref pos argument with 0 being the leftmost digit. The @ref length
  256. //! argument is the number of digits to be set. Other digits are not affected.
  257. //!
  258. //! @param segments An array of size @ref length containing the raw segment values
  259. //! @param length The number of digits to be modified
  260. //! @param pos The position from which to start the modification (0 - leftmost, 3 - rightmost)
  261. void setSegments(const uint8_t segments[], uint8_t length = 16, uint8_t pos = 0);
  262. //! Displayes a decimal number
  263. //!
  264. //! Dispalyes the given argument as a decimal number
  265. //!
  266. //! @param num The number to be shown
  267. //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
  268. //! blank
  269. //! @param length The number of digits to set. The user must ensure that the number to be shown
  270. //! fits to the number of digits requested (for example, if two digits are to be displayed,
  271. //! the number must be between 0 to 99)
  272. //! @param pos The position most significant digit (0 - leftmost, 3 - rightmost)
  273. void showNumberDec(int num, bool leading_zero = false, uint8_t length = 16, uint8_t pos = 0);
  274. //! Displayes a decimal number, with dot control
  275. //!
  276. //! Dispalyes the given argument as a decimal number. The dots between the digits (or colon)
  277. //! can be individually controlled
  278. //!
  279. //! @param num The number to be shown
  280. //! @param dots Dot/Colon enable. The argument is a bitmask, with each bit corresponding to a dot
  281. //! between the digits (or colon mark, as implemented by each module). i.e.
  282. //! For displays with dots between each digit:
  283. //! * 0.000 (0b10000000)
  284. //! * 00.00 (0b01000000)
  285. //! * 000.0 (0b00100000)
  286. //! * 0.0.0.0 (0b11100000)
  287. //! For displays with just a colon:
  288. //! * 00:00 (0b01000000)
  289. //! For displays with dots and colons colon:
  290. //! * 0.0:0.0 (0b11100000)
  291. //! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
  292. //! blank
  293. //! @param length The number of digits to set. The user must ensure that the number to be shown
  294. //! fits to the number of digits requested (for example, if two digits are to be displayed,
  295. //! the number must be between 0 to 99)
  296. //! @param pos The position least significant digit (0 - leftmost, 3 - rightmost)
  297. void showNumberDecEx(int num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 16, uint8_t pos = 0);
  298. void clear(uint8_t length = 16);
  299. //! Translate a single digit into 7 segment code
  300. //!
  301. //! The method accepts a number between 0 - 15 and converts it to the
  302. //! code required to display the number on a 7 segment display.
  303. //! Numbers between 10-15 are converted to hexadecimal digits (A-F)
  304. //!
  305. //! @param digit A number between 0 to 15
  306. //! @return A code representing the 7 segment image of the digit (LSB - segment A;
  307. //! bit 6 - segment G; bit 7 - always zero)
  308. uint8_t encodeDigit(uint8_t digit);
  309. uint8_t encodeChar(char ch);
  310. void showString(char str[]);
  311. protected:
  312. void bitDelay();
  313. void start();
  314. void stop();
  315. bool writeByte(uint8_t b);
  316. private:
  317. uint8_t m_pinClk;
  318. uint8_t m_pinDIO;
  319. uint8_t m_brightness;
  320. };
  321. #endif // __LM1640Display__