77 lines
1.7 KiB
C++
77 lines
1.7 KiB
C++
#include <Arduino.h>
|
|
#include "lcd_i2c.h"
|
|
#include <Wire.h>
|
|
|
|
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t address, uint8_t cols, uint8_t rows) {
|
|
_address = address;
|
|
begin(cols, rows);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t rows) {
|
|
Wire.begin();
|
|
command(0x33);
|
|
command(0x32);
|
|
command(0x28);
|
|
command(0x0C);
|
|
command(0x06);
|
|
command(0x01);
|
|
delay(5);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::clear() {
|
|
command(0x01);
|
|
delay(5);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::home() {
|
|
command(0x02);
|
|
delay(5);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row) {
|
|
uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
|
|
if (row >= 2) {
|
|
row = 1;
|
|
}
|
|
command(0x80 | (col + row_offsets[row]));
|
|
}
|
|
|
|
void LiquidCrystal_I2C::print(const char* str) {
|
|
while (*str) {
|
|
print(*str++);
|
|
}
|
|
}
|
|
|
|
void LiquidCrystal_I2C::print(char c) {
|
|
send(c, 1);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::command(uint8_t value) {
|
|
send(value, 0);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
|
|
write4bits(value, mode);
|
|
write4bits(value << 4, mode);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::write4bits(uint8_t value, uint8_t mode) {
|
|
uint8_t valueToSend = value | _backlight | mode;
|
|
Wire.beginTransmission(_address);
|
|
Wire.write(valueToSend);
|
|
pulseEnable(valueToSend);
|
|
Wire.endTransmission();
|
|
}
|
|
|
|
void LiquidCrystal_I2C::write8bits(uint8_t value, uint8_t mode) {
|
|
write4bits(value >> 4, mode);
|
|
write4bits(value, mode);
|
|
}
|
|
|
|
void LiquidCrystal_I2C::pulseEnable(uint8_t value) {
|
|
Wire.write(value | 0x04); // Assuming En is bit 2
|
|
delayMicroseconds(1);
|
|
Wire.write((value & ~0x04) | _backlight); // Assuming En is bit 2
|
|
delayMicroseconds(50);
|
|
}
|