|
1 | 1 | # Lecture 5 |
2 | 2 |
|
3 | 3 | ## Lecture 5 Concepts |
4 | | -- [Class Overview](#class-overview) |
5 | | -- [Create Files](#create-files) |
6 | | -- [Imports and Constructor](#imports-and-constructor) |
7 | | -- [Create a Single Beep](#create-a-single-beep) |
8 | | -- [Implement a Non-Blocking Audio Notification](#implement-a-non-blocking-audio-notification) |
9 | | -- [Turn Audio Notification Off](#turn-audio-notification-off) |
10 | 4 |
|
11 | | -## Class Overview |
| 5 | +- [Audio_Notification Class](#audio_notification-class) |
| 6 | + - [Create Files](#create-files) |
| 7 | +- [What Are Stubs and Drivers?](#what-are-stubs-and-drivers) |
| 8 | + - [Why Use Stubs and Drivers?](#why-use-stubs-and-drivers) |
| 9 | + - [Stub Example](#stub-example) |
| 10 | + - [Driver Example](#driver-example) |
| 11 | +- [Implement the Audio_Notification Class](#implement-the-audio_notification-class) |
| 12 | + - [Imports and Constructor](#imports-and-constructor) |
| 13 | + - [Create a Single Beep](#create-a-single-beep) |
| 14 | + - [Implement a Non-Blocking Audio Notification](#implement-a-non-blocking-audio-notification) |
| 15 | + - [Turn Audio Notification Off](#turn-audio-notification-off) |
| 16 | + |
| 17 | +## Audio_Notification Class |
12 | 18 |
|
13 | 19 | The Audio_Notification extends the machine.PWM to provide an interface for controlling a piezo buzzer or speaker, with optional debug output. It supports warning beeps and custom tones. |
14 | 20 |
|
15 | | -## Create Files |
| 21 | +### Create Files |
16 | 22 |
|
17 | 23 | 1. Create a Python file in `project\lib` called `audio_notification.py` |
18 | 24 | 2. Create a Python file in `project\py_scripts` called `v06.py` |
19 | 25 |
|
| 26 | +## What Are Stubs and Drivers? |
| 27 | + |
| 28 | +Imagine you're building a puzzle: |
| 29 | +- A **stub** is a fake puzzle piece you use temporarily |
| 30 | +- A **driver** is a simple tool to check if your real pieces fit together |
| 31 | + |
| 32 | +Definitions |
| 33 | +A **stub** is a simplified version that replaces a real lower component so it doesn't need to be full implemented. |
| 34 | +A **driver** is a simple program in a higher system that tests a lower component without fully implementing the higher system. |
| 35 | + |
| 36 | +### Why Use Stubs and Drivers? |
| 37 | + |
| 38 | +1. **Test in parts**: Check each piece of your code separately |
| 39 | +2. **No dependencies**: Test without needing everything to be finished |
| 40 | +3. **Controlled testing**: Create specific test scenarios easily |
| 41 | +4. **Faster development**: Don't need to wait for other parts to be done |
| 42 | + |
| 43 | +This simple approach helps you build and test your code without needing to complete your system! |
| 44 | + |
| 45 | +### Stub Example |
| 46 | + |
| 47 | +Remember a **stub** is a simplified version that replaces a real lower component so it doesn't need to be full implemented. We are going to implement the Audio_Notification Class as a Stub so we can test the components that we will later use in a PedestrianSubSystem before fully implementing the Audio_Notification Class. |
| 48 | + |
| 49 | +```python |
| 50 | +# audio_notification.py Stub Implementation |
| 51 | +class Audio_Notification: |
| 52 | + def __init__(self, pin): |
| 53 | + self.__pin = pin |
| 54 | + |
| 55 | + def warning_on(self): |
| 56 | + # Just pretend to Beep |
| 57 | + print("Beep") |
| 58 | +``` |
| 59 | + |
| 60 | +### Driver Example |
| 61 | + |
| 62 | +Remember a **driver** is a simple program in a higher system that tests a lower component without fully implementing the higher system. |
| 63 | + |
| 64 | +```python |
| 65 | +# v05.py Driver Implementation |
| 66 | + |
| 67 | +# Import the real component we want to test |
| 68 | +from audio_notification import Audio_Notification |
| 69 | +from led_light import Led_Light |
| 70 | + |
| 71 | +def PedestrianSubsystem_driver(): |
| 72 | + print("Testing Traffic Light...") |
| 73 | + |
| 74 | + # Create the walk light |
| 75 | + led_pedestrian_green = Led_Light(17, True, True) |
| 76 | + |
| 77 | + # Create the Audio Notification |
| 78 | + audio_stub = Audio_Notification(27, False) |
| 79 | + |
| 80 | + # Test walk state |
| 81 | + led_pedestrian_green.on() |
| 82 | + audio_stub.warning_on() |
| 83 | + |
| 84 | + print("Test complete!") |
| 85 | + |
| 86 | +# Run the test |
| 87 | +test_traffic_light() |
| 88 | +``` |
| 89 | + |
| 90 | +## Implement the Audio_Notification Class |
| 91 | + |
20 | 92 | ## Imports and Constructor |
21 | 93 |
|
22 | 94 | In your `audio_notification.py`, include your imports, define the class, and configure the initialiser with the parameters' pin' and' debug'. Add the required parameters to store time and set the duty cycle. |
@@ -62,5 +134,4 @@ class Audio_Notification(PWM): |
62 | 134 | if self.__debug: |
63 | 135 | print("Warning off") |
64 | 136 | self.duty_u16(0) # Turn off sound |
65 | | -``` |
66 | | - |
| 137 | +``` |
0 commit comments