Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Software Design Week V Advanced Python 7- segment Display.

Similar presentations


Presentation on theme: "Embedded Software Design Week V Advanced Python 7- segment Display."— Presentation transcript:

1 Embedded Software Design Week V Advanced Python 7- segment Display

2 Formatting Formatting Numbers >>> x = 1.2345678 >>> "x={:.2f}".format(x) 'x=1.23' >>> Formatting Dates >>> from datetime import datetime >>> d = datetime.now() >>> "{:%Y-%m-%d %H:%M:%S}".format(d) '2013-05-02 16:00:45' >>>

3 Returning More Than One Value >>> def calculate_temperatures(kelvin): celsius = kelvin - 273 fahrenheit = celsius * 9 / 5 + 32 return celsius, fahrenheit >>> c, f = calculate_temperatures(340) >>> >>> print(c) 67 >>> print(f) 152.6 Better way?

4 Defining a class class Person: """This class represents a person object""" def __init__(self, name, tel): self.name = name self.tel = tel import Person p = Person.Person("Ali", "123") print(p.name)

5 Defining a Method class Person: """This class represents a person object""" def __init__(self, name, surname, tel): self.name = name self.surname = surname self.tel = tel def full_name(self): return self.name + " " + self.surname import Person p = Person.Person("Ali", ”Eren", "123") print(p.name)

6 Inheritance class Employee(Person): def __init__(self, first_name, surname, tel, salary): super().__init__(first_name, surname, tel) self.salary = salary def give_raise(self, amount): self.salary = self.salary + amount

7 Writing to a File >>> f = open('test.txt', 'w') >>> f.write('This file is not empty') >>> f.close() Read from a File >>> f = open('test.txt') >>> d= f.read() >>> f.close()

8 Pickling >>> import pickle >>> mylist = ['some text', 123, [4, 5, True]] >>> f = open('mylist.pickle', 'wb') >>> pickle.dump(mylist, f) >>> f.close() >>> f = open('mylist.pickle') >>> other_array = pickle.load(f) >>> f.close() >>> other_array ['some text', 123, [4, 5, True]]

9 Handling Errors try: f = open('test.txt') s = f.read() f.close() except IOError: print("Cannot open the file") >>> list = [1, 2, 3] >>> list[4] Traceback (most recent call last): File " ", line 1, in list[4] IndexError: list index out of range

10 Using Modules Module => Library import random from random import * from random import randint print(randint(1,6)) import random as R R.randint(1, 6) Random Numbers Random, really? random.choice(['a', 'b', 'c'])

11 Making Web Requests from Python import urllib.request with urllib.request.urlopen('http://www.google.com/') as response: html = response.read()

12 Command-Line Arguments in Python import sys for (i, value) in enumerate(sys.argv): print("arg: %d %s " % (i, value)) $ python cmd_line.py a b c arg: 0 cmd_line.py arg: 1 a arg: 2 b arg: 3 c

13 Sending e-mail from Python See e-mail.py at web site

14 Writing a Simple Web Server in Python First install bottle $ wget http://bottlepy.org/bottle.pyhttp://bottlepy.org/bottle.py OR $ sudo pip install bottle # recommended $ sudo easy_install bottle # alternative without pip $ sudo apt-get install python-bottle # works for debian, ubuntu,... See web_server.py at the web site Finally, $ sudo python web_server.py

15 7 - Segment Display http://www.high-altitude-balloon.com/Red-LED-Display-Digital-Tube-7-Segment-10-Pin-1-Bit

16 7-Segment Wiring Seg / Digit7 Seg BitResistorGPIO # Bottom Left1Yes7 Bottom Center2Yes8 3NoGRND Bottom Right4Yes23 Decimal Point5Yes25 Top Right6Yes4 Top Center7Yes11 8NoGRND Top Left9Yes10 Middle Center10Yes18

17 7 - Segment Python Code Part-1 # code modified, tweaked and tailored from code by bertwert # on RPi forum thread topic 91796 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.cleanup() # GPIO ports for the 7seg pins segments = (11, 4, 23, 8, 7, 10, 18, 25) # 7seg_segment_pins (11,7,4,2,1,10,5,3) + 100R inline for segment in segments: GPIO.setup(segment, GPIO.OUT) GPIO.output(segment, 0)

18 7 - Segment Python Code Part-2 num = { ' ':(0,0,0,0,0,0,0), '0':(1,1,1,1,1,1,0), '1':(0,1,1,0,0,0,0), '2':(1,1,0,1,1,0,1), '3':(1,1,1,1,0,0,1), '4':(0,1,1,0,0,1,1), '5':(1,0,1,1,0,1,1), '6':(1,0,1,1,1,1,1), '7':(1,1,1,0,0,0,0), '8':(1,1,1,1,1,1,1), '9':(1,1,1,1,0,1,1)} try: while True: for loop in range(0,10): for led in range(0,7): pin = segments[led] pin_val = num[str(loop)][led] GPIO.output(pin, pin_val) time.sleep(1) finally: GPIO.cleanup()


Download ppt "Embedded Software Design Week V Advanced Python 7- segment Display."

Similar presentations


Ads by Google