LCD LIB


SUBMITTED BY: bitcoinsachen

DATE: Dec. 20, 2016, 1:39 p.m.

FORMAT: Text only

SIZE: 2.7 kB

HITS: 708

  1. import smbus
  2. from time import *
  3. # General i2c device class so that other devices can be added easily
  4. class i2c_device:
  5. def __init__(self, addr, port):
  6. self.addr = addr
  7. self.bus = smbus.SMBus(port)
  8. def write(self, byte):
  9. self.bus.write_byte(self.addr, byte)
  10. def read(self):
  11. return self.bus.read_byte(self.addr)
  12. def read_nbytes_data(self, data, n): # For sequential reads > 1 byte
  13. return self.bus.read_i2c_block_data(self.addr, data, n)
  14. class lcd:
  15. #initializes objects and lcd
  16. '''
  17. Port definitions
  18. addr, en,rw,rs,d4,d5,d6,d7,bl
  19. 0x27, 2, 1, 0, 4, 5, 6, 7, 3
  20. '''
  21. def __init__(self, addr, port):
  22. self.lcd_device = i2c_device(addr, port)
  23. self.backlight=1; #default backlight on
  24. #set 4 bit mode
  25. self.lcd_device_writebl(0x20)
  26. self.lcd_strobe()
  27. sleep(0.0005)
  28. self.lcd_strobe()
  29. sleep(0.0005)
  30. self.lcd_strobe()
  31. sleep(0.0005)
  32. self.lcd_write(0x28)
  33. self.lcd_write(0x08)
  34. self.lcd_write(0x01)
  35. self.lcd_write(0x06)
  36. self.lcd_write(0x0C)
  37. self.lcd_write(0x0F)
  38. #wrapper to self.lcd_device.write for backlight control
  39. def lcd_device_writebl(self,value):
  40. if self.backlight:
  41. self.lcd_device.write(value | 0x08);
  42. else:
  43. self.lcd_device.write(value)
  44. # control backlight on=1 or off=0
  45. def lcd_backlight(self,on):
  46. self.backlight=on
  47. self.lcd_strobe()
  48. # clocks EN to latch command
  49. def lcd_strobe(self):
  50. #bit 2
  51. self.lcd_device_writebl((self.lcd_device.read() | 0x04))
  52. self.lcd_device_writebl((self.lcd_device.read() & 0xFB))
  53. # write a command to lcd
  54. def lcd_write(self, cmd):
  55. self.lcd_device_writebl((cmd >> 4)<<4)
  56. self.lcd_strobe()
  57. self.lcd_device_writebl((cmd & 0x0F)<<4)
  58. self.lcd_strobe()
  59. self.lcd_device_writebl(0x0)
  60. # write a character to lcd (or character rom)
  61. def lcd_write_char(self, charvalue):
  62. self.lcd_device_writebl((0x01 | (charvalue >> 4)<<4))
  63. self.lcd_strobe()
  64. self.lcd_device_writebl((0x01 | (charvalue & 0x0F)<<4))
  65. self.lcd_strobe()
  66. self.lcd_device_writebl(0x0)
  67. # put char function
  68. def lcd_putc(self, char):
  69. self.lcd_write_char(ord(char))
  70. # put string function
  71. def lcd_puts(self, string, line):
  72. if line == 1:
  73. self.lcd_write(0x80)
  74. if line == 2:
  75. self.lcd_write(0xC0)
  76. if line == 3:
  77. self.lcd_write(0x94)
  78. if line == 4:
  79. self.lcd_write(0xD4)
  80. for char in string:
  81. self.lcd_putc(char)
  82. # clear lcd and set to home
  83. def lcd_clear(self):
  84. self.lcd_write(0x1)
  85. self.lcd_write(0x2)
  86. # add custom characters (0 - 7)
  87. def lcd_load_custon_chars(self, fontdata):
  88. self.lcd_device.bus.write(0x40);
  89. for char in fontdata:
  90. for line in char:
  91. self.lcd_write_char(line)

comments powered by Disqus