예제 #1
0
class dist_us:
    def __init__(self):
        self.dus = None

        # Create IP Connection
        self.ipcon = IPConnection() 

        # Register IP Connection callbacks
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, 
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, 
                                     self.cb_connected)

        # Connect to brickd, will trigger cb_connected
        self.ipcon.connect(constants.ownIP, PORT) 
        #self.ipcon.enumerate()                 
       
    
    def cb_distance(self, distance):        
        dicti = {}
        dicti['value'] = str(distance)
        dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str(self.dus.get_identity()[5])
        mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) 
        mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) 
       
    
    # Callback handles device connections and configures possibly lost 
    # configuration of lcd and temperature callbacks, backlight etc.
    def cb_enumerate(self, uid, connected_uid, position, hardware_version, 
                     firmware_version, device_identifier, enumeration_type):

        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            
            # Enumeration for Distance US
            if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER:
                self.dus = BrickletDistanceUS(uid, self.ipcon)
                self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance)
                self.dus.set_distance_callback_period(10000)

        
    def cb_connected(self, connected_reason):
        # Enumerate devices again. If we reconnected, the Bricks/Bricklets
        # may have been offline and the configuration may be lost.
        # In this case we don't care for the reason of the connection
        self.ipcon.enumerate()          
예제 #2
0
class dist_us:
    def __init__(self):
        self.dus = None

        # Create IP Connection
        self.ipcon = IPConnection()

        # Register IP Connection callbacks
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.cb_connected)

        # Connect to brickd, will trigger cb_connected
        self.ipcon.connect(constants.ownIP, PORT)
        #self.ipcon.enumerate()

    def cb_distance(self, distance):
        dicti = {}
        dicti['value'] = str(distance)
        dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str(
            self.dus.get_identity()[5])
        mySocket.sendto(str(dicti), (constants.server1, constants.broadPort))
        mySocket.sendto(str(dicti), (constants.server1, constants.broadPort))

    # Callback handles device connections and configures possibly lost
    # configuration of lcd and temperature callbacks, backlight etc.
    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):

        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:

            # Enumeration for Distance US
            if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER:
                self.dus = BrickletDistanceUS(uid, self.ipcon)
                self.dus.register_callback(self.dus.CALLBACK_DISTANCE,
                                           self.cb_distance)
                self.dus.set_distance_callback_period(10000)

    def cb_connected(self, connected_reason):
        # Enumerate devices again. If we reconnected, the Bricks/Bricklets
        # may have been offline and the configuration may be lost.
        # In this case we don't care for the reason of the connection
        self.ipcon.enumerate()
예제 #3
0
HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Distance US Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_distance_us import BrickletDistanceUS


# Callback function for distance value callback
def cb_distance(distance):
    print("Distance Value: " + str(distance))


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    dus = BrickletDistanceUS(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Register distance value callback to function cb_distance
    dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance)

    # Set period for distance value callback to 0.2s (200ms)
    # Note: The distance value callback is only called every 0.2 seconds
    #       if the distance value has changed since the last call!
    dus.set_distance_callback_period(200)

    input("Press key to exit\n")  # Use raw_input() in Python 2
    ipcon.disconnect()
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Distance US Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_distance_us import BrickletDistanceUS


# Callback function for distance value reached callback
def cb_distance_reached(distance):
    print("Distance Value: " + str(distance))


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    dus = BrickletDistanceUS(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Get threshold callbacks with a debounce time of 10 seconds (10000ms)
    dus.set_debounce_period(10000)

    # Register distance value reached callback to function cb_distance_reached
    dus.register_callback(dus.CALLBACK_DISTANCE_REACHED, cb_distance_reached)

    # Configure threshold for distance value "smaller than 200"
    dus.set_distance_callback_threshold("<", 200, 0)

    input("Press key to exit\n")  # Use raw_input() in Python 2
    ipcon.disconnect()
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_distance_us import BrickletDistanceUS

# Callback function for distance value callback
def cb_distance(distance):
    print("Distance Value: " + str(distance))

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    dus = BrickletDistanceUS(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Register distance value callback to function cb_distance
    dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance)

    # Set period for distance value callback to 0.2s (200ms)
    # Note: The distance value callback is only called every 0.2 seconds
    #       if the distance value has changed since the last call!
    dus.set_distance_callback_period(200)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()
HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_distance_us import BrickletDistanceUS

# Callback function for distance value reached callback
def cb_distance_reached(distance):
    print("Distance Value: " + str(distance))

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    dus = BrickletDistanceUS(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Get threshold callbacks with a debounce time of 10 seconds (10000ms)
    dus.set_debounce_period(10000)

    # Register distance value reached callback to function cb_distance_reached
    dus.register_callback(dus.CALLBACK_DISTANCE_REACHED, cb_distance_reached)

    # Configure threshold for distance value "smaller than 200"
    dus.set_distance_callback_threshold("<", 200, 0)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()