clang-format
This commit is contained in:
parent
275e012695
commit
440359d775
2 changed files with 43 additions and 76 deletions
104
lcd.cpp
104
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,
|
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)
|
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{i2c}, i2c_addr{i2c_addr}, num_cols{num_cols}, num_lines{num_lines} {
|
||||||
{
|
i2c_init(i2c1, 100 * 1000);
|
||||||
i2c_init(i2c1, 100 * 1000);
|
gpio_set_function(gpio_sda, GPIO_FUNC_I2C);
|
||||||
gpio_set_function(gpio_sda, GPIO_FUNC_I2C);
|
gpio_set_function(gpio_scl, GPIO_FUNC_I2C);
|
||||||
gpio_set_function(gpio_scl, GPIO_FUNC_I2C);
|
gpio_pull_up(gpio_sda);
|
||||||
gpio_pull_up(gpio_sda);
|
gpio_pull_up(gpio_scl);
|
||||||
gpio_pull_up(gpio_scl);
|
|
||||||
|
|
||||||
sendByte(0x03, Mode::COMMAND);
|
sendByte(0x03, Mode::COMMAND);
|
||||||
sendByte(0x03, Mode::COMMAND);
|
sendByte(0x03, Mode::COMMAND);
|
||||||
sendByte(0x03, Mode::COMMAND);
|
sendByte(0x03, Mode::COMMAND);
|
||||||
sendByte(0x02, Mode::COMMAND);
|
sendByte(0x02, Mode::COMMAND);
|
||||||
|
|
||||||
sendByte(LCD_ENTRYMODESET | LCD_ENTRYLEFT, Mode::COMMAND);
|
sendByte(LCD_ENTRYMODESET | LCD_ENTRYLEFT, Mode::COMMAND);
|
||||||
sendByte(LCD_FUNCTIONSET | LCD_2LINE, Mode::COMMAND);
|
sendByte(LCD_FUNCTIONSET | LCD_2LINE, Mode::COMMAND);
|
||||||
sendByte(LCD_DISPLAYCONTROL | LCD_DISPLAYON, Mode::COMMAND);
|
sendByte(LCD_DISPLAYCONTROL | LCD_DISPLAYON, Mode::COMMAND);
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// go to location on LCD
|
// go to location on LCD
|
||||||
|
@ -54,65 +53,38 @@ void LCD::setCursor(int line, int position) {
|
||||||
sendByte(val, Mode::COMMAND);
|
sendByte(val, Mode::COMMAND);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LCD::sendChar(char val) {
|
void LCD::sendChar(char val) { sendByte(val, Mode::CHARACTER); }
|
||||||
sendByte(val, Mode::CHARACTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LCD::sendString(const std::string &str) {
|
void LCD::sendString(const std::string &str) {
|
||||||
for (const char &c : str) {
|
for (const char &c : str) {
|
||||||
sendChar(c);
|
sendChar(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LCD::clear() {
|
void LCD::clear() { sendByte(LCD_CLEARDISPLAY, Mode::COMMAND); }
|
||||||
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)
|
void LCD::toggleEnable(uint8_t val) {
|
||||||
{
|
// Toggle enable pin on LCD display
|
||||||
i2c_write_blocking(i2c, i2c_addr, &val, 1, false);
|
// We cannot do this too quickly or things don't work
|
||||||
}
|
constexpr uint64_t DELAY_US = 600;
|
||||||
|
sleep_us(DELAY_US);
|
||||||
void LCD::toggleEnable(uint8_t val)
|
i2cWriteByte(val | LCD_ENABLE_BIT);
|
||||||
{
|
sleep_us(DELAY_US);
|
||||||
// Toggle enable pin on LCD display
|
i2cWriteByte(val & ~LCD_ENABLE_BIT);
|
||||||
// We cannot do this too quickly or things don't work
|
sleep_us(DELAY_US);
|
||||||
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
|
// The display is sent a byte as two separate nibble transfers
|
||||||
void LCD::sendByte(uint8_t val, Mode mode)
|
void LCD::sendByte(uint8_t val, Mode mode) {
|
||||||
{
|
uint8_t high = static_cast<int>(mode) | (val & 0xF0) | LCD_BACKLIGHT;
|
||||||
uint8_t high = static_cast<int>(mode) | (val & 0xF0) | LCD_BACKLIGHT;
|
uint8_t low = static_cast<int>(mode) | ((val << 4) & 0xF0) | LCD_BACKLIGHT;
|
||||||
uint8_t low = static_cast<int>(mode) | ((val << 4) & 0xF0) | LCD_BACKLIGHT;
|
|
||||||
|
|
||||||
i2cWriteByte(high);
|
i2cWriteByte(high);
|
||||||
toggleEnable(high);
|
toggleEnable(high);
|
||||||
i2cWriteByte(low);
|
i2cWriteByte(low);
|
||||||
toggleEnable(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;
|
|
||||||
}
|
}
|
15
lcd.h
15
lcd.h
|
@ -6,19 +6,14 @@
|
||||||
#include "hardware/i2c.h"
|
#include "hardware/i2c.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
|
|
||||||
enum class Mode
|
enum class Mode { COMMAND, CHARACTER };
|
||||||
{
|
|
||||||
COMMAND,
|
|
||||||
CHARACTER
|
|
||||||
};
|
|
||||||
|
|
||||||
class LCD
|
class LCD {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
LCD(i2c_inst_t *i2c, const uint gpio_sda, const uint gpio_scl,
|
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);
|
const uint8_t i2c_addr = 0x27, uint8_t num_cols = 16,
|
||||||
int backlight_off();
|
uint8_t num_lines = 2);
|
||||||
int backlight_on();
|
|
||||||
void sendString(const std::string &str);
|
void sendString(const std::string &str);
|
||||||
void setCursor(int line, int position);
|
void setCursor(int line, int position);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
Loading…
Reference in a new issue