Skip to content

Commit 3d14cdf

Browse files
fix
1 parent 0e1cc01 commit 3d14cdf

9 files changed

Lines changed: 598 additions & 595 deletions

File tree

tutorials/Lecture1.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,71 @@
22

33
## Lecture 1 Concepts
44
- Unit Testing
5+
- UML (Unified Modelling Language)
56
- Generalisation
67
- Parent/Child Class
78
- Pin & PWM Parent Classes
89
- Instantiation
910
- Inheritance
1011

12+
1113
## Unit Testing
1214

1315
Unit testing is the process of writing and running small, isolated tests that check the correctness of individual pieces of code (such as functions or modules). The goal is to ensure that each unit of your embedded software works as expected, independently from the rest of the system.
1416

1517
Unit testing is especially valuable in embedded systems, such as the Pi Pico, where debugging can be challenging due to limited resources and hardware complexity. By ensuring each component works independently, you can build more complex mechatronic solutions.
1618

19+
## UML Class Diagrams
20+
21+
A UML (Unified Modelling Language) class diagram visually describes the structure of a system by showing its classes, their attributes (variables), methods (functions), and relationships (like inheritance).
22+
23+
```text
24+
----------------------
25+
| Student | <-- Class Name
26+
----------------------
27+
| -name: String | <-- Attributes
28+
| -age: int |
29+
----------------------
30+
| +getName(): String | <-- Methods
31+
| +setAge(int): void |
32+
----------------------
33+
```
34+
35+
### Mermaid Markdown UML Class Diagram Example
36+
Below is a Mermaid class diagram for a Pi Pico GPIO Pin and a custom Led_Light class that inherits from it.
37+
38+
```mermaid
39+
classDiagram
40+
41+
class Pin {
42+
-pin: int
43+
+__init__(pin: int)
44+
+value()
45+
+high()
46+
+low()
47+
+toggle()
48+
}
49+
50+
class Led_Light {
51+
-debug: bool
52+
-pin: int
53+
-flashing: bool
54+
+__init__(pin: int, flashing: bool, debug: bool)
55+
+on()
56+
+off()
57+
+toggle()
58+
}
59+
Pin <|-- Led_Light : inherits
60+
```
61+
62+
Explanation:
63+
64+
1. Class Name (Top Section): This is the uppermost part of the box. It displays the name of the class (e.g., Student, Order, Car).
65+
2. Attributes (Middle Section): This section lists the attributes (or properties/fields) of the class. Each attribute is typically shown with its visibility (+ for public, - for private, # for protected), name, and type.
66+
3. Methods (Bottom Section): This section lists the methods (or operations/functions) that belong to the class.
67+
Each method is shown with its visibility, name, parameters, and return type.
68+
4. Connections (lines and arrows): These lines and arrows represent relationships (such as inheritance, association, aggregation, and composition) between class boxes.
69+
1770
## Generalisation
1871

1972
On the Pi Pico, the Pin and PWM libraries are designed to be generalised to provide flexible and reusable interfaces for interacting with the microcontroller’s hardware. The simple `on()`, `off()` or `high()`, `low()` methods are not specific to any hardware but are generalised to the nature of a digital GPIO Pin. As a generalised class is inherited and the classes are extended, they become more specific (less generalised) and functional to specific hardware for which they are designed.

tutorials/Lecture2.md

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,19 @@
11
# Lecture 2
22

33
## Lecture 2 Concepts
4-
- UML (Unified Modelling Language)
54
- Overriding Polymorphism
65
- DRY
6+
- Encapsulation
7+
- Setters & Getters
78
- Overloading Polymorphism
89

9-
## UML Class Diagrams
10-
11-
A UML (Unified Modelling Language) class diagram visually describes the structure of a system by showing its classes, their attributes (variables), methods (functions), and relationships (like inheritance).
12-
13-
```text
14-
----------------------
15-
| Student | <-- Class Name
16-
----------------------
17-
| -name: String | <-- Attributes
18-
| -age: int |
19-
----------------------
20-
| +getName(): String | <-- Methods
21-
| +setAge(int): void |
22-
----------------------
23-
```
24-
25-
### Mermaid Markdown UML Class Diagram Example
26-
Below is a Mermaid class diagram for a Pi Pico GPIO Pin and a custom Led_Light class that inherits from it.
27-
28-
```mermaid
29-
classDiagram
30-
31-
class Pin {
32-
-pin: int
33-
+__init__(pin: int)
34-
+value()
35-
+high()
36-
+low()
37-
+toggle()
38-
}
39-
40-
class Led_Light {
41-
-debug: bool
42-
-pin: int
43-
-flashing: bool
44-
+__init__(pin: int, flashing: bool, debug: bool)
45-
+on()
46-
+off()
47-
+toggle()
48-
}
49-
Pin <|-- Led_Light : inherits
50-
```
51-
52-
Explanation:
53-
54-
1. Class Name (Top Section): This is the uppermost part of the box. It displays the name of the class (e.g., Student, Order, Car).
55-
2. Attributes (Middle Section): This section lists the attributes (or properties/fields) of the class. Each attribute is typically shown with its visibility (+ for public, - for private, # for protected), name, and type.
56-
3. Methods (Bottom Section): This section lists the methods (or operations/functions) that belong to the class.
57-
Each method is shown with its visibility, name, parameters, and return type.
58-
4. Connections (lines and arrows): These lines and arrows represent relationships (such as inheritance, association, aggregation, and composition) between class boxes.
59-
6010
## Overriding Polymorphism
6111

6212
**Polymorphism means “many forms.”**
6313

6414
Polymorphism Overriding occurs when a child class (subclass) provides a new implementation for a method it inherits from its parent class (superclass).
6515

66-
The method in the child class has the same name and parameters as the one in the parent class. When the method is called on an object of the child class, Python (or any object-oriented language) uses the child’s versioneven if the object is referenced using the parent type.
16+
The method in the child class has the same name and parameters as the one in the parent class. When the method is called on an object of the child class, Python (or any object-oriented language) uses the child’s version, even if the object is referenced using the parent type.
6717

6818
```python
6919
from machine import Pin
@@ -107,6 +57,7 @@ while True:
10757
```
10858

10959
## DRY
60+
11061
The DRY pattern stands for "**Don't Repeat Yourself**"; it is a fundamental principle of programming aimed at reducing repetition of code and logic. This is to avoid duplicating code, logic, or data.
11162

11263
### Why Use DRY?
@@ -160,6 +111,59 @@ The DRY pattern stands for "**Don't Repeat Yourself**"; it is a fundamental prin
160111
self.off()
161112

162113
```
114+
## Encapsulation
115+
Encapsulation restricts direct access to some of an object's components (such as attributes or methods), meaning the internal representation of the object is hidden from the outside. This is typically achieved by making certain attributes or methods private (i.e., inaccessible from outside the class) and providing public methods (such as getters and setters) to access or modify those private members.
116+
117+
### Benefits of Encapsulation:
118+
119+
- Data Hiding: Internal object details are hidden, exposing only what is necessary.
120+
- Improved Security: Prevents external code from directly modifying internal state in unexpected ways.
121+
- Modularity: Each object manages its own state and behaviour, making code more modular and easier to maintain.
122+
- Flexibility: Implementation can change without affecting code that uses the object, as long as the public interface remains the same.
123+
124+
```python
125+
while True:
126+
print(red_light.led_light_state) # Allowed
127+
red_light.led_light_state = 1 # Allowed
128+
print(f"Not allowed: {red_light.__pin} ???") # Not allowed, should raise AttributeError
129+
```
130+
131+
> [!Note]
132+
> In Python, identifiers (variable or method names) that start with double underscores (e.g., `__my_var`) are not truly private in the sense of other languages like C# or C++. Instead, Python uses a mechanism called name mangling. When you define a variable with double underscores, Python changes its name internally to `_ClassName__my_var`. This means it is harder (but not impossible) to access it from outside the class.
133+
134+
## Setters & Getters
135+
136+
Setters and getters are special methods used in object-oriented programming to access (get) or modify (set) the values of private or protected attributes of a class. They help encapsulate the internal state of an object, providing controlled access.
137+
138+
### Getter
139+
140+
- A getter is a method that retrieves (gets) the value of a private attribute.
141+
- It allows you to read the value without providing direct access to the underlying variable.
142+
143+
### Setter
144+
145+
- A setter is a method that sets (updates) the value of a private attribute.
146+
- It allows you to validate or restrict changes before updating the attribute.
147+
148+
```python
149+
@property
150+
def led_light_state(self):
151+
# method overloading polymorphism in this class
152+
return self.value()
153+
154+
@led_light_state.setter
155+
def led_light_state(self, value):
156+
# method overloading polymorphism in this class
157+
if value == 1:
158+
self.off()
159+
elif value == 0:
160+
self.on()
161+
```
162+
163+
### Why Use Setters & Getters
164+
- Encapsulation: Protects the internal state of the object.
165+
- Validation: Allows you to add checks before changing values.
166+
- Abstraction: Hides implementation details from users of the class.
163167

164168
## Overloading Polymorphism
165169

tutorials/Lecture3.md

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,9 @@
11
# Lecture 3
22

3-
## Lecture 2 Concepts
4-
- Encapsulation
5-
- Setters & Getters
3+
## Lecture 3 Concepts
64
- Abstraction
7-
- Extend LED functionality
8-
- Complete led light class
9-
- Unit test the led light class
10-
11-
## Encapsulation
12-
Encapsulation restricts direct access to some of an object's components (such as attributes or methods), meaning the internal representation of the object is hidden from the outside. This is typically achieved by making certain attributes or methods private (i.e., inaccessible from outside the class) and providing public methods (such as getters and setters) to access or modify those private members.
13-
14-
### Benefits of Encapsulation:
15-
16-
- Data Hiding: Internal object details are hidden, exposing only what is necessary.
17-
- Improved Security: Prevents external code from directly modifying internal state in unexpected ways.
18-
- Modularity: Each object manages its own state and behaviour, making code more modular and easier to maintain.
19-
- Flexibility: Implementation can change without affecting code that uses the object, as long as the public interface remains the same.
20-
21-
```python
22-
while True:
23-
print(red_light.led_light_state) # Allowed
24-
red_light.led_light_state = 1 # Allowed
25-
print(f"Not allowed: {red_light.__pin} ???") # Not allowed, should raise AttributeError
26-
```
27-
> [!Note]
28-
> In Python, identifiers (variable or method names) that start with double underscores (e.g., `__my_var`) are not truly private in the sense of other languages like C# or C++. Instead, Python uses a mechanism called name mangling. When you define a variable with double underscores, Python changes its name internally to `_ClassName__my_var`. This means it is harder (but not impossible) to access it from outside the class.
29-
30-
## Setters & Getters
31-
32-
Setters and getters are special methods used in object-oriented programming to access (get) or modify (set) the values of private or protected attributes of a class. They help encapsulate the internal state of an object, providing controlled access.
33-
34-
### Getter
35-
36-
- A getter is a method that retrieves (gets) the value of a private attribute.
37-
- It allows you to read the value without providing direct access to the underlying variable.
38-
39-
### Setter
40-
41-
- A setter is a method that sets (updates) the value of a private attribute.
42-
- It allows you to validate or restrict changes before updating the attribute.
43-
44-
```python
45-
@property
46-
def led_light_state(self):
47-
# method overloading polymorphism in this class
48-
return self.value()
49-
50-
@led_light_state.setter
51-
def led_light_state(self, value):
52-
# method overloading polymorphism in this class
53-
if value == 1:
54-
self.off()
55-
elif value == 0:
56-
self.on()
57-
```
58-
59-
### Why Use Setters & Getters
60-
- Encapsulation: Protects the internal state of the object.
61-
- Validation: Allows you to add checks before changing values.
62-
- Abstraction: Hides implementation details from users of the class.
5+
- Implement a non blocking flash method
6+
- Complete and Unit Test led light class
637

648
## Abstraction
659

@@ -90,7 +34,7 @@ while True:
9034
sleep(0.25)
9135
```
9236

93-
## Extend the functionality of the led_light class
37+
## Implement a non blocking flash method
9438

9539
> [!Important]
9640
> Make sure you edit the class in the `project\lib\led_light.py`, not your main.py implementation.

0 commit comments

Comments
 (0)