8
6
"""Calculates PWM levels for visually-linear steps.
10
# Get parameters from the user
11
v = dict(pwm_max=255, pwm2_max=255)
13
(int, 'num_levels', 4, 'How many total levels do you want?'),
14
(int, 'pwm_min', 6, 'Lowest visible PWM level, for moon mode:'),
15
(float, 'lm_min', 0.25, 'How bright is moon mode, in lumens?'),
16
#(int, 'pwm_max', 255, 'Highest PWM level:'),
17
(float, 'lm_max', 1000, 'How bright is the highest level, in lumens?'),
18
(str, 'dual_pwm', 'n', 'Use dual PWM? [y/n]'),
19
(float, 'pwm2_min', 6, 'Second channel, lowest visible PWM level:'),
20
(float, 'lm2_min', 0.25, 'Second channel, how bright is the lowest mode, in lumens?'),
21
#(float, 'pwm2_max', 255, 'Second channel, highest PWM level:'),
22
(float, 'lm2_max', 140, 'Second channel, how bright is maximum, in lumens?'),
24
for typ, name, default, text in questions:
25
value = get_value(text, default, args)
31
if (name == 'dual_pwm' and value == 'n'):
32
# skip remaining questions if not using dual PWM
35
if v['dual_pwm'] == 'y':
40
if interactive: # Wait on exit, in case user invoked us by clicking an icon
41
print 'Press Enter to exit:'
45
"""Estimate the PWM levels for a one-channel driver."""
46
visual_min = invpower(v['lm_min'])
47
visual_max = invpower(v['lm_max'])
48
step_size = (visual_max - visual_min) / (v['num_levels']-1)
8
# change these values for each device:
9
pwm_min = 0 # lowest visible PWM level, for moon mode
10
lm_min = 10.0 # how bright is moon mode, in lumens?
11
pwm_max = 255 # highest PWM level
12
lm_max = 1300 # how bright is the highest level, in lumens?
13
num_levels = 4 # how many total levels do you want?
14
# The rest should work fine without changes
15
visual_min = math.pow(lm_min, 1.0/3)
16
visual_max = math.pow(lm_max, 1.0/3)
17
step_size = (visual_max - visual_min) / (num_levels-1)
51
for i in range(v['num_levels']):
53
#pwm_float = ((goal_lm / v['lm_max']) * (256-v['pwm_min'])) + v['pwm_min'] - 1
54
pwm_float = (((goal_lm-v['lm_min']) / (v['lm_max']-v['lm_min'])) \
55
* (255-v['pwm_min'])) \
20
for i in range(num_levels):
21
pwm_float = (((goal**3) / lm_max) * (256-pwm_min)) + pwm_min - 1
57
22
pwm = int(round(pwm_float))
58
pwm = max(min(pwm,v['pwm_max']),v['pwm_min'])
23
pwm = max(min(pwm,pwm_max),pwm_min)
60
print '%i: visually %.2f (%.2f lm): %.2f/255' % (i+1, goal, goal_lm, pwm_float)
63
print 'PWM values:', ','.join([str(i) for i in modes])
66
"""Estimate the PWM levels for a two-channel driver.
67
Assume the first channel is the brighter one, and second will be used for moon/low modes.
69
#visual_min = math.pow(v['lm2_min'], 1.0/power)
70
#visual_max = math.pow(v['lm_max'], 1.0/power)
71
visual_min = invpower(v['lm2_min'])
72
visual_max = invpower(v['lm_max'])
73
step_size = (visual_max - visual_min) / (v['num_levels']-1)
76
for i in range(v['num_levels']):
78
# Up to the second channel's limit, calculate things just like a
79
# single-channel driver (first channel will be zero)
80
if goal_lm <= v['lm2_max']:
82
#pwm2_float = ((goal_lm / v['lm2_max']) * (256-v['pwm2_min'])) + v['pwm2_min'] - 1
83
pwm2_float = (((goal_lm-v['lm2_min']) / (v['lm2_max']-v['lm2_min'])) \
84
* (255-v['pwm2_min'])) \
86
pwm1 = int(round(pwm1_float))
87
pwm2 = int(round(pwm2_float))
88
pwm2 = max(min(pwm2,v['pwm2_max']),v['pwm2_min'])
89
modes.append((int(pwm1),int(pwm2)))
90
# Above the second channel's limit, things get a little more
91
# complicated (second channel will be 255, first channel will be
92
# adjusted down by the max output of the second channel)
94
if len(modes) == v['num_levels'] -1: # turbo is special
95
#pwm1_float = ((goal_lm / v['lm_max']) * (256-v['pwm_min'])) + v['pwm_min'] - 1
96
pwm1_float = float(v['pwm_max'])
97
# on a FET+7135 driver, turbo works better without the 7135
98
# (we're assuming FET+7135 here)
100
else: # not the highest mode yet
101
#pwm1_float = (((goal_lm-v['lm2_max']) / v['lm_max']) * (256-v['pwm_min'])) + v['pwm_min'] - 1
102
pwm1_float = (((goal_lm-v['lm_min']-v['lm2_max']) / (v['lm_max']-v['lm_min'])) \
103
* (255-v['pwm_min'])) \
106
pwm1 = int(round(pwm1_float))
107
pwm2 = int(round(pwm2_float))
108
pwm1 = max(min(pwm1,v['pwm_max']),v['pwm_min'])
109
modes.append((int(pwm1),int(pwm2)))
110
print '%i: visually %.2f (%.2f lm): %.2f/255, %.2f/255' % (i+1, goal, goal_lm, pwm1_float, pwm2_float)
113
print 'PWM1/FET values:', ','.join([str(i[0]) for i in modes])
114
print 'PWM2/7135 values:', ','.join([str(i[1]) for i in modes])
115
print 'On a non-FET driver, the last mode should be 255 on both channels.'
117
def get_value(text, default, args):
118
"""Get input from the user, or from the command line args."""
125
print text, '(%s)' % (default),
127
result = result.strip()
138
#return math.pow(x, 1/5.0)
139
return math.pow(x, 1/3.0)
140
#return math.pow(x, 1/2.0)
141
#return math.log(x, math.e)
142
#return math.log(x, 2.0)
25
print '%i: visually %.2f (%.2f lm): %.2f/255' % (i+1, goal, goal ** 3, pwm_float)
28
print ','.join([str(i) for i in modes])
144
30
if __name__ == "__main__":