4 #include "libs/Kernel.h"
5 #include "I2C.h" // mbed.h lib
6 #include "libs/utils.h"
7 #include "DigipotBase.h"
11 class MCP4451
: public DigipotBase
{
15 this->i2c
= new mbed::I2C(p9
, p10
);
16 this->i2c
->frequency(20000);
17 for (int i
= 0; i
< 8; i
++) currents
[i
] = -1;
24 void set_current( int channel
, float current
)
27 currents
[channel
]= -1;
30 current
= min( (float) max( current
, 0.0f
), this->max_current
);
31 currents
[channel
] = current
;
39 this->i2c_send( addr
, 0x40, 0xff );
40 this->i2c_send( addr
, 0xA0, 0xff );
42 // Set actual wiper value
43 char addresses
[4] = { 0x00, 0x10, 0x60, 0x70 };
44 this->i2c_send( addr
, addresses
[channel
], this->current_to_wiper(current
) );
47 float get_current(int channel
)
49 return currents
[channel
];
54 void i2c_send( char first
, char second
, char third
){
56 this->i2c
->write(first
);
57 this->i2c
->write(second
);
58 this->i2c
->write(third
);
62 char current_to_wiper( float current
){
63 return char(ceilf(float((this->factor
*current
))));