×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Super Contributor
    Join Date
    Feb 2008
    Location
    Belo Horizonte, Brazil
    Posts
    744

    What's a event based code?

    I was reading a PDF about Python and it talked about event based code. It explains what it is but I couldn't understand.

    It says:

    Code:
    Event Loop 
    ● e32.Ao_lock waits for events, but stops execution 
    ● Game: 
    App. has to be active all the time 
    But still needs respond to events (keys, ...)
    
    
    Initialize event call-backs 
    while <end condition>: 
    Update game state 
    Redraw screen 
    Pause / yield 
    
    
    Sleep(+ execute waiting active objects):  
    e32.ao_sleep(interval) 
    Yield(just execute ready active objects with 
    higher priority)
     
    e32.ao_yield()

    Could someone give me a clear explanation about this? I was also talking to JOM via email and he told me event based applications could be better, faster and more improved on system response. Unfortunaly, he is now travelling and I couldn't ask him about this. Can somebody explain me? What's the difference between a event based and a non-event bases application?

    Sorry for asking too much

  2. #2
    Regular Contributor
    Join Date
    Mar 2007
    Location
    Brazil
    Posts
    103

    Re: What's a event based code?

    Basically the flow of execution of your application is defined by assyncronous events. For e.g a key press.

    read this:

    http://en.wikipedia.org/wiki/Event-driven_programming

    i´m falling asleep now, when i wake up i´ll try to make a better explanation.
    Wiki Contributor of the Month (May).
    Winner of the May Contest on Wiki.

  3. #3
    Super Contributor
    Join Date
    Feb 2008
    Location
    Belo Horizonte, Brazil
    Posts
    744

    Re: What's a event based code?

    Thanks, cabezonxdg!

    I'm starting to understand!

    But still waiting your detailed explanation!

    (or anybody else)

    Thanks for the reply and link!

  4. #4
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    Re: What's a event based code?

    Ok, I'll try to explain my understanding of event-based code on a simple example:
    Lets assume we make an application that prints "Message handled!" when we receive a sms.

    The event based solution:

    Code:
    import inbox, appuifw, e32
    
    def message_received(msg_id):
            box = inbox.Inbox()
            sms_text = box.content(msg_id) 
            appuifw.note(u"sms content: " + sms_text , "info")
            app_lock.signal()
    
    box = inbox.Inbox()
    box.bind(message_received)
    
    print "Waiting for new SMS messages.."
    app_lock = e32.Ao_lock()
    app_lock.wait()
    print "Message handled!"
    The relevant part of the code above is "box.bind(message_received)"
    Here we are using a predefined callback (predefined by the makers of python). As soon as a SMS was received Python is throwing an event. We are just reacting on this event by calling the callback function.
    So basically we just need to care about what we want to do when a predefined thing (event) is happening.

    The non event-based solution:
    We build an infinite while loop. In this while loop we check the latest SMS-Count in the sms inbox (each incomming SMS is increasing the SMS-Count).
    If we see that the SMS-Count has increased, we know that we have received a new sms and can print "Message handled!".
    So basically we are actively checking if a new sms has arrived over and over again, as long as our application is running.

    The disadvantage of this solution is the need for a power consuming while loop.

    Conclusion
    If you can make your application event based, you should do it.
    But in real life, for many applications you can not allways work with predefined events. Especially when you need to controll something with your application. For most cases there is no predefined event implemented in python that you could use.

    hope it was usefull
    novis

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    49

    Re: What's a event based code?

    Quote Originally Posted by Rafael T. View Post
    I was reading a PDF about Python and it talked about event based code. It explains what it is but I couldn't understand.

    It says:

    Code:
    Event Loop 
    ● e32.Ao_lock waits for events, but stops execution 
    ● Game: 
    App. has to be active all the time 
    But still needs respond to events (keys, ...)
    
    
    Initialize event call-backs 
    while <end condition>: 
    Update game state 
    Redraw screen 
    Pause / yield 
    
    
    Sleep(+ execute waiting active objects):  
    e32.ao_sleep(interval) 
    Yield(just execute ready active objects with 
    higher priority)
     
    e32.ao_yield()

    Could someone give me a clear explanation about this? I was also talking to JOM via email and he told me event based applications could be better, faster and more improved on system response. Unfortunaly, he is now travelling and I couldn't ask him about this. Can somebody explain me? What's the difference between a event based and a non-event bases application?

    Sorry for asking too much
    Hello Rafael T.!

    Look at the link over here.
    Getting Started with Game Programming

    The above example is based on the event based code.

    In the above code/example which i mentioned, i have used these two lines;
    Code:
    app_lock=e32.Ao_lock()
    app_lock.wait()
    These two lines makes the application listen for events. we can also call it the main method of the game or application. if you test this example and remove these two lines, the application will appear and then disappear.

    so the point is these two lines makes the application run forever now how to stop it.

    then when we want to stop it, we send it a signal, you can see it in my code in quit() method;

    Code:
    app_lock.signal()
    this was all about event based code

    now if you wants to see a non event based code, here it is;
    Note: i have removed the above mentioned lines.
    Code:
    # __author__='Fayyaz Ali'
    # version = '1.0 beta'
    # filename='GameSample.py'
    # license='GNU GPL'
     
    # import the required modules
    import e32, appuifw, key_codes, graphics
     
    # define the exit function
    def quit():running=0
    appuifw.app.exit_key_handler=quit
     
    # make the screen large
    appuifw.app.screen='large'
     
    global x,y, running
    x = 110
    y = 160
    
    running = 1 
    # this method is for handling the redraw event of canvas
    def handle_redraw(rect):
         canvas.blit(img)
     
    # this method is called when the ok button is pressed
    def press_select():
        appuifw.note(u'hit','info')
     
    # this method is called when the right button is pressed
    def press_right():
        global x
        x=x+5
        update()
     
    # this method is called when the left button is pressed
    def press_left():
        global x
        x=x-5
        update()
     
    # this method is called when the up button is pressed
    def press_up():
        global y
        y=y-5
        update()
     
    # this method is called when the down button is pressed
    def press_down():
        global y
        y=y+5
        update()
     
    def update():
        # clear img
        img.clear()
        # open an image
        img1=graphics.Image.open("e:\\Images\\area.png")
        # resize the image
        img2 = img1.resize((20,20))
        # copy the img1 to img
        img.blit(img2, (0,0), (x, y))
        handle_redraw(None)
     
    # create an empty image according to the resolution of the phone
    img=graphics.Image.new((240,320))
     
    # create an object of the Canvas
    canvas=appuifw.Canvas(redraw_callback=handle_redraw)
     
    # bind the canvas with keys, so that it can listen to 
    # key presses of those keys and do some actions on each key
     
    # we are passing two parameters to the bind method of canvas class. 
    # The first is the key code to which we want our application to 
    # listen to events. And the second is handler for the event.
    canvas.bind(key_codes.EKeySelect, press_select)
    canvas.bind(key_codes.EKeyDownArrow, press_down)
    canvas.bind(key_codes.EKeyUpArrow, press_up)
    canvas.bind(key_codes.EKeyRightArrow, press_right)
    canvas.bind(key_codes.EKeyLeftArrow, press_left)
     
    # set the application body to canvas
    appuifw.app.body=canvas
    
    while running:
         update()
     
    #app_lock=e32.Ao_lock()
    #app_lock.wait()
    i have just added a new global variable "running", you can see it here;
    Code:
    global x,y, running
    changed the quit method to;
    Code:
    def quit():running=0
    and changed the last three lines to;
    Code:
    while running:
         update()
     
    #app_lock=e32.Ao_lock()
    #app_lock.wait()

  6. #6
    Super Contributor
    Join Date
    Feb 2008
    Location
    Belo Horizonte, Brazil
    Posts
    744

    Re: What's a event based code?

    I undertood it completely now!!!

    This DiBo just rocks!


    Thanks for the help guys!

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    Re: What's a event based code?

    Hello,
    I am new to Symbian.
    I have the following questions.
    1. How to send iser-defined events/(and its associated event payload) from one app to itself or another app?
    2. Is there two kind of event delivery? Synchronous and Asynchronous event dispatch mechanism? If so, how to achieve both?
    3. What is the difference between EVENT and MESSAGE in symbian?

    Can someone please help me wiht these questions?

    Thanks

Similar Threads

  1. event based framework need ideas for impl.
    By acode in forum Symbian
    Replies: 3
    Last Post: 2008-02-01, 14:47
  2. How to get event from Outbox folder in our code..
    By sumit_rusia in forum Symbian Networking & Messaging (Closed)
    Replies: 20
    Last Post: 2006-11-22, 08:22
  3. Code Required:add event or delete event from log
    By SingerSinger in forum Mobile Java General
    Replies: 1
    Last Post: 2005-12-10, 21:12
  4. Nokia: what is the license on example code???
    By sbwoodside in forum Symbian
    Replies: 1
    Last Post: 2004-03-18, 05:13

Posting Permissions

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