From b13099de3f98f00914bde588ee7ecc6524df37d1 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 2 Feb 2018 03:15:01 -0600 Subject: [PATCH] General cleanup of HAL code --- Marlin/src/HAL/HAL_LPC1768/Wire.cpp | 61 +++++++------------ .../HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp | 6 +- frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h | 6 +- frameworks/CMSIS/LPC1768/lib/Print.cpp | 52 ++++++++-------- 4 files changed, 53 insertions(+), 72 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/Wire.cpp b/Marlin/src/HAL/HAL_LPC1768/Wire.cpp index 2229062893..e3b97ccdb4 100644 --- a/Marlin/src/HAL/HAL_LPC1768/Wire.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/Wire.cpp @@ -75,7 +75,8 @@ void TwoWire::begin(void) { PINSEL_CFG_Type PinCfg; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; - #if ((USEDI2CDEV_M == 0)) + + #if USEDI2CDEV_M == 0 PinCfg.Funcnum = 1; PinCfg.Pinnum = 27; PinCfg.Portnum = 0; @@ -83,7 +84,8 @@ void TwoWire::begin(void) { PinCfg.Pinnum = 28; PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1 #endif - #if ((USEDI2CDEV_M == 1)) + + #if USEDI2CDEV_M == 1 PinCfg.Funcnum = 3; PinCfg.Pinnum = 0; PinCfg.Portnum = 0; @@ -91,7 +93,8 @@ void TwoWire::begin(void) { PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL #endif - #if ((USEDI2CDEV_M == 2)) + + #if USEDI2CDEV_M == 2 PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; @@ -102,17 +105,16 @@ void TwoWire::begin(void) { // Initialize I2C peripheral I2C_Init(I2CDEV_M, 100000); - + // Enable Master I2C operation I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); } uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { // clamp to buffer length - if(quantity > BUFFER_LENGTH){ + if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; - } - + // perform blocking read into buffer I2C_M_SETUP_Type transferMCfg; transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift @@ -158,7 +160,7 @@ uint8_t TwoWire::endTransmission(void) { transferMCfg.rx_length = 0; transferMCfg.retransmissions_max = 3; Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING); - + // reset tx buffer iterator vars txBufferIndex = 0; txBufferLength = 0; @@ -166,25 +168,19 @@ uint8_t TwoWire::endTransmission(void) { // indicate that we are done transmitting transmitting = 0; - if (status == SUCCESS) - return 0; // success - else - return 4; // other error + return status == SUCCESS ? 0 : 4; } // must be called after beginTransmission(address) size_t TwoWire::write(uint8_t data) { if (transmitting) { // don't bother if buffer is full - if (txBufferLength >= BUFFER_LENGTH) { - return 0; - } + if (txBufferLength >= BUFFER_LENGTH) return 0; // put byte in tx buffer - txBuffer[txBufferIndex] = data; - ++txBufferIndex; + txBuffer[txBufferIndex++] = data; - // update amount in buffer + // update amount in buffer txBufferLength = txBufferIndex; } @@ -195,40 +191,25 @@ size_t TwoWire::write(uint8_t data) { size_t TwoWire::write(const uint8_t *data, size_t quantity) { size_t sent = 0; if (transmitting) - for(sent = 0; sent < quantity; ++sent) - if (!write(data[sent])) - break; + for (sent = 0; sent < quantity; ++sent) + if (!write(data[sent])) break; return sent; } -// must be called after requestFrom(address, numBytes) +// Must be called after requestFrom(address, numBytes) int TwoWire::available(void) { return rxBufferLength - rxBufferIndex; } -// must be called after requestFrom(address, numBytes) +// Must be called after requestFrom(address, numBytes) int TwoWire::read(void) { - int value = -1; - - // get each successive byte on each call - if(rxBufferIndex < rxBufferLength) { - value = rxBuffer[rxBufferIndex]; - ++rxBufferIndex; - } - - return value; + return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1; } -// must be called after requestFrom(address, numBytes) +// Must be called after requestFrom(address, numBytes) int TwoWire::peek(void) { - int value = -1; - - if(rxBufferIndex < rxBufferLength){ - value = rxBuffer[rxBufferIndex]; - } - - return value; + return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex] : -1; } // Preinstantiate Objects ////////////////////////////////////////////////////// diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp index 3ec7c3148f..3d52302fb5 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_spi_Teensy.cpp @@ -91,12 +91,12 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) { SPI.beginTransaction(spiConfig); SPDR = token; for (uint16_t i = 0; i < 512; i += 2) { - while (!TEST(SPSR, SPIF)) { /* nada */ }; + while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i]; - while (!TEST(SPSR, SPIF)) { /* nada */ }; + while (!TEST(SPSR, SPIF)) { /* nada */ }; SPDR = buf[i + 1]; } - while (!TEST(SPSR, SPIF)) { /* nada */ }; + while (!TEST(SPSR, SPIF)) { /* nada */ }; SPI.endTransaction(); } diff --git a/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h b/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h index 57fe82fe8b..2373eeee3e 100644 --- a/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h +++ b/frameworks/CMSIS/LPC1768/lib/LiquidCrystal.h @@ -60,7 +60,7 @@ public: void init(uint8_t fourbitmode, pin_t rs, pin_t rw, pin_t enable, pin_t d0, pin_t d1, pin_t d2, pin_t d3, pin_t d4, pin_t d5, pin_t d6, pin_t d7); - + void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); void clear(); @@ -81,10 +81,10 @@ public: void setRowOffsets(int row1, int row2, int row3, int row4); void createChar(uint8_t, uint8_t[]); - void setCursor(uint8_t, uint8_t); + void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); - + using Print::write; private: void send(uint8_t, uint8_t); diff --git a/frameworks/CMSIS/LPC1768/lib/Print.cpp b/frameworks/CMSIS/LPC1768/lib/Print.cpp index 9219d0515b..92c3eb3a4e 100644 --- a/frameworks/CMSIS/LPC1768/lib/Print.cpp +++ b/frameworks/CMSIS/LPC1768/lib/Print.cpp @@ -1,21 +1,21 @@ /* Print.cpp - Base class that provides print() and println() Copyright (c) 2008 David A. Mellis. All right reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - + Modified 23 November 2006 by David A. Mellis Modified 03 August 2015 by Chuck Todd */ @@ -37,7 +37,7 @@ typedef signed long sint32_t; /* default implementation: may be overridden */ size_t Print::write(const uint8_t *buffer, size_t size) { - + size_t n = 0; while (size--) { if (write(*buffer++)) n++; @@ -49,7 +49,7 @@ size_t Print::write(const uint8_t *buffer, size_t size) size_t Print::print(const char str[]) { - + //while(1); return write(str); } @@ -195,15 +195,15 @@ size_t Print::printNumber(unsigned long n, uint8_t base) { return write(str); } -size_t Print::printFloat(double number, uint8_t digits) -{ +size_t Print::printFloat(double number, uint8_t digits) +{ size_t n = 0; - + if (isnan(number)) return print("nan"); if (isinf(number)) return print("inf"); if (number > 4294967040.0) return print ("ovf"); // constant determined empirically if (number <-4294967040.0) return print ("ovf"); // constant determined empirically - + // Handle negative numbers if (number < 0.0) { @@ -215,7 +215,7 @@ size_t Print::printFloat(double number, uint8_t digits) double rounding = 0.5; for (uint8_t i=0; i 0) { - n += print("."); + n += print("."); } // Extract digits from the remainder one at a time @@ -234,14 +234,14 @@ size_t Print::printFloat(double number, uint8_t digits) remainder *= 10.0; int toPrint = int(remainder); n += print(toPrint); - remainder -= toPrint; - } - + remainder -= toPrint; + } + return n; } -#if (PrintfEnable == 1) +#if (PrintfEnable == 1) size_t Print::printf(const char *argList, ...) { const char *ptr; @@ -279,7 +279,7 @@ size_t Print::printf(const char *argList, ...) else { numOfDigits = 0xff; - } + } switch(ch) /* Decode the type of the argument */ @@ -297,14 +297,14 @@ size_t Print::printf(const char *argList, ...) case 'D': num_s32 = va_arg(argp, int); print(num_s32, 10); - break; + break; case 'u': case 'U': /* Argument type is of integer, hence read 32bit unsigend data */ num_u32 = va_arg(argp, uint32_t); - print(num_u32, 10); - break; + print(num_u32, 10); + break; @@ -312,21 +312,21 @@ size_t Print::printf(const char *argList, ...) case 'x': case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */ num_u32 = va_arg(argp, uint32_t); - print(num_u32, 16); + print(num_u32, 16); break; case 'b': case 'B': /* Argument type is of binary,Read int and convert to binary */ - num_u32 = va_arg(argp, uint32_t); - print(num_u32, 2); + num_u32 = va_arg(argp, uint32_t); + print(num_u32, 2); break; case 'F': case 'f': /* Argument type is of float, hence read double data from the argp */ - floatNum_f32 = va_arg(argp, double); + floatNum_f32 = va_arg(argp, double); printFloat(floatNum_f32,10); break; @@ -335,7 +335,7 @@ size_t Print::printf(const char *argList, ...) case 'S': case 's': /* Argument type is of string, hence get the pointer to sting passed */ str = va_arg(argp, char *); - print(str); + print(str); break; @@ -356,4 +356,4 @@ size_t Print::printf(const char *argList, ...) } -#endif +#endif