from machine import Pin
from machine import PWM
import time
class Audio_Notification(PWM):
def __init__(self, pin, debug):
super().__init__(pin)
self.__debug = debug
def warning_on(self):
if self.__debug:
print("Warning on")
self.freq(1000)
def warning_off(self):
if self.__debug:
print("Warning off")
self.freq(50)
class Pedestrian_Button(Pin):
# child class inherits the parent 'Pin' class
def __init__(self, pin, debug):
super().__init__(pin, Pin.IN, Pin.PULL_DOWN)
self.__debug = debug
self.__pin = pin
self.button_state
self.pedestrian_waiting = False
self.irq(trigger=Pin.IRQ_RISING, handler=self.callback)
def callback(self, pin):
self.pedestrian_waiting = True
self.pedestrian_waiting
@property
def pedestrian_waiting(self):
return self.__pedestrian_waiting
@pedestrian_waiting.setter
def pedestrian_waiting(self, state=None):
if state == False:
self.__pedestrian_waiting = False
elif state == True:
self.__pedestrian_waiting = True
if self.__debug:
print(f"Button connected to Pin {self.__pin} is waiting")
@property
def button_state(self):
self.__button_state = self.value()
print(self.__button_state)
if self.__debug:
print(f"Button connected to Pin {self.__pin} is {self.__button_state}")
return self.__button_state
class Led_Light(Pin):
# child class inherits the parent 'Pin' class
def __init__(self, pin, debug=False):
super().__init__(pin, Pin.OUT)
self.led_light_state
self.__debug = debug
self.__pin = pin
def on(self):
# method overiding polymorphism of the parent class
self.high()
if self.__debug:
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
def off(self):
# method overiding polymorphism of the parent class
self.low()
if self.__debug:
print(f"LED connected to Pin {self.__pin} is {self.led_light_state}")
def toggle(self):
# method overiding polymorphism of the parent class
if self.value():
self.on()
else:
self.off()
@property
def led_light_state(self):
# method overloading polymorphism in this class
return self.value()
@led_light_state.setter
def led_light_state(self, value):
# method overloading polymorphism in this class
if value == 0:
self.off()
elif value == 1:
self.on()
class Walk_Light():
def __init__(self, led_pin, buz_pin, debug):
self.LED = Led_Light(led_pin, debug)
self.BUZ = Audio_Notification(buz_pin, debug)
self.__debug = debug
def walk_on(self):
if self.__debug:
print("Beep and Light on")
self.LED.on()
self.BUZ.warning_on()
def walk_off(self):
if self.__debug:
print("Beep and Light off")
self.LED.off()
self.BUZ.warning_off()
class Walk_Light2():
def __init__(self, led, buz, debug):
self.LED = led
self.BUZ = buz
self.__debug = debug
def walk_on(self):
if self.__debug:
print("Beep and Light on")
self.LED.on()
self.BUZ.warning_on()
def walk_off(self):
if self.__debug:
print("Beep and Light off")
self.LED.off()
self.BUZ.warning_off()
test = Walk_Light(17, 27, True)
LED = Led_Light(19, True)
BUZ = Audio_Notification(27, True)
test2 = Walk_Light2(LED, BUZ, True)
while True:
test2.walk_on()
test.walk_on()
print(1)
time.sleep(1)
test.walk_off()
test2.walk_off()
time.sleep(1)