Embedded Software Design Week V Python Lists and Dictionaries PWM LED 1-Wire Temperature Sensor.

Slides:



Advertisements
Similar presentations
Chapter 12: File System Implementation
Advertisements

Container Types in Python
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
Taylor Holmes, Jason Partin, William Rody, Malcolm Stagg.
Computer Science 111 Fundamentals of Programming I Dictionaries.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
File Management Chapter 12. File Management A file is a named entity used to save results from a program or provide data to a program. Access control.
Sequences The range function returns a sequence
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
Introduction to Python. Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Chapter 8 Inverters AC Power • Inverters • Power Conditioning Units • Inverter Features and Specifications.
Introduction.
Lecture – 7 Basic input and output
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Classes and objects Practice 2. Basic terms  Classifier is an element of the model, which specifies some general features for a set of objects. Features.
Peripherals and their Control An overview of industrially available “peripheral devices” that use “pulse-width modulation” for information passing. Review.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
LEGNARO - LCS - LEGNARO - LCS - IFMIF/EVEDA – RFQ Legnaro Local Control System M. Montis INFN-LNL Monthly Collaboration Meeting - May 29, 2012.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
PWM Circuit Based on the 555 Timer. Introduction In applications LED Brightness Control we may want to vary voltage given to it. Most often we use a variable.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
1 File Management Chapter File Management n File management system consists of system utility programs that run as privileged applications n Concerned.
Built-in Data Structures in Python An Introduction.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 STRING LIST TUPLE DICTIONARY. Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object.
GE3M25: Computer Programming for Biologists Python, Class 5
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
BLDC Motor Speed Control with RPM Display. Introduction BLDC Motor Speed Control with RPM Display  The main objective of this.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
Pulse-Width Modulation: Simulating variable DC output
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
Intro to CS Nov 21, 2016.
Computer System Laboratory
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
Electric Motors.
Internet-of-Things (IoT)
Programming Logic and Design Fourth Edition, Comprehensive
Recitation Outline C++ STL associative containers Examples
Intelligent HVAC Control
Pulse-Width Modulation: Simulating variable DC output
Introduction to Computer Science
Presentation transcript:

Embedded Software Design Week V Python Lists and Dictionaries PWM LED 1-Wire Temperature Sensor

Creating a List >>> a = [34, 'Fred', 12, False, 72.3] Python list is not fixed size Elements may have different types >>> a = [] -> empty list

Accessing Elements of a List >>> a = [34, 'Fred', 12, False, 72.3] >>> a = [1] 'Fred’ First item in the list has index 0 >>> a = [34, 'Fred', 12, False, 72.3] >>> a[1] = 777 >>>a [34,777, 12, False, 72.3]

Finding the Length of a List >>> a = [34, 'Fred', 12, False, 72.3] >>> len(a) 5 Function len also works on strings

Adding Elements to a List Append >>> a = [34, 'Fred', 12, False, 72.3] >>> a.append(“new”) >>> a [34, 'Fred', 12, False, 72.3, ’new’] Insert >>> a = [34, 'Fred', 12, False, 72.3] >>> a.insert(“new2”) >>> a [34, 'Fred', ’new2’, 12, False, 72.3] Extend >>> a = [34, 'Fred', 12, False, 72.3] >>> b = [74, 75] >>> a.extend(b) >>> a [34, 'Fred', 12, False, 72.3, 74, 75]

Removing Elements from a List pop function >>> a = [34, 'Fred', 12, False, 72.3] a.pop() 72.3 >>> a a = [34, 'Fred', 12, False] pop with index >>> a = [34, 'Fred', 12, False, 72.3] a.pop(0) 34

Creating a List by parsing a String >>> "abc def ghi".split() ['abc', 'def', 'ghi'] >>> "abc--de--ghi".split('--') ['abc', 'de', 'ghi']

Iterating over a List

Enumerating a List Alternative

Sorting a List >>> a = ["it", "was", "the", "best", "of", "times"] >>> a.sort() >>> a ['best', 'it', 'of', 'the', 'times', 'was'] >>> import copy >>> a = ["it", "was", "the", "best", "of", "times"] >>> b = copy.copy(a) >>> b.sort() >>> a ['it', 'was', 'the', 'best', 'of', 'times'] >>> b ['best', 'it', 'of', 'the', 'times', 'was'] >>> sort function modifies the contents of the array, use copy function if you need original version.

Cutting Up a List Use [:] >>> l = ["a", "b", "c", "d"] >>> l[1:3] ['b', 'c'] >>> l = ["a", "b", "c", "d"] >>> l[:3] ['a', 'b', 'c'] >>> l[3:] ['d']

Applying a Function to List Comprehensions >>> l = ["abc", "def", "ghi", "ijk"] >>> [x.upper() for x in l] ['ABC', 'DEF', 'GHI', 'IJK']

Creating a Dictionary Key-Value pairs >>> phone_numbers = {'Simon':' ', 'Jane':' '}

Creating a Dictionary Cont. Dictionaries as values of other dictionaries >>> a = {'key1':'value1', 'key2':2} >>> a {'key2': 2, 'key1': 'value1'} >>> b = {'b_key1':a} >>> b {'b_key1': {'key2': 2, 'key1': 'value1'}} Items in a dictionary is placed in random order (hashing)

Accessing a Dictionary >>> phone_numbers = {'Simon':' ', 'Jane':' '} >>> phone_numbers['Simon'] ' ' >>> phone_numbers['Jane'] ' ’ If there is no match >>> phone_numbers = {'Simon':' ', 'Jane':' '} >>> phone_numbers['Phil'] Traceback (most recent call last): File " ", line 1, in KeyError: 'Phil'

Adding/Modifying a Dictionary >>> phone_numbers['Pete'] = ' ' >>> phone_numbers['Pete'] ' ’ If key is present, value is overwritten Else if a new key-value pair is created

Removing Things from a Dictionary Use pop command >>> phone_numbers = {'Simon':' ', 'Jane':' '} >>> phone_numbers.pop('Jane') ' ' >>> phone_numbers {'Simon': ' '}

Iterating over Dictionaries Use for command >>> phone_numbers = {'Simon':' ', 'Jane':' '} >>> for name in phone_numbers:... print(name)... Jane Simon Iterate over value-pairs >>> phone_numbers = {'Simon':' ', 'Jane':' '} >>> for name, num in phone_numbers.items():... print(name + " " + num)... Jane Simon

PWM* An example of PWM in an idealized inductor** driven by a ■ voltage source modulated as a series of pulses, resulting in a ■ sine-like current in the inductor. The rectangular voltage pulses nonetheless result in a smoother and smoother current waveform as the switching frequency increases. Note that the current waveform is the integral of the voltage waveform. * ** Inductor is a device that stores energy in the terms of magnetic field

Using PWM to Adjust Brightness of a LED See 11_chage_led_brightness.py on the web site!

1-wire? 1-Wire* is a device communications bus system designed by Dallas Semiconductor Corp. Provides low-speed data, signaling, and power over a single signal. Similar in concept to I²C, but with lower data rates and longer range. Used to communicate with small inexpensive devices such as digital thermometers and weather instruments. İstanbul Akbil smart ticket is one of the implementations of 1-wire *

1-wire Support on Raspberry Pi ADD FOLLOWING LINE TO /boot/config.txt dtoverlay=w1-gpio Load “w1-gpio” kernel driver sudo modprobe w1-gpio Check the following directory /sys/bus/w1/devices/

Temperature Sensor Module (DS18B20) Measures the temperature and reports it through the 1-wire bus digitally to the micro-controller. Includes the special 1-wire serial interface as well as control logic and the temperature sensor itself Load “w1-therm” module sudo modprobe w1-therm

Making work altogether Do the following before running the code. 1. Finish the wiring (Signal pin should be connect to GPIO Pin #4) 2. > sudo modprobe w1-gpio 3. > sudo modprobe w1-therm 4. > cd /sys/bus/w1/devices 5. > ls –l total 0 lrwxrwxrwx 1 root root 0 Jan 31 20: d >../../../devices/w1_bus_master1/ d50803 lrwxrwxrwx 1 root root 0 Jan 31 20:34 w1_bus_master1 ->../../../devices/w1_bus_master1 If you see “ d50803“ << this then it’s working!!! 6. To run the python code “ sudo python 7_temperature.py ” << See what the temperature is!