sys_display.c 24.4 KB
Newer Older
Rahix's avatar
Rahix committed
1
#include "epicardium.h"
2
#include <stdint.h>
Rahix's avatar
Rahix committed
3

Gerd's avatar
Gerd committed
4
5
#include "py/obj.h"
#include "py/objint.h"
Rahix's avatar
Rahix committed
6
#include "py/objstr.h"
Gerd's avatar
Gerd committed
7
8
#include "py/runtime.h"

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
//#include <stdio.h>

#if 1
#define CTX_MIN_JOURNAL_SIZE    384
#define CTX_MAX_JOURNAL_SIZE    384
#define CTX_RASTERIZER          0
#define CTX_GRADIENT_CACHE      0
#define CTX_LIMIT_FORMATS       1
#define CTX_RENDERSTREAM_STATIC 1
#define CTX_EVENTS              0
#define CTX_PARSER              0
#define CTX_FORMATTER           0
#define CTX_FONTS_FROM_FILE     0
#define CTX_IMPLEMENTATION      1
#define _CTX_INTERNAL_FONT_

#define malloc  m_malloc
#define realloc m_realloc
#define free    m_free
#include "../../lib/ctx/ctx-font-regular-spacing.h"
#include "../../lib/ctx/ctx.h"
#undef free
#undef malloc
#undef realloc

static Ctx *ctx = NULL;

void full_cb (CtxRenderstream *rs, void *data)
{
	epic_disp_ctx (rs->entries, rs->count * 9);
	rs->count = 0;
}

static Ctx *ensure_ctx (void)
{
  if (!ctx)
  {
    ctx = ctx_new ();
    ctx_reset (ctx);
    //ctx_set_full_cb (ctx, full_cb, NULL);


  }
  CtxRenderstream *rs = &ctx->renderstream;

  /* with full_cb this used to be more precise - and
   * it will go wrong with very long text strings.
   */
  if (rs->count > CTX_MAX_JOURNAL_SIZE-64)
  {
    epic_disp_ctx (rs->entries, rs->count * 9);
    rs->count = 0;
  }
  return ctx;
}
#endif
Gerd's avatar
Gerd committed
65
66
67
68
69
70
71

static uint16_t rgb888_to_rgb565(uint8_t *bytes)
{
	return ((bytes[0] & 0b11111000) << 8) | ((bytes[1] & 0b11111100) << 3) |
	       (bytes[2] >> 3);
}

72
73
74
75
76
77
78
79
80
81
static inline void rgb565_to_rgb888 (uint16_t pixel, 
                                     uint8_t *red,
                                     uint8_t *green, 
                                     uint8_t *blue) 
{ 
  *blue   = (pixel & 31)<<3; 
  *green  = ((pixel>>5) & 63)<<2; 
  *red    = ((pixel>>11) & 31)<<3; 
}

Gerd's avatar
Gerd committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
static uint16_t get_color(mp_obj_t color_in)
{
	if (mp_obj_get_int(mp_obj_len(color_in)) < 3) {
		mp_raise_ValueError("color must have 3 elements");
	}

	uint8_t red = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL)
	);
	uint8_t green = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL)
	);
	uint8_t blue = mp_obj_get_int(
		mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL)
	);
	uint8_t colors[3] = { red, green, blue };
	return rgb888_to_rgb565(colors);
}

101
102
103
104
static int ctx_mode = 0;
static int ctx_mode_text = 2;
static int ctx_commands_received = 0;

Gerd's avatar
Gerd committed
105
106
107
108
109
110
111
/* print something on the display */
static mp_obj_t mp_display_print(size_t n_args, const mp_obj_t *args)
{
	if (!mp_obj_is_str_or_bytes(args[0])) {
		mp_raise_TypeError("input text must be a string");
	}
	GET_STR_DATA_LEN(args[0], print, print_len);
112
113
114
115
	int32_t posx = mp_obj_get_int(args[1]);
	int32_t posy = mp_obj_get_int(args[2]);
	uint32_t fg  = get_color(args[3]);
	uint32_t bg  = get_color(args[4]);
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
	int res = 0;
	if (ctx_mode || ctx_mode_text) {
		Ctx *ctx = ensure_ctx();
		float font_size = 20.0f;
		ctx_font_size (ctx, font_size);
		if (fg!=bg) {
	  		uint8_t r,g,b;
          		rgb565_to_rgb888 (bg, &r, &g, &b);
	  		ctx_rgba8 (ctx, r, g, b, 255);
	  		ctx_rectangle (ctx, posx, posy,
				       ctx_text_width (ctx, (const char*)print), font_size);
	  		ctx_fill (ctx);
		}
		{
	  		uint8_t r,g,b;
          		rgb565_to_rgb888 (fg, &r, &g, &b);
	  		ctx_rgba8 (ctx, r, g, b, 255);
		}
		ctx_move_to (ctx, posx, posy + font_size * 0.8f);
		ctx_text (ctx, (const char*)print);
		if (!ctx_mode)
		{
			CtxRenderstream *rs = &ctx->renderstream;
			epic_disp_ctx (rs->entries, rs->count * 9);
    		        ctx_reset (ctx);
                        ctx_commands_received=0;
			rs->count = 0;
		}
	} else {
		res = epic_disp_print(posx, posy, (const char *)print, fg, bg);
	}
Gerd's avatar
Gerd committed
147
148
149
150
151
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
152
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
153
154
	display_print_obj, 5, 5, mp_display_print
);
Gerd's avatar
Gerd committed
155

156
157
158
159
160
161
162
/* print something on the display */
static mp_obj_t mp_display_print_adv(size_t n_args, const mp_obj_t *args)
{
	if (!mp_obj_is_str_or_bytes(args[0])) {
		mp_raise_TypeError("input text must be a string");
	}
	GET_STR_DATA_LEN(args[0], print, print_len);
163
164
	int32_t posx     = mp_obj_get_int(args[1]);
	int32_t posy     = mp_obj_get_int(args[2]);
165
166
167
	uint32_t fg      = get_color(args[3]);
	uint32_t bg      = get_color(args[4]);
	uint8_t fontName = mp_obj_get_int(args[5]);
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
	int res = 0;
	if (ctx_mode || ctx_mode_text) {
		Ctx *ctx = ensure_ctx();
		float font_size = 20.0f;
		switch (fontName)
		{
	  		case 0: font_size = 8.0;  break;
	  		case 1: font_size = 12.0; break;
	  		case 2: font_size = 16.0; break;
	  		case 3: font_size = 20.0; break;
	  		case 4: font_size = 24.0; break;
	  		case 5: font_size = 32.0; break;
	  		case 6: font_size = 40.0; break;
	  		case 7: font_size = 64.0; break;
	  		case 8: font_size = 80.0; break;
		}
		ctx_font_size (ctx, font_size);
		if (fg != bg) {
			uint8_t r,g,b;
			rgb565_to_rgb888 (bg, &r, &g, &b);
	  		ctx_rgba8 (ctx, r, g, b, 255);
	  		ctx_rectangle (ctx, posx, posy,
			               ctx_text_width (ctx, (const char*)print), font_size);
	  		ctx_fill (ctx);
		}
		{
			uint8_t r,g,b;
			rgb565_to_rgb888 (fg, &r, &g, &b);
			ctx_rgba8 (ctx, r, g, b, 255);
		}
		ctx_move_to (ctx, posx, posy + font_size * 0.8);
		ctx_text (ctx, (const char*)print);
		if (!ctx_mode)
		{
			CtxRenderstream *rs = &ctx->renderstream;
			epic_disp_ctx (rs->entries, rs->count * 9);
    		        ctx_reset (ctx);
                        ctx_commands_received=0;
			rs->count = 0;
		}
	} else {
		res = epic_disp_print_adv(
                	fontName, posx, posy, (const char *)print, fg, bg);
	}
212
213
214
215
216
217
218
219
220
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_print_adv_obj, 6, 6, mp_display_print_adv
);

221
222
223
/* draw pixel on the display */
static mp_obj_t mp_display_pixel(size_t n_args, const mp_obj_t *args)
{
224
225
	int16_t x    = mp_obj_get_int(args[0]);
	int16_t y    = mp_obj_get_int(args[1]);
226
227
	uint16_t col = get_color(args[2]);

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	int res = 0;
	if (ctx_mode) {
		Ctx *ctx = ensure_ctx();
		uint8_t r,g,b;
		rgb565_to_rgb888 (col, &r, &g, &b);
#if 0
		ctx_rgb (ctx, r/255.0, g/255.0, b/255.0);
		ctx_rectangle (ctx, x, y, 1, 1);
		ctx_fill (ctx);
#else
		ctx_set_pixel_u8 (ctx, x, y, r, g, b, 255);
#endif
	} else {
		res = epic_disp_pixel (x, y, col);
	}
243
244
245
246
247
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
248
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
249
250
	display_pixel_obj, 3, 3, mp_display_pixel
);
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* set display backlight brightness */
static mp_obj_t mp_display_backlight(size_t n_args, const mp_obj_t *args)
{
	uint16_t brightness = mp_obj_get_int(args[0]);

	int res = epic_disp_backlight(brightness);
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_backlight_obj, 1, 1, mp_display_backlight
);

267
268


Gerd's avatar
Gerd committed
269
270
271
/* draw line on the display */
static mp_obj_t mp_display_line(size_t n_args, const mp_obj_t *args)
{
272
273
274
275
	int16_t xs   = mp_obj_get_int(args[0]);
	int16_t ys   = mp_obj_get_int(args[1]);
	int16_t xe   = mp_obj_get_int(args[2]);
	int16_t ye   = mp_obj_get_int(args[3]);
Gerd's avatar
Gerd committed
276
277
278
279
	uint16_t col = get_color(args[4]);
	uint16_t ls  = mp_obj_get_int(args[5]);
	uint16_t ps  = mp_obj_get_int(args[6]);

280
	if (ls > 1) {
Gerd's avatar
Gerd committed
281
282
283
		mp_raise_ValueError("Line style has to be 0 or 1");
	}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	int res = 0;
	if (ctx_mode) {
		Ctx *ctx = ensure_ctx();
		ctx_line_width (ctx, ps);
		ctx_move_to (ctx, xs, ys);
		ctx_line_to (ctx, xe, ye);
		uint8_t r,g,b;
		rgb565_to_rgb888 (col, &r, &g, &b);
		if (ls == LINESTYLE_DOTTED)
			ctx_rgba8 (ctx, r, g, b, 127);
		else
			ctx_rgba8 (ctx, r, g, b, 255);
		ctx_stroke (ctx);
	} else {
		res = epic_disp_line(xs, ys, xe, ye, col, ls, ps);
	}
Gerd's avatar
Gerd committed
300
301
302
303
304
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
305
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
306
307
	display_line_obj, 7, 7, mp_display_line
);
Gerd's avatar
Gerd committed
308
309
310
311

/* draw rectangle on the display */
static mp_obj_t mp_display_rect(size_t n_args, const mp_obj_t *args)
{
312
313
314
315
	int16_t xs   = mp_obj_get_int(args[0]);
	int16_t ys   = mp_obj_get_int(args[1]);
	int16_t xe   = mp_obj_get_int(args[2]);
	int16_t ye   = mp_obj_get_int(args[3]);
Gerd's avatar
Gerd committed
316
317
318
319
	uint16_t col = get_color(args[4]);
	uint16_t fs  = mp_obj_get_int(args[5]);
	uint16_t ps  = mp_obj_get_int(args[6]);

320
	if (fs > 1) {
Gerd's avatar
Gerd committed
321
322
323
		mp_raise_ValueError("Fill style has to be 0 or 1");
	}

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
	int res = 0;
	if (ctx_mode) {
		Ctx *ctx = ensure_ctx();
		ctx_rectangle (ctx, xs, ys, xe-xs, ye-ys);
		uint8_t r,g,b;
		rgb565_to_rgb888 (col, &r, &g, &b);
		ctx_rgba8 (ctx, r, g, b, 255);

		if (fs == FILLSTYLE_FILLED)
		{
			ctx_fill (ctx);
		}
		else
		{
			ctx_line_width (ctx, ps);
			ctx_stroke (ctx);
		}
	} else {
		res = epic_disp_rect(xs, ys, xe, ye, col, fs, ps);
	}
Gerd's avatar
Gerd committed
344
345
346
347
348
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
349
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
350
351
	display_rect_obj, 7, 7, mp_display_rect
);
Gerd's avatar
Gerd committed
352
353
354
355

/* draw rectangle on the display */
static mp_obj_t mp_display_circ(size_t n_args, const mp_obj_t *args)
{
356
357
	int16_t x    = mp_obj_get_int(args[0]);
	int16_t y    = mp_obj_get_int(args[1]);
Gerd's avatar
Gerd committed
358
359
360
361
362
	uint16_t rad = mp_obj_get_int(args[2]);
	uint16_t col = get_color(args[3]);
	uint16_t fs  = mp_obj_get_int(args[4]);
	uint16_t ps  = mp_obj_get_int(args[5]);

363
	if (fs > 1) {
364
	  mp_raise_ValueError("Fill style has to be 0 or 1");
Gerd's avatar
Gerd committed
365
366
	}

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
	int res = 0;
	if (ctx_mode) {
		Ctx *ctx = ensure_ctx();
		ctx_arc (ctx, x, y, rad, 0.0, CTX_PI*1.85, 0);
		uint8_t r,g,b;
		rgb565_to_rgb888 (col, &r, &g, &b);
		if (fs == FILLSTYLE_EMPTY && ps == LINESTYLE_DOTTED)
			ctx_rgba8 (ctx, r, g, b, 127);
		/*XXX:dotted and half transparent isn't really the same thing.. */
		else
			ctx_rgba8 (ctx, r, g, b, 255);

		if (fs == FILLSTYLE_FILLED) {
			ctx_fill (ctx);
		} else {
			ctx_line_width (ctx, ps);
			ctx_stroke (ctx);
		}
	} else {
		res = epic_disp_circ(x, y, rad, col, fs, ps);
	}
Gerd's avatar
Gerd committed
388
	if (res < 0) {
389
	  mp_raise_OSError(-res);
Gerd's avatar
Gerd committed
390
391
392
	}
	return mp_const_none;
}
393
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
394
395
	display_circ_obj, 6, 6, mp_display_circ
);
Gerd's avatar
Gerd committed
396
397
398
399
400

/* clear the display */
static mp_obj_t mp_display_clear(mp_obj_t col)
{
	uint16_t color = get_color(col);
401
402
403
404
405
406
407
408
409
410
411
412
	int res = 0;
	if (ctx_mode) {
		Ctx *ctx = ensure_ctx();
        	ctx_reset (ctx);
		uint8_t r,g,b;
		rgb565_to_rgb888 (color, &r, &g, &b);
		ctx_rgba8 (ctx, r, g, b, 255);
		ctx_rectangle (ctx, 0, 0, DISP_WIDTH, DISP_HEIGHT);
		ctx_fill (ctx);
    	} else {
		res = epic_disp_clear(color);
	}
Gerd's avatar
Gerd committed
413
414
415
416
417
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
418
static MP_DEFINE_CONST_FUN_OBJ_1(display_clear_obj, mp_display_clear);
Gerd's avatar
Gerd committed
419
420
421

static mp_obj_t mp_display_update()
{
422
423
424
425
426
427
428
	if (ctx_mode || ctx_commands_received) {
		Ctx *ctx = ensure_ctx ();
    		epic_disp_ctx (ctx->renderstream.entries,
			       ctx->renderstream.count * 9);
    		ctx_reset (ctx);
                ctx_commands_received=0;
	}
Gerd's avatar
Gerd committed
429
430
431
432
433
434
	int res = epic_disp_update();
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
435
static MP_DEFINE_CONST_FUN_OBJ_0(display_update_obj, mp_display_update);
Gerd's avatar
Gerd committed
436
437
438
439
440
441
442
443
444

static mp_obj_t mp_display_open()
{
	int res = epic_disp_open();
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
445
static MP_DEFINE_CONST_FUN_OBJ_0(display_open_obj, mp_display_open);
Gerd's avatar
Gerd committed
446
447
448
449
450
451
452
453
454

static mp_obj_t mp_display_close()
{
	int res = epic_disp_close();
	if (res < 0) {
		mp_raise_OSError(-res);
	}
	return mp_const_none;
}
455
static MP_DEFINE_CONST_FUN_OBJ_0(display_close_obj, mp_display_close);
Gerd's avatar
Gerd committed
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
static mp_obj_t mp_display_scale(mp_obj_t arg0, mp_obj_t arg1)
{
	float x  = mp_obj_get_float(arg0);
	float y  = mp_obj_get_float(arg1);
        ctx_scale (ensure_ctx(), x, y);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_scale_obj, mp_display_scale);

static mp_obj_t mp_display_translate(mp_obj_t arg0, mp_obj_t arg1)
{
	float x  = mp_obj_get_float(arg0);
	float y  = mp_obj_get_float(arg1);
        ctx_translate (ensure_ctx(), x, y);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_translate_obj, mp_display_translate);

static mp_obj_t mp_display_rotate(mp_obj_t arg)
{
	float x1  = mp_obj_get_float(arg);
        ctx_rotate (ensure_ctx(), x1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_rotate_obj, mp_display_rotate);

static mp_obj_t mp_display_line_width(mp_obj_t arg)
{
	float x1  = mp_obj_get_float(arg);
        ctx_line_width (ensure_ctx(), x1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_line_width_obj,
                                 mp_display_line_width);

static mp_obj_t mp_display_gray(mp_obj_t arg)
{
	float x1  = mp_obj_get_float(arg);
        ctx_gray (ensure_ctx(), x1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_gray_obj,
                                 mp_display_gray);

static mp_obj_t mp_display_global_alpha(mp_obj_t arg)
{
	float x1  = mp_obj_get_float(arg);
        ctx_global_alpha (ensure_ctx(), x1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_global_alpha_obj,
                                 mp_display_global_alpha);

static mp_obj_t mp_display_font_size(mp_obj_t arg)
{
	float x1  = mp_obj_get_float(arg);
        ctx_font_size (ensure_ctx(), x1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_font_size_obj,
                                 mp_display_font_size);



static mp_obj_t mp_display_line_to(mp_obj_t arg0, mp_obj_t arg1)
{
	float x1  = mp_obj_get_float(arg0);
	float y1  = mp_obj_get_float(arg1);
        ctx_line_to (ensure_ctx(), x1, y1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_line_to_obj,
                                 mp_display_line_to);

static mp_obj_t mp_display_move_to(mp_obj_t arg0, mp_obj_t arg1)
{
	float x1 = mp_obj_get_float(arg0);
	float y1 = mp_obj_get_float(arg1);
        ctx_move_to (ensure_ctx(), x1, y1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_move_to_obj,
                                 mp_display_move_to);

static mp_obj_t mp_display_quad_to(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
        ctx_quad_to (ensure_ctx(), x1, y1, x2, y2);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_quad_to_obj, 4, 4, mp_display_quad_to
);

static mp_obj_t mp_display_linear_gradient(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
        ctx_linear_gradient (ensure_ctx(), x1, y1, x2, y2);
	return mp_const_none;
}

static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_linear_gradient_obj, 4, 4, mp_display_linear_gradient
);

static mp_obj_t mp_display_radial_gradient(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float r1  = mp_obj_get_float(args[2]);
	float x2  = mp_obj_get_float(args[3]);
	float y2  = mp_obj_get_float(args[4]);
	float r2  = mp_obj_get_float(args[5]);
        ctx_radial_gradient (ensure_ctx(), x1, y1, r1, x2, y2, r2);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_radial_gradient_obj, 6, 6, mp_display_radial_gradient
);

static mp_obj_t mp_display_rectangle(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
        ctx_rectangle (ensure_ctx(), x1, y1, x2, y2);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_rectangle_obj, 4, 4, mp_display_rectangle
);


static mp_obj_t mp_display_curve_to(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
	float x3  = mp_obj_get_float(args[4]);
	float y3  = mp_obj_get_float(args[5]);
        ctx_curve_to (ensure_ctx(), x1, y1, x2, y2, x3, y3);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_curve_to_obj, 6, 6, mp_display_curve_to
);

static mp_obj_t mp_display_arc(size_t n_args, const mp_obj_t *args)
{
	float x  = mp_obj_get_float(args[0]);
	float y  = mp_obj_get_float(args[1]);
	float radius  = mp_obj_get_float(args[2]);
	float angle1  = mp_obj_get_float(args[3]);
	float angle2  = mp_obj_get_float(args[4]);
	int direction  = mp_obj_get_int(args[5]);
        ctx_arc (ensure_ctx(), x, y, radius, angle1, angle2, direction);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_arc_obj, 6, 6, mp_display_arc
);

static mp_obj_t mp_display_rel_line_to(mp_obj_t arg0, mp_obj_t arg1)
{
	float x1  = mp_obj_get_float(arg0);
	float y1  = mp_obj_get_float(arg1);
        ctx_rel_line_to (ensure_ctx(), x1, y1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_rel_line_to_obj,
                                 mp_display_rel_line_to);

static mp_obj_t mp_display_rel_move_to(mp_obj_t arg0, mp_obj_t arg1)
{
	float x1  = mp_obj_get_float(arg0);
	float y1  = mp_obj_get_float(arg1);
        ctx_rel_move_to (ensure_ctx(), x1, y1);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(display_rel_move_to_obj,
                                 mp_display_rel_move_to);

static mp_obj_t mp_display_rel_quad_to(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
        ctx_rel_quad_to (ensure_ctx(), x1, y1, x2, y2);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_rel_quad_to_obj, 4, 4, mp_display_rel_quad_to
);

static mp_obj_t mp_display_rel_curve_to(size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
	float x3  = mp_obj_get_float(args[4]);
	float y3  = mp_obj_get_float(args[5]);
        ctx_rel_curve_to (ensure_ctx(), x1, y1, x2, y2, x3, y3);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_rel_curve_to_obj, 6, 6, mp_display_rel_curve_to
);

static mp_obj_t mp_display_rgba (size_t n_args, const mp_obj_t *args)
{
	float x1  = mp_obj_get_float(args[0]);
	float y1  = mp_obj_get_float(args[1]);
	float x2  = mp_obj_get_float(args[2]);
	float y2  = mp_obj_get_float(args[3]);
        ctx_rgba (ensure_ctx(), x1, y1, x2, y2);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_rgba_obj, 4, 4, mp_display_rgba
);

static mp_obj_t mp_display_gradient_add_stop (size_t n_args, const mp_obj_t *args)
{
	float pos  = mp_obj_get_float(args[0]);
	float r  = mp_obj_get_float(args[1]);
	float g  = mp_obj_get_float(args[2]);
	float b  = mp_obj_get_float(args[3]);
	float a  = mp_obj_get_float(args[4]);
        ctx_gradient_add_stop (ensure_ctx(), pos, r, g, b, a);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
	display_gradient_add_stop_obj, 5, 5, mp_display_gradient_add_stop
);

static mp_obj_t mp_display_fill ()
{
        ctx_fill (ensure_ctx());
        ctx_commands_received ++;
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_fill_obj, mp_display_fill);

static mp_obj_t mp_display_clip ()
{
        ctx_clip (ensure_ctx());
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_clip_obj, mp_display_clip);

static mp_obj_t mp_display_stroke ()
{
        ctx_stroke (ensure_ctx());
        ctx_commands_received ++;
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_stroke_obj, mp_display_stroke);

static mp_obj_t mp_display_save ()
{
        ctx_save (ensure_ctx());
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_save_obj, mp_display_save);

static mp_obj_t mp_display_restore ()
{
        ctx_restore (ensure_ctx());
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_restore_obj,
                                 mp_display_restore);

static mp_obj_t mp_display_begin_path ()
{
        ctx_begin_path (ensure_ctx());
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_begin_path_obj, mp_display_begin_path);

static mp_obj_t mp_display_close_path ()
{
        ctx_close_path (ensure_ctx());
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_0(display_close_path_obj,
                                 mp_display_close_path);

/* print something on the display */
static mp_obj_t mp_display_text(mp_obj_t arg)
{
	if (!mp_obj_is_str_or_bytes(arg)) {
		mp_raise_TypeError("input text must be a string");
	}
	GET_STR_DATA_LEN(arg, print, print_len);
	ctx_text (ensure_ctx(), (const char *)print);
        ctx_commands_received ++;
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_text_obj, mp_display_text);

static mp_obj_t mp_display_font(mp_obj_t arg)
{
	if (!mp_obj_is_str_or_bytes(arg)) {
		mp_raise_TypeError("arg must be string");
	}
	GET_STR_DATA_LEN(arg, print, print_len);
	ctx_font (ensure_ctx(), (const char *)print);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_font_obj, mp_display_font);

#if 0
static mp_obj_t mp_svg_path(mp_obj_t arg)
{
	if (!mp_obj_is_str_or_bytes(arg)) {
		mp_raise_TypeError("argument must be a string");
	}
	GET_STR_DATA_LEN(arg, d, print_len);
	ctx_svg_path (ensure_ctx(), (const char *)d);
	return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(display_svg_path_obj, mp_svg_path);
#endif



Gerd's avatar
Gerd committed
794
795
796
797
798
799
/* The globals table for this module */
static const mp_rom_map_elem_t display_module_globals_table[] = {
	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_display) },
	{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&display_open_obj) },
	{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&display_close_obj) },
	{ MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&display_print_obj) },
800
	{ MP_ROM_QSTR(MP_QSTR_print_adv), MP_ROM_PTR(&display_print_adv_obj) },
801
	{ MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&display_pixel_obj) },
802
	{ MP_ROM_QSTR(MP_QSTR_backlight), MP_ROM_PTR(&display_backlight_obj) },
Gerd's avatar
Gerd committed
803
804
805
806
807
	{ MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&display_line_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&display_rect_obj) },
	{ MP_ROM_QSTR(MP_QSTR_circ), MP_ROM_PTR(&display_circ_obj) },
	{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&display_clear_obj) },
	{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&display_update_obj) },
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842

#if 1
	{ MP_ROM_QSTR(MP_QSTR_line_to), MP_ROM_PTR(&display_line_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_move_to), MP_ROM_PTR(&display_move_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_curve_to), MP_ROM_PTR(&display_curve_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_arc), MP_ROM_PTR(&display_arc_obj) },
	{ MP_ROM_QSTR(MP_QSTR_quad_to), MP_ROM_PTR(&display_quad_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_linear_gradient), MP_ROM_PTR(&display_linear_gradient_obj) },
	{ MP_ROM_QSTR(MP_QSTR_radial_gradient), MP_ROM_PTR(&display_radial_gradient_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rectangle), MP_ROM_PTR(&display_rectangle_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rel_line_to), MP_ROM_PTR(&display_rel_line_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rel_move_to), MP_ROM_PTR(&display_rel_move_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rel_curve_to), MP_ROM_PTR(&display_rel_curve_to_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rel_quad_to), MP_ROM_PTR(&display_rel_quad_to_obj) },

	{ MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&display_text_obj) },
	{ MP_ROM_QSTR(MP_QSTR_font), MP_ROM_PTR(&display_font_obj) },
	{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&display_scale_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rotate), MP_ROM_PTR(&display_rotate_obj) },
	{ MP_ROM_QSTR(MP_QSTR_font_size), MP_ROM_PTR(&display_font_size_obj) },
	{ MP_ROM_QSTR(MP_QSTR_line_width), MP_ROM_PTR(&display_line_width_obj) },
	{ MP_ROM_QSTR(MP_QSTR_gray), MP_ROM_PTR(&display_gray_obj) },
	{ MP_ROM_QSTR(MP_QSTR_global_alpha), MP_ROM_PTR(&display_global_alpha_obj) },
	{ MP_ROM_QSTR(MP_QSTR_translate), MP_ROM_PTR(&display_translate_obj) },
	{ MP_ROM_QSTR(MP_QSTR_rgba), MP_ROM_PTR(&display_rgba_obj) },
	{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&display_fill_obj) },
	{ MP_ROM_QSTR(MP_QSTR_clip), MP_ROM_PTR(&display_clip_obj) },
	{ MP_ROM_QSTR(MP_QSTR_gradient_add_stop), MP_ROM_PTR(&display_gradient_add_stop_obj) },
	{ MP_ROM_QSTR(MP_QSTR_stroke), MP_ROM_PTR(&display_stroke_obj) },
	{ MP_ROM_QSTR(MP_QSTR_save), MP_ROM_PTR(&display_save_obj) },
	{ MP_ROM_QSTR(MP_QSTR_restore), MP_ROM_PTR(&display_restore_obj) },
	{ MP_ROM_QSTR(MP_QSTR_begin_path), MP_ROM_PTR(&display_begin_path_obj) },
	{ MP_ROM_QSTR(MP_QSTR_close_path), MP_ROM_PTR(&display_close_path_obj) },
	//{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&display_flush_obj) },
#endif
Gerd's avatar
Gerd committed
843
844
845
846
847
848
849
850
851
852
853
854
};
static MP_DEFINE_CONST_DICT(
	display_module_globals, display_module_globals_table
);

const mp_obj_module_t display_module = {
	.base    = { &mp_type_module },
	.globals = (mp_obj_dict_t *)&display_module_globals,
};

/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_sys_display, display_module, MODULE_DISPLAY_ENABLED);