Jump to content

Python Ambient Light & Linear Poti Code Examples


Recommended Posts

I'm learning Python so I wrote some scripts for the Ambient Light and Linear Poti bricklets.  This may be useful for people who are new to programming.

This gives a basic idea on how to use the bricklets as a switch.

 

Ambient Light:

#!/usr/bin/env python
# -*- coding: utf-8 -*-  
import os

HOST = "localhost"
PORT = 4223
UID = "xxx" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ambient_light import AmbientLight

# Callback function for illuminance callback (parameter has unit Lux/10)
def cb_illuminance(illuminance):

    less = '''Light.\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo\n
    oooooooooooooooooooooooooo'''

    more = '''Dark.\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------\n
    --------------------------'''

    running = True
    while running:
    # Test with print('Illuminance: ' + str(illuminance/10.0) + ' Lux')
        lux = str(illuminance/10.0)
        if str(49) < lux:
            os.system('clear')
            print less
            break
        if str(51) > lux:
            os.system('clear')
            print more
            break

if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    al = AmbientLight(UID) # Create device object
    ipcon.add_device(al) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Set Period for illuminance callback to 1s (1000ms)
    # Note: The illuminance callback is only called every second if the 
    #       illuminance has changed since the last call!
    al.set_illuminance_callback_period(1000)

    # Register illuminance callback to function cb_illuminance
    al.register_callback(al.CALLBACK_ILLUMINANCE, cb_illuminance)

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

 

Linear Poti:

#!/usr/bin/env python
# -*- coding: utf-8 -*-  
import os

HOST = "localhost"
PORT = 4223
UID = "xxx" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_linear_poti import LinearPoti

# Callback function for position callback (parameter has range 0-100)
def cb_position(position):

    less = '''Switch Off.\n
    -------------\n
    -------------\n
    -------------\n
    0000000000000'''

    more = '''Switch On.\n
    0000000000000\n
    -------------\n
    -------------\n
    -------------'''

    running = True
    while running:
    # Test with print('Position: ' + str(position))
        slide = str(position)
        if str(49) < slide:
            os.system('clear')
            print less
            break
        if str(51) > slide:
            os.system('clear')
            print more
            break
        
if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    poti = LinearPoti(UID) # Create device object
    ipcon.add_device(poti) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Set Period for position callback to 0.05s (50ms)
    # Note: The position position is only called every 50ms if the 
    #       position has changed since the last call!
    poti.set_position_callback_period(50)

    # Register position callback to function cb_position
    poti.register_callback(poti.CALLBACK_POSITION, cb_position)

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

Link to comment
Share on other sites

I moved your code to its own page and added it to both categories (code kitchen and python) http://www.tinkerunity.org/wiki/index.php/Ambient_Light_Switch

Its easier to get an overview, if there are more code examples. You can see all Python code examples on the python page automatically http://www.tinkerunity.org/wiki/index.php/Category:Python

 

If you have some more code snippets, just create a new page in the wiki and add the following to the bottom of the wiki page: [[Category:Code-Sammlung]] [[Category:Python]] and your new page will appear on both category pages.

Link to comment
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...