-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathled_dim.comp
More file actions
74 lines (71 loc) · 2.64 KB
/
led_dim.comp
File metadata and controls
74 lines (71 loc) · 2.64 KB
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
/******************************************************************************
*
* Copyright (C) 2015 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 is part of the Machinekit HAL project. For more
* information, go to https://github.com/machinekit.
*
******************************************************************************/
component led_dim "HAL component for dimming LEDs";
pin in float in "Brightness input value -> 0 to 1";
pin out float out "Luminance output value -> 0 to 1";
function _ "Update the output value";
description """
Component for LED dimming according to human perception of brightness of light.
.LP
The output is calculated using the CIE 1931 formula.
""";
license "GPL";
author "Alexander Rössler";
option period no;
variable hal_float_t last_in = 0.0;
;;
#include <rtapi_math.h>
#define MIN(x,y) (x < y ? x : y)
#define MAX(x,y) (x > y ? x : y)
FUNCTION(_) {
if (last_in != in)
{
hal_float_t input = MIN(MAX(in, 0.0), 1.0); // clamp to 0.0, 1.0
// luminance calculation based on CIE 1931 formula
// see http://forum.arduino.cc/index.php/topic,147810.0.html
if (input < 0.079996)
{
out = input / 9.033;
}
else
{
out = pow((input + 0.16) / 1.16, 3.0);
}
last_in = in;
}
}