You shouldn’t use the draw functions directly, except inside a custom object implementation. Instead, you should make use of widgets like lv_line or lv_obj for drawing basic shapes.
Alternatively, you can draw custom graphics on a canvas, but this is the most expensive method in terms of memory.
I thought it could be a nice exercise to show how to create a custom widget in Micropython.
Here is a new widget “MyLine” which is just a diagonal line (drawn with the lv.draw_line function).
It implements both signal and design callbacks. design draws the line, and signal responses to press/release events and changes the diagonal line accordingly.
That’s a lot of code just for calling the old signal function. I assume it’s because the function can’t be called directly from MicroPython. Could we add a utility function that makes this kind of thing easier?
Currently the Micropython Binding does not support calling C function pointers, so using lv.signal_send is a workaround.
I don’t think there is a need to support that generically, since it’s very rare.
What utility function did you have in mind @embeddedt ? A function that receives a signal function pointer and calls it? My opinion is that for now we can keep current workaround.
So,if I want create a custom widget,just need to implement two functions:
signal
design
But I got one question, the parameters how to passed?In your example 2 points of the line is inside of the design function,but how can we by passed the points to it?
You can add setter member functions to your widget class, and update properties on self.
You can access self from signal and design, because they are member functions of the widget.
You can also pass arguments to the widget’s constructor. Now it receives only par but you can add others.
In C it would be possible to call self.ancestor_signal(sign, param) directly. This is a lot more natural to the user than saving the previous signal function and attaching a new one. We can leave the current behavior for now though.
I tried, but fail…Maybe I need review the C function how to and do it.
The most important thing is I don’t know the constructor function how it works in micropython.
Hi @amirgon,
It works!Thank you for your help.You gave me a direction to solve the problem!
I did some change(maybe it was not perfect,but it works!), it has more function to change the line. simple coustom widget example
I will continue to do it.But I think it is more convenient to create custom widgets with C code?