Embedded Software Design

Download Report

Transcript Embedded Software Design

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':'01234 567899', 'Jane':'01234 666666'}
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':'01234 567899', 'Jane':'01234 666666'}
>>> phone_numbers['Simon']
'01234 567899'
>>> phone_numbers['Jane']
'01234 666666’
• If there is no match
>>> phone_numbers = {'Simon':'01234 567899', 'Jane':'01234 666666'}
>>> phone_numbers['Phil']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Phil'
Adding/Modifying a Dictionary
>>> phone_numbers['Pete'] = '01234 777555'
>>> phone_numbers['Pete']
'01234 777555’
• 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':'01234 567899', 'Jane':'01234 666666'}
>>> phone_numbers.pop('Jane')
'01234 666666'
>>> phone_numbers
{'Simon': '01234 567899'}
Iterating over Dictionaries
• Use for command
>>> phone_numbers = {'Simon':'01234 567899', 'Jane':'01234 666666'}
>>> for name in phone_numbers:
... print(name)
...
Jane
Simon
• Iterate over value-pairs
>>> phone_numbers = {'Simon':'01234 567899', 'Jane':'01234 666666'}
>>> for name, num in phone_numbers.items():
... print(name + " " + num)
...
Jane 01234 666666
Simon 01234 567899
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.
*https://en.wikipedia.org/wiki/Pulse-width_modulation
** 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
*https://en.wikipedia.org/wiki/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)
Load “w1-therm” module
sudo modprobe w1-therm
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
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:34 28-000004d50803 -> ../../../devices/w1_bus_master1/28000004d50803
lrwxrwxrwx 1 root root 0 Jan 31 20:34 w1_bus_master1 -> ../../../devices/w1_bus_master1
If you see “28-000004d50803“ << this then it’s working!!! 
6. To run the python code “sudo python 7_temperature.py” << See what the temperature is!