-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcd.cpp
161 lines (137 loc) · 3.25 KB
/
lcd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* author : Shuichi TAKANO
* since : Sat Feb 24 2024 22:26:00
*/
#include "lcd.h"
#include "i2c_manager.h"
#include <cassert>
#include <array>
namespace
{
inline constexpr uint8_t LCD_ADDR = 0x7c >> 1;
inline constexpr int I2C_PRIO = 0;
}
LCD &LCD::instance()
{
static LCD lcd;
return lcd;
}
void LCD::init()
{
sleep_ms(40);
uint8_t data[] = {
0x38, // function set
0x39, // function set
0x14, // internal OSC frequency
0x70, // contrast set
0x56, // power/icon/contrast control
0x6c, // follower control
0x38, // function set
0x0c, // display on
0x01, // clear display
};
for (auto d : data)
{
writeInstruction(d);
}
sleep_ms(2); // clear display が遅い
}
void LCD::writeInstruction(uint8_t cmd)
{
std::array<uint8_t, 2> buf = {0x00, cmd};
getI2CManager().sendBlocking(LCD_ADDR, buf.data(), buf.size());
sleep_us(27);
}
void LCD::writeData(uint8_t data)
{
std::array<uint8_t, 2> buf = {0x40, data};
getI2CManager().sendBlocking(LCD_ADDR, buf.data(), buf.size());
}
void LCD::puts(const char *s)
{
while (char ch = *s++)
{
if (ch == 160)
{
ch = 0;
}
writeData(ch);
}
}
void LCD::locate(int x, int y)
{
writeInstruction(0x80 | ((y ? 0x40 : 0) + x));
}
void LCD::clear()
{
writeInstruction(0x01);
sleep_ms(2);
}
void LCD::defineChar(int n, const uint8_t *data)
{
writeInstruction(0x40 | (n << 3));
for (int i = 0; i < 8; ++i)
{
writeData(data[i]);
}
}
void LCD::writeInstructionNonBlocking(uint8_t cmd)
{
std::array<uint8_t, 2> buf = {0x00, cmd};
getI2CManager().sendNonBlocking(I2C_PRIO, LCD_ADDR, buf.data(), buf.size());
}
void LCD::writeDataNonBlocking(uint8_t data)
{
std::array<uint8_t, 2> buf = {0x40, data};
getI2CManager().sendNonBlocking(I2C_PRIO, LCD_ADDR, buf.data(), buf.size());
}
void LCD::locateNonBlocking(int x, int y)
{
writeInstructionNonBlocking(0x80 | ((y ? 0x40 : 0) + x));
}
void LCD::putCharNonBlocking(char ch)
{
writeDataNonBlocking(ch);
}
bool LCD::printNonBlocking(int x, int y, const char *s, int n)
{
auto &i2c = getI2CManager();
if (!i2c.reserve(I2C_PRIO, LCD_ADDR, 2 + 1 + n))
{
return false;
}
locateNonBlocking(x, y);
// i2c_write_byte_raw(i2c_, 0x40);
uint8_t cmd = 0x40;
i2c.sendNonBlockingUnsafe(LCD_ADDR, &cmd, 1, true);
i2c.sendNonBlockingUnsafe(LCD_ADDR, reinterpret_cast<const uint8_t *>(s), n);
return true;
}
bool LCD::defineCharNonBlocking(int n, const uint8_t *data)
{
auto &i2c = getI2CManager();
if (!i2c.reserve(I2C_PRIO, LCD_ADDR, 2 + 9))
{
return false;
}
writeInstructionNonBlocking(0x40 | (n << 3));
uint8_t cmd = 0x40;
i2c.sendNonBlockingUnsafe(LCD_ADDR, &cmd, 1, true);
i2c.sendNonBlockingUnsafe(LCD_ADDR, data, 8);
return true;
}
void LCD::setContrast(int v)
{
writeInstruction(0x39); // function set IS=1
writeInstruction(0x70 + (v & 15));
writeInstruction(0x38); // function set IS=0
}
void LCD::setDisplayOnOff(bool on)
{
if (dispOn_ != on)
{
writeInstruction(on ? 0x0c : 0x08);
writeInstruction(on ? 0x56 : 0x50);
dispOn_ = on;
}
}