Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function, division, absolute_import, unicode_literals
  4.  
  5. import sys
  6.  
  7.  
  8. from RPLCD_i2c import CharLCD
  9. from RPLCD_i2c import Alignment, CursorMode, ShiftMode
  10. from RPLCD_i2c import cursor, cleared
  11. from time import strftime, sleep
  12. from datetime import datetime
  13. # build big digits
  14. def disp_number(lcd_char, position):
  15.  
  16. if lcd_char=="0" :
  17. lcd.cursor_pos = (0, position)
  18. lcd.write_string(unichr(4))
  19. lcd.write_string(unichr(0))
  20. lcd.write_string(unichr(5))
  21. lcd.cursor_pos = (1, position)
  22. lcd.write_string(unichr(7))
  23. lcd.write_string(unichr(1))
  24. lcd.write_string(unichr(6))
  25.  
  26. elif lcd_char=="1" :
  27. lcd.cursor_pos = (0, position)
  28. lcd.write_string(unichr(0))
  29. lcd.write_string(unichr(5))
  30. lcd.write_string(unichr(254))
  31. lcd.cursor_pos = (1, position)
  32. lcd.write_string(unichr(1))
  33. lcd.write_string(unichr(255))
  34. lcd.write_string(unichr(1))
  35.  
  36. elif lcd_char=="2" :
  37. lcd.cursor_pos = (0, position)
  38. lcd.write_string(unichr(2))
  39. lcd.write_string(unichr(2))
  40. lcd.write_string(unichr(5))
  41. lcd.cursor_pos = (1, position)
  42. lcd.write_string(unichr(255))
  43. lcd.write_string(unichr(1))
  44. lcd.write_string(unichr(1))
  45.  
  46. elif lcd_char=="3" :
  47. lcd.cursor_pos = (0, position)
  48. lcd.write_string(unichr(0))
  49. lcd.write_string(unichr(2))
  50. lcd.write_string(unichr(5))
  51. lcd.cursor_pos = (1, position)
  52. lcd.write_string(unichr(1))
  53. lcd.write_string(unichr(1))
  54. lcd.write_string(unichr(6))
  55.  
  56. elif lcd_char=="4" :
  57. lcd.cursor_pos = (0, position)
  58. lcd.write_string(unichr(255))
  59. lcd.write_string(unichr(1))
  60. lcd.write_string(unichr(255))
  61. lcd.cursor_pos = (1, position)
  62. lcd.write_string(unichr(254))
  63. lcd.write_string(unichr(254))
  64.  
  65. elif lcd_char=="5" :
  66. lcd.cursor_pos = (0, position)
  67. lcd.write_string(unichr(255))
  68. lcd.write_string(unichr(2))
  69. lcd.write_string(unichr(2))
  70. lcd.cursor_pos = (1, position)
  71. lcd.write_string(unichr(1))
  72. lcd.write_string(unichr(1))
  73. lcd.write_string(unichr(6))
  74.  
  75. elif lcd_char=="6" :
  76. lcd.cursor_pos = (0, position)
  77. lcd.write_string(unichr(4))
  78. lcd.write_string(unichr(2))
  79. lcd.write_string(unichr(2))
  80. lcd.cursor_pos = (1, position)
  81. lcd.write_string(unichr(7))
  82. lcd.write_string(unichr(1))
  83. lcd.write_string(unichr(6))
  84.  
  85. elif lcd_char=="7" :
  86. lcd.cursor_pos = (0, position)
  87. lcd.write_string(unichr(0))
  88. lcd.write_string(unichr(0))
  89. lcd.write_string(unichr(5))
  90. lcd.cursor_pos = (1, position)
  91. lcd.write_string(unichr(254))
  92. lcd.write_string(unichr(254))
  93. lcd.write_string(unichr(6))
  94.  
  95. elif lcd_char=="8" :
  96. lcd.cursor_pos = (0, position)
  97. lcd.write_string(unichr(4))
  98. lcd.write_string(unichr(2))
  99. lcd.write_string(unichr(5))
  100. lcd.cursor_pos = (1, position)
  101. lcd.write_string(unichr(7))
  102. lcd.write_string(unichr(1))
  103. lcd.write_string(unichr(6))
  104.  
  105. elif lcd_char=="9" :
  106. lcd.cursor_pos = (0, position)
  107. lcd.write_string(unichr(255))
  108. lcd.write_string(unichr(2))
  109. lcd.write_string(unichr(5))
  110. lcd.cursor_pos = (1, position)
  111. lcd.write_string(unichr(1))
  112. lcd.write_string(unichr(1))
  113. lcd.write_string(unichr(6))
  114.  
  115. elif lcd_char=="C" :
  116. lcd.cursor_pos = (0, position)
  117. lcd.write_string(unichr(255))
  118. lcd.write_string(unichr(2))
  119. lcd.cursor_pos = (1, position)
  120. lcd.write_string(unichr(254))
  121. lcd.write_string(unichr(254))
  122.  
  123. else:
  124. lcd.cursor_pos = (0, position)
  125. lcd.write_string(unichr(254))
  126. lcd.write_string(unichr(254))
  127. lcd.write_string(unichr(254))
  128. lcd.cursor_pos = (1, position)
  129. lcd.write_string(unichr(254))
  130. lcd.write_string(unichr(254))
  131. lcd.write_string(unichr(254))
  132.  
  133. return lcd_char;
  134.  
  135. # scroll effect
  136. def shift(direction):
  137. if direction=="left" :
  138. for x in range(0, 16):
  139. lcd.shift_display(-1)
  140. sleep(0.05)
  141. else :
  142. for x in range(0, 16):
  143. lcd.shift_display(1)
  144. sleep(0.05)
  145.  
  146.  
  147. def clock_dots():
  148. # display two dots
  149. lcd.cursor_pos = (0, 8)
  150. lcd.write_string(unichr(3))
  151. lcd.cursor_pos = (1, 8)
  152. lcd.write_string(unichr(3))
  153. sleep(0.5)
  154.  
  155. # remove the two dots
  156. lcd.cursor_pos = (0, 8)
  157. lcd.write_string(unichr(254))
  158. lcd.cursor_pos = (1, 8)
  159. lcd.write_string(unichr(254))
  160. sleep(0.5)
  161.  
  162. def clock_date(digits, month, day_name):
  163. # previous text leaving to left
  164. shift("left")
  165. # preparing date buffer
  166. tens_day = disp_number(digits[4], 0)
  167. day = disp_number(digits[5], 4)
  168. lcd.cursor_pos = (0, 8)
  169. lcd.write_string(' ')
  170. lcd.cursor_pos = (1, 8)
  171. lcd.write_string(' ')
  172. lcd.cursor_pos = (0, 8)
  173. lcd.write_string(month[:8])
  174. lcd.cursor_pos = (1, 8)
  175. lcd.write_string(day_name[:8])
  176. # date entering from left
  177. shift("right")
  178. sleep(2)
  179.  
  180.  
  181. def clock_temp():
  182. # previous text leaving to left
  183. shift("left")
  184. # preparing temp buffer
  185. lcd.cursor_pos = (0, 0)
  186. lcd.write_string('Temp: ')
  187. lcd.cursor_pos = (1, 0)
  188. lcd.write_string(' ')
  189.  
  190. temp_digits = str(read_temp());
  191. disp_number(temp_digits[0], 6)
  192. disp_number(temp_digits[1], 10)
  193. disp_number("C", 14)
  194. lcd.cursor_pos = (0, 13)
  195.  
  196. def clock_hour(digits):
  197. # date leaving to left
  198. shift("left")
  199. # preparing hour buffer
  200. lcd.cursor_pos = (0, 0)
  201. lcd.write_string(' ')
  202. lcd.cursor_pos = (1, 0)
  203. lcd.write_string(' ')
  204. disp_number(digits[0], 0)
  205. disp_number(digits[1], 4)
  206. disp_number(digits[2], 9)
  207. disp_number(digits[3], 13)
  208. # hour entering from left
  209. shift("right")
  210.  
  211.  
  212. def main():
  213.  
  214. try:
  215. input = raw_input
  216. except NameError:
  217. pass
  218.  
  219. try:
  220. unichr = unichr
  221. except NameError:
  222. unichr = chr
  223. old_time = 0
  224. counter = 0
  225.  
  226. # custom symbols
  227. lcd.clear()
  228. top_line = (0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000)
  229. bottom_line = (0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111)
  230. both_lines = (0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111)
  231. dot = (0b00000, 0b00000, 0b00000, 0b11000, 0b11000, 0b00000, 0b00000, 0b00000)
  232. up_left = (0b00111, 0b01111, 0b01111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111)
  233. up_right = (0b11100, 0b11110, 0b11110, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111)
  234. bot_right = (0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11110, 0b11110, 0b11100)
  235. bot_left = (0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b01111, 0b01111, 0b00111)
  236.  
  237. lcd.create_char(0, top_line)
  238. lcd.create_char(1, bottom_line)
  239. lcd.create_char(2, both_lines)
  240. lcd.create_char(3, dot)
  241. lcd.create_char(4, up_left)
  242. lcd.create_char(5, up_right)
  243. lcd.create_char(6, bot_right)
  244. lcd.create_char(7, bot_left)
  245.  
  246. #main loop
  247. while True:
  248. counter += 1
  249. new_time = datetime.now().strftime('%H%M%d')
  250. month = datetime.now().strftime('%B')
  251. day_name = datetime.now().strftime('%A')
  252.  
  253. # time changed, update LCD buffer
  254. if new_time!=old_time :
  255. digits = str(new_time)
  256.  
  257. tens_hour = disp_number(digits[0], 0)
  258. hour = disp_number(digits[1], 4)
  259. tens_minutes = disp_number(digits[2], 9)
  260. minutes = disp_number(digits[3], 13)
  261. old_time = new_time
  262.  
  263. cur_hour = int(tens_hour)*10 + int(hour)
  264. # enable backlight by night
  265. lcd.set_backlight(cur_hour>=19 or cur_hour<9)
  266.  
  267. # dots blink
  268. clock_dots()
  269.  
  270. if counter==15 :
  271. counter=0
  272. # date display
  273. clock_date(digits, month, day_name)
  274. # temperature display
  275. # temperature display
  276. clock_hour(digits)
  277.  
  278.  
  279. if __name__ == '__main__':
  280.  
  281. lcd = CharLCD(address=0x27, port=1, cols=16, rows=2, dotsize=8)
  282.  
  283. try:
  284. main()
  285. except KeyboardInterrupt:
  286. pass
  287. finally:
  288. lcd.clear()
  289. lcd.set_backlight(False)
  290. lcd.home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement