-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP28_Command_Pattern.py
More file actions
94 lines (75 loc) · 2.24 KB
/
Copy pathP28_Command_Pattern.py
File metadata and controls
94 lines (75 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File: main.py
# Author: Armstrong Subero
# Platform: Raspberry Pi Pico (RP2040) with MicroPython
# Program: P28_Command_Pattern
# Interpreter: MicroPython (latest version)
# Program Version: 1.0
#
# Program Description: This program demonstrates the Command design pattern by
# creating a set of commands that can be executed on an LED.
# The pattern encapsulates requests as objects, allowing for
# parameterization and queuing of requests. It also maintains
# a history of executed commands, enabling potential undo functionality.
#
# Hardware Description: An LED is connected to pin 16.
#
# Created: August 25th, 2024, 12:15 AM
# Last Updated: August 25th, 2024, 12:15 AM
from machine import Pin
import utime
# Receiver: LED class
class LED:
def __init__(self, pin_number):
self.led = Pin(pin_number, Pin.OUT)
def on(self):
self.led.value(1)
print("LED is ON")
def off(self):
self.led.value(0)
print("LED is OFF")
# Command interface
class Command:
def execute(self):
pass
def undo(self):
pass
# Concrete command classes
class LEDOnCommand(Command):
def __init__(self, led):
self.led = led
def execute(self):
self.led.on()
def undo(self):
self.led.off()
class LEDOffCommand(Command):
def __init__(self, led):
self.led = led
def execute(self):
self.led.off()
def undo(self):
self.led.on()
# Invoker: Remote control that executes commands and keeps a history
class RemoteControl:
def __init__(self):
self.history = []
def submit(self, command):
command.execute()
self.history.append(command)
def undo_last(self):
if self.history:
last_command = self.history.pop()
last_command.undo()
else:
print("No commands to undo")
# Usage
led = LED(16)
remote = RemoteControl()
on_command = LEDOnCommand(led)
off_command = LEDOffCommand(led)
# Turn the LED on and off using the remote control
remote.submit(on_command)
utime.sleep(1)
remote.submit(off_command)
utime.sleep(1)
# Undo the last command (turns the LED back on)
remote.undo_last()