Shunxin 8 months ago
parent
commit
a701a6993e
2 changed files with 46 additions and 14 deletions
  1. 44 7
      libs/foo/__init__.py
  2. 2 7
      main.py

+ 44 - 7
libs/foo/__init__.py

@@ -1,8 +1,9 @@
 """
 Example Lib
 """
+from machine import Pin, Timer
 
-class foo:
+class foo(object):
     """
     Example class.
     """
@@ -11,13 +12,49 @@ class foo:
 
 
     ##############################           Constructor          ##############################
-    def __init__(self):
-        pass
+    def __init__(self,led_pin_time:Pin,led_pin_button:Pin,button:Pin):
+        """_summary_
 
-    ##############################             Methods            ##############################
-    def bar(self):
+        Args:
+            led_pin_time (Pin): _description_
+            led_pin_button (Pin): _description_
+            button (Pin): _description_
         """
-        Example Method.
+        
+        self.__led_pin_timer = self.__check_pin(led_pin_time)
+        self.__led_pin_button = self.__check_pin(led_pin_button)
+        self.__button = self.__check_pin(button)
+        self.__led_pin_timer.init(mode=Pin.OUT)
+        self.__led_pin_button.init(mode=Pin.OUT)
+        self.__button.init(mode=Pin.IN)
+        self.__timer = Timer()
+        self.__timer.init(mode=Timer.PERIODIC;period = 200,callback= self.__bliink_handler)
+        self.__button.irq(handler=self.__int_handler,hard=True,trigger=Pin.IRQ_FALLING)
+        
+    ##############################             Property            ##############################
+    @property
+    def button(self):
+        return self.__button
+    @button.setter
+    def button(self,pin):
+        #self.__button = self.__check_pin(pin)
+        raise AttributeError("Du darfst Pin nicht verändern")
+    ##############################             Methods            ##############################
+    
+        
+    def __check_pin(self,pin:Pin):
+        """_summary_
+
+        Args:
+            pin (Pin): _description_
         """
+        if not isinstance(pin,Pin):
+            raise ValueError(f"Value\"{pin}\" of \"pin\" is not type \"Pin\"!")
+        else:
+            return pin
 
-        print("class foo, method bar")
+def __blink_handler(self,timer):
+    self.__led_pin_timer.toggle()
+    
+def __int_handler(self,pin):
+    self.__led_pin_button.toggle()

+ 2 - 7
main.py

@@ -14,7 +14,7 @@ from libs.foo import foo
 
 ##############################            Global Variables            ##############################
 
-baz = foo()
+baz = foo(Pin(0),Pin(1),Pin(20))
 
 ##############################                 Main                   ##############################
 
@@ -28,12 +28,7 @@ def main():
     led = Pin("LED", Pin.OUT)
     
     ##########################                Code                  ############################
-    
-    # test example class
-    baz.bar()
-    
-    # test example function
-    bar()
+
 
     # LED blinking
     while True: