Skip to content

Commit 8bb5e17

Browse files
committed
Fixed minor formatting issues
1 parent b8cdff8 commit 8bb5e17

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

micropython/examples/galactic_unicorn/light_sensor.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import time
22
from galactic import GalacticUnicorn
33
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
4-
import math
54

6-
'''
5+
"""
76
Auto brightness feature for the galactic unicorn
87
Uses the onboard light sensor to detect the light
98
The brightness percentage is displayed with brightness auto adjusted
10-
'''
9+
"""
1110
# set up unicorn and drawing variables
1211
gu = GalacticUnicorn()
1312
graphics = PicoGraphics(DISPLAY)
@@ -27,71 +26,75 @@
2726
# the onboard light sensor has a wide range from 0 t0 4095
2827
# It is therefore needed to set a lower max and a higher minimum
2928
MIN_LS_VALUE = 10
30-
MAX_LS_VALUE = 295 # 4095 to use the full range
29+
MAX_LS_VALUE = 295 # 4095 to use the full range
3130
MIN_RANGE = 0.1
3231
MAX_RANGE = 1
3332

3433

3534
# perform linear interpolation to map a range of values to discrete
36-
def map_range(x, min_input = MIN_LS_VALUE,
37-
max_input = MAX_LS_VALUE,
38-
min_output = MIN_RANGE,
39-
max_output = MAX_RANGE):
40-
return (x - min_input) * (max_output - min_output) / (max_input - min_input) + min_output
41-
35+
def map_range(
36+
x,
37+
min_input=MIN_LS_VALUE,
38+
max_input=MAX_LS_VALUE,
39+
min_output=MIN_RANGE,
40+
max_output=MAX_RANGE,
41+
):
42+
return (x - min_input) * (max_output - min_output) / (
43+
max_input - min_input
44+
) + min_output
45+
4246

4347
# gets the light sensor value from onboard sensor and interpolates it
4448
# clamps the brightness values
4549
def calculate_brightness(current_lsv):
46-
brightness_value = map_range(current_lsv)
47-
if brightness_value > 1:
48-
brightness_value = 1
49-
elif brightness_value < 0.1:
50-
brightness_value = 0.1
51-
52-
return brightness_value
50+
brightness_val = map_range(current_lsv)
51+
if brightness_val > 1:
52+
brightness_val = 1
53+
elif brightness_val < 0.1:
54+
brightness_val = 0.1
55+
56+
return brightness_val
5357

5458

5559
# sets up a handy function we can call to clear the screen
5660
def clear():
5761
graphics.set_pen(BLACK)
58-
graphics.clear()
62+
graphics.clear()
63+
5964

6065
mode = "auto"
6166
last = time.ticks_ms()
6267
while True:
6368
current = time.ticks_ms()
64-
65-
66-
69+
6770
# get light sensor value from the sensor
6871
ls_value = gu.light()
6972
brightness_value = calculate_brightness(ls_value)
7073
gu.set_brightness(brightness_value)
7174
# calculate brightness percentage
7275
bp = (brightness_value / MAX_RANGE) * 100
73-
76+
7477
# deactivate auto brightness by pressing A
7578
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
7679
print("Auto brightness off")
7780
mode = "off"
78-
81+
7982
# reactivate auto brightness by pressing A
8083
if gu.is_pressed(GalacticUnicorn.SWITCH_B):
8184
print("Auto brightness on")
8285
mode = "auto"
83-
86+
8487
# set brightness to default value if off
8588
if mode == "off":
8689
gu.set_brightness(0.5)
87-
88-
# set text update rate after a certain time to reduce flickering
90+
91+
# set text update rate after a certain time to reduce flickering
8992
if current - last >= TEXT_SLEEP:
9093
clear()
91-
94+
9295
# calculate colour from the brightness value
9396
hue = max(0, HUE_START + ((bp - 0) * (HUE_END - HUE_START) / (100 - 0)))
94-
97+
9598
# create pens with this colour (and with the high / low colours)
9699
CURRENT_COLOUR = graphics.create_pen_hsv(hue / 360, 1.0, 0.8)
97100
HIGH_COLOUR = graphics.create_pen_hsv(HUE_END / 360, 1.0, 0.8)
@@ -100,10 +103,10 @@ def clear():
100103
# draw the text
101104
graphics.set_pen(CURRENT_COLOUR)
102105
graphics.text("BRT: ", 0, 1, scale=1)
103-
# measure the rest of the text before drawing so that we can right align it
106+
# measure the rest of the text before drawing to right align it
104107
text_width = graphics.measure_text(f"{bp:.0f}/°", scale=1)
105108
graphics.text(f"{bp:.0f}%", WIDTH - text_width, 1, scale=1)
106-
109+
107110
# draw a bar for the background
108111
graphics.set_pen(GREY)
109112
graphics.rectangle(0, 9, WIDTH, 10)
@@ -116,5 +119,3 @@ def clear():
116119
# time to update the display
117120
gu.update(graphics)
118121
# time.sleep(1)
119-
120-

0 commit comments

Comments
 (0)