2 Copyright 2017 Balz Guenat
3 based on work by Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
19 #include "actuation_point.h"
22 ///////////////////////////////////////////////////////////////////////////////
24 // AD5258 I2C digital potentiometer
25 // http://www.analog.com/media/en/technical-documentation/data-sheets/AD5258.pdf
27 #define AD5258_ADDR 0b0011000
28 #define AD5258_INST_RDAC 0x00
29 #define AD5258_INST_EEPROM 0x20
31 uint8_t read_rdac(void) {
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);
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);
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);
58 void actuation_point_up(void) {
59 // write RDAC register: lower value makes actuation point shallow
60 uint8_t rdac = read_rdac();
67 void actuation_point_down(void) {
68 // write RDAC register: higher value makes actuation point deep
69 uint8_t rdac = read_rdac();
76 void adjust_actuation_point(int offset) {
78 uint8_t rdac = read_eeprom() + offset;
79 if (rdac > 63) { // protects from under and overflows