Module 5
Module 5
Inheritance
Inheritance is the process of forming a new class from an existing class or base class.
Function overloading
Function overloading is a form of polymorphism that allows a function to have different
meanings, depending on its context.
Operator overloading
Operator overloading is a form of polymorphism that allows assignment of more than one
function to a particular operator.
Function overriding
Function overriding allows a child class to provide a specific implementation of a function that
is already provided by the base class. Child class implementation of the overridden function
has the same name, parameters and return type as the function in the base class.
Dr. Uma Narayanan, CSE, RSET
The variable studentCount is a class
variable that is shared by all instances
of the class Student and is accessed by
Student.studentCount.
The variables name, id and grades are
instance variables which are specific
to each instance of the class.
There is a special method by the name
__init__() which is the class
constructor.
The class constructor initializes a new
instance when it is created. The
function __del__() is the class
destructor.
Dr. Uma Narayanan, CSE, RSET
In this example, Shape is the base class and Circle is the derived class.
The class Circle inherits the attributes of the Shape class.
The child class Circle overrides the methods and attributes of the base
class (e.g., the draw() function defined in the base class Shape is
overridden in child class Circle).
Arch
Arch is an Arch Linux port for AMD devices.
Pidora
Pidora Linux is a Fedora Linux optimized for Raspberry Pi.
RaspBMC
RaspBMC is an XBMC media-center distribution for Raspberry Pi.
OpenELEC
OpenELEC is a fast and user-friendly XBMC media-center distribution.
RISC OS
RISC OS is a very fast and compact operating system
Dr. Uma Narayanan, CSE, RSET
To interact with the Raspberry Pi interfaces using Python, you can use
libraries such as "RPi.GPIO" to control the GPIO pins and
"Adafruit_Python_DHT" to read data from temperature and humidity
sensors. You can also use Python to control LEDs, read button presses,
and collect sensor data.
Python also has several libraries and frameworks that can be used for
IoT applications on Raspberry Pi.
Flask is a popular web framework that can be used to build web
interfaces for controlling IoT devices.
Pygame is a library for creating games and multimedia applications
on Raspberry Pi, which can be used for IoT applications such as
creating user interfaces for smart home devices.
PySerial is a library that can be used for serial communication
between Raspberry Pi and other devices, such as Arduino boards.
Dr. Uma Narayanan, CSE, RSET
GPIO Zero: GPIO Zero is a Python library that simplifies the process
of controlling Raspberry Pi's GPIO pins. It provides a simple and
intuitive API for working with buttons, LEDs, motors, and other GPIO-
connected components. GPIO Zero is often used for building
interactive projects and prototyping hardware ideas.
Picamera: Picamera is a Python library for controlling the Raspberry
Pi camera module. It provides a simple interface for capturing
images and video, as well as controlling camera settings like
exposure and white balance. Picamera is often used for creating
timelapse videos, security systems, and video streaming applications
on Raspberry Pi.
I2C
The I2C interface pins on Raspberry Pi allow you to connect hardware
modules. I2C interface allows synchronous data transfer with just two pins -
SDA (data line) and SCL (clock line)
Dr. Uma Narayanan, CSE, RSET
Reading button presses:
To read button presses, you can use the RPi.GPIO library and set up the GPIO
pin connected to the button as an input pin.
Here's an example code to detect a button press on GPIO pin 23:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(23)
if input_state == False:
print('Button Pressed')
time.sleep(0.2)
Dr. Uma Narayanan, CSE, RSET
Collecting sensor data:
To collect sensor data, you need to connect the sensor to the appropriate
GPIO pins and use a library specific to the sensor.
Here's an example code to collect temperature data using
the Adafruit_DHT library with a DHT11 sensor connected to GPIO pin 4:
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temperature={0:0.1f}C Humidity={1:0.1f}%'.format(temperature,
humidity))
else:
print('Failed to retrieve data from sensor')
Dr. Uma Narayanan, CSE, RSET
PROGRAM TO BLINKING LED
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
GPIO.output(18, True)
time.sleep(1)
GPIO.output(18, False)
time.sleep(1)