Use static kernel singleton pointer instead of per-class instance pointer
[clinton/Smoothieware.git] / src / modules / utils / currentcontrol / CurrentControl.cpp
CommitLineData
0e8b102e
AW
1#include "libs/Kernel.h"
2#include "CurrentControl.h"
3#include "libs/nuts_bolts.h"
4#include "libs/utils.h"
13a520c1
MM
5
6#include "Gcode.h"
7
6de40109
JM
8// add new digipot chips here
9#include "mcp4451.h"
10#include "ad5206.h"
11
0e8b102e
AW
12#include <string>
13using namespace std;
14
f86783ab 15CurrentControl::CurrentControl(){
f1fb4fa4 16 digipot= NULL;
f86783ab 17}
0e8b102e
AW
18
19void CurrentControl::on_module_loaded(){
314ab8f7 20 if( !THEKERNEL->config->value( currentcontrol_module_enable_checksum )->by_default(false)->as_bool() ){
f1fb4fa4
JM
21 // as this module is not needed free up the resource
22 delete this;
23 return;
24 }
25
26 // allocate digipot, if already allocated delete it first
27 delete digipot;
28
29 // see which chip to use
314ab8f7 30 int chip_checksum = get_checksum(THEKERNEL->config->value(digipotchip_checksum)->by_default("mcp4451")->as_string());
f1fb4fa4
JM
31 if(chip_checksum == mcp4451_checksum) {
32 digipot = new MCP4451();
33 }else if(chip_checksum == ad5206_checksum) {
34 digipot = new AD5206();
35 }else { // need a default so use smoothie
36 digipot = new MCP4451();
37 }
0e8b102e
AW
38
39 // Get configuration
314ab8f7
MM
40 this->alpha_current = THEKERNEL->config->value(alpha_current_checksum )->by_default(0.8)->as_number();
41 this->beta_current = THEKERNEL->config->value(beta_current_checksum )->by_default(0.8)->as_number();
42 this->gamma_current = THEKERNEL->config->value(gamma_current_checksum )->by_default(0.8)->as_number();
43 this->delta_current = THEKERNEL->config->value(delta_current_checksum )->by_default(0.8)->as_number();
44 this->epsilon_current = THEKERNEL->config->value(epsilon_current_checksum)->by_default(-1)->as_number();
45 this->zeta_current = THEKERNEL->config->value(zeta_current_checksum )->by_default(-1)->as_number();
46 this->eta_current = THEKERNEL->config->value(eta_current_checksum )->by_default(-1)->as_number();
47 this->theta_current = THEKERNEL->config->value(theta_current_checksum )->by_default(-1)->as_number();
48
49 digipot->set_max_current( THEKERNEL->config->value(digipot_max_current )->by_default(2.0)->as_number());
50 digipot->set_factor( THEKERNEL->config->value(digipot_factor )->by_default(113.33)->as_number());
e9efb3d1 51
f86783ab
JM
52 this->digipot->set_current(0, this->alpha_current);
53 this->digipot->set_current(1, this->beta_current );
54 this->digipot->set_current(2, this->gamma_current);
55 this->digipot->set_current(3, this->delta_current);
5110b07d
L
56 if(this->epsilon_current >= 0){
57 this->digipot->set_current(4, this->epsilon_current);
58 this->digipot->set_current(5, this->zeta_current );
59 this->digipot->set_current(6, this->eta_current);
60 this->digipot->set_current(7, this->theta_current);
61 }
0e8b102e 62
13a520c1 63 this->register_for_event(ON_GCODE_RECEIVED);
0e8b102e
AW
64}
65
66
13a520c1
MM
67void CurrentControl::on_gcode_received(void *argument)
68{
69 Gcode *gcode = static_cast<Gcode*>(argument);
5110b07d 70 char alpha[8] = { 'X', 'Y', 'Z', 'E', 'A', 'B', 'C', 'D' };
13a520c1
MM
71 if (gcode->has_m)
72 {
73 if (gcode->m == 907)
74 {
75 int i;
5110b07d 76 for (i = 0; i < 8; i++)
13a520c1
MM
77 {
78 if (gcode->has_letter(alpha[i]))
f86783ab 79 this->digipot->set_current(i, gcode->get_value(alpha[i]));
5110b07d 80 gcode->stream->printf("%c:%3.1fA%c", alpha[i], this->digipot->get_current(i), (i == 7)?'\n':' ');
13a520c1
MM
81 }
82 }
83 }
84}