summaryrefslogtreecommitdiff
path: root/firmware/keypad.c
blob: 1291a6e24d2a14341055e4430b4a70ba58ad221f (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 *  spreadspace avr projects
 *
 *
 *  Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of spreadspace avr projects.
 *
 *  spreadspace avr projects 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.
 *
 *  spreadspace avr projects 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 spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
 */

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

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

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

static struct {
  uint16_t timebase;
  uint16_t cnt;
} led_blink_state[KEYPAD_NUM_KEYS];
#define KEYPAD_BLINK_TIMEBASE_MULT 128

void keypad_init(void)
{
      // KEYS are at F4..F7
  DDRF &= 0x0F;
  PORTF |= 0xF0;
  uint8_t i;
  for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
    keypad_state[i].last_sent = 0;
    keypad_state[i].lp_cnt = 0;

    led_blink_state[i].timebase = 0;
    led_blink_state[i].cnt = 0;
  }

      // LEDS are at D0..D3
  DDRD |= 0x0F;
  PORTD &= 0xFC;
  PORTD |= 0x0C;
}

void keypad_led_on(uint8_t led)
{
  switch(led) {
    case 0: PORTD |= 0x01; break;
    case 1: PORTD |= 0x02; break;
    case 2: PORTD &= 0xFB; break;
    case 3: PORTD &= 0xF7; break;
    case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD |= 0x03; PORTD &= 0xF3;; break;
  }
  keypad_led_blink(led, 0);
}

void keypad_led_off(uint8_t led)
{
  switch(led) {
    case 0: PORTD &= 0xFE; break;
    case 1: PORTD &= 0xFD; break;
    case 2: PORTD |= 0x04; break;
    case 3: PORTD |= 0x08; break;
    case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD &= 0xFC; PORTD |= 0x0C; break;
  }
  keypad_led_blink(led, 0);
}

void keypad_led_toggle(uint8_t led)
{
  switch(led) {
    case 0:
    case 1:
    case 2:
    case 3: PORTD ^= (1 << led); break;
    case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD ^= 0x0F; break;
  }
}

static uint8_t keypad_led_get_state(uint8_t led)
{
  switch(led) {
    case 0:
    case 1: return (PORTD & (1 << led)) ? 0 : 1;
    case 2:
    case 3: return (PORTD & (1 << led)) ? 1 : 0;
    default: return 0;
  }
}

static void keypad_led_blink_sync(uint8_t led)
{
  uint8_t i;
  for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
    if(i != led && led_blink_state[i].timebase == led_blink_state[led].timebase) {
      led_blink_state[led].cnt = led_blink_state[i].cnt;
      if(keypad_led_get_state(led) != keypad_led_get_state(i))
        keypad_led_toggle(led);
      break;
    }
  }
}

void keypad_led_blink(uint8_t led, uint8_t value)
{
  if(led < KEYPAD_NUM_KEYS) {
    led_blink_state[led].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
    led_blink_state[led].cnt = 0;
    if(value)
      keypad_led_blink_sync(led);

  } else {
    uint8_t i;
    for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
      led_blink_state[i].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
      led_blink_state[i].cnt = 0;
    }
  }
}

static inline 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;
      eventqueue_push(KEYPAD_MIDI_NOTE_OFFSET + key_idx, ((current_state) ? 0 : 1));
    }
  }
}

static inline void keypad_led_blinking(uint8_t key_idx)
{
  if(led_blink_state[key_idx].timebase > 1) {
    if(++led_blink_state[key_idx].cnt >= led_blink_state[key_idx].timebase) {
      keypad_led_toggle(key_idx);
      led_blink_state[key_idx].cnt = 0;
    }
  }
}

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(20);

    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);
      uint8_t current_state = PINF & (1 << (row+4));
      keypad_key_lowpass(key_idx, current_state);

      keypad_led_blinking(key_idx);
    }
  }
//  KEYPAD_DDR = 0x00;
}