Commit | Line | Data |
---|---|---|
6de40109 JM |
1 | #ifndef MCP4451_H |
2 | #define MCP4451_H | |
0e8b102e | 3 | |
0e8b102e | 4 | #include "libs/Kernel.h" |
da3a10b9 | 5 | #include "I2C.h" // mbed.h lib |
0e8b102e | 6 | #include "libs/utils.h" |
6de40109 | 7 | #include "DigipotBase.h" |
0e8b102e AW |
8 | #include <string> |
9 | #include <math.h> | |
10 | ||
6de40109 | 11 | class MCP4451 : public DigipotBase { |
0e8b102e | 12 | public: |
6de40109 | 13 | MCP4451(){ |
da57b276 MM |
14 | // I2C com |
15 | this->i2c = new mbed::I2C(p9, p10); | |
5110b07d | 16 | this->i2c->frequency(20000); |
2b08b195 | 17 | for (int i = 0; i < 8; i++) currents[i] = -1; |
f1fb4fa4 | 18 | } |
e9efb3d1 | 19 | |
f1fb4fa4 JM |
20 | ~MCP4451(){ |
21 | delete this->i2c; | |
22 | } | |
0e8b102e | 23 | |
1ad23cd3 | 24 | void set_current( int channel, float current ) |
da57b276 | 25 | { |
2b08b195 JM |
26 | if(current < 0) { |
27 | currents[channel]= -1; | |
28 | return; | |
29 | } | |
1ad23cd3 | 30 | current = min( (float) max( current, 0.0f ), this->max_current ); |
5110b07d L |
31 | currents[channel] = current; |
32 | char addr = 0x58; | |
33 | while(channel > 3){ | |
34 | addr += 0x02; | |
35 | channel -= 4; | |
36 | } | |
0e8b102e | 37 | |
df27a6a3 | 38 | // Initial setup |
5110b07d L |
39 | this->i2c_send( addr, 0x40, 0xff ); |
40 | this->i2c_send( addr, 0xA0, 0xff ); | |
0e8b102e AW |
41 | |
42 | // Set actual wiper value | |
5110b07d L |
43 | char addresses[4] = { 0x00, 0x10, 0x60, 0x70 }; |
44 | this->i2c_send( addr, addresses[channel], this->current_to_wiper(current) ); | |
da57b276 | 45 | } |
0e8b102e | 46 | |
1ad23cd3 | 47 | float get_current(int channel) |
da57b276 MM |
48 | { |
49 | return currents[channel]; | |
f1fb4fa4 | 50 | } |
e9efb3d1 | 51 | |
f1fb4fa4 JM |
52 | private: |
53 | ||
54 | void i2c_send( char first, char second, char third ){ | |
55 | this->i2c->start(); | |
56 | this->i2c->write(first); | |
57 | this->i2c->write(second); | |
58 | this->i2c->write(third); | |
59 | this->i2c->stop(); | |
60 | } | |
61 | ||
1ad23cd3 | 62 | char current_to_wiper( float current ){ |
9502f9d5 | 63 | return char(ceilf(float((this->factor*current)))); |
f1fb4fa4 JM |
64 | } |
65 | ||
66 | mbed::I2C* i2c; | |
1ad23cd3 | 67 | float currents[8]; |
0e8b102e AW |
68 | }; |
69 | ||
70 | ||
71 | #endif |