summaryrefslogtreecommitdiff
path: root/software/rhmixxx/keypad.c
blob: 7a58ad0d65d09bd41e3effa6b9f1dc11351a3863 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 *  rhmidi
 *
 *  Copyright (C) 2014 Christian Pointner <equinox@helsinki.at>
 *
 *  This file is part of rhmidi.
 *
 *  rhmidi 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 3 of the License, or
 *  any later version.
 *
 *  rhmidi 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 rhmidi. If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include <util/delay.h>

#include "keypad.h"
#include "eventqueue.h"

#define KEYPAD_PIN PINA
#define KEYPAD_PORT PORTA
#define KEYPAD_DDR DDRA
#define KEYPAD_NUM_COLS 4
#define KEYPAD_NUM_ROWS 4
#define KEYPAD_NUM_KEYS KEYPAD_NUM_COLS * KEYPAD_NUM_ROWS

#define KEYPAD_LP_CNT_MAX 200
static struct {
  uint8_t last_sent;
  int16_t lp_cnt;
} keypad_state[KEYPAD_NUM_KEYS];

void keypad_init(void)
{
  KEYPAD_DDR = 0x00;
  KEYPAD_PORT = 0x0F;
  uint8_t i;
  for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
    keypad_state[i].last_sent = 0;
    keypad_state[i].lp_cnt = 0;
  }

  DDRC = 0xFF;
  DDRB |= 0xF0;
  DDRD |= 0xF0;

  PORTC = 0x00;
  PORTB &= 0x0F;
  PORTD &= 0x0F;
}

void keypad_led_on(uint8_t led)
{
  switch(led) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7: PORTC |= (1 << led); break;
    case 8:
    case 9:
    case 10:
    case 11: PORTB |= (1 << (led - 8 + 4)); break;
    case 12:
    case 13:
    case 14:
    case 15: PORTD |= (1 << (led - 12 + 4)); break;
    case 127: PORTC = 0xFF; PORTB |= 0xF0; PORTD |= 0xF0; break;
  }
}

void keypad_led_off(uint8_t led)
{
  switch(led) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7: PORTC &= ~(1 << led); break;
    case 8:
    case 9:
    case 10:
    case 11: PORTB &= ~(1 << (led - 8 + 4)); break;
    case 12:
    case 13:
    case 14:
    case 15: PORTD &= ~(1 << (led - 12 + 4)); break;
    case 127: PORTC = 0x00; PORTB &= 0x0F; PORTD &= 0x0F; break;
  }
}

void keypad_led_toggle(uint8_t led)
{
  switch(led) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7: PORTC ^= (1 << led); break;
    case 8:
    case 9:
    case 10:
    case 11: PORTB ^= (1 << (led - 8 + 4)); break;
    case 12:
    case 13:
    case 14:
    case 15: PORTD ^= (1 << (led - 12 + 4)); break;
    case 127: PORTC ^= 0xFF; PORTB ^= 0xF0; PORTD ^= 0xF0; break;
  }
}

static void keypad_key_lowpass(uint8_t key_idx, uint8_t current_state)
{
  keypad_state[key_idx].lp_cnt += current_state ? -1 : +1;
  if(keypad_state[key_idx].lp_cnt <= 0 ||
     keypad_state[key_idx].lp_cnt >= KEYPAD_LP_CNT_MAX) {

    keypad_state[key_idx].lp_cnt = keypad_state[key_idx].lp_cnt <= 0 ? 0 : KEYPAD_LP_CNT_MAX;

    if(current_state != keypad_state[key_idx].last_sent) {
      keypad_state[key_idx].last_sent = current_state;

      if(current_state) {
        eventqueue_push(key_idx, 0);
      } else {
        eventqueue_push(key_idx, 1);
      }
    }
  }
}

void keypad_task(void)
{
  uint8_t col, row;
  for(col = 0; col < KEYPAD_NUM_COLS; ++col) {
    KEYPAD_DDR = 1 << (col + 4);
    KEYPAD_PORT = 0x0F;
    _delay_us(10);

    for(row = 0; row < KEYPAD_NUM_ROWS; ++row) {
      uint8_t key_idx = col*KEYPAD_NUM_ROWS + row;

      uint8_t current_state = KEYPAD_PIN & (1 << row);
      keypad_key_lowpass(key_idx, current_state);
    }
  }
  KEYPAD_DDR = 0x00;
}