Commit | Line | Data |
---|---|---|
0e8b102e AW |
1 | #ifndef DIGIPOT_H |
2 | #define DIGIPOT_H | |
3 | ||
0e8b102e | 4 | #include "libs/Kernel.h" |
da3a10b9 | 5 | #include "I2C.h" // mbed.h lib |
0e8b102e AW |
6 | #include "libs/utils.h" |
7 | #include <string> | |
8 | #include <math.h> | |
9 | ||
10 | class Digipot{ | |
11 | public: | |
da57b276 MM |
12 | Digipot(){ |
13 | // I2C com | |
14 | this->i2c = new mbed::I2C(p9, p10); | |
15 | for (int i = 0; i < 4; i++) | |
16 | currents[i] = 0.0; | |
17 | } | |
18 | ||
0e8b102e | 19 | char current_to_wiper( double current ){ |
df27a6a3 | 20 | return char(ceil(double((113.33*current)))); |
0e8b102e AW |
21 | } |
22 | ||
23 | void i2c_send( char first, char second, char third ){ | |
24 | this->i2c->start(); | |
25 | this->i2c->write(first); | |
26 | this->i2c->write(second); | |
27 | this->i2c->write(third); | |
df27a6a3 | 28 | this->i2c->stop(); |
0e8b102e AW |
29 | } |
30 | ||
da57b276 MM |
31 | void set_current( int channel, double current ) |
32 | { | |
0e8b102e AW |
33 | current = min( max( current, 0.0L ), 2.0L ); |
34 | ||
df27a6a3 MM |
35 | // Initial setup |
36 | this->i2c_send( 0x58, 0x40, 0xff ); | |
37 | this->i2c_send( 0x58, 0xA0, 0xff ); | |
0e8b102e AW |
38 | |
39 | // Set actual wiper value | |
df27a6a3 MM |
40 | char adresses[4] = { 0x00, 0x10, 0x60, 0x70 }; |
41 | this->i2c_send( 0x58, adresses[channel], this->current_to_wiper(current) ); | |
da57b276 MM |
42 | currents[channel] = current; |
43 | } | |
0e8b102e | 44 | |
da57b276 MM |
45 | double get_current(int channel) |
46 | { | |
47 | return currents[channel]; | |
df27a6a3 | 48 | } |
0e8b102e | 49 | |
da3a10b9 | 50 | mbed::I2C* i2c; |
da57b276 | 51 | double currents[4]; |
0e8b102e AW |
52 | }; |
53 | ||
54 | ||
55 | #endif |