Kaleidoscope
AtmegaScanner.h
Go to the documentation of this file.
1 /* -*- mode: c++ -*-
2  * AtmegaScanner -- Atmega-based keyboard scanner for Kaleidoscope
3  * Copyright (C) 2017 Gergely Nagy
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <Arduino.h>
22 
23 template<uint8_t cols_, uint8_t rows_>
25  public:
26  typedef struct {
27  uint32_t all;
28  } keydata_t;
29 
30  AtmegaScanner(void);
31 
32  bool readKeys(void);
33 
38 
39  private:
40  void initCols(void);
41  void unselectRows(void);
42  void selectRow(uint8_t row);
43  uint16_t readCols(void);
44 };
45 
46 template<uint8_t cols_, uint8_t rows_>
48  DDRD &= ~0b00110000;
49  PORTD &= ~0b00110000;
50 
51  DDRE &= ~0b01000100;
52  PORTE &= ~0b01000100;
53 }
54 
55 template<uint8_t cols_, uint8_t rows_>
57  DDRB &= ~(1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
58  PORTB |= (1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
59  DDRC &= ~(1<<7 | 1<<6);
60  PORTC |= (1<<7 | 1<<6);
61  DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
62  PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
63  DDRF &= ~(1<<6 | 1<<7);
64  PORTF |= (1<<6 | 1<<7);
65 }
66 
67 template<uint8_t cols_, uint8_t rows_>
69  switch (row) {
70  case 0:
71  DDRD |= (1<<4);
72  PORTD &= ~(1<<4);
73  break;
74  case 1:
75  DDRD |= (1<<5);
76  PORTD &= ~(1<<5);
77  break;
78  case 2:
79  DDRE |= (1<<2);
80  PORTE &= ~(1<<2);
81  break;
82  case 3:
83  DDRE |= (1<<6);
84  PORTE &= ~(1<<6);
85  break;
86  default:
87  break;
88  }
89 }
90 
91 template<uint8_t cols_, uint8_t rows_>
93  return (PINF&(1<<6) ? 0 : (1<<0)) |
94  (PINF&(1<<7) ? 0 : (1<<1)) |
95  (PINB&(1<<4) ? 0 : (1<<2)) |
96  (PINB&(1<<5) ? 0 : (1<<3)) |
97  (PINB&(1<<6) ? 0 : (1<<4)) |
98  (PIND&(1<<7) ? 0 : (1<<5)) |
99  (PINC&(1<<6) ? 0 : (1<<6)) |
100  (PINC&(1<<7) ? 0 : (1<<7)) |
101  (PIND&(1<<3) ? 0 : (1<<8)) | // Rev.A and B
102  (PIND&(1<<2) ? 0 : (1<<9)) |
103  (PIND&(1<<1) ? 0 : (1<<10)) |
104  (PIND&(1<<0) ? 0 : (1<<11)) |
105  (PINB&(1<<7) ? 0 : (1<<12)) |
106  (PINB&(1<<3) ? 0 : (1<<13));
107 }
108 
109 /* ------------------------------------ */
110 
111 template<uint8_t cols_, uint8_t rows_>
113  leftHandState.all = 0;
114  rightHandState.all = 0;
117 
118  unselectRows();
119  initCols();
120 }
121 
122 template<uint8_t cols_, uint8_t rows_>
126 
127  for (uint8_t i = 0; i < rows_; i++) {
128  selectRow(i);
129  uint16_t cols = readCols();
130 
131  for (uint8_t c = 0; c < cols_ / 2; c++) {
132  uint8_t rowPos = i * (cols_ / 2);
133  bitWrite(leftHandState.all, rowPos + c, bitRead(cols, c));
134  bitWrite(rightHandState.all, rowPos + ((cols_ / 2 - 1) - c), bitRead(cols, (cols_ / 2) + c));
135  }
136 
137  unselectRows();
138  }
139 
140  return true;
141 }
Definition: KeyboardioScanner.h:36
Definition: AtmegaScanner.h:26
Definition: AtmegaScanner.h:24
keydata_t leftHandState
Definition: AtmegaScanner.h:34
keydata_t rightHandState
Definition: AtmegaScanner.h:35
uint32_t all
Definition: AtmegaScanner.h:27
keydata_t previousRightHandState
Definition: AtmegaScanner.h:37
keydata_t previousLeftHandState
Definition: AtmegaScanner.h:36
bool readKeys(void)
Definition: AtmegaScanner.h:123
byte row
Definition: TapDance.cpp:229
AtmegaScanner(void)
Definition: AtmegaScanner.h:112