CLEANUP: several code cleanups and static analyzing

This commit is contained in:
Robin Dietzel 2023-01-06 14:14:05 +01:00
parent f55c31ae9d
commit 13116d19b7
3 changed files with 107 additions and 84 deletions

View File

@ -7,6 +7,6 @@
// Auto generated header file containing the last git revision // Auto generated header file containing the last git revision
#define GIT_HASH "1ff7136" #define GIT_HASH "7a3009b"
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H #endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H

View File

@ -15,32 +15,37 @@ namespace floatpump{
LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr); LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr);
public: public:
static LCD_I2C_Driver& getInstance(I2C_HandleTypeDef &handle, uint16_t displayAddr) static LCD_I2C_Driver &getInstance(I2C_HandleTypeDef &handle, uint16_t displayAddr);
{
static LCD_I2C_Driver instance(handle, (uint16_t) displayAddr);
return instance;
}
enum SpecialChars {RightArrow = 0x7e}; enum SpecialChars {
RightArrow = 0x7e
};
public: public:
// Explicitly delete these constructors // Explicitly delete these constructors
LCD_I2C_Driver(LCD_I2C_Driver const &) = delete; LCD_I2C_Driver(LCD_I2C_Driver const &) = delete;
void operator=(LCD_I2C_Driver const &) = delete; void operator=(LCD_I2C_Driver const &) = delete;
// Functions sending text // Functions sending text
void LCDSendCString(char *str); void LCDSendCString(char *str);
void LCDSendChar(char chr); void LCDSendChar(char chr);
// Functions for configuration // Functions for configuration
void LCDClearDisplay(void); void LCDClearDisplay();
void LCDSetBacklight(bool enabled); void LCDSetBacklight(bool enabled);
void LCDPower(bool enabled); void LCDPower(bool enabled);
void LCDCursor(bool enabled); void LCDCursor(bool enabled);
void LCDCursorBlink(bool enabled); void LCDCursorBlink(bool enabled);
// These configurations are normally not necessary to change // These configurations are normally not necessary to change
void LCDCursorIncDec(bool enabled); void LCDCursorIncDec(bool enabled);
void LCDDisplayShift(bool enabled); void LCDDisplayShift(bool enabled);
// Move Cursor // Move Cursor
@ -52,9 +57,6 @@ namespace floatpump{
uint8_t y; uint8_t y;
} pos_t; } pos_t;
// Getters
pos_t getPosition();
private: private:
I2C_HandleTypeDef m_handle; I2C_HandleTypeDef m_handle;
uint16_t m_displayAddr; uint16_t m_displayAddr;
@ -69,6 +71,7 @@ namespace floatpump{
pos_t m_curPos; pos_t m_curPos;
void m_lcdSendData(char data); void m_lcdSendData(char data);
void m_lcdSendCmnd(char cmnd); void m_lcdSendCmnd(char cmnd);
}; };

View File

@ -39,10 +39,8 @@ namespace floatpump {
HAL_I2C_Master_Transmit(&m_handle, m_displayAddr, (uint8_t *) data_t, 4, 100); HAL_I2C_Master_Transmit(&m_handle, m_displayAddr, (uint8_t *) data_t, 4, 100);
} }
LCD_I2C_Driver::LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr) { LCD_I2C_Driver::LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr) : m_handle(handle),
m_handle = handle; m_displayAddr(displayAddr) {
m_displayAddr = displayAddr;
// Initialization by Instruction (from HD44780U Datasheet) // Initialization by Instruction (from HD44780U Datasheet)
HAL_Delay(50); // wait for >40ms HAL_Delay(50); // wait for >40ms
@ -58,10 +56,12 @@ namespace floatpump {
HAL_Delay(10); HAL_Delay(10);
m_lcdSendCmnd (0x28); // Function set 00001 DL N F - - => DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters) m_lcdSendCmnd(
0x28); // Function set 00001 DL N F - - => DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
HAL_Delay(1); HAL_Delay(1);
m_lcdSendCmnd (0x08); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off m_lcdSendCmnd(
0x08); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off
HAL_Delay(1); HAL_Delay(1);
m_lcdSendCmnd(0x01); // clear display 00000001 m_lcdSendCmnd(0x01); // clear display 00000001
@ -71,7 +71,8 @@ namespace floatpump {
m_lcdSendCmnd(0x06); //Entry mode set 000001 I/D S => I/D = 1 increment cursor, S = 0 display shift m_lcdSendCmnd(0x06); //Entry mode set 000001 I/D S => I/D = 1 increment cursor, S = 0 display shift
HAL_Delay(1); HAL_Delay(1);
m_lcdSendCmnd (0x0C); // Display on/off control 00001 D C B => D=1 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off m_lcdSendCmnd(
0x0C); // Display on/off control 00001 D C B => D=1 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off
HAL_Delay(1); HAL_Delay(1);
m_powered = true; m_powered = true;
@ -92,7 +93,7 @@ namespace floatpump {
m_lcdSendData(chr); m_lcdSendData(chr);
} }
void LCD_I2C_Driver::LCDClearDisplay(void) { void LCD_I2C_Driver::LCDClearDisplay() {
m_lcdSendCmnd(0x01); m_lcdSendCmnd(0x01);
HAL_Delay(1); HAL_Delay(1);
} }
@ -107,19 +108,22 @@ namespace floatpump {
void LCD_I2C_Driver::LCDPower(bool enabled) { void LCD_I2C_Driver::LCDPower(bool enabled) {
char com_build = 0x08 | (enabled << 2) | (m_cursor << 1) | (m_cursorBlink); char com_build = 0x08 | (enabled << 2) | (m_cursor << 1) | (m_cursorBlink);
m_lcdSendCmnd (com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off m_lcdSendCmnd(
com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off
m_powered = enabled; m_powered = enabled;
} }
void LCD_I2C_Driver::LCDCursor(bool enabled) { void LCD_I2C_Driver::LCDCursor(bool enabled) {
char com_build = 0x08 | (m_powered << 2) | (enabled << 1) | (m_cursorBlink); char com_build = 0x08 | (m_powered << 2) | (enabled << 1) | (m_cursorBlink);
m_lcdSendCmnd (com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off m_lcdSendCmnd(
com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off
m_cursor = enabled; m_cursor = enabled;
} }
void LCD_I2C_Driver::LCDCursorBlink(bool enabled) { void LCD_I2C_Driver::LCDCursorBlink(bool enabled) {
char com_build = 0x08 | (m_powered << 2) | (m_cursor << 1) | (enabled); char com_build = 0x08 | (m_powered << 2) | (m_cursor << 1) | (enabled);
m_lcdSendCmnd (com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off m_lcdSendCmnd(
com_build); // Display on/off control 00001 D C B => D=0 Display on/off, C=0 Cursor on/off, B=0 Cursor blink on/off
m_cursorBlink = enabled; m_cursorBlink = enabled;
} }
@ -136,7 +140,7 @@ namespace floatpump {
} }
void LCD_I2C_Driver::LCDSetCursor(uint8_t x, uint8_t y) { void LCD_I2C_Driver::LCDSetCursor(uint8_t x, uint8_t y) {
char addr = 0x00; char addr;
char startat = 0x00; char startat = 0x00;
// Generate start addresses for each row // Generate start addresses for each row
@ -145,10 +149,21 @@ namespace floatpump {
// |0x40 -------------- line 2 -------------- 0x53||0x54 -------------- line 4 -------------- 0x67| // |0x40 -------------- line 2 -------------- 0x53||0x54 -------------- line 4 -------------- 0x67|
switch (y) { switch (y) {
case 0: startat = 0x00; break; case 0:
case 1: startat = 0x40; break; startat = 0x00;
case 2: startat = 0x14; break; break;
case 3: startat = 0x54; break; case 1:
startat = 0x40;
break;
case 2:
startat = 0x14;
break;
case 3:
startat = 0x54;
break;
default:
startat = 0x00;
break;
} }
addr = startat + x; addr = startat + x;
@ -158,4 +173,9 @@ namespace floatpump {
m_curPos = {x, y}; m_curPos = {x, y};
} }
LCD_I2C_Driver &LCD_I2C_Driver::getInstance(I2C_HandleTypeDef &handle, uint16_t displayAddr) {
static LCD_I2C_Driver instance(handle, (uint16_t) displayAddr);
return instance;
}
} // floatpump } // floatpump