-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
264 lines (237 loc) · 6.19 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// #include <M5StickCPlus.h>
#include <M5Unified.h>
// #include <TFT_eSPI.h>
#include <string>
#include <vector>
// #include "tb.h"
// #include "hakutaka.h"
// #include "hokuto.h"
// #include "nis.h"
// #include "sre.h"
// #include "oexp.h"
// #include "bexp.h"
#include "Unit_MiniEncoderC.h"
#include "otetudai.h"
#include "ogawa.h"
#include "gh.h"
#include "tori.h"
#include "ogawa_aff.h"
#include "dev.h"
#include "ogawa_otetudai.h"
#include "uji.h"
#include "takuya.h"
// #include "tw.h"
#define FREEZING_TIME 1000
#define AUTO_ESCAPE_STEP 3
// #include "kaiso.h"
// #include "sinkai.h"
// #include "kaisoku.h"
// #include "huku.h"
// #include "wanman.h"
// #include "liner.h"
// #include "testr.h"
const uint16_t *imgs[] = {uji, otetudai, takuya, ogawa_otetudai, otetudai, takuya, qr2};//{kaiso, sinkai, kaisoku, huku, wanman, liner, testr};
const size_t imgCount = sizeof(imgs) / sizeof(imgs[0]);
uint8_t currentIndex = 0;
enum State
{
AUTO_SCROLL,
MANUAL_SCROLL,
AUTO_ESCAPE,
};
State state = AUTO_SCROLL;
bool isHold = false;
String StateToString(State state)
{
switch (state)
{
case AUTO_SCROLL:
return "AS";
case MANUAL_SCROLL:
return "MN";
case AUTO_ESCAPE:
return "EC";
}
}
uint8_t scroll_steps[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 2, 3, 3, 4, 3, 2, 2, 2, 1, 1};
static const uint16_t imgWidth = 240;
static const uint16_t imgHeight = 135;
int16_t currentOffset = 0;
uint8_t currentStep = 0;
UNIT_MINIENCODERC sensor;
uint32_t lastMovedTime = 0;
uint32_t encoderValue = 0;
uint32_t lastEncoderValue = 0;
std::vector<uint16_t> autoEscapeSteps;
uint16_t autoEscapeStep = 0;
LGFX_Sprite sprite(&M5.Lcd);
void drawImage(const uint16_t *img)
{
M5.Lcd.startWrite();
for (int x = 0; x < imgWidth; x++)
{
for (int y = 0; y < imgHeight; y++)
{
uint16_t color = img[y * imgWidth + x];
// RGB565 (little endian) の色を正しく表示するためにバイト順を入れ替える
color = (color >> 8) | (color << 8);
M5.Lcd.drawPixel(imgHeight - y, x, color);
}
}
M5.Lcd.endWrite();
}
void drawCombinedImage(const uint16_t *img1, const uint16_t *img2, uint16_t offset)
{
sprite.clear();
// img1からoffset分ずらしてコピー
for (int y = 0; y < imgHeight - offset; y++)
{
for (int x = 0; x < imgWidth; x++)
{
uint16_t color = img1[y * imgWidth + x + offset * imgWidth];
// RGB565 (little endian) の色を正しく表示するためにバイト順を入れ替える
color = (color >> 8) | (color << 8);
sprite.drawPixel(x, y, color);
}
}
// img2から残りをコピー
for (int y = imgHeight - offset; y < imgHeight; y++)
{
for (int x = 0; x < imgWidth; x++)
{
uint16_t color = img2[(y - (imgHeight - offset)) * imgWidth + x];
// RGB565 (little endian) の色を正しく表示するためにバイト順を入れ替える
color = (color >> 8) | (color << 8);
sprite.drawPixel(x, y, color);
}
}
// スプライトを画面に転送
sprite.pushRotated(90);
}
void drawTape(const uint16_t *imgs[], uint8_t imgIndex, int16_t currentOffset)
{
Serial.printf("ImageCount: %d\n", imgCount);
Serial.printf("next imgIndex: %d\n", (imgIndex + 1) % imgCount);
drawCombinedImage(imgs[imgIndex], imgs[(imgIndex + 1) % imgCount], currentOffset);
}
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
sensor.begin(&Wire, MINIENCODERC_ADDR, 0, 26, 100000UL);
state = AUTO_SCROLL;
// スプライトの初期化
sprite.createSprite(imgWidth, imgHeight);
}
void loop()
{
M5.update();
encoderValue = sensor.getEncoderValue();
uint32_t currentTime = millis();
if (M5.BtnA.wasPressed())
{
isHold = !isHold;
}
if (M5.BtnA.wasPressed() || isHold)
{
if (!isHold)
{
isHold = false;
}
else
{
state = MANUAL_SCROLL;
isHold = true;
}
}
if (state == AUTO_SCROLL)
{
if (encoderValue != lastEncoderValue)
{
state = MANUAL_SCROLL;
lastMovedTime = currentTime;
}
else
{
currentOffset += scroll_steps[currentStep] * 5;
if (currentOffset >= imgHeight)
{
currentOffset = 0;
currentIndex = (currentIndex + 1) % imgCount; // インクリメント
}
currentStep++;
if (currentStep >= sizeof(scroll_steps))
{
currentStep = 0;
}
}
}
else if (state == MANUAL_SCROLL)
{
if (currentTime - lastMovedTime > FREEZING_TIME)
{
state = AUTO_ESCAPE;
autoEscapeStep = 0;
}
if (encoderValue != lastEncoderValue)
{
lastMovedTime = currentTime;
currentOffset += (encoderValue - lastEncoderValue) * 2;
if (currentOffset >= imgHeight)
{
currentOffset = 0;
currentIndex = (currentIndex + 1) % imgCount; // インクリメント
}
else if (currentOffset < 0)
{
currentOffset = imgHeight - 1;
currentIndex = (currentIndex == 0) ? imgCount - 1 : currentIndex - 1; // デクリメント
}
}
}
else if (state == AUTO_ESCAPE)
{
if (encoderValue != lastEncoderValue)
{
state = MANUAL_SCROLL;
lastMovedTime = currentTime;
}
else if (autoEscapeStep < autoEscapeSteps.size())
{
currentOffset += autoEscapeSteps[autoEscapeStep] * 5;
autoEscapeStep++;
}
else
{
currentOffset = currentOffset % imgHeight;
if (currentOffset != 0)
{
if (abs(currentOffset) < AUTO_ESCAPE_STEP)
{
currentOffset = 0;
}
else if (currentOffset > 0)
{
currentOffset -= AUTO_ESCAPE_STEP;
}
else
{
currentOffset += AUTO_ESCAPE_STEP;
}
currentStep = 0;
}
else
{
state = AUTO_SCROLL;
}
}
}
drawTape(imgs, currentIndex, currentOffset);
if (encoderValue != lastEncoderValue)
{
lastMovedTime = currentTime;
}
lastEncoderValue = encoderValue;
}