Cleanup of code style

This commit is contained in:
Scott Lahteine 2016-10-07 15:57:24 -05:00
parent 13c9dcc600
commit a1b50f1102
4 changed files with 25 additions and 31 deletions

View File

@ -34,7 +34,7 @@
#if ENABLED(DAC_STEPPER_CURRENT) #if ENABLED(DAC_STEPPER_CURRENT)
uint16_t mcp4728_values[XYZE]; uint16_t mcp4728_values[XYZE];
/** /**
* Begin I2C, get current values (input register and eeprom) of mcp4728 * Begin I2C, get current values (input register and eeprom) of mcp4728
@ -42,16 +42,13 @@ uint16_t mcp4728_values[XYZE];
void mcp4728_init() { void mcp4728_init() {
Wire.begin(); Wire.begin();
Wire.requestFrom(int(DAC_DEV_ADDRESS), 24); Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
while(Wire.available()) { while (Wire.available()) {
int deviceID = Wire.read(); char deviceID = Wire.read(),
int hiByte = Wire.read(); hiByte = Wire.read(),
int loByte = Wire.read(); loByte = Wire.read();
int isEEPROM = (deviceID & 0B00001000) >> 3; if (!(deviceID & 0x08))
int channel = (deviceID & 0B00110000) >> 4; mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
if (isEEPROM != 1) {
mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
}
} }
} }
@ -71,8 +68,8 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
uint8_t mcp4728_eepromWrite() { uint8_t mcp4728_eepromWrite() {
Wire.beginTransmission(DAC_DEV_ADDRESS); Wire.beginTransmission(DAC_DEV_ADDRESS);
Wire.write(SEQWRITE); Wire.write(SEQWRITE);
for (uint8_t channel=0; channel <= 3; channel++) { for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel])); Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
Wire.write(lowByte(mcp4728_values[channel])); Wire.write(lowByte(mcp4728_values[channel]));
} }
return Wire.endTransmission(); return Wire.endTransmission();
@ -83,7 +80,7 @@ uint8_t mcp4728_eepromWrite() {
*/ */
uint8_t mcp4728_setVref_all(uint8_t value) { uint8_t mcp4728_setVref_all(uint8_t value) {
Wire.beginTransmission(DAC_DEV_ADDRESS); Wire.beginTransmission(DAC_DEV_ADDRESS);
Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value); Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
return Wire.endTransmission(); return Wire.endTransmission();
} }
/** /**
@ -91,7 +88,7 @@ uint8_t mcp4728_setVref_all(uint8_t value) {
*/ */
uint8_t mcp4728_setGain_all(uint8_t value) { uint8_t mcp4728_setGain_all(uint8_t value) {
Wire.beginTransmission(DAC_DEV_ADDRESS); Wire.beginTransmission(DAC_DEV_ADDRESS);
Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value); Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
return Wire.endTransmission(); return Wire.endTransmission();
} }
@ -105,21 +102,19 @@ uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
* Return Vout * Return Vout
* *
uint16_t mcp4728_getVout(uint8_t channel) { uint16_t mcp4728_getVout(uint8_t channel) {
uint32_t vref = 2048; uint32_t vref = 2048,
uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096; vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
if (vOut > defaultVDD) vOut = defaultVDD; if (vOut > defaultVDD) vOut = defaultVDD;
return vOut; return vOut;
} }
*/ */
/* Returns DAC values as a 0-100 percentage of drive strength */ /* Returns DAC values as a 0-100 percentage of drive strength */
uint16_t mcp4728_getDrvPct(uint8_t channel) {return (uint16_t)(.5+(((float)mcp4728_values[channel]*100)/DAC_STEPPER_MAX));} uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
/* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */ /* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
void mcp4728_setDrvPct(int16_t pct[XYZE]) { void mcp4728_setDrvPct(int16_t pct[XYZE]) {
for (uint8_t i=0; i <= 3; i++) { LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
mcp4728_values[i] = ((float)pct[i] * DAC_STEPPER_MAX)/100;
}
mcp4728_fastWrite(); mcp4728_fastWrite();
} }
@ -130,7 +125,7 @@ void mcp4728_setDrvPct(int16_t pct[XYZE]) {
*/ */
uint8_t mcp4728_fastWrite() { uint8_t mcp4728_fastWrite() {
Wire.beginTransmission(DAC_DEV_ADDRESS); Wire.beginTransmission(DAC_DEV_ADDRESS);
for (uint8_t channel=0; channel <= 3; channel++) { for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
Wire.write(highByte(mcp4728_values[channel])); Wire.write(highByte(mcp4728_values[channel]));
Wire.write(lowByte(mcp4728_values[channel])); Wire.write(lowByte(mcp4728_values[channel]));
} }

View File

@ -73,7 +73,7 @@
NOMORE(val, 100); NOMORE(val, 100);
mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100); mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
mcp4728_simpleCommand(UPDATE); mcp4728_simpleCommand(UPDATE);
} }
@ -86,10 +86,10 @@
mcp4728_simpleCommand(UPDATE); mcp4728_simpleCommand(UPDATE);
} }
static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; } static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * (1.0 / (DAC_STEPPER_MAX)); }
static float dac_amps(int8_t n) { return (mcp4728_getDrvPct(dac_order[n])*DAC_STEPPER_MAX) / (8.0 * DAC_STEPPER_SENSE); } static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * (0.125 * (DAC_STEPPER_SENSE)); }
int16_t dac_current_get_percent(int8_t axis) {return mcp4728_getDrvPct(dac_order[axis]); } int16_t dac_current_get_percent(AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
void dac_current_set_percents(int16_t pct[XYZE]) { void dac_current_set_percents(int16_t pct[XYZE]) {
LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]]; LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
mcp4728_setDrvPct(dac_channel_pct); mcp4728_setDrvPct(dac_channel_pct);

View File

@ -51,7 +51,7 @@ void dac_current_percent(uint8_t channel, float val);
void dac_current_raw(uint8_t channel, uint16_t val); void dac_current_raw(uint8_t channel, uint16_t val);
void dac_print_values(); void dac_print_values();
void dac_commit_eeprom(); void dac_commit_eeprom();
int16_t dac_current_get_percent(int8_t axis) ; int16_t dac_current_get_percent(AxisEnum axis);
void dac_current_set_percents(int16_t pct[XYZE]); void dac_current_set_percents(int16_t pct[XYZE]);
#endif // STEPPER_DAC_H #endif // STEPPER_DAC_H

View File

@ -864,12 +864,12 @@ void kill_screen(const char* lcd_msg) {
* *
*/ */
#if ENABLED(DAC_STEPPER_CURRENT) #if ENABLED(DAC_STEPPER_CURRENT)
static void dac_driver_getValues() {LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent(i); } static void dac_driver_getValues() { LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i); }
static void dac_driver_commit() { dac_current_set_percents(driverPercent); } static void dac_driver_commit() { dac_current_set_percents(driverPercent); }
static void dac_driver_eeprom_write() { dac_commit_eeprom(); } static void dac_driver_eeprom_write() { dac_commit_eeprom(); }
static void lcd_dac_menu() { static void lcd_dac_menu() {
dac_driver_getValues(); dac_driver_getValues();
START_MENU(); START_MENU();
@ -882,7 +882,6 @@ void kill_screen(const char* lcd_msg) {
END_MENU(); END_MENU();
} }
#endif #endif
/** /**
* *