forked from vladkorotnev/plasma-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gu7000.cpp
217 lines (172 loc) · 5.74 KB
/
gu7000.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <device_config.h>
#if HAS(OUTPUT_GU7000)
#include "display/gu7000.h"
#include <esp32-hal-log.h>
#include <esp32-hal-gpio.h>
#include <esp_err.h>
static char LOG_TAG[] = "GU7000";
static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED;
ItronGU7000Driver::ItronGU7000Driver(
const gpio_num_t databus[8],
const gpio_num_t wr,
const gpio_num_t busy
) {
for(int i = 0; i < 8; i++) {
databus_gpios[i] = databus[i];
}
wr_gpio = wr;
busy_gpio = busy;
}
void ItronGU7000Driver::initialize() {
// TODO use parlio?
ESP_LOGI(LOG_TAG, "Initializing Noritake Itron GU7000 with data bus: %i %i %i %i %i %i %i %i, wr=%i, busy=%i", databus_gpios[0], databus_gpios[1], databus_gpios[2], databus_gpios[3], databus_gpios[4], databus_gpios[5], databus_gpios[6], databus_gpios[7], wr_gpio, busy_gpio);
// Set up parallel output
gpio_config_t io_conf = {
.pin_bit_mask = 0,
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = gpio_pullup_t::GPIO_PULLUP_ENABLE,
};
for(int i = 0; i < sizeof(databus_gpios) / sizeof(databus_gpios[0]); i++) {
io_conf.pin_bit_mask |= 1ULL << databus_gpios[i];
}
io_conf.pin_bit_mask |= 1ULL << wr_gpio;
ESP_ERROR_CHECK(gpio_config(&io_conf));
gpio_set_level(wr_gpio, 1);
// Set up busy input
io_conf = {
.pin_bit_mask = (1ULL << busy_gpio),
.mode = GPIO_MODE_INPUT,
.pull_up_en = gpio_pullup_t::GPIO_PULLUP_DISABLE,
.pull_down_en = gpio_pulldown_t::GPIO_PULLDOWN_DISABLE,
};
ESP_ERROR_CHECK(gpio_config(&io_conf));
show_state = true;
wait_shit_through();
delay(2000);
}
void ItronGU7000Driver::wait_shit_through() {
while(gpio_get_level(busy_gpio)) {
delayMicroseconds(2);
}
}
void ItronGU7000Driver::set_databus(uint8_t data) {
uint8_t local_sts = data;
for(int i = 0; i < 8; i++) {
uint8_t cur_state = (local_sts & 1);
gpio_set_level(databus_gpios[i], cur_state);
local_sts >>= 1;
}
}
void ItronGU7000Driver::pulse_clock() {
gpio_set_level(wr_gpio, 0);
delayMicroseconds(1);
gpio_set_level(wr_gpio, 1);
delayMicroseconds(1);
wait_shit_through();
}
void ItronGU7000Driver::write_string(const char * s) {
taskENTER_CRITICAL(&_spinlock);
int len = strlen(s);
for(int i = 0; i < len; i++) {
set_databus(s[i]);
pulse_clock();
}
taskEXIT_CRITICAL(&_spinlock);
}
void ItronGU7000Driver::reset() {
wait_shit_through();
// Bringup Step 1: Initialize display
write_string("\x1B\x40");
// Power on
set_power(true);
// Cursor OFF (oof the Cstring hits again!)
write_string("\x1F\x43"); set_databus(0); pulse_clock();
clear();
write_string("\x1F\x58\x01");
write_string("uPIS-OS Noritake GU7000 Init");
vTaskDelay(pdMS_TO_TICKS(1000));
clear();
write_string("\x1F\x24"); // cursor move to 0
set_databus(0);
pulse_clock(); pulse_clock(); pulse_clock(); pulse_clock();
}
void ItronGU7000Driver::set_show(bool show) {
taskENTER_CRITICAL(&_spinlock);
ESP_LOGI(LOG_TAG, "Show = %i", show);
/**
* From datasheet:
*
* P=2~4の場合、スクリーンセーバーの起動を行ないます。スクリーンセーバーは
* 次のデータ入力により中断され、コマンド以前の表示状態に復帰します
*
* Thus we need to save the show state and stop data flow when entering screen blanking mode
*/
show_state = show;
write_string("\x1F\x28\x61\x40");
// Turn off the display without turning off the heaters. This helps reduce strain on the heaters and prevent their failure from metal fatigue.
set_databus(show ? 0x1 : 0x2);
pulse_clock();
taskEXIT_CRITICAL(&_spinlock);
}
void ItronGU7000Driver::set_power(bool power) {
taskENTER_CRITICAL(&_spinlock);
/**
* From datasheet:
* P=0~1の場合、表示用電源のON/OFF制御を行ないます。
* 次の表示用電源のON/OFF制御コマンドが入力されるまで保持されます
*
* Thus we don't need to save state unlike the `set_show` command
*/
ESP_LOGI(LOG_TAG, "Power = %i", power);
write_string("\x1F\x28\x61\x40");
set_databus(power ? 0x1 : 0x0);
pulse_clock();
taskEXIT_CRITICAL(&_spinlock);
}
void ItronGU7000Driver::clear() {
taskENTER_CRITICAL(&_spinlock);
set_databus(0x0c);
pulse_clock();
taskEXIT_CRITICAL(&_spinlock);
}
inline uint8_t flipByte(uint8_t c){
char r=0;
for(uint8_t i = 0; i < 8; i++){
r <<= 1;
r |= c & 1;
c >>= 1;
}
return r;
}
void ItronGU7000Driver::write_fanta(const uint8_t * strides, size_t count) {
taskENTER_CRITICAL(&_spinlock);
if(show_state) { // <- see technote in `set_show` method above
write_string("\x1F\x24"); // cursor move to 0
set_databus(0);
pulse_clock(); pulse_clock(); pulse_clock(); pulse_clock();
write_string("\x1F\x28\x66\x11");
set_databus(HWCONF_DISPLAY_WIDTH_PX & 0x00FF);
pulse_clock();
set_databus(HWCONF_DISPLAY_WIDTH_PX & 0xFF00);
pulse_clock();
set_databus((HWCONF_DISPLAY_HEIGHT_PX / 8) & 0x00FF);
pulse_clock();
set_databus((HWCONF_DISPLAY_HEIGHT_PX / 8) & 0xFF00);
pulse_clock();
set_databus(1);
pulse_clock();
for(int i = 0; i < count; i++) {
set_databus(flipByte(strides[i]));
pulse_clock();
}
}
taskEXIT_CRITICAL(&_spinlock);
}
void ItronGU7000Driver::set_bright(bool bright) {
taskENTER_CRITICAL(&_spinlock);
char brightness = bright ? 6 : 2; // range 1..8, 25% and 75% in this case
write_string("\x1F\x58");
set_databus(brightness); pulse_clock();
taskEXIT_CRITICAL(&_spinlock);
}
#endif