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

Slides:



Advertisements
Similar presentations
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
Advertisements

LED Display. LED Typical LED forward bias voltage: 1.5 to 2.0 V Typical currents needed to light LED range from 2 to 15 mA.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
V Avon High School Tech Club Agenda Old Business –Executive Committee –LCCUG meeting volunteer(s) –Reward Points Program New Business –Weekly.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
COEN 445 Communication Networks and Protocols Lab 4
Domain Name System (DNS) is a distributed database that's typically used to resolve hostnames into IP addresses. There are several different DNS modules.
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Embedded Programming and Robotics Lesson 19 Raspberry Pi Programming in C 1.
Fundamentals of Python: From First Programs Through Data Structures
Embedded Programming and Robotics
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
 Name Space ◦ modname.funcname ◦ Main 의 module name: ‘__main__’ if __name__ == ‘__main__’:  Scopes.
Functions Reading/writing files Catching exceptions
By Ryan Smith The Standard Library In Python. Python’s “Batteries Included” Philosophy Python’s standard library was designed to be able to handle as.
Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
PC204 Lecture 9 Conrad Huang Genentech Hall, N453A x
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Introduction to Information Security Python. Python motivation Python is to a Hacker what Matlab is to an engineer Lots of built-in modules Lots of 3.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Welcome to Week 4 at the Summer Computer Club Raspberry Pi (contd)
 Name Space ◦ modname.funcname ◦ Main 의 module name: ‘__main__’ if __name__ == ‘__main__’:  Scopes.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lecture 4 Python Basics Part 3.
Chapter 16 Web Pages And CGI Scripts Department of Biomedical Informatics University of Pittsburgh School of Medicine
Python’s Standard Library Part 2 Francis Ryan. Output formatting, 1 repr module – When using large or deeply nested containers, allows better printing.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Python Documentation Fran Fitzpatrick. Overview  Comments  Documentation Strings  Pydoc  Comments  Documentation Strings  Pydoc.
Embedded Software Design Week V Python Lists and Dictionaries PWM LED 1-Wire Temperature Sensor.
Introduction to Web Science Old Dominion University Department of Computer Science Hany SalahEldeen CS495/595 – Introduction to Web Science Fall 2014 Lecture.
Raspberry Pi Project Control Your Home Lights with a Raspberry Pi.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Control Statements 1. Conditionals 2 Loops 3 4 Simple Loop Examples def echo(): while echo1(): pass def echo1(): line = input('Say something: ') print('You.
Microcontroller basics
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Xi Wang Yang Zhang. 1. Strings 2. Text Files 3. Exceptions 4. Classes  Refer to basics 2.py for the example codes for this section.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Example: Vehicles What attributes do objects of Sedan have?
Lecture 4 Python Basics Part 3.
UltraSonic Sensor VCC is the pin that powers the device, connect to 5V. Trigger is the pin that sends out the burst. Echo is the pin that outputs when.
WORKSHOP LED CONTROL.
RASPBERRY PI WORKSHOP.
Building Raspberry Pi Controllers with Python
Session 3 DIY Moderate Project
Lighting LEDs with a RASPBERRY PI
Internet-of-Things (IoT)
Lecture 4 Python Basics Part 3.
Using Text Files in Python
Writing Functions( ) (Part 4)
Raspberry Pi 2/3 GPIO - LED, Button
RPi 2/3 GPIO + Web(Flask)
Introduction to Value-Returning Functions: Generating Random Numbers
By Ryan Christen Errors and Exceptions.
Controlling LED with PWM
BUILDING A BLOCKCHAIN USING PYTHON
Presentation transcript:

Embedded Software Design Week V Advanced Python 7- segment Display

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

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

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)

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)

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

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()

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]]

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

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'])

Making Web Requests from Python import urllib.request with urllib.request.urlopen(' as response: html = response.read()

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

Sending from Python See .py at web site

Writing a Simple Web Server in Python First install bottle $ wget 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

7 - Segment Display

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

7 - Segment Python Code Part-1 # code modified, tweaked and tailored from code by bertwert # on RPi forum thread topic 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)

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()