My swift wrapper proof of concept

Just a quick link to my experimental swift wrapper around LVGL. It uses an SDL simulator. Concept is to wrap objects in swift. Make a very swift like API (blocks, properties, etc).

This has wrappers right now for Screen, Theme, Style, Button, Label, and Object… Also stores self on user_data that is used in event handling and getting the current screen.

Example code in demo:

import Foundation
import LVGLSwift
 
// We need to setup callback
lv_init()
simulation_hal_init()

let theme = DarkTheme()
lv_disp_set_theme(lv_disp_get_default(), &theme.lvTheme)

let screen:Screen = Screen()
let button = Button(parent: screen)
let label = Label(parent: button);

button.center();
button.onClick(handler: { _ in
    print("was clicked")
})

label.text = "Foo"
screen.load();


while true {
    lv_task_handler()
    usleep(5 * 1000)
}
1 Like

Amazing! Glad to see that you use v8. :slight_smile:

Can I try it on Linux?

You can try. I haven’t booted up my pi running Ubuntu yet to try. Was just trying a quick proof of concept. The only change (I think) may be to the SDL path. I was going to work on getting it working this weekend.

You should be able to build now. I have fixed things up and verified it builds on Ubuntu with swift 5.4. Its a bit heavy due to runtimes and build not being optimized.

Where the project it? :slight_smile:

Sorry… Been so heads down with day job and this forgot to post the link.

1 Like

Thanks!
I’m new to swift so is there a brief guide about how to get started with it on Linux?

And is there a way to run LVGL on iOS?

I haven’t tried iOS. There would need to be a GL wrapper written for it. It definitely wouldn’t feel like a mobile app. I am doing this for an embedded project using something with a bit of ram in it. Also see if I can share some basic business logic between device, mobile, web. I have also spent so much tie in swift that c++ felt a bit chaotic (I wrote a basic wrapper there too) after being away for 10 years.

As far as getting swift to run on linux. There is a lengthy article here:

If running ARM you may need to follow these directions:

Thanks! I’ll try it out. :wink:

Another attempt, inspired by @scottandrew:

Hi Luke,

Thank you for sharing! Is it written manually, or generated?

Manual. Generated would be better, but I’m lazy. However it uses property wrappers to handle styles and flags without too much boilerplate.

I see, thanks.

Could you tell what is the real life use case of an Swift binding? It’s not necessary to have one, you could do it just for fun too :slight_smile:

Working on an embedded device and would prefer to avoid writing C where possible :slight_smile:

Also, combined with Tokamak or SwiftCrossUI, this would be really powerful.

So, I’ve started a LVGL backend for Tokamak:

Not really doing much yet, but when done should make it possible to port SwiftUI apps to embedded platforms using LVGL.

(Would be nice if LVGL used reference counting, weak references everywhere can make things a bit tricky integrating with Swift. But not impossible.)

1 Like

Interesting projects Luke, but I think I am missing the point.

Is the idea to create UIs using swift or tokamak and then convert that to C code which uses LVGL?

Tokamak is a declarative UI framework that is modelled on SwiftUI.

Tokamak has multiple backends – HTML DOM (via WASI), GTK, and now, LVGL (via LVGLSwift).

SwiftUI provides a high level of abstraction and allows for code reuse between embedded, iOS and macOS targets.

Edit: there is no code generation.

Sounds really interesting!