]>
git.zerfleddert.de Git - fnordlicht-mini/blob - firmware/fnordlicht-controller/ir-cluster.c
e4dcb632267becfea3961ff761e59e7c9378d5d4
2 * ox - infrared to usb keyboard/mouse adapter
4 * by Alexander Neumann <alexander@lochraster.org>
6 * inspired by InfraHID by Alex Badea,
7 * see http://vamposdecampos.googlepages.com/infrahid.html
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * For more information on the GPL, please go to:
23 * http://www.gnu.org/copyleft/gpl.html
26 #include "ir-cluster.h"
28 /* return the position of the (first) minimum value within data[],
29 * considering only values at even positions */
30 static uint8_t minimum(uint16_t data
[], uint8_t len
)
34 for (uint8_t i
= 0; i
< 2*len
; i
+= 2) {
35 if (data
[i
] < data
[min
])
42 /* search next bigger value (starting at data[from]) within data[], just
43 * considering the even values, return -1 on error (no bigger value found) */
44 static int8_t next(uint16_t data
[], uint8_t len
, uint8_t from
)
48 uint16_t old
= data
[from
];
50 /* test if the same value appears again within data[] after from */
51 for (uint8_t i
= from
+2; i
< len
; i
+= 2) {
53 /* found the same value again, at pos i */
57 /* else search for the next bigger value */
59 for (uint8_t i
= 0; i
< len
; i
+= 2) {
61 /* if the current value is lower than the old value, try next */
65 /* if we haven't found a bigger value yet, or if the current value
66 * is smaller than the value we looked at before,
67 * consider the current position as the next value */
68 if (pos
< 0 || data
[i
] < data
[pos
])
75 /* search for (one-dimensional) clusters within data[],
76 * consider only values at even positions */
77 uint8_t ir_cluster(uint16_t data
[], uint8_t len
, uint16_t cluster
[], uint8_t max
)
81 /* search minimum within data[] */
82 uint8_t pos
= minimum(data
, len
);
84 /* initialize mean value */
85 uint32_t mean
= data
[pos
];
88 /* iterate over data[], processing the values (at even positions) in
90 for (uint8_t i
= 0; i
< len
-1; i
++) {
92 /* search position of the next element within data[] */
93 uint8_t nextpos
= next(data
, len
, pos
);
95 /* as a shortcut, name values a and b */
96 uint16_t a
= data
[pos
];
97 uint16_t b
= data
[nextpos
];
99 /* check if b > 1.5*a */
103 /* reached a step, found a cluster */
105 cluster
[cindex
++] = (uint16_t)mean
;
107 /* stop processing since max cluster values is reached */
111 /* reset mean value */
116 /* add value to mean */
120 /* advance position */
124 /* if there are some values left in mean, this is the last cluster */
127 cluster
[cindex
++] = (uint16_t)mean
;
133 /* get cluster index belonging to data */
134 uint8_t ir_min_cluster(uint16_t data
, uint16_t cluster
[], uint8_t len
)
137 uint16_t diff
= 0xffff;
139 /* iterate over possible clusters */
140 for (uint8_t i
= 0; i
< len
; i
++) {
143 /* get positive difference */
144 if (data
< cluster
[i
])
145 curdiff
= cluster
[i
] - data
;
147 curdiff
= data
- cluster
[i
];
149 /* if difference is lower, remember this cluster */
150 if (curdiff
< diff
) {
154 /* stop here, since difference is larger than for the last cluster
155 * (cluster[] is ordered ascending */