Skip to content

Commit aed7aee

Browse files
add steps
1 parent 5a012e3 commit aed7aee

11 files changed

Lines changed: 349 additions & 74 deletions

File tree

project/py_scripts/v03.py

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,16 @@
1-
'''
2-
Build basic light class that inherits the Pin class
3-
Extend class to include functiuonality specific to design requirements
4-
Class demonstrates both method overloading and overriding in inheritance
5-
'''
1+
"""
2+
Lecture 1 - Instantiation
3+
"""
64

75
from machine import Pin
8-
from time import sleep, time
6+
from time import sleep
97

10-
class Led_Light(Pin):
11-
# child class inherits the parent 'Pin' class
12-
def __init__(self, pin, flashing=False, debug=False):
13-
super().__init__(pin, Pin.OUT)
14-
self.led_light_state
15-
self.__debug = debug
16-
self.__pin = pin
17-
self.__flashing = flashing
18-
19-
def on(self):
20-
# method overiding polymorphism of the parent class
21-
self.high()
22-
if self.__debug:
23-
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
24-
25-
def off(self):
26-
# method overiding polymorphism of the parent class
27-
self.low()
28-
if self.__debug:
29-
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
30-
31-
def toggle(self):
32-
# method overiding polymorphism of the parent class
33-
if self.value() == 0:
34-
self.on()
35-
elif self.value() == 1:
36-
self.off()
37-
38-
@property
39-
def led_light_state(self):
40-
# method overloading polymorphism in this class
41-
return self.value()
42-
43-
@led_light_state.setter
44-
def led_light_state(self, value):
45-
# method overloading polymorphism in this class
46-
if value == 1:
47-
self.off()
48-
elif value == 0:
49-
self.on()
50-
51-
52-
red_light = Led_Light(3, False, False)
8+
led_car_red = Pin(3, Pin.OUT)
9+
led_car_orange = Pin(5, Pin.OUT)
10+
led_car_green = Pin(6, Pin.OUT)
5311

5412
while True:
55-
print(red_light.led_light_state)
56-
red_light.led_light_state = 1
57-
sleep(0.25)
58-
print(red_light.led_light_state)
59-
red_light.led_light_state = 0
60-
sleep(0.25)
13+
led_car_red.toggle()
14+
led_car_orange.toggle()
15+
led_car_green.toggle()
16+
sleep(1)

project/py_scripts/v04.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
'''
2-
Abstraction as simple interface hides (facade design pattern)
3-
The complexity of the implementation of the class 'Led_Light' in the module 'led_light.py'
4-
Extend the implementation to provide on for duration and flash for duration methods
5-
'''
1+
"""
2+
Lecture 1 - Inhertance
3+
"""
64

7-
from led_light import Led_Light
5+
from machine import Pin
86
from time import sleep
97

10-
red_light = Led_Light(3, True, True)
8+
9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin):
12+
super().__init__(pin, Pin.OUT)
13+
14+
15+
red_light = Led_Light(3)
1116

1217
while True:
13-
red_light.on_for(5)
14-
sleep(3)
18+
red_light.on()
19+
sleep(0.5)
20+
red_light.off()
21+
sleep(0.5)
22+
red_light.high()
23+
sleep(2)
24+
red_light.low()
25+
sleep(2)
26+
red_light.toggle()
27+
sleep(4)
28+
red_light.toggle()
29+
sleep(4)

project/py_scripts/v05.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
1-
'''
2-
Implement a debounced button event manager that inherits Pin class
3-
'''
1+
"""
2+
Lecture 2 - Overriding Polymorphism
3+
"""
44

5-
from led_light import Led_Light
6-
from pedestrian_button import Pedestrian_Button
5+
from machine import Pin
76
from time import sleep
87

98

9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin, flashing=False, debug=False):
12+
super().__init__(pin, Pin.OUT)
13+
self.__debug = debug
14+
self.__pin = pin
15+
self.__flashing = flashing
1016

11-
red_light = Led_Light(3, False)
12-
ped_button = Pedestrian_Button(22, True)
17+
def on(self):
18+
# method overriding polymorphism of the parent class
19+
self.high()
20+
if self.__debug:
21+
print(f"LED connected to Pin {self.__pin} is high")
22+
23+
def off(self):
24+
# method overriding polymorphism of the parent class
25+
self.low()
26+
if self.__debug:
27+
print(f"LED connected to Pin {self.__pin} is low")
28+
29+
def toggle(self):
30+
# method overriding polymorphism of the parent class
31+
if self.value() == 0:
32+
self.high()
33+
if self.__debug:
34+
print(f"LED connected to Pin {self.__pin} is high")
35+
elif self.value() == 1:
36+
self.low()
37+
if self.__debug:
38+
print(f"LED connected to Pin {self.__pin} is low")
39+
40+
41+
red_light = Led_Light(3, False, False)
1342

1443
while True:
15-
red_light.led_light_state = ped_button.button_state
16-
sleep(0.5)
44+
red_light.toggle()
45+
sleep(1)

project/py_scripts/v06.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Lecture 2 - Polymorphism WET v DRY
3+
"""
4+
5+
from machine import Pin
6+
from time import sleep
7+
8+
9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin, flashing=False, debug=False):
12+
super().__init__(pin, Pin.OUT)
13+
self.__debug = debug
14+
self.__pin = pin
15+
self.__flashing = flashing
16+
17+
def on(self):
18+
# method overriding polymorphism of the parent class
19+
self.high()
20+
if self.__debug:
21+
print(f"LED connected to Pin {self.__pin} is high")
22+
23+
def off(self):
24+
# method overriding polymorphism of the parent class
25+
self.low()
26+
if self.__debug:
27+
print(f"LED connected to Pin {self.__pin} is low")
28+
29+
def toggle(self):
30+
# method overriding polymorphism of the parent class
31+
if self.value() == 0:
32+
self.high()
33+
if self.__debug:
34+
print(f"LED connected to Pin {self.__pin} is high")
35+
elif self.value() == 1:
36+
self.low()
37+
if self.__debug:
38+
print(f"LED connected to Pin {self.__pin} is low")
39+
40+
41+
red_light = Led_Light(3, False, False)
42+
43+
while True:
44+
red_light.toggle()
45+
sleep(1)

project/py_scripts/v07.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Lecture 2 - Overloading Polymorphism
3+
"""
4+
5+
from machine import Pin
6+
from time import sleep, time
7+
8+
9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin, flashing=False, debug=False):
12+
super().__init__(pin, Pin.OUT)
13+
self.led_light_state
14+
self.__debug = debug
15+
self.__pin = pin
16+
self.__flashing = flashing
17+
18+
def on(self):
19+
# method overriding polymorphism of the parent class
20+
self.high()
21+
if self.__debug:
22+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
23+
24+
def off(self):
25+
# method overriding polymorphism of the parent class
26+
self.low()
27+
if self.__debug:
28+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
29+
30+
def toggle(self):
31+
# method overriding polymorphism of the parent class
32+
if self.value() == 0:
33+
self.on()
34+
elif self.value() == 1:
35+
self.off()
36+
37+
@property
38+
def led_light_state(self):
39+
# method overloading polymorphism in this class
40+
return self.value()
41+
42+
@led_light_state.setter
43+
def led_light_state(self, value):
44+
# method overloading polymorphism in this class
45+
if value == 1:
46+
self.off()
47+
elif value == 0:
48+
self.on()
49+
50+
51+
red_light = Led_Light(3, False, False)
52+
53+
while True:
54+
print(red_light.led_light_state)
55+
red_light.led_light_state = 1
56+
sleep(0.25)
57+
print(red_light.led_light_state)
58+
red_light.led_light_state = 0
59+
sleep(0.25)

project/py_scripts/v08.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Lecture 3 - Encapsulation
3+
"""
4+
5+
from machine import Pin
6+
from time import sleep, time
7+
8+
9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin, flashing=False, debug=False):
12+
super().__init__(pin, Pin.OUT)
13+
self.led_light_state
14+
self.__debug = debug
15+
self.__pin = pin
16+
self.__flashing = flashing
17+
18+
def on(self):
19+
# method overriding polymorphism of the parent class
20+
self.high()
21+
if self.__debug:
22+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
23+
24+
def off(self):
25+
# method overriding polymorphism of the parent class
26+
self.low()
27+
if self.__debug:
28+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
29+
30+
def toggle(self):
31+
# method overriding polymorphism of the parent class
32+
if self.value() == 0:
33+
self.on()
34+
elif self.value() == 1:
35+
self.off()
36+
37+
@property
38+
def led_light_state(self):
39+
# method overloading polymorphism in this class
40+
return self.value()
41+
42+
@led_light_state.setter
43+
def led_light_state(self, value):
44+
# method overloading polymorphism in this class
45+
if value == 1:
46+
self.off()
47+
elif value == 0:
48+
self.on()
49+
50+
51+
red_light = Led_Light(3, False, False)
52+
53+
while True:
54+
print(red_light.led_light_state) # Allowed
55+
red_light.led_light_state = 1 # Allowed
56+
print(
57+
f"Not allowed: {red_light.__pin} ???"
58+
) # Not allowed, should raise AttributeError

project/py_scripts/v09.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Lecture 3 - Setters & Getters
3+
"""
4+
5+
from machine import Pin
6+
from time import sleep, time
7+
8+
9+
class Led_Light(Pin):
10+
# child class inherits the parent 'Pin' class
11+
def __init__(self, pin, flashing=False, debug=False):
12+
super().__init__(pin, Pin.OUT)
13+
self.led_light_state
14+
self.__debug = debug
15+
self.__pin = pin
16+
self.__flashing = flashing
17+
18+
def on(self):
19+
# method overriding polymorphism of the parent class
20+
self.high()
21+
if self.__debug:
22+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
23+
24+
def off(self):
25+
# method overriding polymorphism of the parent class
26+
self.low()
27+
if self.__debug:
28+
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
29+
30+
def toggle(self):
31+
# method overriding polymorphism of the parent class
32+
if self.value() == 0:
33+
self.on()
34+
elif self.value() == 1:
35+
self.off()
36+
37+
@property
38+
def led_light_state(self):
39+
# method overloading polymorphism in this class
40+
return self.value()
41+
42+
@led_light_state.setter
43+
def led_light_state(self, value):
44+
# method overloading polymorphism in this class
45+
if value == 1:
46+
self.off()
47+
elif value == 0:
48+
self.on()
49+
50+
51+
red_light = Led_Light(3, False, False)
52+
53+
while True:
54+
print(red_light.led_light_state) # Allowed
55+
red_light.led_light_state = 1 # Allowed
56+
print(
57+
f"Not allowed: {red_light.__pin} ???"
58+
) # Not allowed, should raise AttributeError

0 commit comments

Comments
 (0)