buttons.c


1
/* $Id: buttons.c 11 2009-10-14 06:21:07Z andreas $ */
2
3
/*
4
 * Copyright (c) 2006-2009 Andreas Schroeder <andreas@a-netz.de>
5
 *
6
 * Implement functions of an infrared remote control and RC5 protocol with an
7
 * Atmel AVR controller.
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
24
#include <avr/io.h>
25
26
#include "buttons.h"
27
#include "rc5_codes.h"
28
29
/*
30
 * Initialize the hardware to read the button states.
31
 *
32
 * Set the ports to input with pullups enabled.
33
 */
34
void buttons_init(void) {
35
  DDRD &= ~(_BV(PD0) | _BV(PD1) | _BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5) | _BV(PD6));
36
  PORTD = _BV(PD0) | _BV(PD1) | _BV(PD2) | _BV(PD3) | _BV(PD4) | _BV(PD5) | _BV(PD6);
37
  DDRB &= ~(_BV(PB0) | _BV(PB1));
38
  PORTB = _BV(PB0) | _BV(PB1);
39
}
40
41
/*
42
 * Callback function from the rc5 module. This reads in the current button 
43
 * state and returns the corresponding rc5 command code.
44
 */
45
uint8_t rc5_get_cmd(rc5_cmd_t *cmd) {
46
 
47
   
48
  *cmd = 0xff;
49
50
  
51
  if((PIND & _BV(PD0) == 0) {
52
      *cmd = RC5_CMD_VOLUME_DEC;
53
      return 0;
54
      
55
    }
56
  else if((PIND & _BV(PD1) == 0) {
57
      *cmd = RC5_CMD_UNKNOWN1;
58
      return 0;
59
  }
60
  else if((PIND & _BV(PD3) == 0) {
61
      *cmd = RC5_CMD_VOLUME_INK;
62
      return 0;
63
  }
64
  else if((PIND & _BV(PD4) ==0)  {
65
      *cmd = RC5_CMD_UNKNOWN2;
66
      return 0;
67
  }
68
  else if((PIND & _BV(PD5) == 0) {
69
      *cmd = RC5_CMD_PROGRAM_DEC;
70
      return 0;
71
  }
72
  else if((PIND & _BV(PD6) == 0) {
73
      *cmd = RC5_CMD_PROGRAM_INC;
74
      return 0;
75
  }
76
  else if((PINB & _BV(PB0) == 0) {
77
      *cmd = RC5_CMD_UNKNOWN3;
78
      return 0;
79
  }
80
  else if((PINB & _BV(PB1) == 0)  {
81
      *cmd = RC5_CMD_UNKNOWN4;
82
      return 0;
83
  }
84
      
85
  
86
  
87
  return 1;
88
  }