Guest User

Untitled

a guest
May 16th, 2017
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import smbus    # System Management Bus / i2c
  3. import time     # Used to delay when sending signals
  4. import random   # For fun
  5.  
  6. # Configure to your device, find your device address by using (i2cdetect -y 1)
  7. bus = smbus.SMBus(1)    # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
  8. DEVICE_ADDRESS = 0x2F   #7 bit address (will be left shifted to add the read write bit)
  9.  
  10. # Enable or Disable Backlight
  11. def setLight(state):
  12.     if state == True:
  13.         bus.write_byte_data(DEVICE_ADDRESS, 0x0D, 0xFF)
  14.     else:
  15.         bus.write_byte_data(DEVICE_ADDRESS, 0x0D, 0x00)
  16.     time.sleep(0.02)
  17.  
  18. # Blink Light - for dramatic effect
  19. def blinkLight(n, delay):
  20.     for count in range(n):
  21.         setLight(False)
  22.         time.sleep(delay)
  23.         setLight(True)
  24.         time.sleep(delay)
  25.  
  26. # Switch display mode
  27. #       msg = built-in POST messages (boring)
  28. #       timer = uptime timer display mode (needs work, find ways to reset, etc)
  29. #       text = displays registers 0x05 - 0x0C
  30. def setMode(mode):
  31.     if mode == 'msg':
  32.         bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0x00)
  33.     elif mode == 'timer':
  34.         bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0x01)
  35.     elif mode == 'text':
  36.         bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0x02)
  37.     time.sleep(0.02)
  38.  
  39. # Display 8 ASCII coded characters
  40. #       Currently setting the registers individually, there may be a way to
  41. #       send all 8 at once but I haven't found it
  42. def writeText(data):
  43.     data = data.upper()         # lowercase ASCII characters won't show up
  44.     for msg in range(5, 13):    # iterate the registers 0x05 to 0x0C
  45.         setMode('text')         # update the display mode
  46.         b = msg - 5
  47.  
  48.         if (b+1) > len(data):
  49.             out = 0
  50.         else:
  51.             out = ord(data[b])
  52.  
  53.         try:
  54.             bus.write_i2c_block_data(DEVICE_ADDRESS, msg, [out])
  55.             time.sleep(0.01)
  56.         except KeyboardInterrupt:
  57.             break
  58.         except IOError:
  59.             time.sleep(0.01)
  60.             continue
  61.     setMode('text')
  62.  
  63. # Remove all text from the display
  64. def clearScreen():
  65.     writeText('')
  66.  
  67. # Scroll text right to left, just for fun and makes it possible to display
  68. # messages longer than 8 characters
  69. def scrollText(data):
  70.     clearScreen()
  71.     data = '       ' + data
  72.     s = 0
  73.     while s < len(data):
  74.         writeText(data[s:])
  75.         s = s + 1
  76.     clearScreen()
  77.  
  78. ## =============================================================== Tests
  79.  
  80. if __name__=="__main__":
  81.     setLight(False)
  82.     clearScreen()
  83.     time.sleep(1)
  84.     setLight(True)
  85.     scrollText("Imgur...")
  86.     blinkLight(1, 0.5)
  87.     writeText("Sendnudz")
  88.     time.sleep(0.5)
  89.     blinkLight(4, 0.15)
  90.     time.sleep(2)
  91.     clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment