-
Notifications
You must be signed in to change notification settings - Fork 0
/
XY2.cpp
777 lines (631 loc) · 20 KB
/
XY2.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright (c) 2021 Mathema GmbH
// SPDX-License-Identifier: BSD-3-Clause
// Author: Günter Woigk (Kio!)
// Copyright (c) 2021 [email protected]
// BSD 2-clause license
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
//#include "hardware/dma.h"
#include "hardware/pio.h"
#include "hardware/pio_instructions.h"
#include "pico/multicore.h"
#include "cdefs.h"
#include "XY2.h"
#include "XY2-100.pio.h"
#include "hardware/pwm.h"
#include "hardware/sync.h"
#include <string.h>
//static inline FLOAT sin (FLOAT a) { return sinf(a); }
//static inline FLOAT cos (FLOAT a) { return cosf(a); }
static const FLOAT pi = FLOAT(3.1415926538);
// application defined parameter sets, set 0 is used for jump
#define FAST SCANNER_MAX_SPEED
#define SLOW SCANNER_MAX_SPEED*2/3
LaserSet laser_set[8] =
{
#define A LASER_ON_DELAY
#define E LASER_OFF_DELAY
#define M LASER_MIDDLE_DELAY
#define J LASER_JUMP_DELAY
LaserSet{.speed=FAST, .pattern=0x003, .delay_a=0, .delay_m=0, .delay_e=J}, // jump
LaserSet{.speed=FAST, .pattern=0x3FF, .delay_a=A, .delay_m=M, .delay_e=E}, // fast straight
LaserSet{.speed=SLOW, .pattern=0x3FF, .delay_a=A, .delay_m=M, .delay_e=E}, // slow straight
LaserSet{.speed=FAST, .pattern=0x3FF, .delay_a=A, .delay_m=0, .delay_e=E}, // fast rounded
LaserSet{.speed=SLOW, .pattern=0x3FF, .delay_a=A, .delay_m=0, .delay_e=E}, // slow rounded
#undef E
#undef A
#undef M
#undef J
};
#define pio PIO_XY2
//static
uint XY2::heart_beat_counter = 1000;
uint XY2::heart_beat_state = 0;
Point XY2::pos0{};
LaserQueue laser_queue; // command queue
Transformation XY2::transformation0; // transformation used by core0
Transformation XY2::transformation1; // transformation used by core1
Transformation XY2::transformation_stack[8];// transformation used by core0 and push stack
uint XY2::transformation_stack_index = 0;
static constexpr uint transformation_stack_mask = NELEM(XY2::transformation_stack) - 1;
uint XY2::pwm_slice_num;
int XY2::pwm_underruns;
static uint laser_delay_queue[16] = {0};
static uint laser_delay_size = LASER_QUEUE_DELAY;
static uint laser_delay_index = 0;
static_assert(LASER_QUEUE_DELAY <= NELEM(laser_delay_queue),"");
static volatile bool core1_running = false; // core1 was started. only ever set.
static volatile bool core1_suspend = false; // request core1 to suspend & detach from flash
static volatile bool core1_suspended = false; // core1 is suspended & detached from flash
// store new value in laser_delay_queue[] and return old value
uint XY2::delayed_laser_value (uint value)
{
if (laser_delay_index >= laser_delay_size) laser_delay_index = 0;
std::swap(value,laser_delay_queue[laser_delay_index++]);
return value;
}
// core0: push to laser_queue:
void XY2::moveTo (const Point& p)
{
laser_queue.push(CMD_MOVETO);
laser_queue.push(p);
}
void XY2::drawTo (const Point& p, const LaserSet& set)
{
laser_queue.push(CMD_DRAWTO);
laser_queue.push(&set);
laser_queue.push(p);
}
void XY2::drawLine (const Point& p1, const Point& p2, const LaserSet& set)
{
laser_queue.push(CMD_LINE);
laser_queue.push(&set);
laser_queue.push(p1);
laser_queue.push(p2);
}
void XY2::drawRect (const Rect& rect, const LaserSet& set)
{
laser_queue.push(CMD_RECT);
laser_queue.push(&set);
laser_queue.push(rect);
}
void XY2::drawEllipse (const Rect& bbox, FLOAT angle, uint steps, const LaserSet& set)
{
Point center = bbox.center();
FLOAT fx = bbox.width()/2;
FLOAT fy = bbox.height()/2;
FLOAT step = 2*pi / FLOAT(steps);
drawPolyLine(steps,[center,fx,fy,step,&angle]()
{
FLOAT a = angle;
angle += step;
return center + Dist(fx*cos(a),fy*sin(a));
},
set, POLYLINE_CLOSED);
}
void XY2::drawPolyLine (uint count, std::function<Point()> nextPoint, const LaserSet& set,
PolyLineOptions flags)
{
laser_queue.push(CMD_POLYLINE);
laser_queue.push(&set);
laser_queue.push(flags);
laser_queue.push(count);
for (uint i=0; i<count; i++) laser_queue.push(nextPoint());
}
void XY2::drawPolyLine (uint count, const Point points[], const LaserSet& set,
PolyLineOptions flags)
{
laser_queue.push(CMD_POLYLINE);
laser_queue.push(&set);
laser_queue.push(flags);
laser_queue.push(count);
for (uint i=0; i<count; i++) laser_queue.push(points[i]);
}
void XY2::drawPolygon (uint count, std::function<Point()> nextPoint, const LaserSet& set)
{
drawPolyLine(count,nextPoint,set,POLYLINE_CLOSED);
}
void XY2::drawPolygon (uint count, const Point points[], const LaserSet& set)
{
drawPolyLine(count,points,set,POLYLINE_CLOSED);
}
#include "vt_vector_font.h" // int8 vt_font_data[];
static uint vt_font_col1[256]; // index in vt_font_data[]
static int8 vt_font_width[256]; // character print width (including +1 for line width but no spacing)
static void vt_init_vector_font()
{
for (uint i = 0, c=' '; c<NELEM(vt_font_col1) && i<NELEM(vt_font_data); c++)
{
vt_font_col1[c] = i;
i += 2; // left-side mask and right-side mask
int8 width = 0;
while (vt_font_data[i++] > E)
{
while (vt_font_data[i] < E)
{
width = max(width,vt_font_data[i]);
i += 2;
}
}
vt_font_width[c] = width + 1;
assert(vt_font_data[i-1] == E);
}
}
FLOAT printWidth (cstr s)
{
// calculate print width for string
// as printed by drawing command DrawText
int width = 0;
uint8 mask = 0;
while (uint8 c = uint8(*s++))
{
width += vt_font_width[c];
int8* p = vt_font_data + vt_font_col1[c];
if (mask & uint8(*p)) width++; // +1 if glyphs would touch
mask = uint8(*(p+1)); // remember for next
}
return FLOAT(width);
}
void XY2::printText (Point start, FLOAT scale_x, FLOAT scale_y, cstr text, bool centered,
const LaserSet& straight, const LaserSet& rounded)
{
// CMD_PRINT_TEXT, 2*LaserSet, Point, 2*FLOAT, n*char, 0
if (centered) start.x -= printWidth(text) * scale_x / 2;
laser_queue.push(CMD_PRINT_TEXT);
laser_queue.push(&straight);
laser_queue.push(&rounded);
laser_queue.push(start);
laser_queue.push(scale_x);
laser_queue.push(scale_y);
char c; do { laser_queue.push(c = *text++); } while (c);
}
void XY2::update_transformation ()
{
static_assert (sizeof(Data32)==sizeof(FLOAT), "booboo");
laser_queue.push(transformation0.is_projected ? CMD_SET_TRANSFORMATION_3D : CMD_SET_TRANSFORMATION);
uint n = transformation0.is_projected ? 9 : 6;
while (laser_queue.free() < n) { gpio_put(LED_CORE0_IDLE,1); }
gpio_put(LED_CORE0_IDLE,0);
Data32* data = reinterpret_cast<Data32*>(&transformation0);
laser_queue.write(data,n);
}
void XY2::resetTransformation()
{
transformation0.reset();
laser_queue.push(CMD_RESET_TRANSFORMATION);
}
void XY2::pushTransformation()
{
transformation_stack[--transformation_stack_index & transformation_stack_mask] = transformation0;
}
void XY2::popTransformation()
{
transformation0 = transformation_stack[transformation_stack_index++ & transformation_stack_mask];
update_transformation();
}
void XY2::setRotation (FLOAT rad) // and reset scale and shear
{
transformation0.setRotation(rad);
update_transformation();
}
void XY2::setScale (FLOAT f)
{
transformation0.setScale(f);
update_transformation();
}
void XY2::setScale (FLOAT fx, FLOAT fy)
{
transformation0.setScale(fx,fy);
update_transformation();
}
void XY2::setOffset(FLOAT dx, FLOAT dy)
{
transformation0.setOffset(dx,dy);
update_transformation();
}
void XY2::setShear (FLOAT sx, FLOAT sy)
{
transformation0.setShear(sx,sy);
update_transformation();
}
void XY2::setProjection (FLOAT px, FLOAT py, FLOAT pz)
{
transformation0.setProjection(px,py,pz);
update_transformation();
}
void XY2::setRotationAndScale (FLOAT rad, FLOAT fx, FLOAT fy)
{
transformation0.setRotationAndScale(rad,fx,fy);
update_transformation();
}
void XY2::setTransformation (const Transformation& transformation)
{
transformation0 = transformation;
update_transformation();
}
void XY2::setTransformation (FLOAT fx, FLOAT fy, FLOAT sx, FLOAT sy, FLOAT dx, FLOAT dy)
{
new(&transformation0) Transformation(fx,fy,sx,sy,dx,dy);
update_transformation();
}
void XY2::setTransformation (FLOAT fx, FLOAT fy, FLOAT sx, FLOAT sy, FLOAT dx, FLOAT dy, FLOAT px, FLOAT py, FLOAT pz)
{
new(&transformation0) Transformation(fx,fy,sx,sy,dx,dy,px,py,pz);
update_transformation();
}
void XY2::rotate (FLOAT rad)
{
transformation0.rotate(rad);
update_transformation();
}
void XY2::rotateAndScale (FLOAT rad, FLOAT fx, FLOAT fy)
{
transformation0.rotateAndScale(rad,fx,fy);
update_transformation();
}
void XY2::scale (FLOAT f)
{
transformation0.scale(f);
update_transformation();
}
void XY2::scale (FLOAT fx, FLOAT fy)
{
transformation0.scale(fx,fy);
update_transformation();
}
void XY2::addOffset(FLOAT dx, FLOAT dy)
{
transformation0.addOffset(dx,dy);
update_transformation();
}
void XY2::transform (const Transformation& transformation)
{
transformation0.addTransformation(transformation);
update_transformation();
}
void XY2::transform (FLOAT fx, FLOAT fy, FLOAT sx, FLOAT sy, FLOAT dx, FLOAT dy)
{
transformation0.addTransformation(fx,fy,sx,sy,dx,dy);
update_transformation();
}
void XY2::start()
{
if (!core1_running) multicore_launch_core1(XY2::worker);
while (!core1_running) {}
printf("core1 up and running\n");
}
void XY2::init()
{
// initialize pio and state machines
// initialize LEDs:
gpio_init(led_heartbeat); gpio_set_dir(led_heartbeat, GPIO_OUT); gpio_put(led_heartbeat,0);
gpio_init(led_core0_idle); gpio_set_dir(led_core0_idle, GPIO_OUT); gpio_put(led_core0_idle,0);
gpio_init(led_core1_idle); gpio_set_dir(led_core1_idle, GPIO_OUT); gpio_put(led_core1_idle,0);
gpio_init(led_error); gpio_set_dir(led_error, GPIO_OUT); gpio_put(led_error,0);
// initialize pio:
// for some reason this is done using a state machine:
// set initial pin states in the pio:
// set all to 1 except laser:
uint mask = (1<<pin_laser)+(1<<pin_sync_xy)+(3<<pin_clock)+(3<<pin_sync)+(3<<pin_x)+(3<<pin_y);
uint value = ~(1u<<pin_laser);
pio_sm_set_pins_with_mask(pio, sm_x, value, mask);
// set i/o direction for pins in the pio:
// set all to output:
pio_sm_set_pindirs_with_mask(pio, sm_x, -1u, mask);
// set gpio to use pio output for pins:
// for some reason there is no convenience method:
for(uint i=0;i<32;i++) { if (mask & (1<<i)) pio_gpio_init(pio, i); }
// initialize state machines:
// load CLOCK program into pio:
uint offset = pio_add_program(pio, &xy2_clock_program);
// configure the SM:
pio_sm_config c = xy2_clock_program_get_default_config(offset);
sm_config_set_sideset_pins(&c, pin_clock);
sm_config_set_clkdiv(&c, float(MAIN_CLOCK) / XY2_SM_CLOCK);
pio_sm_init(pio, sm_clock, offset + xy2_clock_offset_start, &c);
// load DATA program into pio:
offset = pio_add_program(pio, &xy2_data_program);
// configure state machines:
c = xy2_data_program_get_default_config(offset);
sm_config_set_clkdiv(&c, float(MAIN_CLOCK) / XY2_SM_CLOCK);
sm_config_set_out_shift(&c, false/*shift left not right*/, false /*!autopull*/, 32 /*pull_threshold*/);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
sm_config_set_jmp_pin(&c, pin_sync_xy);
sm_config_set_sideset_pins(&c, pin_x); // pins for 'side setting' of x+ and x-
pio_sm_init(pio, sm_x, offset + xy2_data_offset_start, &c);
sm_config_set_sideset_pins(&c, pin_y); // pins for 'side setting' of y+ and y-
pio_sm_init(pio, sm_y, offset + xy2_data_offset_start, &c);
// load LASER program into pio:
offset = pio_add_program(pio, &xy2_laser_program);
// configure state machines:
c = xy2_laser_program_get_default_config(offset);
sm_config_set_clkdiv(&c, float(MAIN_CLOCK) / XY2_SM_CLOCK);
sm_config_set_out_shift(&c, true/*shift right*/, false /*!autopull*/, 10 /*pull_threshold*/);
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
sm_config_set_mov_status(&c, STATUS_TX_LESSTHAN, 1); // for MOV X,STATUS
sm_config_set_out_pins(&c, pin_laser, 1); // pin used to shift out laser data
sm_config_set_sideset_pins(&c, pin_sync_xy); // pin used for fifo synchronization
pio_sm_init(pio, sm_laser, offset + xy2_laser_offset_start, &c);
// final configuration of GPIOs:
gpio_set_outover(pin_laser,GPIO_OVERRIDE_INVERT); // invert output: '1' = ON => pin low
// count underruns:
// => setup a counter to count pulses on PIN_XY2_SYNC_XY
// which should be high all the time but is low on fifo empty state.
// it seems not possible to read back PIN_XY2_SYNC_XY directly:
const uint gpio = pin_sync_xy_readback;
// Only PWM B pins (odd pin numbers) can be used as inputs:
assert(pwm_gpio_to_channel(gpio) == PWM_CHAN_B);
pwm_slice_num = pwm_gpio_to_slice_num(gpio);
// configure the PWM for counter mode:
pwm_config cfg = pwm_get_default_config();
pwm_config_set_clkdiv_mode(&cfg, PWM_DIV_B_RISING);
pwm_init(pwm_slice_num, &cfg, true);
gpio_set_function(gpio, GPIO_FUNC_PWM);
gpio_set_dir(gpio,GPIO_IN);
pwm_underruns = pwm_get_counter(pwm_slice_num);
// misc.:
vt_init_vector_font();
}
void __no_inline_not_in_flash_func(suspend_no_flash) ()
{
core1_suspended = true;
do { __wfe(); } while (core1_suspend);
core1_suspended = false;
}
void XY2::suspend()
{
core1_suspend = true;
do { __sev(); } while (!core1_suspended);
}
void XY2::resume()
{
core1_suspend = false;
do { __sev(); } while (core1_suspended);
}
void XY2::worker()
{
while (laser_queue.avail()) (void)laser_queue.pop();
core1_running = true;
core1_suspended = false;
// start the state machines:
pio_sm_set_enabled(pio, sm_laser, false);
pio_sm_set_enabled(pio, sm_clock, false);
pio_sm_set_enabled(pio, sm_x, false);
pio_sm_set_enabled(pio, sm_y, false);
pio_sm_clear_fifos(pio, sm_laser);
pio_sm_clear_fifos(pio, sm_x);
pio_sm_clear_fifos(pio, sm_y);
// Enable multiple PIO state machines synchronizing their clock dividers
pio_enable_sm_mask_in_sync(pio, (1<<sm_clock)+(1<<sm_x)+(1<<sm_y)+(1<<sm_laser));
// send first point to center and switch off laser
memset(laser_delay_queue,0,sizeof(laser_delay_queue));
pio_send_data(FLOAT(0),FLOAT(0), 0x000);
for(;;)
{
if (core1_suspend)
{
uint old_state = save_and_disable_interrupts();
suspend_no_flash();
restore_interrupts(old_state);
}
DrawCmd cmd = laser_queue.pop().cmd;
switch(cmd)
{
default:
{
gpio_put(led_error,1);
for(;;);
}
case CMD_END:
{
return;
}
case CMD_MOVETO: // Point
{
Point p1 = laser_queue.pop_Point();
move_to(p1);
continue;
}
case CMD_DRAWTO: // LaserSet, Point
{
const LaserSet* set = laser_queue.pop().set;
Point p1 = laser_queue.pop_Point();
uint laser_on_delay=0;
draw_to(p1,set->speed,set->pattern,laser_on_delay,set->delay_m);
continue;
}
case CMD_LINETO: // LaserSet, Point
{
const LaserSet* set = laser_queue.pop().set;
Point p1 = laser_queue.pop_Point();
line_to(p1,*set);
continue;
}
case CMD_LINE: // LaserSet, 2*Point
{
const LaserSet* set = laser_queue.pop().set;
Point p1 = laser_queue.pop_Point();
Point p2 = laser_queue.pop_Point();
draw_line(p1,p2,*set);
continue;
}
case CMD_RECT: // LaserSet, Rect
{
const LaserSet* set = laser_queue.pop().set;
Rect rect = laser_queue.pop_Rect();
draw_rect(rect,*set);
continue;
}
case CMD_POLYLINE: // LaserSet, flags, n, n*Point
{
const LaserSet* set = laser_queue.pop().set;
uint flags = laser_queue.pop().u;
uint count = laser_queue.pop().u;
draw_polyline(count, [](){return laser_queue.pop_Point();}, *set, flags);
continue;
}
case CMD_PRINT_TEXT: // 2*LaserSet, Point, 2*FLOAT, n*char, 0
{
const LaserSet* straight = laser_queue.pop().set;
const LaserSet* rounded = laser_queue.pop().set;
Point start = laser_queue.pop_Point();
FLOAT scale_x = laser_queue.pop().f;
FLOAT scale_y = laser_queue.pop().f;
uint8 rmask = 0;
while (char c = char(laser_queue.pop().u))
{
print_char (start, scale_x, scale_y, *straight, *rounded, rmask, c);
}
continue;
}
case CMD_RESET_TRANSFORMATION: // --
{
transformation1.reset();
continue;
}
case CMD_SET_TRANSFORMATION: // fx fy sx sy dx dy
{
while (laser_queue.avail()<6) {} // __wfi
Data32* dest = reinterpret_cast<Data32*>(&transformation1);
laser_queue.read(dest,6);
transformation1.is_projected = false;
continue;
}
case CMD_SET_TRANSFORMATION_3D: // fx fy sx sy dx dy px py pz
{
while (laser_queue.avail()<9) {} // __wfi
Data32* dest = reinterpret_cast<Data32*>(&transformation1);
laser_queue.read(dest,9);
transformation1.is_projected = true;
continue;
}
}
}
}
uint16 XY2::getUnderruns()
{
int d = pwm_get_counter(pwm_slice_num) - pwm_underruns;
pwm_underruns += d;
return uint16(d);
}
void __not_in_flash_func(XY2::draw_to) (Point dest, FLOAT speed, uint laser_on_pattern, uint& laser_on_delay, uint end_delay)
{
// draw line to dest with speed
// while laser_on_delay > 0 use laser_off_pattern
// thereafter use laser_on_pattern
// at end of line wait delay
transformation1.transform(dest);
Dist dist = dest - pos0;
FLOAT line_length = dist.length(); // SQRT
Dist step = dist * (speed / line_length);
if (laser_on_delay)
{
uint laser_off_pattern = laser_set[0].pattern;
while (line_length > speed)
{
line_length -= speed;
send_data_blocking(pos0+step, laser_off_pattern);
if (--laser_on_delay == 0) goto a;
}
if (pos0 != dest)
{
send_data_blocking(dest, laser_off_pattern);
if (--laser_on_delay == 0) goto c;
}
while (end_delay--)
{
send_data_blocking(dest, laser_off_pattern);
if (--laser_on_delay == 0) goto c;
}
}
else
{
a: while (line_length > speed)
{
line_length -= speed;
send_data_blocking(pos0+step, laser_on_pattern);
}
if (pos0 != dest)
{
send_data_blocking(dest, laser_on_pattern);
}
c: while (end_delay--)
{
send_data_blocking(dest, laser_on_pattern);
}
}
}
void XY2::line_to (const Point& dest, const LaserSet& set)
{
const FLOAT speed = set.speed;
const uint laser_on_pattern = set.pattern;
const uint delay = set.delay_e;
uint laser_on_delay = set.delay_a;
draw_to(dest,speed,laser_on_pattern,laser_on_delay,delay);
}
void __not_in_flash_func(XY2::draw_polyline) (uint count, std::function<Point()> next_point, const LaserSet& set, uint flags)
{
// draw polygon or polyline with 'count' points
//
// support for drawing long lines in chunks:
// if flags = no_start then resume drawing previous line without jump to first point
// if flags = no_end then don't wait for line end but for middle point only after last point
// if flags = closed then draw closing line for a polygon
bool closed = flags == POLYLINE_CLOSED; // => POLYGON
bool no_start = flags & POLYLINE_NO_START;
bool no_end = flags & POLYLINE_NO_END;
// get starting point and jump to it:
Point start;
if (!no_start && count--) { start = next_point(); move_to(start); }
if (count == 0) return;
uint laser_on_delay = no_start ? 0 : set.delay_a;
const FLOAT speed = set.speed;
const uint laser_on_pattern = set.pattern;
uint delay = set.delay_m;
while (count--)
{
Point dest = next_point();
if (count==0 && !no_end) delay = set.delay_e;
draw_to(dest,speed,laser_on_pattern,laser_on_delay,delay);
}
if (closed) draw_to(start,speed,laser_on_pattern,laser_on_delay,set.delay_e);
}
void XY2::draw_line (const Point& start, const Point& dest, const LaserSet& set)
{
move_to(start);
line_to(dest,set);
}
void XY2::draw_rect (const Rect& bbox, const LaserSet& set)
{
move_to(bbox.top_left());
line_to(bbox.top_right(), set);
line_to(bbox.bottom_right(), set);
line_to(bbox.bottom_left(), set);
line_to(bbox.top_left(), set);
}
void XY2::print_char (Point& p0, FLOAT scale_x, FLOAT scale_y, const LaserSet& straight, const LaserSet& rounded, uint8& rmask, char c)
{
int8* p = vt_font_data + vt_font_col1[uchar(c)];
uint lmask = uint8(*p++);
if (rmask & lmask) p0.x += scale_x; // apply kerning
rmask = uint8(*p++); // for next kerning
while (*p != E)
{
int line_type = *p++;
const LaserSet& set = line_type == L ? straight : rounded;
Point pt;
pt.x = p0.x + *p++ * scale_x;
pt.y = p0.y + *p++ * scale_y;
move_to(pt);
uint delay_a = set.delay_a;
while (*p < E)
{
pt.x = p0.x + *p++ * scale_x;
pt.y = p0.y + *p++ * scale_y;
draw_to(pt, set.speed, set.pattern, delay_a, *p<E ? set.delay_m : set.delay_e);
}
}
p0.x += vt_font_width[uchar(c)] * scale_x; // update print position
}