update firmware.bin
[clinton/Smoothieware.git] / src / modules / utils / currentcontrol / ad5206.h
1 #ifndef AD5206_H
2 #define AD5206_H
3
4 #include "libs/Kernel.h"
5 #include "libs/utils.h"
6 #include <libs/Pin.h>
7 #include "mbed.h"
8 #include <string>
9 #include <math.h>
10
11 #define max(a,b) (((a) > (b)) ? (a) : (b))
12
13 class AD5206 : public DigipotBase {
14 public:
15 AD5206(){
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);
19 for (int i = 0; i < 6; i++) currents[i] = -1;
20 }
21
22 void set_current( int channel, float current )
23 {
24 if(channel<6){
25 if(current < 0) {
26 currents[channel]= -1;
27 return;
28 }
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 }
37 }
38
39
40 //taken from 4pi firmware
41 unsigned char current_to_wiper( float current ){
42 unsigned int count = int((current*1000)*100/743); //6.8k resistor and 10k pot
43
44 return (unsigned char)count;
45 }
46
47 float get_current(int channel)
48 {
49 if(channel < 6)
50 return currents[channel];
51 return -1;
52 }
53
54 private:
55
56 Pin cs;
57 mbed::SPI* spi;
58 float currents[6];
59 };
60
61
62 #endif