Skip to content
This repository has been archived by the owner on Jan 9, 2018. It is now read-only.

Commit

Permalink
add ir receive support
Browse files Browse the repository at this point in the history
  • Loading branch information
fd0 committed Apr 30, 2010
1 parent c5288ff commit 0b4589e
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#define _OUTPORT(name) _CONCAT(PORT, name)
#define _INPORT(name) _CONCAT(PIN, name)
#define _DDRPORT(name) _CONCAT(DDR, name)
#define _PCIE(name) _CONCAT(PCIE, name)
#define _PCIF(name) _CONCAT(PCIF, name)
#define _PCMSK(name) _CONCAT(PCMSK, name)
#define _PCINT(name) _CONCAT(PCINT, name)

/* __noinline attribute (opposite of inline attribute */
#define __noinline __attribute__((noinline))
Expand Down
3 changes: 3 additions & 0 deletions fnordlicht-controller/fcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "timer.h"
#include "uart.h"
#include "ui.h"
#include "ir.h"

/* NEVER CALL DIRECTLY! */
void disable_watchdog(void) \
Expand All @@ -54,6 +55,7 @@ int main(void)
timer_init();
uart_init();
ui_init();
ir_init();

/* enable interrupts globally */
sei();
Expand All @@ -65,5 +67,6 @@ int main(void)
while (1) {
usb_poll();
ui_poll();
ir_poll();
}
}
5 changes: 5 additions & 0 deletions fnordlicht-controller/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
#define BTN1_PIN 5
#define BTN2_PIN 4

/* infrared receiver */
#define IR_PORTNAME C
#define IR_PIN 1
#define IR_INTNUM 9 /* PC1 is PCINT9 */

/* convenient naming */
#define LED_PORT _OUTPORT(LED_PORTNAME)
#define LED_DDR _DDRPORT(LED_PORTNAME)
Expand Down
160 changes: 160 additions & 0 deletions fnordlicht-controller/ir-cluster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* ox - infrared to usb keyboard/mouse adapter
*
* by Alexander Neumann <[email protected]>
*
* inspired by InfraHID by Alex Badea,
* see http://vamposdecampos.googlepages.com/infrahid.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/

#include "ir-cluster.h"

/* return the position of the (first) minimum value within data[],
* considering only values at even positions */
static uint8_t minimum(uint16_t data[], uint8_t len)
{
uint8_t min = 0;

for (uint8_t i = 0; i < 2*len; i += 2) {
if (data[i] < data[min])
min = i;
}

return min;
}

/* search next bigger value (starting at data[from]) within data[], just
* considering the even values, return -1 on error (no bigger value found) */
static int8_t next(uint16_t data[], uint8_t len, uint8_t from)
{
len *= 2;

uint16_t old = data[from];

/* test if the same value appears again within data[] after from */
for (uint8_t i = from+2; i < len; i += 2) {
if (data[i] == old)
/* found the same value again, at pos i */
return i;
}

/* else search for the next bigger value */
int16_t pos = -1;
for (uint8_t i = 0; i < len; i += 2) {

/* if the current value is lower than the old value, try next */
if (data[i] <= old)
continue;

/* if we haven't found a bigger value yet, or if the current value
* is smaller than the value we looked at before,
* consider the current position as the next value */
if (pos < 0 || data[i] < data[pos])
pos = i;
}

return pos;
}

/* search for (one-dimensional) clusters within data[],
* consider only values at even positions */
uint8_t ir_cluster(uint16_t data[], uint8_t len, uint16_t cluster[], uint8_t max)
{
uint8_t cindex = 0;

/* search minimum within data[] */
uint8_t pos = minimum(data, len);

/* initialize mean value */
uint32_t mean = data[pos];
uint8_t count = 1;

/* iterate over data[], processing the values (at even positions) in
* ascending order */
for (uint8_t i = 0; i < len-1; i++) {

/* search position of the next element within data[] */
uint8_t nextpos = next(data, len, pos);

/* as a shortcut, name values a and b */
uint16_t a = data[pos];
uint16_t b = data[nextpos];

/* check if b > 1.5*a */
a += a/2;
if (b > a) {

/* reached a step, found a cluster */
mean /= count;
cluster[cindex++] = (uint16_t)mean;

/* stop processing since max cluster values is reached */
if (cindex == max)
return max;

/* reset mean value */
mean = 0;
count = 0;
}

/* add value to mean */
mean += b;
count++;

/* advance position */
pos = nextpos;
}

/* if there are some values left in mean, this is the last cluster */
if (count > 0) {
mean /= count;
cluster[cindex++] = (uint16_t)mean;
}

return cindex;
}

/* get cluster index belonging to data */
uint8_t ir_min_cluster(uint16_t data, uint16_t cluster[], uint8_t len)
{
uint8_t min = 0;
uint16_t diff = 0xffff;

/* iterate over possible clusters */
for (uint8_t i = 0; i < len; i++) {
uint16_t curdiff;

/* get positive difference */
if (data < cluster[i])
curdiff = cluster[i] - data;
else
curdiff = data - cluster[i];

/* if difference is lower, remember this cluster */
if (curdiff < diff) {
diff = curdiff;
min = i;
} else
/* stop here, since difference is larger than for the last cluster
* (cluster[] is ordered ascending */
break;
}

return min;
}
38 changes: 38 additions & 0 deletions fnordlicht-controller/ir-cluster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* ox - infrared to usb keyboard/mouse adapter
*
* by Alexander Neumann <[email protected]>
*
* inspired by InfraHID by Alex Badea,
* see http://vamposdecampos.googlepages.com/infrahid.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/

#include <stdint.h>

#ifndef __IR_CLUSTER_H
#define __IR_CLUSTER_H

/* search for (one-dimensional) clusters within data[],
* consider only values at even positions */
uint8_t ir_cluster(uint16_t data[], uint8_t len, uint16_t cluster[], uint8_t max);

/* get cluster index belonging to data */
uint8_t ir_min_cluster(uint16_t data, uint16_t cluster[], uint8_t len);

#endif
Loading

0 comments on commit 0b4589e

Please sign in to comment.