Python для DM42/Free42

Время от времени балуюсь наработками DM42-сообщества.
PyDemo

Из интересного для меня на сегодня:
1. Утилита VirtVar для удобного доступа к виртуальным переменным DM42:

Graphic mode:
hp = 131x16 legacy HP 42S graphic mode
200 = 200x120 full screen
400 = 400x240 full screen

LCD refreshing (any combination of the three items is allowed to cover the 8 values of the RefLCD variable):
Main: Main area
Goos: Goose
Ann: Annunciators

VirtVar 1VirtVar 2

2. Python to RPN компилятор. Ниже приводится код Python-а, для отрисовки вышеупомянутого демо. А еще ниже результат компиляции в RPN. Рисовать это демо средствами языка RPN я бы не решился.

def dd2_draw_demo():  # rpn: export
    FIX(0)
    SIZE(60)
    CLLCD()
 
    XRES = 400      # Hi-resolution of DM42
    YRES = 240
 
    # some pixels
    for i in range(1, XRES, 5):
        x = i
        row = YRES / 2
        col = x
        PIXEL(row, col)
 
    # some lines (note the quite likely 'Moire pattern')
    for i in range(1, XRES / 4, 2):
        x = i
        #PRA('moire', 0, 0, x, YRES)
        draw_line(0, 0, x, YRES)
 
    # some rectangles
    fromx = XRES / 4
    fromy = YRES / 2
    width = XRES / 4
    height = YRES / 4
    draw_rect(fromx, fromy, width, height)
 
    fromx = XRES / 4 + 10
    fromy = YRES / 2 + 4
    width = XRES / 4 - 20
    height = YRES / 4
    draw_rect(fromx, fromy, width, height)
 
    fromx = XRES / 8
    width = XRES / 4
    height = YRES / 4
    fill_rect(fromx, 1, width, height)
 
    # some circles
    for i in range(2, YRES / 2, 2):
        d = i
        draw_circle(3 * XRES / 4, 2 * YRES / 4, d)
 
    fill_circle(15 * XRES / 16, 2 * YRES / 4, YRES / 3)
    fill_circle(8 * XRES / 13, 3 * YRES / 4, YRES / 2)
 
def draw_line(x0, y0, x1, y1):  # rpn: int
    dx = ABS(x1 - x0)
    dy = ABS(y1 - y0)
    if x0 < x1:
        sx = 1
    else:
        sx = -1
    if y0 < y1:
        sy = 1
    else:
        sy = -1
    err = dx - dy
    done = 0
    while not done:
        #PRA('pixel', x0, y0)
        row = y0
        col = x0
        PIXEL(row, col)
        if (x0 == x1) and (y0 == y1):
            done = 1
        else:
            e2 = 2 * err
            if e2 > -dy:
                err = err - dy
                x0 = x0 + sx
            if e2 < dx:
                err = err + dx
                y0 = y0 + sy
 
# (x0, y0) = left top corner coordinates
# w = width and h = height
def draw_rect(x0, y0, w, h):  # rpn: int
  #PRA('draw_rect', x0, y0, w, h)
  y1 = y0 + h
  draw_line(x0, y0, x0 + w, y0)  # top
  draw_line(x0, y0, x0, y1)  # left
  draw_line(x0, y0 + h, x0 + w, y1)  # bottom
  draw_line(x0 + w, y0, x0 + w, y1)  # right
 
def fill_rect(x0, y0, w, h):  # rpn: int
  #for (y = 0; y < h; y++):  # TODO make any var work in a for loop
  for i in range(h):  # TODO make any var work in a for loop
    y = i
    y1 = y0 + y
    draw_line(x0, y0 + y, x0 + w, y1)
 
def draw_circle(x0, y0, r):  # rpn: int
    x = r;
    y = 0;
    radiusError = 1 - x;
 
    while (x >= y):
        # Note: 42S pixel row is a y coordinate (top to bottom), 42S col is the x coordinate (left to right)
        # So I swap the parameters from the javascript version
 
        # PIXEL(-y + x0, -x + y0)  # top left
        # PIXEL(y + x0, -x + y0)  # top right
        # PIXEL(-x + x0, -y + y0)  # upper middle left
        # PIXEL(x + x0, -y + y0)  # upper middle right
        # PIXEL(-x + x0, y + y0)  # lower middle left
        # PIXEL(x + x0, y + y0)  # lower middle right
        # PIXEL(-y + x0, x + y0)  # bottom left
        # PIXEL(y + x0, x + y0)  # bottom right
 
        PIXEL(-x + y0, -y + x0)  # top left
        PIXEL(-x + y0, y + x0)  # top right
        PIXEL(-y + y0, -x + x0)  # upper middle left
        PIXEL(-y + y0, x + x0)  # upper middle right
        PIXEL(y + y0, -x + x0)  # lower middle left
        PIXEL(y + y0, x + x0)  # lower middle right
        PIXEL(x + y0, -y + x0)  # bottom left
        PIXEL(x + y0, y + x0)  # bottom right
 
        y += 1;
        if (radiusError < 0):
            radiusError += 2 * y + 1
        else:
            x -= 1
            radiusError += 2 * (y - x + 1)
 
def fill_circle(x0, y0, r):  # rpn: int
    x = r
    y = 0
    radiusError = 1 - x
 
    while (x >= y):
        fromx = -y + x0
        fromy = -x + y0
        tox = y + x0
        toy = -x + y0
        draw_line(fromx, fromy, tox, toy)  # top
 
        fromx = -x + x0
        fromy = -y + y0
        tox = x + x0
        toy = -y + y0
        draw_line(fromx, fromy, tox, toy)  # upper middle
 
        fromx = -x + x0
        fromy = y + y0
        tox = x + x0
        toy = y + y0
        draw_line(fromx, fromy, tox, toy)  # lower middle
 
        fromx = -y + x0
        fromy = x + y0
        tox = y + x0
        toy = x + y0
        draw_line(fromx, fromy, tox, toy)  # bottom
 
        y += 1
        if radiusError < 0:
            radiusError += 2 * y + 1
        else:
            x -= 1
            radiusError += 2 * (y - x + 1)
01 LBL "dd2_dra"
02 FIX 00
03 SIZE 0060
04 CLLCD
05 400
06 STO "XRES"
07 240
08 STO "YRES"
09 1
10 RCL "XRES"
11 5
12 XEQ 78
13 STO 00
14 LBL 00
15 ISG 00
16 GTO 01
17 GTO 02
18 LBL 01
19 RCL 00
20 IP
21 STO 01
22 RCL "YRES"
23 2
24 /
25 STO 02
26 RCL 01
27 STO 03
28 RCL 02
29 RCL 03
30 PIXEL
31 GTO 00
32 LBL 02
33 1
34 RCL "XRES"
35 4
36 /
37 2
38 XEQ 78
39 STO 00
40 LBL 03
41 ISG 00
42 GTO 04
43 GTO 05
44 LBL 04
45 RCL 00
46 IP
47 STO 01
48 0
49 0
50 RCL 01
51 RCL "YRES"
52 XEQ A
53 GTO 03
54 LBL 05
55 RCL "XRES"
56 4
57 /
58 STO 04
59 RCL "YRES"
60 2
61 /
62 STO 05
63 RCL "XRES"
64 4
65 /
66 STO 06
67 RCL "YRES"
68 4
69 /
70 STO 07
71 RCL 04
72 RCL 05
73 RCL 06
74 RCL 07
75 XEQ B
76 RCL "XRES"
77 4
78 /
79 10
80 +
81 STO 04
82 RCL "YRES"
83 2
84 /
85 4
86 +
87 STO 05
88 RCL "XRES"
89 4
90 /
91 20
92 -
93 STO 06
94 RCL "YRES"
95 4
96 /
97 STO 07
98 RCL 04
99 RCL 05
100 RCL 06
101 RCL 07
102 XEQ B
103 RCL "XRES"
104 8
105 /
106 STO 04
107 RCL "XRES"
108 4
109 /
110 STO 06
111 RCL "YRES"
112 4
113 /
114 STO 07
115 RCL 04
116 1
117 RCL 06
118 RCL 07
119 XEQ C
120 2
121 RCL "YRES"
122 2
123 /
124 2
125 XEQ 78
126 STO 00
127 LBL 06
128 ISG 00
129 GTO 07
130 GTO 08
131 LBL 07
132 RCL 00
133 IP
134 STO 08
135 3
136 RCL "XRES"
137 *
138 4
139 /
140 2
141 RCL "YRES"
142 *
143 4
144 /
145 RCL 08
146 XEQ D
147 GTO 06
148 LBL 08
149 15
150 RCL "XRES"
151 *
152 16
153 /
154 2
155 RCL "YRES"
156 *
157 4
158 /
159 RCL "YRES"
160 3
161 /
162 XEQ E
163 8
164 RCL "XRES"
165 *
166 13
167 /
168 3
169 RCL "YRES"
170 *
171 4
172 /
173 RCL "YRES"
174 2
175 /
176 XEQ E
177 RTN
178 LBL A
179 XEQ 67
180 IP
181 STO 09
182 RDN
183 IP
184 STO 10
185 RDN
186 IP
187 STO 11
188 RDN
189 IP
190 STO 12
191 RDN
192 RCL 11
193 RCL 09
194 -
195 ABS
196 STO 13
197 RCL 12
198 RCL 10
199 -
200 ABS
201 STO 14
202 RCL 09
203 RCL 11
204 XEQ 79
205 X≠0?
206 GTO 09
207 GTO 11
208 LBL 09
209 1
210 STO 15
211 GTO 10
212 LBL 11
213 -1
214 STO 15
215 LBL 10
216 RCL 10
217 RCL 12
218 XEQ 79
219 X≠0?
220 GTO 12
221 GTO 14
222 LBL 12
223 1
224 STO 16
225 GTO 13
226 LBL 14
227 -1
228 STO 16
229 LBL 13
230 RCL 13
231 RCL 14
232 -
233 STO 17
234 0
235 STO 18
236 LBL 15
237 RCL 18
238 XEQ 70
239 XEQ 87
240 X≠0?
241 GTO 16
242 GTO 17
243 LBL 16
244 RCL 10
245 STO 19
246 RCL 09
247 STO 20
248 RCL 19
249 RCL 20
250 PIXEL
251 RCL 09
252 RCL 11
253 XEQ 72
254 RCL 10
255 RCL 12
256 XEQ 72
257 XEQ 63
258 AND
259 X≠0?
260 GTO 18
261 GTO 20
262 LBL 18
263 1
264 STO 18
265 GTO 19
266 LBL 20
267 2
268 RCL 17
269 *
270 STO 21
271 RCL 21
272 RCL 14
273 CHS
274 XEQ 76
275 X≠0?
276 GTO 21
277 GTO 22
278 LBL 21
279 RCL 17
280 RCL 14
281 -
282 STO 17
283 RCL 09
284 RCL 15
285 +
286 STO 09
287 LBL 22
288 RCL 21
289 RCL 13
290 XEQ 79
291 X≠0?
292 GTO 23
293 GTO 24
294 LBL 23
295 RCL 17
296 RCL 13
297 +
298 STO 17
299 RCL 10
300 RCL 16
301 +
302 STO 10
303 LBL 24
304 LBL 19
305 GTO 15
306 LBL 17
307 RTN
308 LBL B
309 XEQ 67
310 IP
311 STO 22
312 RDN
313 IP
314 STO 23
315 RDN
316 IP
317 STO 24
318 RDN
319 IP
320 STO 25
321 RDN
322 RCL 23
323 RCL 25
324 +
325 STO 26
326 RCL 22
327 RCL 23
328 RCL 22
329 RCL 24
330 +
331 RCL 23
332 XEQ A
333 RCL 22
334 RCL 23
335 RCL 22
336 RCL 26
337 XEQ A
338 RCL 22
339 RCL 23
340 RCL 25
341 +
342 RCL 22
343 RCL 24
344 +
345 RCL 26
346 XEQ A
347 RCL 22
348 RCL 24
349 +
350 RCL 23
351 RCL 22
352 RCL 24
353 +
354 RCL 26
355 XEQ A
356 RTN
357 LBL C
358 XEQ 67
359 IP
360 STO 27
361 RDN
362 IP
363 STO 28
364 RDN
365 IP
366 STO 29
367 RDN
368 IP
369 STO 30
370 RDN
371 0
372 RCL 30
373 1
374 XEQ 78
375 STO 31
376 LBL 25
377 ISG 31
378 GTO 26
379 GTO 27
380 LBL 26
381 RCL 31
382 IP
383 STO 32
384 RCL 28
385 RCL 32
386 +
387 STO 33
388 RCL 27
389 RCL 28
390 RCL 32
391 +
392 RCL 27
393 RCL 29
394 +
395 RCL 33
396 XEQ A
397 GTO 25
398 LBL 27
399 RTN
400 LBL D
401 XEQ 66
402 IP
403 STO 34
404 RDN
405 IP
406 STO 35
407 RDN
408 IP
409 STO 36
410 RDN
411 RCL 36
412 STO 37
413 0
414 STO 38
415 1
416 RCL 37
417 -
418 STO 39
419 LBL 28
420 RCL 37
421 RCL 38
422 XEQ 77
423 X≠0?
424 GTO 29
425 GTO 30
426 LBL 29
427 RCL 37
428 CHS
429 RCL 35
430 +
431 RCL 38
432 CHS
433 RCL 34
434 +
435 PIXEL
436 RCL 37
437 CHS
438 RCL 35
439 +
440 RCL 38
441 RCL 34
442 +
443 PIXEL
444 RCL 38
445 CHS
446 RCL 35
447 +
448 RCL 37
449 CHS
450 RCL 34
451 +
452 PIXEL
453 RCL 38
454 CHS
455 RCL 35
456 +
457 RCL 37
458 RCL 34
459 +
460 PIXEL
461 RCL 38
462 RCL 35
463 +
464 RCL 37
465 CHS
466 RCL 34
467 +
468 PIXEL
469 RCL 38
470 RCL 35
471 +
472 RCL 37
473 RCL 34
474 +
475 PIXEL
476 RCL 37
477 RCL 35
478 +
479 RCL 38
480 CHS
481 RCL 34
482 +
483 PIXEL
484 RCL 37
485 RCL 35
486 +
487 RCL 38
488 RCL 34
489 +
490 PIXEL
491 1
492 STO+ 38
493 RCL 39
494 0
495 XEQ 79
496 X≠0?
497 GTO 31
498 GTO 33
499 LBL 31
500 2
501 RCL 38
502 *
503 1
504 +
505 STO+ 39
506 GTO 32
507 LBL 33
508 1
509 STO- 37
510 2
511 RCL 38
512 RCL 37
513 -
514 1
515 +
516 *
517 STO+ 39
518 LBL 32
519 GTO 28
520 LBL 30
521 RTN
522 LBL E
523 XEQ 66
524 IP
525 STO 40
526 RDN
527 IP
528 STO 41
529 RDN
530 IP
531 STO 42
532 RDN
533 RCL 42
534 STO 43
535 0
536 STO 44
537 1
538 RCL 43
539 -
540 STO 45
541 LBL 34
542 RCL 43
543 RCL 44
544 XEQ 77
545 X≠0?
546 GTO 35
547 GTO 36
548 LBL 35
549 RCL 44
550 CHS
551 RCL 40
552 +
553 STO 46
554 RCL 43
555 CHS
556 RCL 41
557 +
558 STO 47
559 RCL 44
560 RCL 40
561 +
562 STO 48
563 RCL 43
564 CHS
565 RCL 41
566 +
567 STO 49
568 RCL 46
569 RCL 47
570 RCL 48
571 RCL 49
572 XEQ A
573 RCL 43
574 CHS
575 RCL 40
576 +
577 STO 46
578 RCL 44
579 CHS
580 RCL 41
581 +
582 STO 47
583 RCL 43
584 RCL 40
585 +
586 STO 48
587 RCL 44
588 CHS
589 RCL 41
590 +
591 STO 49
592 RCL 46
593 RCL 47
594 RCL 48
595 RCL 49
596 XEQ A
597 RCL 43
598 CHS
599 RCL 40
600 +
601 STO 46
602 RCL 44
603 RCL 41
604 +
605 STO 47
606 RCL 43
607 RCL 40
608 +
609 STO 48
610 RCL 44
611 RCL 41
612 +
613 STO 49
614 RCL 46
615 RCL 47
616 RCL 48
617 RCL 49
618 XEQ A
619 RCL 44
620 CHS
621 RCL 40
622 +
623 STO 46
624 RCL 43
625 RCL 41
626 +
627 STO 47
628 RCL 44
629 RCL 40
630 +
631 STO 48
632 RCL 43
633 RCL 41
634 +
635 STO 49
636 RCL 46
637 RCL 47
638 RCL 48
639 RCL 49
640 XEQ A
641 1
642 STO+ 44
643 RCL 45
644 0
645 XEQ 79
646 X≠0?
647 GTO 37
648 GTO 39
649 LBL 37
650 2
651 RCL 44
652 *
653 1
654 +
655 STO+ 45
656 GTO 38
657 LBL 39
658 1
659 STO- 43
660 2
661 RCL 44
662 RCL 43
663 -
664 1
665 +
666 *
667 STO+ 45
668 LBL 38
669 GTO 34
670 LBL 36
671 RTN
672 LBL 50
673 "-Utility Funcs-"
674 RTN
675 LBL 63
676 XEQ 70
677 X<>Y
678 XEQ 70
679 X<>Y
680 RTN
681 LBL 66
682 X<>Y
683 RDN
684 RDN
685 X<>Y
686 RDN
687 RTN
688 LBL 67
689 X<>Y
690 RDN
691 RDN
692 X<>Y
693 RTN
694 LBL 70
695 CF 00
696 X≠0?
697 SF 00
698 RDN
699 FS? 00
700 1
701 FC? 00
702 0
703 RTN
704 LBL 72
705 CF 00
706 X=Y?
707 SF 00
708 XEQ 61
709 RTN
710 LBL 76
711 CF 00
712 X<Y?
713 SF 00
714 XEQ 61
715 RTN
716 LBL 77
717 CF 00
718 X≤Y?
719 SF 00
720 XEQ 61
721 RTN
722 LBL 78
723 RCL T
724 STO "pSaveT"
725 RDN
726 CF 99
727 CF 98
728 IP
729 1
730 X=Y?
731 SF 98
732 RDN
733 RCL ST Z
734 IP
735 RCL ST Z
736 IP
737 1
738 -
739 999
740 X<Y?
741 XEQ 73
742 RDN
743 X<>Y
744 RCL ST Z
745 -
746 X>=0?
747 GTO 51
748 ABS
749 SF 99
750 LBL 51
751 X<>Y
752 1000
753 /
754 +
755 FS?C 98
756 GTO 52
757 RCL ST Z
758 100000
759 /
760 +
761 LBL 52
762 FS?C 99
763 +/-
764 RCL "pSaveT"
765 X<>Y
766 RTN
767 LBL 79
768 CF 00
769 X>Y?
770 SF 00
771 XEQ 61
772 RTN
773 LBL 87
774 X≠0?
775 SF 00
776 RDN
777 FS? 00
778 0
779 FC? 00
780 1
781 RTN
782 LBL 61
783 RDN
784 RDN
785 FS? 00
786 1
787 FC? 00
788 0
789 RTN
790 LBL 73
791 RDN
792 "range() limited"
793" to 999: got "
794 ARCL ST X
795 AVIEW
796 STOP
797 RTN
Метки публикаций: 

Комментарии

797 шагов программы - надо быть фанатом, чтобы реализовывать подобное прямо на RPN. Ну, или небесплатно :)

эпоха RPN в частности и ПМК в целом уже прошлое. Только поностальгировать.

Вот еще пример пользовательского интерфейса c soft-кнопками на Питоне и результирующем RPN:
UI 1UI 2UI 3

def ui1():
  menu("hello", "func2", "add")  # new easy menu technique
 
def hello():
  print('you chose hello')
 
def func2():
  print('you chose func2')
  return 50
 
def add(a, b):
  return a + b
01 LBL "ui1"
02 CLMENU
03 "hello"
04 KEY 1 GTO A
05 "func2"
06 KEY 2 GTO B
07 "add"
08 KEY 3 GTO C
09 MENU
10 RTN
11 LBL A
12 "you chose hello"
13 AVIEW
14 RTN
15 LBL B
16 "you chose func2"
17 AVIEW
18 50
19 RTN
20 LBL C
21 XEQ 65
22 STO 00
23 RDN
24 STO 01
25 RDN
26 RCL 00
27 RCL 01
28 +
29 RTN
30 LBL 50
31 "-Utility Funcs-"
32 RTN
33 LBL 65
34 X<>Y
35 RTN

Хочется писать сразу в Питоне на "борту" калькулятора :)

---------------------------
Истина где-то рядом
www.litres.ru/vitaliy-samurov/dozvonitsya-do-devy/

тоже в какой то степени сделаны для "рисования",
но на PostScript никто почти не программирует напрямую. :)
а PDF, вообще, больше для отображения документов.

P.S. Для векторной графики придумано много форматов и в частности SVG.

это покруче Гипертекстового фидонета будет :)

---------------------------
Истина где-то рядом
www.litres.ru/vitaliy-samurov/dozvonitsya-do-devy/

Кидаешь на стек пару объектов (сам SVG, прямоугольник-цель в координатах индикатора) и вызываешь DRAW. Надо только улучшить быстродействие и объём памяти ПМК, чтобы хранить и обрабатывать векторные изображения.