1
1
import time
2
2
from galactic import GalacticUnicorn
3
3
from picographics import PicoGraphics , DISPLAY_GALACTIC_UNICORN as DISPLAY
4
- import math
5
4
6
- '''
5
+ """
7
6
Auto brightness feature for the galactic unicorn
8
7
Uses the onboard light sensor to detect the light
9
8
The brightness percentage is displayed with brightness auto adjusted
10
- '''
9
+ """
11
10
# set up unicorn and drawing variables
12
11
gu = GalacticUnicorn ()
13
12
graphics = PicoGraphics (DISPLAY )
27
26
# the onboard light sensor has a wide range from 0 t0 4095
28
27
# It is therefore needed to set a lower max and a higher minimum
29
28
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
31
30
MIN_RANGE = 0.1
32
31
MAX_RANGE = 1
33
32
34
33
35
34
# 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
+
42
46
43
47
# gets the light sensor value from onboard sensor and interpolates it
44
48
# clamps the brightness values
45
49
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
53
57
54
58
55
59
# sets up a handy function we can call to clear the screen
56
60
def clear ():
57
61
graphics .set_pen (BLACK )
58
- graphics .clear ()
62
+ graphics .clear ()
63
+
59
64
60
65
mode = "auto"
61
66
last = time .ticks_ms ()
62
67
while True :
63
68
current = time .ticks_ms ()
64
-
65
-
66
-
69
+
67
70
# get light sensor value from the sensor
68
71
ls_value = gu .light ()
69
72
brightness_value = calculate_brightness (ls_value )
70
73
gu .set_brightness (brightness_value )
71
74
# calculate brightness percentage
72
75
bp = (brightness_value / MAX_RANGE ) * 100
73
-
76
+
74
77
# deactivate auto brightness by pressing A
75
78
if gu .is_pressed (GalacticUnicorn .SWITCH_A ):
76
79
print ("Auto brightness off" )
77
80
mode = "off"
78
-
81
+
79
82
# reactivate auto brightness by pressing A
80
83
if gu .is_pressed (GalacticUnicorn .SWITCH_B ):
81
84
print ("Auto brightness on" )
82
85
mode = "auto"
83
-
86
+
84
87
# set brightness to default value if off
85
88
if mode == "off" :
86
89
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
89
92
if current - last >= TEXT_SLEEP :
90
93
clear ()
91
-
94
+
92
95
# calculate colour from the brightness value
93
96
hue = max (0 , HUE_START + ((bp - 0 ) * (HUE_END - HUE_START ) / (100 - 0 )))
94
-
97
+
95
98
# create pens with this colour (and with the high / low colours)
96
99
CURRENT_COLOUR = graphics .create_pen_hsv (hue / 360 , 1.0 , 0.8 )
97
100
HIGH_COLOUR = graphics .create_pen_hsv (HUE_END / 360 , 1.0 , 0.8 )
@@ -100,10 +103,10 @@ def clear():
100
103
# draw the text
101
104
graphics .set_pen (CURRENT_COLOUR )
102
105
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
104
107
text_width = graphics .measure_text (f"{ bp :.0f} /°" , scale = 1 )
105
108
graphics .text (f"{ bp :.0f} %" , WIDTH - text_width , 1 , scale = 1 )
106
-
109
+
107
110
# draw a bar for the background
108
111
graphics .set_pen (GREY )
109
112
graphics .rectangle (0 , 9 , WIDTH , 10 )
@@ -116,5 +119,3 @@ def clear():
116
119
# time to update the display
117
120
gu .update (graphics )
118
121
# time.sleep(1)
119
-
120
-
0 commit comments