diff options
Diffstat (limited to 'keyboards/fc660c/actuation_point.c')
-rw-r--r-- | keyboards/fc660c/actuation_point.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/keyboards/fc660c/actuation_point.c b/keyboards/fc660c/actuation_point.c new file mode 100644 index 000000000..5f4cb16f4 --- /dev/null +++ b/keyboards/fc660c/actuation_point.c | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | Copyright 2017 Balz Guenat | ||
3 | based on work by Jun Wako <wakojun@gmail.com> | ||
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 2 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 | #include "actuation_point.h" | ||
20 | #include "i2c.h" | ||
21 | |||
22 | /////////////////////////////////////////////////////////////////////////////// | ||
23 | // | ||
24 | // AD5258 I2C digital potentiometer | ||
25 | // http://www.analog.com/media/en/technical-documentation/data-sheets/AD5258.pdf | ||
26 | // | ||
27 | #define AD5258_ADDR 0b0011000 | ||
28 | #define AD5258_INST_RDAC 0x00 | ||
29 | #define AD5258_INST_EEPROM 0x20 | ||
30 | |||
31 | uint8_t read_rdac(void) { | ||
32 | // read RDAC register | ||
33 | i2c_start_write(AD5258_ADDR); | ||
34 | i2c_master_write(AD5258_INST_RDAC); | ||
35 | i2c_start_read(AD5258_ADDR); | ||
36 | uint8_t ret = i2c_master_read(I2C_NACK); | ||
37 | i2c_master_stop(); | ||
38 | return ret; | ||
39 | }; | ||
40 | |||
41 | uint8_t read_eeprom(void) { | ||
42 | i2c_start_write(AD5258_ADDR); | ||
43 | i2c_master_write(AD5258_INST_EEPROM); | ||
44 | i2c_start_read(AD5258_ADDR); | ||
45 | uint8_t ret = i2c_master_read(I2C_NACK); | ||
46 | i2c_master_stop(); | ||
47 | return ret; | ||
48 | }; | ||
49 | |||
50 | void write_rdac(uint8_t rdac) { | ||
51 | // write RDAC register: | ||
52 | i2c_start_write(AD5258_ADDR); | ||
53 | i2c_master_write(AD5258_INST_RDAC); | ||
54 | i2c_master_write(rdac & 0x3F); | ||
55 | i2c_master_stop(); | ||
56 | }; | ||
57 | |||
58 | void actuation_point_up(void) { | ||
59 | // write RDAC register: lower value makes actuation point shallow | ||
60 | uint8_t rdac = read_rdac(); | ||
61 | if (rdac == 0) | ||
62 | write_rdac(0); | ||
63 | else | ||
64 | write_rdac(rdac-1); | ||
65 | }; | ||
66 | |||
67 | void actuation_point_down(void) { | ||
68 | // write RDAC register: higher value makes actuation point deep | ||
69 | uint8_t rdac = read_rdac(); | ||
70 | if (rdac == 63) | ||
71 | write_rdac(63); | ||
72 | else | ||
73 | write_rdac(rdac+1); | ||
74 | }; | ||
75 | |||
76 | void adjust_actuation_point(int offset) { | ||
77 | i2c_master_init(); | ||
78 | uint8_t rdac = read_eeprom() + offset; | ||
79 | if (rdac > 63) { // protects from under and overflows | ||
80 | if (offset > 0) | ||
81 | write_rdac(63); | ||
82 | else | ||
83 | write_rdac(0); | ||
84 | } else { | ||
85 | write_rdac(rdac); | ||
86 | } | ||
87 | } | ||