Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import time
  4. import smbus
  5. import socket
  6. import fcntl
  7. import struct
  8.  
  9. BUS = smbus.SMBus(1)
  10.  
  11. def write_word(addr, data):
  12. global BLEN
  13. temp = data
  14. if BLEN == 1:
  15. temp |= 0x08
  16. else:
  17. temp &= 0xF7
  18. BUS.write_byte(addr ,temp)
  19.  
  20. def send_command(comm):
  21. # Send bit7-4 firstly
  22. buf = comm & 0xF0
  23. buf |= 0x04 # RS = 0, RW = 0, EN = 1
  24. write_word(LCD_ADDR ,buf)
  25. time.sleep(0.002)
  26. buf &= 0xFB # Make EN = 0
  27. write_word(LCD_ADDR ,buf)
  28.  
  29. # Send bit3-0 secondly
  30. buf = (comm & 0x0F) << 4
  31. buf |= 0x04 # RS = 0, RW = 0, EN = 1
  32. write_word(LCD_ADDR ,buf)
  33. time.sleep(0.002)
  34. buf &= 0xFB # Make EN = 0
  35. write_word(LCD_ADDR ,buf)
  36.  
  37. def send_data(data):
  38. # Send bit7-4 firstly
  39. buf = data & 0xF0
  40. buf |= 0x05 # RS = 1, RW = 0, EN = 1
  41. write_word(LCD_ADDR ,buf)
  42. time.sleep(0.002)
  43. buf &= 0xFB # Make EN = 0
  44. write_word(LCD_ADDR ,buf)
  45.  
  46. # Send bit3-0 secondly
  47. buf = (data & 0x0F) << 4
  48. buf |= 0x05 # RS = 1, RW = 0, EN = 1
  49. write_word(LCD_ADDR ,buf)
  50. time.sleep(0.002)
  51. buf &= 0xFB # Make EN = 0
  52. write_word(LCD_ADDR ,buf)
  53.  
  54. def init(addr, bl):
  55. # global BUS
  56. # BUS = smbus.SMBus(1)
  57. global LCD_ADDR
  58. global BLEN
  59. LCD_ADDR = addr
  60. BLEN = bl
  61. try:
  62. send_command(0x33) # Must initialize to 8-line mode at first
  63. time.sleep(0.005)
  64. send_command(0x32) # Then initialize to 4-line mode
  65. time.sleep(0.005)
  66. send_command(0x28) # 2 Lines & 5*7 dots
  67. time.sleep(0.005)
  68. send_command(0x0C) # Enable display without cursor
  69. time.sleep(0.005)
  70. send_command(0x01) # Clear Screen
  71. BUS.write_byte(LCD_ADDR, 0x08)
  72. except:
  73. return False
  74. else:
  75. return True
  76.  
  77. def clear():
  78. send_command(0x01) # Clear Screen
  79.  
  80. def openlight(): # Enable the backlight
  81. BUS.write_byte(0x27,0x08)
  82. BUS.close()
  83.  
  84. def write(x, y, str):
  85. if x < 0:
  86. x = 0
  87. if x > 15:
  88. x = 15
  89. if y <0:
  90. y = 0
  91. if y > 1:
  92. y = 1
  93.  
  94. # Move cursor
  95. addr = 0x80 + 0x40 * y + x
  96. send_command(addr)
  97.  
  98. for chr in str:
  99. send_data(ord(chr))
  100.  
  101.  
  102. def get_ip_address(ifname):
  103. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  104. return socket.inet_ntoa(fcntl.ioctl(
  105. s.fileno(),
  106. 0x8915,
  107. struct.pack('256s', ifname[:15])
  108. )[20:24])
  109.  
  110.  
  111. if __name__ == '__main__':
  112. init(0x27, 1)
  113. write(0, 1, 'IP: ' + get_ip_address('wlan0'))
  114. write(0, 0, time.strftime('%b')) #month
  115. write(4, 0, time.strftime('%d')) #day of month
  116. write(8, 0, time.strftime('%H:')) #hour
  117. write(11, 0, time.strftime('%M:')) #minute
  118.  
  119. while True:
  120. write(14, 0, time.strftime('%S'))#second
  121. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement