Commit | Line | Data |
---|---|---|
6de40109 JM |
1 | #ifndef AD5206_H |
2 | #define AD5206_H | |
3 | ||
4 | #include "libs/Kernel.h" | |
5 | #include "libs/utils.h" | |
a6dad2b1 JM |
6 | #include <libs/Pin.h> |
7 | #include "mbed.h" | |
6de40109 JM |
8 | #include <string> |
9 | #include <math.h> | |
10 | ||
41fd89e0 JM |
11 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
12 | ||
6de40109 JM |
13 | class AD5206 : public DigipotBase { |
14 | public: | |
15 | AD5206(){ | |
f1fb4fa4 JM |
16 | this->spi= new mbed::SPI(P0_9,P0_8,P0_7); //should be able to set those pins in config |
17 | cs.from_string("4.29")->as_output(); //this also should be configurable | |
18 | cs.set(1); | |
2b08b195 | 19 | for (int i = 0; i < 6; i++) currents[i] = -1; |
6de40109 JM |
20 | } |
21 | ||
1ad23cd3 | 22 | void set_current( int channel, float current ) |
6de40109 | 23 | { |
ed1ae674 | 24 | if(channel<6){ |
2b08b195 JM |
25 | if(current < 0) { |
26 | currents[channel]= -1; | |
27 | return; | |
28 | } | |
ed1ae674 JM |
29 | current = min( max( current, 0.0L ), 2.0L ); |
30 | char adresses[6] = { 0x05, 0x03, 0x01, 0x00, 0x02, 0x04 }; | |
31 | currents[channel] = current; | |
32 | cs.set(0); | |
33 | spi->write((int)adresses[channel]); | |
34 | spi->write((int)current_to_wiper(current)); | |
35 | cs.set(1); | |
36 | } | |
6de40109 JM |
37 | } |
38 | ||
a6dad2b1 JM |
39 | |
40 | //taken from 4pi firmware | |
1ad23cd3 | 41 | unsigned char current_to_wiper( float current ){ |
f1fb4fa4 | 42 | unsigned int count = int((current*1000)*100/743); //6.8k resistor and 10k pot |
a6dad2b1 | 43 | |
f1fb4fa4 JM |
44 | return (unsigned char)count; |
45 | } | |
a6dad2b1 | 46 | |
1ad23cd3 | 47 | float get_current(int channel) |
6de40109 | 48 | { |
2b08b195 JM |
49 | if(channel < 6) |
50 | return currents[channel]; | |
51 | return -1; | |
6de40109 JM |
52 | } |
53 | ||
f1fb4fa4 | 54 | private: |
6de40109 | 55 | |
a6dad2b1 JM |
56 | Pin cs; |
57 | mbed::SPI* spi; | |
1ad23cd3 | 58 | float currents[6]; |
6de40109 JM |
59 | }; |
60 | ||
61 | ||
62 | #endif |