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
#define GIT_HASH "1ff7136"
#define GIT_HASH "7a3009b"
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H

View File

@ -7,71 +7,74 @@
#include "stm32f4xx_hal.h"
namespace floatpump{
namespace floatpump {
class LCD_I2C_Driver {
class LCD_I2C_Driver {
private:
LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr);
private:
LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr);
public:
static LCD_I2C_Driver& getInstance(I2C_HandleTypeDef &handle, uint16_t displayAddr)
{
static LCD_I2C_Driver instance(handle, (uint16_t) displayAddr);
return instance;
}
public:
static LCD_I2C_Driver &getInstance(I2C_HandleTypeDef &handle, uint16_t displayAddr);
enum SpecialChars {RightArrow = 0x7e};
public:
// Explicitly delete these constructors
LCD_I2C_Driver(LCD_I2C_Driver const&) = delete;
void operator=(LCD_I2C_Driver const&) = delete;
// Functions sending text
void LCDSendCString(char *str);
void LCDSendChar(char chr);
// Functions for configuration
void LCDClearDisplay(void);
void LCDSetBacklight(bool enabled);
void LCDPower(bool enabled);
void LCDCursor(bool enabled);
void LCDCursorBlink(bool enabled);
// These configurations are normally not necessary to change
void LCDCursorIncDec(bool enabled);
void LCDDisplayShift(bool enabled);
// Move Cursor
void LCDSetCursor(uint8_t x, uint8_t y);
// Position
typedef struct {
uint8_t x;
uint8_t y;
} pos_t;
// Getters
pos_t getPosition();
private:
I2C_HandleTypeDef m_handle;
uint16_t m_displayAddr;
bool m_backlight{};
bool m_cursor{};
bool m_cursorBlink{};
bool m_incrementDecrement{};
bool m_autoShift{};
bool m_powered;
pos_t m_curPos;
void m_lcdSendData(char data);
void m_lcdSendCmnd(char cmnd);
enum SpecialChars {
RightArrow = 0x7e
};
public:
// Explicitly delete these constructors
LCD_I2C_Driver(LCD_I2C_Driver const &) = delete;
void operator=(LCD_I2C_Driver const &) = delete;
// Functions sending text
void LCDSendCString(char *str);
void LCDSendChar(char chr);
// Functions for configuration
void LCDClearDisplay();
void LCDSetBacklight(bool enabled);
void LCDPower(bool enabled);
void LCDCursor(bool enabled);
void LCDCursorBlink(bool enabled);
// These configurations are normally not necessary to change
void LCDCursorIncDec(bool enabled);
void LCDDisplayShift(bool enabled);
// Move Cursor
void LCDSetCursor(uint8_t x, uint8_t y);
// Position
typedef struct {
uint8_t x;
uint8_t y;
} pos_t;
private:
I2C_HandleTypeDef m_handle;
uint16_t m_displayAddr;
bool m_backlight{};
bool m_cursor{};
bool m_cursorBlink{};
bool m_incrementDecrement{};
bool m_autoShift{};
bool m_powered;
pos_t m_curPos;
void m_lcdSendData(char data);
void m_lcdSendCmnd(char cmnd);
};
} // floatpump
#endif //FLOATPUMP_LCD_I2C_DRIVER_H

View File

@ -39,39 +39,40 @@ namespace floatpump {
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) {
m_handle = handle;
m_displayAddr = displayAddr;
LCD_I2C_Driver::LCD_I2C_Driver(I2C_HandleTypeDef &handle, uint16_t displayAddr) : m_handle(handle),
m_displayAddr(displayAddr) {
// Initialization by Instruction (from HD44780U Datasheet)
HAL_Delay(50); // wait for >40ms
m_lcdSendCmnd(0x30);
HAL_Delay(5); // wait for >4.1ms
m_lcdSendCmnd (0x30);
m_lcdSendCmnd(0x30);
HAL_Delay(1); // wait for >100us
m_lcdSendCmnd (0x30);
m_lcdSendCmnd(0x30);
HAL_Delay(10); // Wait for command execution length
// Configuration
m_lcdSendCmnd (0x20); // Set Interface to 4-bit mode
m_lcdSendCmnd(0x20); // Set Interface to 4-bit mode
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);
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);
m_lcdSendCmnd (0x01); // clear display 00000001
m_lcdSendCmnd(0x01); // clear display 00000001
HAL_Delay(1);
HAL_Delay(1);
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);
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);
m_powered = true;
@ -92,8 +93,8 @@ namespace floatpump {
m_lcdSendData(chr);
}
void LCD_I2C_Driver::LCDClearDisplay(void) {
m_lcdSendCmnd (0x01);
void LCD_I2C_Driver::LCDClearDisplay() {
m_lcdSendCmnd(0x01);
HAL_Delay(1);
}
@ -107,19 +108,22 @@ namespace floatpump {
void LCD_I2C_Driver::LCDPower(bool enabled) {
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;
}
void LCD_I2C_Driver::LCDCursor(bool enabled) {
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;
}
void LCD_I2C_Driver::LCDCursorBlink(bool 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;
}
@ -136,7 +140,7 @@ namespace floatpump {
}
void LCD_I2C_Driver::LCDSetCursor(uint8_t x, uint8_t y) {
char addr = 0x00;
char addr;
char startat = 0x00;
// Generate start addresses for each row
@ -144,18 +148,34 @@ namespace floatpump {
// |0x00 -------------- line 1 -------------- 0x13||0x14 -------------- line 3 -------------- 0x27|
// |0x40 -------------- line 2 -------------- 0x53||0x54 -------------- line 4 -------------- 0x67|
switch(y) {
case 0: startat = 0x00; break;
case 1: startat = 0x40; break;
case 2: startat = 0x14; break;
case 3: startat = 0x54; break;
switch (y) {
case 0:
startat = 0x00;
break;
case 1:
startat = 0x40;
break;
case 2:
startat = 0x14;
break;
case 3:
startat = 0x54;
break;
default:
startat = 0x00;
break;
}
addr = startat + x;
m_lcdSendCmnd(( 0x80 | addr));
m_lcdSendCmnd((0x80 | addr));
HAL_Delay(1);
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