Python Event Driven Serial

1/8/2018by

Import turtle turtle. Setup ( 400, 500 ) # Determine the window size wn = turtle. Screen () # Get a reference to the window wn. Title ( 'Handling keypresses! Battleship 2012 Full Movie Free Download In Tamil. ' ) # Change the window title wn.

Bgcolor ( 'lightgreen' ) # Set the background color tess = turtle. Turtle () # Create our favorite turtle # The next four functions are our 'event handlers'. Def h1 (): tess. Forward ( 30 ) def h2 (): tess.

Look at most relevant Python event driven serial websites out of 15 at KeyOptimize.com. Python event driven serial found at eli.thegreenplace.net, mail.python.org. Has anyone done a script that will rspond to the serial com port(s) receive buffer interrupt. Read timeout, you're closer to being interrupt-driven than you are to what. __objSerialPort.inWaiting() if (intNoChars >0): strReceivedString = self.__objSerialPort.read(intNoChars) #or you could fire an event self.

Python Event Driven Serial

Left ( 45 ) def h3 (): tess. Right ( 45 ) def h4 (): wn. Bye () # Close down the turtle window # These lines 'wire up' keypresses to the handlers we've defined.

Python Event Driven Serial

Onkey ( h1, 'Up' ) wn. Onkey ( h2, 'Left' ) wn. Onkey ( h3, 'Right' ) wn.

Onkey ( h4, 'q' ) # Now we need to tell the window to start listening for events, # If any of the keys that we're monitoring is pressed, its # handler will be called. Listen () wn.

Mainloop () Here are some points to note: • We need the call to the window’s listen method at line 31, otherwise it won’t notice our keypresses. • We named our handler functions h1, h2 and so on, but we can choose better names. The handlers can be arbitrarily complex functions that call other functions, etc. • Pressing the q key on the keyboard calls function h4 (because we bound the q key to h4 on line 26).

While executing h4, the window’s bye method (line 24) closes the turtle window, which causes the window’s mainloop call (line 31) to end its execution. Since we did not write any more statements after line 32, this means that our program has completed everything, so it too will terminate. • We can refer to keys on the keyboard by their character code (as we did in line 26), or by their symbolic names. Some of the symbolic names to try are Cancel (the Break key), BackSpace, Tab, Return(the Enter key), Shift_L (any Shift key), Control_L (any Control key), Alt_L (any Alt key), Pause, Caps_Lock, Escape, Prior (Page Up), Next (Page Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, and Scroll_Lock.

Import turtle turtle. Setup ( 400, 500 ) wn = turtle. Screen () wn. Title ( 'How to handle mouse clicks on the window!' Bgcolor ( 'lightgreen' ) tess = turtle. Turtle () tess.

Color ( 'purple' ) tess. Pensize ( 3 ) tess. Shape ( 'circle' ) def h1 ( x, y ): tess. Goto ( x, y ) wn.

Onclick ( h1 ) # Wire up a click on the window. Mainloop () There is a new turtle method used at line 14 — this allows us to move the turtle to an absolute coordinate position. (Most of the examples that we’ve seen so far move the turtle relative to where it currently is).

So what this program does is move the turtle (and draw a line) to wherever the mouse is clicked. If we add this line before line 14, we’ll learn a useful debugging trick too. Import turtle turtle. Setup ( 400, 500 ) wn = turtle. Screen () wn.

Title ( 'Using a timer' ) wn. Bgcolor ( 'lightgreen' ) tess = turtle. Turtle () tess. Color ( 'purple' ) tess. Pensize ( 3 ) def h1 (): tess.

Forward ( 100 ) tess. Left ( 56 ) wn.

Ontimer ( h1, 2000 ) wn. Mainloop () On line 16 the timer is started and set to explode in 2000 milliseconds (2 seconds). When the event does occur, the handler is called, and tess springs into action.

Unfortunately, when one sets a timer, it only goes off once. So a common idiom, or style, is to restart the timer inside the handler.

In this way the timer will keep on giving new events. Try this program. An example: state machines A state machine is a system that can be in one of a few different states. We draw a state diagram to represent the machine, where each state is drawn as a circle or an ellipse. Certain events occur which cause the system to leave one state and transition into a different state.

These state transitions are usually drawn as an arrow on the diagram. This idea is not new: when first turning on a cellphone, it goes into a state which we could call “Awaiting PIN”.

When the correct PIN is entered, it transitions into a different state — say “Ready”. Then we could lock the phone, and it would enter a “Locked” state, and so on. A simple state machine that we encounter often is a traffic light. Here is a state diagram which shows that the machine continually cycles through three different states, which we’ve numbered 0, 1 and 2. We’re going to build a program that uses a turtle to simulate the traffic lights. There are three lessons here.

The first shows off some different ways to use our turtles. The second demonstrates how we would program a state machine in Python, by using a variable to keep track of the current state, and a number of different if statements to inspect the current state, and take the actions as we change to a different state. The third lesson is to use events from the keyboard to trigger the state changes. Copy and run this program.

Make sure you understand what each line does, consulting the documentation as you need to. Import turtle # Tess becomes a traffic light. Setup ( 400, 500 ) wn = turtle. Screen () wn. Title ( 'Tess becomes a traffic light!'

Bgcolor ( 'lightgreen' ) tess = turtle. Turtle () def draw_housing (): '' Draw a nice housing to hold the traffic lights '' tess. Pensize ( 3 ) tess.

Color ( 'black', 'darkgrey' ) tess. Begin_fill () tess.

Forward ( 80 ) tess. Left ( 90 ) tess. Forward ( 200 ) tess. Circle ( 40, 180 ) tess. Forward ( 200 ) tess.

Left ( 90 ) tess. End_fill () draw_housing () tess. Penup () # Position tess onto the place where the green light should be tess. Forward ( 40 ) tess.

Left ( 90 ) tess. Forward ( 50 ) # Turn tess into a big green circle tess. Shape ( 'circle' ) tess. Shapesize ( 3 ) tess. Fillcolor ( 'green' ) # A traffic light is a kind of state machine with three states, # Green, Orange, Red. We number these states 0, 1, 2 # When the machine changes state, we change tess' position and # her fillcolor.

# This variable holds the current state of the machine state_num = 0 def advance_state_machine (): global state_num if state_num == 0: # Transition from state 0 to state 1 tess. Forward ( 70 ) tess. Fillcolor ( 'orange' ) state_num = 1 elif state_num == 1: # Transition from state 1 to state 2 tess. Forward ( 70 ) tess. Fillcolor ( 'red' ) state_num = 2 else: # Transition from state 2 to state 0 tess.

Back ( 140 ) tess. Fillcolor ( 'green' ) state_num = 0 # Bind the event handler to the space key. Onkey ( advance_state_machine, 'space' ) wn. Listen () # Listen for events wn. Mainloop () The new Python statement is at line 46. The global keyword tells Python not to create a new local variable for state_num (in spite of the fact that the function assigns to this variable at lines 50, 54, and 58). Instead, in this function, state_num always refers to the variable that was created at line 42.

What the code in advance_state_machine does is advance from whatever the current state is, to the next state. On the state change we move tess to her new position, change her color, and, of course, we assign to state_num the number of the new state we’ve just entered. Each time the space bar is pressed, the event handler causes the traffic light machine to move to its new state.

Exercises • Add some new key bindings to the first sample program: • Pressing keys R, G or B should change tess’ color to Red, Green or Blue. • Pressing keys + or - should increase or decrease the width of tess’ pen.

Ensure that the pen size stays between 1 and 20 (inclusive). • Handle some other keys to change some attributes of tess, or attributes of the window, or to give her new behaviour that can be controlled from the keyboard. • Change the traffic light program so that changes occur automatically, driven by a timer.

• In an earlier chapter we saw two turtle methods, hideturtle and showturtle that can hide or show a turtle. This suggests that we could take a different approach to the traffic lights program.

Add to your program above as follows: draw a second housing for another set of traffic lights. Create three separate turtles to represent each of the green, orange and red lights, and position them appropriately within your new housing. As your state changes occur, just make one of the three turtles visible at any time. Once you’ve made the changes, sit back and ponder some deep thoughts: you’ve now got two different ways to use turtles to simulate the traffic lights, and both seem to work.

Is one approach somehow preferable to the other? Which one more closely resembles reality — i.e. The traffic lights in your town? • Now that you’ve got a traffic light program with different turtles for each light, perhaps the visibility / invisibility trick wasn’t such a great idea.

If we watch traffic lights, they turn on and off — but when they’re off they are still there, perhaps just a darker color. Modify the program now so that the lights don’t disappear: they are either on, or off.

But when they’re off, they’re still visible. • Your traffic light controller program has been patented, and you’re about to become seriously rich. But your new client needs a change. They want four states in their state machine: Green, then Green and Orange together, then Orange only, and then Red. Additionally, they want different times spent in each state. The machine should spend 3 seconds in the Green state, followed by one second in the Green+Orange state, then one second in the Orange state, and then 2 seconds in the Red state. Change the logic in the state machine.

• If you don’t know how tennis scoring works, ask a friend or consult Wikipedia. A single game in tennis between player A and player B always has a score. We want to think about the “state of the score” as a state machine. The game starts in state (0, 0), meaning neither player has any score yet. We’ll assume the first element in this pair is the score for player A. If player A wins the first point, the score becomes (15, 0).

If B wins the first point, the state becomes (0, 15). Below are the first few states and transitions for a state diagram. In this diagram, each state has two possible outcomes (A wins the next point, or B does), and the uppermost arrow is always the transition that happens when A wins the point. Complete the diagram, showing all transitions and all states. (Hint: there are twenty states, if you include the duece state, the advantage states, and the “A wins” and “B wins” states in your diagram.).

Comments are closed.