浏览代码

Cleanup of code style

Scott Lahteine 8 年前
父节点
当前提交
a1b50f1102
共有 4 个文件被更改,包括 25 次插入31 次删除
  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 查看文件

@@ -34,7 +34,7 @@
34 34
 
35 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 40
  * Begin I2C, get current values (input register and eeprom) of mcp4728
@@ -42,16 +42,13 @@ uint16_t     mcp4728_values[XYZE];
42 42
 void mcp4728_init() {
43 43
   Wire.begin();
44 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,8 +68,8 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
71 68
 uint8_t mcp4728_eepromWrite() {
72 69
   Wire.beginTransmission(DAC_DEV_ADDRESS);
73 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 73
     Wire.write(lowByte(mcp4728_values[channel]));
77 74
   }
78 75
   return Wire.endTransmission();
@@ -83,7 +80,7 @@ uint8_t mcp4728_eepromWrite() {
83 80
  */
84 81
 uint8_t mcp4728_setVref_all(uint8_t value) {
85 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 84
   return Wire.endTransmission();
88 85
 }
89 86
 /**
@@ -91,7 +88,7 @@ uint8_t mcp4728_setVref_all(uint8_t value) {
91 88
  */
92 89
 uint8_t mcp4728_setGain_all(uint8_t value) {
93 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 92
   return Wire.endTransmission();
96 93
 }
97 94
 
@@ -105,21 +102,19 @@ uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
105 102
  * Return Vout
106 103
  *
107 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 107
   if (vOut > defaultVDD) vOut = defaultVDD;
111 108
   return vOut;
112 109
 }
113 110
 */
114 111
 
115 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 115
 /* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
119 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 118
   mcp4728_fastWrite();
124 119
 }
125 120
 
@@ -130,7 +125,7 @@ void mcp4728_setDrvPct(int16_t pct[XYZE]) {
130 125
  */
131 126
 uint8_t mcp4728_fastWrite() {
132 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 129
     Wire.write(highByte(mcp4728_values[channel]));
135 130
     Wire.write(lowByte(mcp4728_values[channel]));
136 131
   }

+ 4
- 4
Marlin/stepper_dac.cpp 查看文件

@@ -73,7 +73,7 @@
73 73
 
74 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 77
     mcp4728_simpleCommand(UPDATE);
78 78
   }
79 79
 
@@ -86,10 +86,10 @@
86 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 93
   void dac_current_set_percents(int16_t pct[XYZE]) {
94 94
     LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
95 95
     mcp4728_setDrvPct(dac_channel_pct);

+ 1
- 1
Marlin/stepper_dac.h 查看文件

@@ -51,7 +51,7 @@ void dac_current_percent(uint8_t channel, float val);
51 51
 void dac_current_raw(uint8_t channel, uint16_t val);
52 52
 void dac_print_values();
53 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 55
 void dac_current_set_percents(int16_t pct[XYZE]);
56 56
 
57 57
 #endif // STEPPER_DAC_H

+ 4
- 5
Marlin/ultralcd.cpp 查看文件

@@ -864,12 +864,12 @@ void kill_screen(const char* lcd_msg) {
864 864
    *
865 865
    */
866 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 869
     static void dac_driver_commit() { dac_current_set_percents(driverPercent); }
870
-  
870
+
871 871
     static void dac_driver_eeprom_write() { dac_commit_eeprom(); }
872
-    
872
+
873 873
     static void lcd_dac_menu() {
874 874
       dac_driver_getValues();
875 875
       START_MENU();    
@@ -882,7 +882,6 @@ void kill_screen(const char* lcd_msg) {
882 882
       END_MENU();
883 883
     }
884 884
   #endif
885
-  
886 885
 
887 886
   /**
888 887
    *

正在加载...
取消
保存