Monday 24 September 2012

twitter feed on LCD

I used the python-twitter module from https://code.google.com/p/python-twitter/ to add the ability to display the latest mention of a twitter account to the LCD, along with manual scrolling of the text to fit into the 16 character wide display. You'll need to log into dev.twitter.com with the account and enable an 'application' to get the oauth keys which you place into the initial twitter.Api() call, replacing the xxx entries with your own. The code fetches the latest mentions every 30 seconds. In between those fetches, it scrolls the text from right to left and repeats when the line has all been shown.

Here's the new main routine adapted from the original code as referenced in the post on LCD a couple of days ago:


def main():
  # Main program block
  api = twitter.Api(consumer_key='xxx',
    consumer_secret='xxx',
    access_token_key='xxx',
    access_token_secret='xxx')

  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers

  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable

  # Initialise display

  lcd_init()

  GPIO.output(LED_ON, True)

  
  while(True):
    nowTime = time.time()
    loopTime = nowTime + 30
    print 'loop'
    results = api.GetMentions()
    msg1 = '               from @' + results[0].user.screen_name + '                '
    msg2 = '               ' + results[0].text + '                 '
    
    msg1Idx = 0
    msg2Idx = 0
    while(time.time() < loopTime):
      view1 = msg1[msg1Idx:msg1Idx + 16]
      if(msg1Idx + 16 > len(msg1)):
        msg1Idx = 0
      view2 = msg2[msg2Idx:msg2Idx + 16]
      if(msg2Idx + 16 > len(msg2)):
        msg2Idx = 0
       
      lcd_byte(LCD_LINE_1, LCD_CMD)
      lcd_string(view1, 1)
      lcd_byte(LCD_LINE_2, LCD_CMD)
      lcd_string(view2, 1)
      time.sleep(0.2) 
      msg1Idx += 1
      msg2Idx += 1

No comments:

Post a Comment