Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Embedded Software Design Week V Python Lists and Dictionaries PWM LED 1-Wire Temperature Sensor."— Presentation transcript:

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

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

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

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

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

6 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

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

8 Iterating over a List

9 Enumerating a List Alternative

10 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.

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

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

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

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

15 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 " ", line 1, in KeyError: 'Phil'

16 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

17 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'}

18 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

19 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

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

21 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

22 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/

23 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

24 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/28- 000004d50803 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!


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

Similar presentations


Ads by Google