forked from thecooltool/UNI-PRINT-3D
/
led_dim.comp
66 lines (64 loc) · 2.53 KB
/
led_dim.comp
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
/******************************************************************************
*
* Copyright (C) 2014 Alexander Rössler
*
*
* This module allows dimming LEDs using HAL
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* THE AUTHORS OF THIS PROGRAM ACCEPT ABSOLUTELY NO LIABILITY FOR
* ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE
* TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of
* harming persons must have provisions for completely removing power
* from all motors, etc, before persons enter any danger area. All
* machinery must be designed to comply with local and national safety
* codes, and the authors of this software can not, and do not, take
* any responsibility for such compliance.
*
* This code was written as part of the LinuxCNC project. For more
* information, go to www.linuxcnc.org.
*
******************************************************************************/
component led_dim "LinuxCNC HAL component for dimming LEDs";
pin in u32 steps "Input steps";
pin in u32 max_pwm "PWM steps (usually 2^n)";
pin in float factor "The dim factor";
pin in float input "Input value -> 0 to 1";
pin out float output "Output value -> 0 to 1";
function _ fp "Update the output value";
description """
Component for dimming LEDs
.LP
The output value can be tuned using the the factor input
""";
license "GPL";
variable hal_float_t basis = 1.0;
variable hal_float_t lastInput = 0.0;
;;
#include <rtapi_math.h>
FUNCTION(_) {
hal_float_t value;
if (lastInput != input)
{
basis = rtapi_pow((hal_float_t)max_pwm/factor, 1.0 / (hal_float_t)(steps-1));
value = (factor * rtapi_pow(basis, input * (hal_float_t)steps)) / ((hal_float_t)(max_pwm));
output = (value > 1.0) ? 1.0 : value;
lastInput = input;
}
}