Jump to content

Timeout on LCD20X4.is_backlight_on


thieled

Recommended Posts

A very new python programmer here who has built the Tinkerforge weather station connected to a Raspberry Pi.  I am starting to tinker with the Tinkerforge code and want to implement the is_backlight_on function for the LCD.  I more often than not get a timeout error when I call this function; sometimes it works but mostly not.

 

Any help would be appreciated for a newbie!

Link zu diesem Kommentar
Share on other sites

Here's the code ...

 

#!/usr/bin/env python

# -*- coding: utf -8 -*-

 

HOST="localhost"

PORT=4223

UID="ogH"

 

 

 

import time;

from tinkerforge.ip_connection import IPConnection

from tinkerforge.bricklet_lcd_20x4 import LCD20x4

 

# Callback functions for button status

def cb_pressed(i):

    print('Pressed: '+ str(i))

    BacklightON = lcd.is_backlight_on()

    print(BacklightON)

 

if __name__ == "__main__":

    ipcon=IPConnection()    # Create IP connection

    lcd=LCD20x4(UID,ipcon)  # Create device object

 

    ipcon.connect(HOST,PORT)    # Connect to brickd

    # Don't use device before ipcon is connected

 

    # Register button status callback to cb_pressed

    lcd.register_callback(lcd.CALLBACK_BUTTON_PRESSED,cb_pressed)

 

    # lcd.backlight_off()

 

    BacklightON = lcd.is_backlight_on()

    print(BacklightON)

 

    while True:

        n=0

                    # Line 0

        Line="Button 0: MAIN MENU"

           

        # Determine start position to be centred

        # Position=(20-len(Line))/2

        Position=0

           

        # Write Line 0

        lcd.write_line(0,Position,Line)

 

        # Line 1

        Line="Button 1: DATE/TIME"

           

        # Determine start position to be centred

        # Position=(20-len(Line))/2

           

        # Write Line 1

        lcd.write_line(1,Position,Line)

 

        # Line 2

        Line="Button 2: WEATHER"

           

        # Determine start position to be centred

        # Position=(20-len(Line))/2

           

        # Write Line 2

        lcd.write_line(2,Position,Line)

 

and here's the error message ...

 

>>>

False

Pressed: 0

Exception in thread Callback-Processor:

Traceback (most recent call last):

  File "/home/pi/weather_station/tinkerforge/ip_connection.py", line 938, in send_request

    response = device.response_queue.get(True, self.timeout)

  File "/usr/lib/python3.2/queue.py", line 193, in get

    raise Empty

queue.Empty

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner

    self.run()

  File "/usr/lib/python3.2/threading.py", line 693, in run

    self._target(*self._args, **self._kwargs)

  File "/home/pi/weather_station/tinkerforge/ip_connection.py", line 812, in callback_loop

    self.dispatch_packet(data)

  File "/home/pi/weather_station/tinkerforge/ip_connection.py", line 793, in dispatch_packet

    cb(self.deserialize_data(payload, form))

  File "/home/pi/weather_station/test_lcd.py", line 17, in cb_pressed

    BacklightON = lcd.is_backlight_on()

  File "/home/pi/weather_station/tinkerforge/bricklet_lcd_20x4.py", line 123, in is_backlight_on

    return self.ipcon.send_request(self, BrickletLCD20x4.FUNCTION_IS_BACKLIGHT_ON, (), '', '?')

  File "/home/pi/weather_station/tinkerforge/ip_connection.py", line 947, in send_request

    raise Error(Error.TIMEOUT, msg)

tinkerforge.ip_connection.Error: -1: Did not receive response for function 5 in time

 

Many thanks borg.

Link zu diesem Kommentar
Share on other sites

Your problem is that the code in your while loop runs as fast as it can. This will fill up all of the buffers and it is not possible for the callbacks to come through anymore.

 

Try to add a small sleep at the end of the while loop, like this:

 

    while True:
        n=0
        # Line 0
        Line="Button 0: MAIN MENU"
            
        # Determine start position to be centred
        # Position=(20-len(Line))/2
        Position=0
            
        # Write Line 0
        lcd.write_line(0,Position,Line)

        # Line 1
        Line="Button 1: DATE/TIME"
            
        # Determine start position to be centred
        # Position=(20-len(Line))/2
            
        # Write Line 1
        lcd.write_line(1,Position,Line)

        # Line 2
        Line="Button 2: WEATHER"
            
        # Determine start position to be centred
        # Position=(20-len(Line))/2
            
        # Write Line 2
        lcd.write_line(2,Position,Line)


        time.sleep(0.1)

Link zu diesem Kommentar
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...