Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/Digipot.h@3:f151d08d335c, 2014-03-02 (annotated)
- Committer:
- Bigcheese
- Date:
- Sun Mar 02 06:33:08 2014 +0000
- Revision:
- 3:f151d08d335c
- Parent:
- 0:31e91bb0ef3c
Bunch of stuff. Need to locally merge in updated USB changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | #ifndef DIGIPOT_H |
scachat | 0:31e91bb0ef3c | 2 | #define DIGIPOT_H |
scachat | 0:31e91bb0ef3c | 3 | |
scachat | 0:31e91bb0ef3c | 4 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 5 | #include "I2C.h" // mbed.h lib |
scachat | 0:31e91bb0ef3c | 6 | #include "libs/utils.h" |
scachat | 0:31e91bb0ef3c | 7 | #include <string> |
scachat | 0:31e91bb0ef3c | 8 | #include <math.h> |
scachat | 0:31e91bb0ef3c | 9 | |
scachat | 0:31e91bb0ef3c | 10 | class Digipot{ |
scachat | 0:31e91bb0ef3c | 11 | public: |
scachat | 0:31e91bb0ef3c | 12 | Digipot(){ } |
scachat | 0:31e91bb0ef3c | 13 | |
scachat | 0:31e91bb0ef3c | 14 | char current_to_wiper( double current ){ |
scachat | 0:31e91bb0ef3c | 15 | return char(ceil(double((113.33*current)))); |
scachat | 0:31e91bb0ef3c | 16 | } |
scachat | 0:31e91bb0ef3c | 17 | |
scachat | 0:31e91bb0ef3c | 18 | void i2c_send( char first, char second, char third ){ |
scachat | 0:31e91bb0ef3c | 19 | this->i2c->start(); |
scachat | 0:31e91bb0ef3c | 20 | this->i2c->write(first); |
scachat | 0:31e91bb0ef3c | 21 | this->i2c->write(second); |
scachat | 0:31e91bb0ef3c | 22 | this->i2c->write(third); |
scachat | 0:31e91bb0ef3c | 23 | this->i2c->stop(); |
scachat | 0:31e91bb0ef3c | 24 | } |
scachat | 0:31e91bb0ef3c | 25 | |
scachat | 0:31e91bb0ef3c | 26 | void set_current( int channel, double current ){ |
scachat | 0:31e91bb0ef3c | 27 | |
scachat | 0:31e91bb0ef3c | 28 | current = min( max( current, 0.0L ), 2.0L ); |
scachat | 0:31e91bb0ef3c | 29 | |
scachat | 0:31e91bb0ef3c | 30 | // I2C com |
scachat | 0:31e91bb0ef3c | 31 | this->i2c = new mbed::I2C(p9, p10); |
scachat | 0:31e91bb0ef3c | 32 | |
scachat | 0:31e91bb0ef3c | 33 | // Initial setup |
scachat | 0:31e91bb0ef3c | 34 | this->i2c_send( 0x58, 0x40, 0xff ); |
scachat | 0:31e91bb0ef3c | 35 | this->i2c_send( 0x58, 0xA0, 0xff ); |
scachat | 0:31e91bb0ef3c | 36 | |
scachat | 0:31e91bb0ef3c | 37 | // Set actual wiper value |
scachat | 0:31e91bb0ef3c | 38 | char adresses[4] = { 0x00, 0x10, 0x60, 0x70 }; |
scachat | 0:31e91bb0ef3c | 39 | this->i2c_send( 0x58, adresses[channel], this->current_to_wiper(current) ); |
scachat | 0:31e91bb0ef3c | 40 | |
scachat | 0:31e91bb0ef3c | 41 | } |
scachat | 0:31e91bb0ef3c | 42 | |
scachat | 0:31e91bb0ef3c | 43 | mbed::I2C* i2c; |
scachat | 0:31e91bb0ef3c | 44 | }; |
scachat | 0:31e91bb0ef3c | 45 | |
scachat | 0:31e91bb0ef3c | 46 | |
scachat | 0:31e91bb0ef3c | 47 | #endif |