diff --git a/lcd.cpp b/lcd.cpp index bbc7380..686ef5d 100644 --- a/lcd.cpp +++ b/lcd.cpp @@ -29,23 +29,22 @@ constexpr int LCD_ENABLE_BIT = 0x04; LCD::LCD(i2c_inst_t *i2c, const uint gpio_sda, const uint gpio_scl, const uint8_t i2c_addr, uint8_t num_cols, uint8_t num_lines) - : i2c{i2c}, i2c_addr{i2c_addr}, num_cols{num_cols}, num_lines{num_lines} -{ - i2c_init(i2c1, 100 * 1000); - gpio_set_function(gpio_sda, GPIO_FUNC_I2C); - gpio_set_function(gpio_scl, GPIO_FUNC_I2C); - gpio_pull_up(gpio_sda); - gpio_pull_up(gpio_scl); + : i2c{i2c}, i2c_addr{i2c_addr}, num_cols{num_cols}, num_lines{num_lines} { + i2c_init(i2c1, 100 * 1000); + gpio_set_function(gpio_sda, GPIO_FUNC_I2C); + gpio_set_function(gpio_scl, GPIO_FUNC_I2C); + gpio_pull_up(gpio_sda); + gpio_pull_up(gpio_scl); - sendByte(0x03, Mode::COMMAND); - sendByte(0x03, Mode::COMMAND); - sendByte(0x03, Mode::COMMAND); - sendByte(0x02, Mode::COMMAND); + sendByte(0x03, Mode::COMMAND); + sendByte(0x03, Mode::COMMAND); + sendByte(0x03, Mode::COMMAND); + sendByte(0x02, Mode::COMMAND); - sendByte(LCD_ENTRYMODESET | LCD_ENTRYLEFT, Mode::COMMAND); - sendByte(LCD_FUNCTIONSET | LCD_2LINE, Mode::COMMAND); - sendByte(LCD_DISPLAYCONTROL | LCD_DISPLAYON, Mode::COMMAND); - clear(); + sendByte(LCD_ENTRYMODESET | LCD_ENTRYLEFT, Mode::COMMAND); + sendByte(LCD_FUNCTIONSET | LCD_2LINE, Mode::COMMAND); + sendByte(LCD_DISPLAYCONTROL | LCD_DISPLAYON, Mode::COMMAND); + clear(); } // go to location on LCD @@ -54,65 +53,38 @@ void LCD::setCursor(int line, int position) { sendByte(val, Mode::COMMAND); } -void LCD::sendChar(char val) { - sendByte(val, Mode::CHARACTER); -} +void LCD::sendChar(char val) { sendByte(val, Mode::CHARACTER); } void LCD::sendString(const std::string &str) { - for (const char &c : str) { - sendChar(c); - } + for (const char &c : str) { + sendChar(c); + } } -void LCD::clear() { - sendByte(LCD_CLEARDISPLAY, Mode::COMMAND); +void LCD::clear() { sendByte(LCD_CLEARDISPLAY, Mode::COMMAND); } + +void LCD::i2cWriteByte(uint8_t val) { + i2c_write_blocking(i2c, i2c_addr, &val, 1, false); } -void LCD::i2cWriteByte(uint8_t val) -{ - i2c_write_blocking(i2c, i2c_addr, &val, 1, false); -} - -void LCD::toggleEnable(uint8_t val) -{ - // Toggle enable pin on LCD display - // We cannot do this too quickly or things don't work - constexpr uint64_t DELAY_US = 600; - sleep_us(DELAY_US); - i2cWriteByte(val | LCD_ENABLE_BIT); - sleep_us(DELAY_US); - i2cWriteByte(val & ~LCD_ENABLE_BIT); - sleep_us(DELAY_US); +void LCD::toggleEnable(uint8_t val) { + // Toggle enable pin on LCD display + // We cannot do this too quickly or things don't work + constexpr uint64_t DELAY_US = 600; + sleep_us(DELAY_US); + i2cWriteByte(val | LCD_ENABLE_BIT); + sleep_us(DELAY_US); + i2cWriteByte(val & ~LCD_ENABLE_BIT); + sleep_us(DELAY_US); } // The display is sent a byte as two separate nibble transfers -void LCD::sendByte(uint8_t val, Mode mode) -{ - uint8_t high = static_cast(mode) | (val & 0xF0) | LCD_BACKLIGHT; - uint8_t low = static_cast(mode) | ((val << 4) & 0xF0) | LCD_BACKLIGHT; +void LCD::sendByte(uint8_t val, Mode mode) { + uint8_t high = static_cast(mode) | (val & 0xF0) | LCD_BACKLIGHT; + uint8_t low = static_cast(mode) | ((val << 4) & 0xF0) | LCD_BACKLIGHT; - i2cWriteByte(high); - toggleEnable(high); - i2cWriteByte(low); - toggleEnable(low); + i2cWriteByte(high); + toggleEnable(high); + i2cWriteByte(low); + toggleEnable(low); } - -int LCD::backlight_off() -{ - uint8_t data = 0; - int ret{0}; - - ret = i2c_write_blocking(i2c1, i2c_addr, &data, 1, false); - - return ret; -} - -int LCD::backlight_on() -{ - int ret{0}; - /*uint8_t data = 1 << SHIFT_BACKLIGHT; - - ret = i2c_write_blocking(i2c1, i2c_addr, &data, 1, false);*/ - - return ret; -} \ No newline at end of file diff --git a/lcd.h b/lcd.h index f6c3875..aca2d96 100644 --- a/lcd.h +++ b/lcd.h @@ -6,19 +6,14 @@ #include "hardware/i2c.h" #include "pico/stdlib.h" -enum class Mode -{ - COMMAND, - CHARACTER -}; +enum class Mode { COMMAND, CHARACTER }; -class LCD -{ +class LCD { public: LCD(i2c_inst_t *i2c, const uint gpio_sda, const uint gpio_scl, - const uint8_t i2c_addr = 0x27, uint8_t num_cols = 16, uint8_t num_lines = 2); - int backlight_off(); - int backlight_on(); + const uint8_t i2c_addr = 0x27, uint8_t num_cols = 16, + uint8_t num_lines = 2); + void sendString(const std::string &str); void setCursor(int line, int position); void clear();