Active Topics

 


Reply
Thread Tools
blubbi's Avatar
Posts: 288 | Thanked: 113 times | Joined on Dec 2009 @ Germany
#21
Mmmh, all I came up without having a clue if there is a special mechanism is the following:

Code:
f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)
This, wrapped in a own thread, could to the trick, but does anyone have a smarter, less CPU-hogging and faster way to get the latest value?

Cheers Bjoern
 
thp's Avatar
Posts: 1,391 | Thanked: 4,272 times | Joined on Sep 2007 @ Vienna, Austria
#22
Originally Posted by blubbi View Post
Mmmh, all I came up without having a clue if there is a special mechanism is the following:

Code:
f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)
This, wrapped in a own thread, could to the trick, but does anyone have a smarter, less CPU-hogging and faster way to get the latest value?
You can keep the file open and get notified in your main loop when its contents change, as in this script:

http://talk.maemo.org/showthread.php?t=54695

The interesting bits here are the call to "gobject.io_add_watch" and the "on_state_changed" callback. You don't even need to have a separate thread for it, simply start a GLib main loop (e.g. call gtk.main()) and the mainloop will call your callback when the file has changed (just waiting while it's not changing).
 

The Following 5 Users Say Thank You to thp For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 04:18.