Advertisement
safwan092

Untitled

Jan 16th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # Original code found at:
  3. # https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
  4.  
  5. """
  6. Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic
  7. Made available under GNU GENERAL PUBLIC LICENSE
  8.  
  9. # Modified Python I2C library for Raspberry Pi
  10. # as found on http://www.recantha.co.uk/blog/?p=4849
  11. # Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library
  12. # added bits and pieces from various sources
  13. # By DenisFromHR (Denis Pleic)
  14. # 2015-02-10, ver 0.1
  15.  
  16. """
  17.  
  18. # i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
  19. I2CBUS = 1
  20.  
  21. # LCD Address
  22. ADDRESS = 0x27
  23.  
  24. import smbus
  25. from time import sleep
  26.  
  27. class i2c_device:
  28. def __init__(self, addr, port=I2CBUS):
  29. self.addr = addr
  30. self.bus = smbus.SMBus(port)
  31.  
  32. # Write a single command
  33. def write_cmd(self, cmd):
  34. self.bus.write_byte(self.addr, cmd)
  35. sleep(0.0001)
  36.  
  37. # Write a command and argument
  38. def write_cmd_arg(self, cmd, data):
  39. self.bus.write_byte_data(self.addr, cmd, data)
  40. sleep(0.0001)
  41.  
  42. # Write a block of data
  43. def write_block_data(self, cmd, data):
  44. self.bus.write_block_data(self.addr, cmd, data)
  45. sleep(0.0001)
  46.  
  47. # Read a single byte
  48. def read(self):
  49. return self.bus.read_byte(self.addr)
  50.  
  51. # Read
  52. def read_data(self, cmd):
  53. return self.bus.read_byte_data(self.addr, cmd)
  54.  
  55. # Read a block of data
  56. def read_block_data(self, cmd):
  57. return self.bus.read_block_data(self.addr, cmd)
  58.  
  59.  
  60. # commands
  61. LCD_CLEARDISPLAY = 0x01
  62. LCD_RETURNHOME = 0x02
  63. LCD_ENTRYMODESET = 0x04
  64. LCD_DISPLAYCONTROL = 0x08
  65. LCD_CURSORSHIFT = 0x10
  66. LCD_FUNCTIONSET = 0x20
  67. LCD_SETCGRAMADDR = 0x40
  68. LCD_SETDDRAMADDR = 0x80
  69.  
  70. # flags for display entry mode
  71. LCD_ENTRYRIGHT = 0x00
  72. LCD_ENTRYLEFT = 0x02
  73. LCD_ENTRYSHIFTINCREMENT = 0x01
  74. LCD_ENTRYSHIFTDECREMENT = 0x00
  75.  
  76. # flags for display on/off control
  77. LCD_DISPLAYON = 0x04
  78. LCD_DISPLAYOFF = 0x00
  79. LCD_CURSORON = 0x02
  80. LCD_CURSOROFF = 0x00
  81. LCD_BLINKON = 0x01
  82. LCD_BLINKOFF = 0x00
  83.  
  84. # flags for display/cursor shift
  85. LCD_DISPLAYMOVE = 0x08
  86. LCD_CURSORMOVE = 0x00
  87. LCD_MOVERIGHT = 0x04
  88. LCD_MOVELEFT = 0x00
  89.  
  90. # flags for function set
  91. LCD_8BITMODE = 0x10
  92. LCD_4BITMODE = 0x00
  93. LCD_2LINE = 0x08
  94. LCD_1LINE = 0x00
  95. LCD_5x10DOTS = 0x04
  96. LCD_5x8DOTS = 0x00
  97.  
  98. # flags for backlight control
  99. LCD_BACKLIGHT = 0x08
  100. LCD_NOBACKLIGHT = 0x00
  101.  
  102. En = 0b00000100 # Enable bit
  103. Rw = 0b00000010 # Read/Write bit
  104. Rs = 0b00000001 # Register select bit
  105.  
  106. class lcd:
  107. #initializes objects and lcd
  108. def __init__(self):
  109. self.lcd_device = i2c_device(ADDRESS)
  110.  
  111. self.lcd_write(0x03)
  112. self.lcd_write(0x03)
  113. self.lcd_write(0x03)
  114. self.lcd_write(0x02)
  115.  
  116. self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
  117. self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
  118. self.lcd_write(LCD_CLEARDISPLAY)
  119. self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
  120. sleep(0.2)
  121.  
  122.  
  123. # clocks EN to latch command
  124. def lcd_strobe(self, data):
  125. self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
  126. sleep(.0005)
  127. self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
  128. sleep(.0001)
  129.  
  130. def lcd_write_four_bits(self, data):
  131. self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
  132. self.lcd_strobe(data)
  133.  
  134. # write a command to lcd
  135. def lcd_write(self, cmd, mode=0):
  136. self.lcd_write_four_bits(mode | (cmd & 0xF0))
  137. self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
  138.  
  139. # write a character to lcd (or character rom) 0x09: backlight | RS=DR<
  140. # works!
  141. def lcd_write_char(self, charvalue, mode=1):
  142. self.lcd_write_four_bits(mode | (charvalue & 0xF0))
  143. self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))
  144.  
  145. # put string function with optional char positioning
  146. def lcd_display_string(self, string, line=1, pos=0):
  147. if line == 1:
  148. pos_new = pos
  149. elif line == 2:
  150. pos_new = 0x40 + pos
  151. elif line == 3:
  152. pos_new = 0x14 + pos
  153. elif line == 4:
  154. pos_new = 0x54 + pos
  155.  
  156. self.lcd_write(0x80 + pos_new)
  157.  
  158. for char in string:
  159. self.lcd_write(ord(char), Rs)
  160.  
  161. # clear lcd and set to home
  162. def lcd_clear(self):
  163. self.lcd_write(LCD_CLEARDISPLAY)
  164. self.lcd_write(LCD_RETURNHOME)
  165.  
  166. # define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
  167. def backlight(self, state): # for state, 1 = on, 0 = off
  168. if state == 1:
  169. self.lcd_device.write_cmd(LCD_BACKLIGHT)
  170. elif state == 0:
  171. self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
  172.  
  173. # add custom characters (0 - 7)
  174. def lcd_load_custom_chars(self, fontdata):
  175. self.lcd_write(0x40);
  176. for char in fontdata:
  177. for line in char:
  178. self.lcd_write_char(line)
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement