You Are Here:

Community: Developer Discussion Boards

#1 Old refresh Image on orientation event - 2008-12-13, 00:23

Join Date: Dec 2008
Posts: 11
Location: Germany
Slowmoe
Offline
Registered User
Hi,

I've got a problem with refreshing Images!

The following App. refreshes the Screen with specific Images (as indication for a pressed button)and sends a String to a PC over Bluetooth at the same time.

Code:
import appuifw, e32, key_codes, socket
from graphics import *
def quit():
    app_lock.signal()

def choose_service(services):
    names = []
    channels = []
    for name, channel in services.items():
        names.append(name)
        channels.append(channel)
    index = appuifw.popup_menu(names, u"Choose service")
    return channels[index]

def up():
    print "Up"
    print >> to_peer, "Up"

def down():
    print "Down"
    print >> to_peer, "Down"

def left():
    print "Left"
    print >> to_peer, "Left"

def right():
    print "Right"
    print >> to_peer, "Right"

def initialize():
    global address, services, channel, conn, to_peer
    address, services = socket.bt_discover()
    channel = choose_service(services)
    conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
    conn.connect((address, channel))
    to_peer = conn.makefile("rw", 0)

def handle_redraw(rect):
   	canvas.blit(bgimage) #"draws" the image on the background canvas

def handle_event(event):
    global rectO, rectU
    #if event['type'] == appuifw.EEventKeyUp:
     #   bgimage.load("E:\\Images\\main.jpg")
      #  handle_redraw(())
    if event['keycode'] == key_codes.EKey2:
        bgimage.load("E:\\Images\\left.jpg")
        handle_redraw(())
    if event['keycode'] == key_codes.EKey8:
        bgimage.load("E:\\Images\\right.jpg")
        handle_redraw(())
    if event['keycode'] == key_codes.EKey4:
        bgimage.load("E:\\Images\\down.jpg")
        handle_redraw(())
    if event['keycode'] == key_codes.EKey6:
        bgimage.load("E:\\Images\\up.jpg")
        handle_redraw(())
def quit():
    app_lock.signal()

appuifw.app.screen='full' #set the screen size (area of screen to be used)
bgimage=Image.open("E:\\Images\\a.jpg") #Image path should be

canvas=appuifw.Canvas(event_callback=handle_event, redraw_callback=handle_redraw)
appuifw.app.body=canvas
e32.ao_sleep(3)
initialize()

bgimage.load("E:\\Images\\b.jpg")
handle_redraw(())

canvas.bind(key_codes.EKey2, up)
canvas.bind(key_codes.EKey4, left)
canvas.bind(key_codes.EKey6, right)
canvas.bind(key_codes.EKey8, down)

appuifw.app.exit_key_handler = quit
app_lock = e32.Ao_lock()
app_lock.wait()
This works fine, because the canvas can use it's event_callback for refreshing the images.

Now I'd like to do the same with orientation events from the sensor class instead. This doesn't work and I don't understand the result! The order of the outgoing strings and the drawn images isn't correct!

Could anybody give an advise how it should work? Or is there a chance to use the event_callback with orientations?

Btw. here is the code:

Code:
import appuifw, e32, key_codes, socket, sensor
from sensor import orientation
from graphics import *

orientations = {
    orientation.TOP:    'Right',
    orientation.BOTTOM: 'Left',
    orientation.LEFT:   'Up',
    orientation.RIGHT:  'Down',
    orientation.FRONT:  'front',
    orientation.BACK:   'Back' }

def choose_service(services):
    names = []
    channels = []
    for name, channel in services.items():
        names.append(name)
        channels.append(channel)
    index = appuifw.popup_menu(names, u"Choose service")
    return channels[index]

def quit():
    orientation_sensor.disconnect()
    app_lock.signal()

def initialize():
    global address, services, channel, conn, to_peer
    address, services = socket.bt_discover()
    channel = choose_service(services)
    conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
    conn.connect((address, channel))
    to_peer = conn.makefile("rw", 0)

def handle_redraw(rect):
    	canvas.blit(bgimage)


def print_orientation(orientation_value):
    print >> to_peer, orientations[orientation_value]
    if orientations[orientation_value] == 'Right':
       bgimage.load("E:\\Images\\right.jpg")
       handle_redraw(())
    elif orientations[orientation_value] == 'Left':
        bgimage.load("E:\\Images\\left.jpg")
        handle_redraw(())
    elif orientations[orientation_value] == 'Up':
        bgimage.load("E:\\Images\\up.jpg")
        handle_redraw(())
    elif orientations[orientation_value] == 'Down':
        bgimage.load("E:\\Images\\down.jpg")
        handle_redraw(())
    else:
        bgimage.load("E:\\Images\\main.jpg")
        handle_redraw(())


appuifw.app.screen='full'
bgimage=Image.open("E:\\Images\\a.jpg")

canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)
appuifw.app.body=canvas
e32.ao_sleep(3)
initialize()

bgimage.load("E:\\Images\\b.jpg")
handle_redraw(())

sensors = sensor.sensors()
acc_sensor_data = sensors['AccSensor']
orientation_sensor = sensor.Sensor(acc_sensor_data['id'], acc_sensor_data['category'])
orientation_sensor.set_event_filter(sensor.OrientationEventFilter())
orientation_sensor.connect(print_orientation)

appuifw.app.exit_key_handler = quit
app_lock = e32.Ao_lock()
app_lock.wait()
THX 4 HELP!
Reply With Quote

#2 Old Re: refresh Image on orientation event - 2008-12-13, 01:10

Join Date: Feb 2008
Posts: 743
Location: Belo Horizonte, Brazil
Send a message via Skype™ to Rafael T.
Rafael T.'s Avatar
Rafael T.
Offline
Forum Nokia Champion
Hi Slowmoe,

Have you tried using resize_callback?

From the Python Library Reference:

Quote:
resize_callback is called when screen size is changed when the Canvas rect size has been changed. The callback takes as its argument a two-element tuple that contains the new clientRect width and height.
BR,

Rafael.
Reply With Quote

#3 Old Re: refresh Image on orientation event - 2008-12-13, 23:02

Join Date: Dec 2008
Posts: 11
Location: Germany
Slowmoe
Offline
Registered User
Hi Rafael!

I didn't try this callback until now, but to tell you the truth...I have no idea how this event could help me to initialize a refresh of the screen directed on a change in the accelerometer orientation!

Or have I misunderstood this event?!?
Reply With Quote

#4 Old Re: refresh Image on orientation event - 2008-12-14, 00:10

Join Date: Feb 2008
Posts: 743
Location: Belo Horizonte, Brazil
Send a message via Skype™ to Rafael T.
Rafael T.'s Avatar
Rafael T.
Offline
Forum Nokia Champion
Quote:
Originally Posted by Slowmoe View Post
Hi Rafael!

I didn't try this callback until now, but to tell you the truth...I have no idea how this event could help me to initialize a refresh of the screen directed on a change in the accelerometer orientation!

Or have I misunderstood this event?!?
I was reading your code and I haven't tested it, but I think you're not using the orientations correctly. Try to use something like this:

Code:
def print_orientation(orientation_value):

    if orientation_value == orientation.LEFT:
       bgimage.load("E:\\Images\\right.jpg")
       handle_redraw(())
    elif orientation_value == orientation.RIGHT:
        bgimage.load("E:\\Images\\left.jpg")
        handle_redraw(())
    elif orientation_value == orientation.TOP:
        bgimage.load("E:\\Images\\up.jpg")
        handle_redraw(())
    elif orientation_value == orientation.BOTTOM:
        bgimage.load("E:\\Images\\down.jpg")
        handle_redraw(())
    else:
        bgimage.load("E:\\Images\\main.jpg")
        handle_redraw(())

Hope it helps,

Rafael.
Reply With Quote

#5 Old Re: refresh Image on orientation event - 2008-12-14, 13:56

Join Date: Dec 2008
Posts: 11
Location: Germany
Slowmoe
Offline
Registered User
Hi,

thx 4 your answer!

This will not work, because orientation_value is an integer delievered from the sensor (every orientation has a special identifier/ integer...for example: "5" for back). It defines the position in the array "orientation".

This part of the code works fine (without GUI).

I have solved the problem now with seperat redraw methods and image objects!
Reply With Quote

#6 Old Re: refresh Image on orientation event - 2008-12-14, 14:48

Join Date: Feb 2008
Posts: 743
Location: Belo Horizonte, Brazil
Send a message via Skype™ to Rafael T.
Rafael T.'s Avatar
Rafael T.
Offline
Forum Nokia Champion
Quote:
Originally Posted by Slowmoe View Post
Hi,

thx 4 your answer!

This will not work, because orientation_value is an integer delievered from the sensor (every orientation has a special identifier/ integer...for example: "5" for back). It defines the position in the array "orientation".

This part of the code works fine (without GUI).

I have solved the problem now with seperat redraw methods and image objects!
Ok, glad you solved it



Good luck,

Rafael.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Is MIDP2.0 over Symbian OS slow? epolitakis Mobile Java Games 7 2007-03-16 10:32
Drawing Lines on an Image (image graphics object returns null) nicenouman Mobile Java General 4 2006-09-21 11:15
saving jpeg image on grid list flicker82 General Symbian C++ 0 2005-01-21 05:22
Image Uploader / Image Downloader / Image Server kehong General Symbian C++ 0 2003-05-12 18:38
Loading Image data from 'raw' bytes LongSteve Mobile Java General 2 2002-11-20 18:38

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia