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
75from 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
5412while 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 )
0 commit comments