-
Notifications
You must be signed in to change notification settings - Fork 0
/
week4_task.py
169 lines (143 loc) · 4.95 KB
/
week4_task.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Write a program that implements a menu system to control LED brightness.
# The menu has two modes: LED selection and brightness control.
# User toggles between the modes by pressing the encoder button.
# When the program is in LED selection mode turning the encoder switches between LEDs.
# The selected LED is displayed on the screen.
# When user presses the button program enters brightness control mode where turning the encoder adjusts the brightness of the selected LED.
# The current brightness is displayed on the screen as both percentage (0 – 100%) and a horizontal bar.
# Turning the knob immediately increases or decreases the brightness of the LED andupdates the display.
# When user presses the button to switch back to LED selection mode the current brightness will remain on the LED.
# Adjusting one LEDs brightness may not affect the other LEDs.
import ssd1306
from machine import Pin, I2C, PWM, ADC
import utime
###############
# GPIO Pins #
###############
# Rotary Encoder
rot_push = Pin(12, mode = Pin.IN, pull = Pin.PULL_UP)
rota = Pin(10, mode = Pin.IN, pull = Pin.PULL_UP)
rotb = Pin(11, mode = Pin.IN, pull = Pin.PULL_UP)
# LEDs
led = [20, 21, 22]
list_of_led = []
for x in range(0,3):
# list_of_led.append(Pin(led[x], Pin.OUT))
list_of_led.append(PWM(Pin(led[x])))
list_of_led[x].freq(1000)
# OLED
i2c = I2C(1, sda=Pin(14), scl=Pin(15))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
###############
# VARIABLES #
###############
# Menu selection variables and switch filtering
mode = 0
count = 0
switch_state = 0
# Variables for LEDs selection
value = 0
previous_value = 1
# Variables for Brightness
brightness = [20000,20000,20000]
previous_state = [1,1,1]
###############
# FUNCTIONS #
###############
# LED selection
def led_changed():
global previous_value
global value
global brightness
if previous_value != rota.value():
if rota.value() == 0:
if rotb.value() == 0:
value = (value - 1)%3
print('anti-clockwise', value+1, brightness[value])
else:
value = (value + 1)%3
print('clockwise', value+1, brightness[value])
previous_value = rota.value()
# OLED displays LED number
def oled_on():
oled.fill(0)
if value == 0:
oled.text('LED1', 0, 0, 1)
oled.text('LED2', 0, 10, 0)
oled.text('LED3', 0, 20, 0)
if value == 1:
oled.text('LED1', 0, 0, 0)
oled.text('LED2', 0, 10, 1)
oled.text('LED3', 0, 20, 0)
if value == 2:
oled.text('LED1', 0, 0, 0)
oled.text('LED2', 0, 10, 0)
oled.text('LED3', 0, 20, 1)
oled.show()
# Brightness selection
def brightness_changed():
global previous_brightness
global brightness
global value
if previous_state[value] != rota.value():
if rota.value() == 0:
if rotb.value() == 0:
if brightness[value] > 0:
brightness[value] = (brightness[value] - 2000)
print("anti-clockwise", brightness[value])
else:
if brightness[value] < 40000:
brightness[value] = (brightness[value] + 2000)
print("clockwise", brightness[value])
previous_state[value] = rota.value()
# OLED displys brightness level
def oled_display():
oled.fill(0)
if value == 0:
oled.text('LED1', 0, 0, 1)
oled.text('LED2', 0, 10, 0)
oled.text('LED3', 0, 20, 0)
if value == 1:
oled.text('LED1', 0, 0, 0)
oled.text('LED2', 0, 10, 1)
oled.text('LED3', 0, 20, 0)
if value == 2:
oled.text('LED1', 0, 0, 0)
oled.text('LED2', 0, 10, 0)
oled.text('LED3', 0, 20, 1)
procentage = int(brightness[value]/400)
oled.text('Brightness: ' + str(procentage) + '%', 0, 36, 1)
oled.rect(15, 50, 101, 10, 1)
oled.fill_rect(15, 50, procentage, 10, 1)
oled.show()
####################
# MAIN PROGRAMME #
####################
while True:
# Filtering the Rotary Push button and enable selection of mode
new_state = rot_push.value()
if new_state != switch_state:
count += 1
if count > 3:
if new_state == 0:
if mode == 0:
mode = 1
else:
mode = 0
switch_state = new_state
count = 0
else:
count = 0
utime.sleep(0.01)
# Functions of the modes
oled_display()
if mode == 0:
# oled_on
for i in range(0,3):
list_of_led[i].duty_u16(0)
led_changed()
list_of_led[value].duty_u16(brightness[value])
else:
new_brightness = brightness_changed()
list_of_led[value].duty_u16(brightness[value])
# oled_display()