Repository navigation
Expand file tree
/
Copy pathSPIRenderer.cpp
More file actions
445 lines (376 loc) · 12.5 KB
/
Copy pathSPIRenderer.cpp
File metadata and controls
445 lines (376 loc) · 12.5 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "SPIRenderer.h"
#include "ILDAFile.h" // 确保 ILDAFile 的定义可用
#include <stdio.h> // 用于 printf
#include <string.h> // 用于 memset
// BL618 SDK 驱动头文件
#include "bflb_gpio.h"
#include "bflb_spi.h"
#include "bflb_mtimer.h"
// 全局变量定义和外部依赖声明
const int bufferFrames = 6;
// static const char *TAGRENDERER = "SPI"; // 移除 ESP32 特定的日志标签
SPIRenderer *renderer;
std::vector<std::string> avaliableMedia;
int curMedia = -1;
// volatile bool TFTLCD_status = false; // TFTLCD屏幕和SD卡不能同时运行,status作为信号量来传递状态
TaskHandle_t fileBufferHandle;
TaskHandle_t drawTaskHandle;
// --- 声明项目中的其他全局变量 ---
extern "C" {
extern ILDAFile *ilda;
extern volatile bool DrawNow_Flag;
extern volatile bool Pause_Flag;
}
// ------------------------------------
// SPI互斥锁,防止多任务同时访问SPI总线
SemaphoreHandle_t spi_mutex = NULL;
// 绘图任务
void draw_task(void *arg)
{
printf("[SPI] Draw task started\n");
for (;;) {
// 调用渲染器的绘制函数
if (renderer) {
renderer->draw();
}
// 控制绘制速率,这个值可能需要调整以获得最佳性能
bflb_mtimer_delay_ms(1);
}
}
// 设置渲染器
void setupRenderer()
{
// 创建渲染器实例
renderer = new SPIRenderer();
if (!renderer) {
printf("[SPI] Failed to create renderer\n");
return;
}
// 创建ILDA文件实例
ilda = new ILDAFile();
if (!ilda) {
printf("[SPI] Failed to create ILDA file handler\n");
return;
}
// 初始化ILDA帧缓冲区
ilda->frames = (ILDA_Frame_t *)malloc(bufferFrames * sizeof(ILDA_Frame_t));
if (!ilda->frames) {
printf("[SPI] Failed to allocate frames buffer\n");
return;
}
// 为每一帧分配记录缓冲区
for (int i = 0; i < bufferFrames; i++) {
ilda->frames[i].records = (ILDA_Record_t *)malloc(4096 * sizeof(ILDA_Record_t));
if (!ilda->frames[i].records) {
printf("[SPI] Failed to allocate records for frame %d\n", i);
return;
}
ilda->frames[i].isBuffered = false;
ilda->frames[i].number_records = 0;
}
// 启动渲染器
renderer->start();
printf("[SPI] Renderer setup complete\n");
}
// SPIRenderer构造函数
SPIRenderer::SPIRenderer()
{
frame_position = 0;
draw_position = 0;
spi = NULL;
gpio = NULL;
}
// SPIRenderer析构函数
SPIRenderer::~SPIRenderer()
{
// 关闭激光
if (gpio) {
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
}
}
// 初始化SPI和GPIO
void SPIRenderer::start()
{
// 获取SPI和GPIO设备句柄
spi = bflb_device_get_by_name("spi0");
gpio = bflb_device_get_by_name("gpio");
if (!spi || !gpio) {
printf("[SPI] ERROR: Failed to get device handles\n");
return;
}
// 初始化SPI配置
struct bflb_spi_config_s spi_cfg = {
.freq = 10000000, // 10MHz
.role = SPI_ROLE_MASTER,
.mode = SPI_MODE_0, // DAC通常使用模式0
.data_width = SPI_DATA_WIDTH_8BIT,
.bit_order = SPI_BIT_MSB,
.byte_order = SPI_BYTE_LSB,
.tx_fifo_threshold = 0,
.rx_fifo_threshold = 0,
};
// 初始化SPI
bflb_spi_init(spi, &spi_cfg);
// 初始化CS引脚
bflb_gpio_init(gpio, PIN_NUM_CS, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_set(gpio, PIN_NUM_CS);
// 初始化LDAC引脚
bflb_gpio_init(gpio, PIN_NUM_LDAC, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_set(gpio, PIN_NUM_LDAC);
// 初始化激光控制引脚
bflb_gpio_init(gpio, PIN_NUM_LASER_R, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, PIN_NUM_LASER_G, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, PIN_NUM_LASER_B, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
// 默认关闭激光
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
// 创建绘图任务
xTaskCreate(draw_task, (char *)"draw", 2048, NULL, 5, &drawTaskHandle);
printf("[SPI] Hardware initialized successfully\n");
}
// 绘图函数
void SPIRenderer::draw()
{
// 获取SPI互斥锁
if (xSemaphoreTake(spi_mutex, pdMS_TO_TICKS(10)) != pdTRUE) {
// 如果无法获取互斥锁,跳过本次绘制
return;
}
// 重新配置SPI为DAC需要的参数
struct bflb_spi_config_s spi_cfg = {
.freq = 10000000,
.role = SPI_ROLE_MASTER,
.mode = SPI_MODE_0,
.data_width = SPI_DATA_WIDTH_8BIT,
.bit_order = SPI_BIT_MSB,
.byte_order = SPI_BYTE_LSB,
};
bflb_spi_init(spi, &spi_cfg);
// 投影SD卡中的media模式
if (DrawNow_Flag == 0)
{
// 这个buffer内已经缓存过内容了,可以投影
if (ilda->frames[frame_position].isBuffered == true && Pause_Flag == 0) // 如果只有缓存暂停,由于总共有bufferFrames个buffer,投影还是会继续,会唤醒缓存
{
// 这一帧还没画完
if (draw_position < ilda->frames[frame_position].number_records)
{
// 位置信号X&Y的处理和DAC输出
const ILDA_Record_t &instruction = ilda->frames[frame_position].records[draw_position];
int y = 2048 + (instruction.x * 1024) / 32768; // 位置信号以两个字节在ILDA文件中储存
int x = 2048 + (instruction.y * 1024) / 32768; // -32767~+32767-->1024~3072,将位置信号转化为12位数据
uint8_t tx_buffer[2];
// DAC channel A
tx_buffer[0] = 0b11010000 | ((x >> 8) & 0xF);
tx_buffer[1] = x & 255;
// 选中DAC
bflb_gpio_reset(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
// 发送数据
bflb_spi_poll_exchange(spi, tx_buffer, NULL, 2);
// 取消选中
bflb_gpio_set(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
// DAC channel B
tx_buffer[0] = 0b01010000 | ((y >> 8) & 0xF);
tx_buffer[1] = y & 255;
// 选中DAC
bflb_gpio_reset(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
// 发送数据
bflb_spi_poll_exchange(spi, tx_buffer, NULL, 2);
// 取消选中
bflb_gpio_set(gpio, PIN_NUM_CS);
// 判断激光状态标志位和颜色
if ((instruction.status_code & 0b01000000) == 0)
// 激光标志位第二位等于0,开始draw
{
if (instruction.color <= 9)
{ // RED
bflb_gpio_reset(gpio, PIN_NUM_LASER_R);
}
else if (instruction.color <= 18)
{ // YELLOW
bflb_gpio_reset(gpio, PIN_NUM_LASER_R);
bflb_gpio_reset(gpio, PIN_NUM_LASER_G);
}
else if (instruction.color <= 27)
{ // GREEN
bflb_gpio_reset(gpio, PIN_NUM_LASER_G);
}
else if (instruction.color <= 36)
{ // CYAN
bflb_gpio_reset(gpio, PIN_NUM_LASER_G);
bflb_gpio_reset(gpio, PIN_NUM_LASER_B);
}
else if (instruction.color <= 45)
{ // BLUE
bflb_gpio_reset(gpio, PIN_NUM_LASER_B);
}
else if (instruction.color <= 54)
{ // Magenta
bflb_gpio_reset(gpio, PIN_NUM_LASER_B);
bflb_gpio_reset(gpio, PIN_NUM_LASER_R);
}
else if (instruction.color <= 63)
{ // WHITE
bflb_gpio_reset(gpio, PIN_NUM_LASER_B);
bflb_gpio_reset(gpio, PIN_NUM_LASER_R);
bflb_gpio_reset(gpio, PIN_NUM_LASER_G);
}
}
else
{ // 不亮的Point
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
}
// DAC输出刷新
bflb_gpio_reset(gpio, PIN_NUM_LDAC);
bflb_mtimer_delay_us(1);
bflb_gpio_set(gpio, PIN_NUM_LDAC);
// 画这一帧中的下一个点
draw_position++;
}
// 这一帧已经画完了
else
{
// 关闭激光
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
// 这一帧已经投影完毕,标记为未缓存状态,可以缓存新的内容
ilda->frames[frame_position].isBuffered = false;
// 通知缓存任务可以继续缓存新帧
xTaskNotifyGive(fileBufferHandle);
// 移动到下一帧
frame_position++;
if (frame_position > bufferFrames - 1) {
frame_position = 0;
}
// 重置绘制位置
draw_position = 0;
}
}
// 如果缓冲区为空或者暂停,则不进行绘制,等待缓冲区有数据或者恢复播放
}
// DrawNow模式
else
{
// 这一帧还没画完,并且确保索引不越界
if (draw_position < ilda->frames[0].number_records &&
draw_position < LASER_POINT_BUFFER_SIZE) // 添加边界检查
{
// 位置信号X&Y的处理和DAC输出
const ILDA_Record_t &instruction = ilda->frames[0].records[draw_position];
int y = 2048 + (instruction.x * 1024) / 32768;
int x = 2048 + (instruction.y * 1024) / 32768;
uint8_t tx_buffer[2];
// DAC channel A
tx_buffer[0] = 0b11010000 | ((x >> 8) & 0xF);
tx_buffer[1] = x & 255;
bflb_gpio_reset(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
bflb_spi_poll_exchange(spi, tx_buffer, NULL, 2);
bflb_gpio_set(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
// DAC channel B
tx_buffer[0] = 0b01010000 | ((y >> 8) & 0xF);
tx_buffer[1] = y & 255;
bflb_gpio_reset(gpio, PIN_NUM_CS);
bflb_mtimer_delay_us(1);
bflb_spi_poll_exchange(spi, tx_buffer, NULL, 2);
bflb_gpio_set(gpio, PIN_NUM_CS);
if ((instruction.status_code & 0b01000000) == 0)
{
if ((instruction.color & 0b11100000) == 0b11100000)
{
bflb_gpio_reset(gpio, PIN_NUM_LASER_R);
}
else
{
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
}
if ((instruction.color & 0b00011000) == 0b00011000)
{
bflb_gpio_reset(gpio, PIN_NUM_LASER_G);
}
else
{
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
}
if ((instruction.color & 0b00000111) == 0b00000111)
{
bflb_gpio_reset(gpio, PIN_NUM_LASER_B);
}
else
{
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
}
}
else
{
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
}
// DAC输出刷新
bflb_gpio_reset(gpio, PIN_NUM_LDAC);
bflb_mtimer_delay_us(1);
bflb_gpio_set(gpio, PIN_NUM_LDAC);
draw_position++;
}
else
{
// 关闭激光
bflb_gpio_set(gpio, PIN_NUM_LASER_R);
bflb_gpio_set(gpio, PIN_NUM_LASER_G);
bflb_gpio_set(gpio, PIN_NUM_LASER_B);
// 重置绘制位置
draw_position = 0;
}
}
// 释放SPI互斥锁
xSemaphoreGive(spi_mutex);
}
// 切换媒体文件
void nextMedia(int position)
{
// 如果没有媒体文件,直接返回
if (avaliableMedia.size() == 0) {
printf("[SPI] No media files available\n");
return;
}
// 更新当前媒体索引
curMedia += position;
// 处理索引边界
if (curMedia < 0) {
curMedia = avaliableMedia.size() - 1;
} else if (curMedia >= (int)avaliableMedia.size()) {
curMedia = 0;
}
// 获取文件名
const char* filename = avaliableMedia[curMedia].c_str();
printf("[SPI] Loading media file: %s\n", filename);
// 构建完整路径
char fullpath[64];
snprintf(fullpath, sizeof(fullpath), "/sd/%s", filename);
// 打开文件
if (!ilda->openFile(fullpath)) {
printf("[SPI] Failed to open ILDA file: %s\n", fullpath);
return;
}
// 重置帧状态
for (int i = 0; i < bufferFrames; i++) {
ilda->frames[i].isBuffered = false;
}
// 重置渲染器状态
frame_position = 0;
draw_position = 0;
printf("[SPI] Media switched to %s\n", filename);
}
//SPIRenderer.cpp 实现了复杂的激光投影系统,可在实时性能和流畅播放之间取得平衡。双模式作允许文件播放和交互式绘图,而圆形缓冲系统确保连续运行而不会丢帧。
//使用 SPI 进行 DAC 通信为激光定位提供精确的模拟控制,而 RGB 激光控制则通过时间调制实现颜色投影。