Browse Source

Cleanup of code style

Scott Lahteine 8 years ago
parent
commit
a1b50f1102
4 changed files with 25 additions and 31 deletions
  1. 16
    21
      Marlin/dac_mcp4728.cpp
  2. 4
    4
      Marlin/stepper_dac.cpp
  3. 1
    1
      Marlin/stepper_dac.h
  4. 4
    5
      Marlin/ultralcd.cpp

+ 16
- 21
Marlin/dac_mcp4728.cpp View File

34
 
34
 
35
 #if ENABLED(DAC_STEPPER_CURRENT)
35
 #if ENABLED(DAC_STEPPER_CURRENT)
36
 
36
 
37
-uint16_t     mcp4728_values[XYZE];
37
+uint16_t mcp4728_values[XYZE];
38
 
38
 
39
 /**
39
 /**
40
  * Begin I2C, get current values (input register and eeprom) of mcp4728
40
  * Begin I2C, get current values (input register and eeprom) of mcp4728
42
 void mcp4728_init() {
42
 void mcp4728_init() {
43
   Wire.begin();
43
   Wire.begin();
44
   Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
44
   Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
45
-  while(Wire.available()) {
46
-    int deviceID = Wire.read();
47
-    int hiByte = Wire.read();
48
-    int loByte = Wire.read();
45
+  while (Wire.available()) {
46
+    char deviceID = Wire.read(),
47
+         hiByte = Wire.read(),
48
+         loByte = Wire.read();
49
 
49
 
50
-    int isEEPROM = (deviceID & 0B00001000) >> 3;
51
-    int channel = (deviceID & 0B00110000) >> 4;
52
-    if (isEEPROM != 1) {
53
-      mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
54
-    }
50
+    if (!(deviceID & 0x08))
51
+      mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
55
   }
52
   }
56
 }
53
 }
57
 
54
 
71
 uint8_t mcp4728_eepromWrite() {
68
 uint8_t mcp4728_eepromWrite() {
72
   Wire.beginTransmission(DAC_DEV_ADDRESS);
69
   Wire.beginTransmission(DAC_DEV_ADDRESS);
73
   Wire.write(SEQWRITE);
70
   Wire.write(SEQWRITE);
74
-  for (uint8_t channel=0; channel <= 3; channel++) {
75
-    Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
71
+  for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
72
+    Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
76
     Wire.write(lowByte(mcp4728_values[channel]));
73
     Wire.write(lowByte(mcp4728_values[channel]));
77
   }
74
   }
78
   return Wire.endTransmission();
75
   return Wire.endTransmission();
83
  */
80
  */
84
 uint8_t mcp4728_setVref_all(uint8_t value) {
81
 uint8_t mcp4728_setVref_all(uint8_t value) {
85
   Wire.beginTransmission(DAC_DEV_ADDRESS);
82
   Wire.beginTransmission(DAC_DEV_ADDRESS);
86
-  Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
83
+  Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
87
   return Wire.endTransmission();
84
   return Wire.endTransmission();
88
 }
85
 }
89
 /**
86
 /**
91
  */
88
  */
92
 uint8_t mcp4728_setGain_all(uint8_t value) {
89
 uint8_t mcp4728_setGain_all(uint8_t value) {
93
   Wire.beginTransmission(DAC_DEV_ADDRESS);
90
   Wire.beginTransmission(DAC_DEV_ADDRESS);
94
-  Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
91
+  Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
95
   return Wire.endTransmission();
92
   return Wire.endTransmission();
96
 }
93
 }
97
 
94
 
105
  * Return Vout
102
  * Return Vout
106
  *
103
  *
107
 uint16_t mcp4728_getVout(uint8_t channel) {
104
 uint16_t mcp4728_getVout(uint8_t channel) {
108
-  uint32_t vref = 2048;
109
-  uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
105
+  uint32_t vref = 2048,
106
+           vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
110
   if (vOut > defaultVDD) vOut = defaultVDD;
107
   if (vOut > defaultVDD) vOut = defaultVDD;
111
   return vOut;
108
   return vOut;
112
 }
109
 }
113
 */
110
 */
114
 
111
 
115
 /* Returns DAC values as a 0-100 percentage of drive strength */
112
 /* Returns DAC values as a 0-100 percentage of drive strength */
116
-uint16_t mcp4728_getDrvPct(uint8_t channel) {return (uint16_t)(.5+(((float)mcp4728_values[channel]*100)/DAC_STEPPER_MAX));}
113
+uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
117
 
114
 
118
 /* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
115
 /* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
119
 void mcp4728_setDrvPct(int16_t pct[XYZE]) {
116
 void mcp4728_setDrvPct(int16_t pct[XYZE]) {
120
-  for (uint8_t i=0; i <= 3; i++) {
121
-    mcp4728_values[i] = ((float)pct[i] * DAC_STEPPER_MAX)/100;
122
-  }
117
+  LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
123
   mcp4728_fastWrite();
118
   mcp4728_fastWrite();
124
 }
119
 }
125
 
120
 
130
  */
125
  */
131
 uint8_t mcp4728_fastWrite() {
126
 uint8_t mcp4728_fastWrite() {
132
   Wire.beginTransmission(DAC_DEV_ADDRESS);
127
   Wire.beginTransmission(DAC_DEV_ADDRESS);
133
-  for (uint8_t channel=0; channel <= 3; channel++) {
128
+  for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
134
     Wire.write(highByte(mcp4728_values[channel]));
129
     Wire.write(highByte(mcp4728_values[channel]));
135
     Wire.write(lowByte(mcp4728_values[channel]));
130
     Wire.write(lowByte(mcp4728_values[channel]));
136
   }
131
   }

+ 4
- 4
Marlin/stepper_dac.cpp View File

73
 
73
 
74
     NOMORE(val, 100);
74
     NOMORE(val, 100);
75
 
75
 
76
-    mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
76
+    mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
77
     mcp4728_simpleCommand(UPDATE);
77
     mcp4728_simpleCommand(UPDATE);
78
   }
78
   }
79
 
79
 
86
     mcp4728_simpleCommand(UPDATE);
86
     mcp4728_simpleCommand(UPDATE);
87
   }
87
   }
88
 
88
 
89
-  static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
90
-  static float dac_amps(int8_t n) { return (mcp4728_getDrvPct(dac_order[n])*DAC_STEPPER_MAX) / (8.0 * DAC_STEPPER_SENSE); }
89
+  static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * (1.0 / (DAC_STEPPER_MAX)); }
90
+  static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * (0.125 * (DAC_STEPPER_SENSE)); }
91
   
91
   
92
-  int16_t dac_current_get_percent(int8_t axis) {return mcp4728_getDrvPct(dac_order[axis]); }
92
+  int16_t dac_current_get_percent(AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
93
   void dac_current_set_percents(int16_t pct[XYZE]) {
93
   void dac_current_set_percents(int16_t pct[XYZE]) {
94
     LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
94
     LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
95
     mcp4728_setDrvPct(dac_channel_pct);
95
     mcp4728_setDrvPct(dac_channel_pct);

+ 1
- 1
Marlin/stepper_dac.h View File

51
 void dac_current_raw(uint8_t channel, uint16_t val);
51
 void dac_current_raw(uint8_t channel, uint16_t val);
52
 void dac_print_values();
52
 void dac_print_values();
53
 void dac_commit_eeprom();
53
 void dac_commit_eeprom();
54
-int16_t dac_current_get_percent(int8_t axis) ;
54
+int16_t dac_current_get_percent(AxisEnum axis);
55
 void dac_current_set_percents(int16_t pct[XYZE]);
55
 void dac_current_set_percents(int16_t pct[XYZE]);
56
 
56
 
57
 #endif // STEPPER_DAC_H
57
 #endif // STEPPER_DAC_H

+ 4
- 5
Marlin/ultralcd.cpp View File

864
    *
864
    *
865
    */
865
    */
866
   #if ENABLED(DAC_STEPPER_CURRENT)
866
   #if ENABLED(DAC_STEPPER_CURRENT)
867
-    static void dac_driver_getValues() {LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent(i); } 
868
-  
867
+    static void dac_driver_getValues() { LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i); }
868
+
869
     static void dac_driver_commit() { dac_current_set_percents(driverPercent); }
869
     static void dac_driver_commit() { dac_current_set_percents(driverPercent); }
870
-  
870
+
871
     static void dac_driver_eeprom_write() { dac_commit_eeprom(); }
871
     static void dac_driver_eeprom_write() { dac_commit_eeprom(); }
872
-    
872
+
873
     static void lcd_dac_menu() {
873
     static void lcd_dac_menu() {
874
       dac_driver_getValues();
874
       dac_driver_getValues();
875
       START_MENU();    
875
       START_MENU();    
882
       END_MENU();
882
       END_MENU();
883
     }
883
     }
884
   #endif
884
   #endif
885
-  
886
 
885
 
887
   /**
886
   /**
888
    *
887
    *

Loading…
Cancel
Save