1PikaScript
2==========
3
4
5What is PikaScript ?
6--------------------
7
8`PikaScript <https://github.com/pikasTech/pikascript>`__ is a Python interpreter designed specifically for
9microcontrollers, and it supports a subset of the common Python3 syntax.
10
11It's lighter, requiring only 32k of code space and 4k of RAM, which means it can run on stm32f103c8 (blue-pill)
12or even stm32g030c8, on the other hand, you can leave valuable space for more material or larger buffer areas.
13
14It is simpler, out of the box, runs with no porting and configuration at all, does not depend on OS or file
15system, has good support for popular IDEs for Windows platforms like Keil, IAR, RT-Thread-Studio, and of course,
16supports linux-gcc development platforms.
17
18It's smarter, with a unique C module mechanism that allows you to generate bindings automatically by simply
19writing the API for the C module in Python, and you don't need to deal with the headache of writing any macros
20or global tables manually. On the other hand, all C modules have sophisticated smart hints, even hinting at the types
21of your arguments .
22
23
24--------------
25
26
27Why PikaScript + LVGL ?
28-----------------------
29
30- PikaScript now supports the main features of LVGL8, and these APIs are fully compatible with MicroPython!
31  This means that you can continue to use already written code from MicroPython, and then use less code space and RAM.
32- Enjoy detailed code hints down to the parameter type for a better programming experience
33- Use a more convenient IDE, such as vs-based simulation projects
34
35
36So how does it look like?
37-------------------------
38
39Here are some examples of lvgl that PikaScript can already run, they are mainly from the lvgl documentation examples
40
41
42LV_ARC
43~~~~~~
44
45.. code-block:: python
46
47    import pika_lvgl as lv
48    import PikaStdLib
49    mem = PikaStdLib.MemChecker()
50    # Create an Arc
51    arc = lv.arc(lv.screen_active())
52    arc.set_end_angle(200)
53    arc.set_size(150, 150)
54    arc.center()
55    print('mem used max: %0.2f kB' % (mem.getMax()))
56    print('mem used now: %0.2f kB' % (mem.getNow()))
57
58
59LV_BAR
60~~~~~~
61
62.. code-block:: python
63
64    import pika_lvgl as lv
65    import PikaStdLib
66    mem = PikaStdLib.MemChecker()
67    bar1 = lv.bar(lv.screen_active())
68    bar1.set_size(200, 20)
69    bar1.center()
70    bar1.set_value(70, lv.ANIM.OFF)
71    print('mem used max: %0.2f kB' % (mem.getMax()))
72    print('mem used now: %0.2f kB' % (mem.getNow()))
73
74
75LV_BTN
76~~~~~~
77
78.. code-block:: python
79
80    import pika_lvgl as lv
81    import PikaStdLib
82    mem = PikaStdLib.MemChecker()
83
84
85    def event_cb_1(evt):
86        print('in evt1')
87        print('mem used now: %0.2f kB' % (mem.getNow()))
88
89
90    def event_cb_2(evt):
91        print('in evt2')
92        print('mem used now: %0.2f kB' % (mem.getNow()))
93
94
95    btn1 = lv.btn(lv.screen_active())
96    btn1.align(lv.ALIGN.TOP_MID, 0, 10)
97    btn2 = lv.btn(lv.screen_active())
98    btn2.align(lv.ALIGN.TOP_MID, 0, 50)
99    btn1.add_event_cb(event_cb_1, lv.EVENT.CLICKED, 0)
100    btn2.add_event_cb(event_cb_2, lv.EVENT.CLICKED, 0)
101    print('mem used max: %0.2f kB' % (mem.getMax()))
102    print('mem used now: %0.2f kB' % (mem.getNow()))
103
104
105LV_CHECKBOX
106~~~~~~~~~~~
107
108.. code-block:: python
109
110    import pika_lvgl as lv
111    import PikaStdLib
112    mem = PikaStdLib.MemChecker()
113    cb = lv.checkbox(lv.screen_active())
114    cb.set_text("Apple")
115    cb.align(lv.ALIGN.TOP_LEFT, 0 ,0)
116    cb = lv.checkbox(lv.screen_active())
117    cb.set_text("Banana")
118    cb.add_state(lv.STATE.CHECKED)
119    cb.align(lv.ALIGN.TOP_LEFT, 0 ,30)
120    cb = lv.checkbox(lv.screen_active())
121    cb.set_text("Lemon")
122    cb.add_state(lv.STATE.DISABLED)
123    cb.align(lv.ALIGN.TOP_LEFT, 0 ,60)
124    cb = lv.checkbox(lv.screen_active())
125    cb.add_state(lv.STATE.CHECKED | lv.STATE.DISABLED)
126    cb.set_text("Melon")
127    cb.align(lv.ALIGN.TOP_LEFT, 0 ,90)
128    print('mem used max: %0.2f kB' % (mem.getMax()))
129    print('mem used now: %0.2f kB' % (mem.getNow()))
130
131
132--------------
133
134
135How does it work?
136-----------------
137
138PikaScript has a unique C module smart binding tool
139
140Just write the Python interface in pika_lvgl.pyi (.pyi is the python interface file)
141
142.. code-block:: python
143
144    # pika_lvgl.pyi
145    class arc(lv_obj):
146        def set_end_angle(self, angle: int): ...
147        def set_bg_angles(self, start: int, end: int): ...
148        def set_angles(self, start: int, end: int): ...
149
150
151Then PikaScript's pre-compiler can automatically bind the following C functions, simply by naming the functions
152in the module_class_method format, without any additional work, and all binding and registration is done automatically.
153
154.. code-block:: c
155
156    /* pika_lvgl_arc.c */
157    void pika_lvgl_arc_set_end_angle(PikaObj* self, int angle) {
158        lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj");
159        lv_arc_set_end_angle(lv_obj, angle);
160    }
161    void pika_lvgl_arc_set_bg_angles(PikaObj *self, int start, int end){
162        lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj");
163        lv_arc_set_bg_angles(lv_obj, start, end);
164    }
165    void pika_lvgl_arc_set_angles(PikaObj *self, int start, int end){
166        lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj");
167        lv_arc_set_angles(lv_obj, start, end);
168    }
169
170
171To use the module, just ``import pika_lvgl`` and the precompiler will automatically scan main.py and bind the
172``pika_lvgl`` module
173
174.. code-block:: shell
175
176   $ ./rust-msc-latest-win10.exe
177   (pikascript) packages installed:
178       pikascript-core==v1.10.0
179       PikaStdLib==v1.10.0
180       PikaStdDevice==v1.10.0
181   (pikascript) pika compiler:
182     scanning main.py...
183       binding pika_lvgl.pyi...
184
185
186The precompiler is written in Rust, runs on windows and linux, and is completely open source.
187
188In addition to binding C modules, the precompiler compiles Python scripts to bytecode in the PC, reducing the
189size of the script and increasing its speed.
190
191
192--------------
193
194
195How can I use it?
196-----------------
197
198The simulation repo on vs is available on https://github.com/pikasTech/lv_pikascript
199