Initial PicoCalc port of lua

After struggling quite a bit with display and keyboard drivers, here is an initial lua interpreter:

Nothing fancy, you can type in a program and run it. It’s based on GitHub - JeremyGrosser/picolua: A minimal example of Lua with Raspberry Pi pico-sdk
The terminal supports a subset of ansi commands; the LCD driver supports hardware scrolling.

Ctrl-C  Clear buffer
Ctrl-D  Execute buffer
Ctrl-L  Clear screen
Ctrl-Alt-Del  Reboot
Ctrl-Alt-F1  Reboot to bootsel for flashing

Next steps will be to have a proper REPL, a code editor and a way to save programs to flash/sd.

10 Likes

Hey thanks Ben I’ll definitely be looking to make use of this I have a few projects in mind for this device in Lua so great you made a start.

New release with a proper REPL, and a small API to access the SD card, draw graphics, read keyboard.

Tested only on pico2, but uf2 available for pico1 as well.
You can put a file named main.lua in the root of your SD card, it will get executed at startup.

Next, I’ll work on an editor.

8 Likes

Nice job and continued progress on this!

It would probably be a major project to implement, but have you considered adapting it to support the subset of commands, special features, and new commands that Pico-8 supports? I wondered if anyone else had done anything like that and found this:

Since it looks like an older project, it would probably only support older Pico-8 stuff, but it could be an alternative to the other old/unfinished Pico-8 port to Pico.

This would require porting the functionality of Love2D, as well, so probably a massive project. But it could bring a sort of standard lua platform to the PicoCalc.

Emulating pico-8 to the point of being able to run unmodified carts is a lot of work. The closest I find is GitHub - yocto-8/yocto-8: A (WIP) PICO-8 cartridge runner for the Raspberry Pi Pico and GitHub - DavidVentura/PicoPico: Pico-8 Player. This is indeed a big endeavour, and main limitation I can see is the requirement for 2MB of lua ram. I have not played with PSRAM yet, but it seems to be complex and slow.

1 Like

I created some custom lua functions in c and have them working in lua replicating some of the PICO-8 apis. I was wanting to try out this mini games framework on an actual device but i dont have a device yet. Also is something ive never done before writing to an actual screen off a device like this so have no idea if it will work.

1 Like

i’ve picked up this project and ran away with it a little bit :sweat_smile: there’s still a lot i want to overhaul (including potential breaking changes on the current API) and some of it lacks documentation yet, but i figured i would put out what i have so far before i disappear in the mist of scope creep

namely, i plan to move all LCD functions to the second core (like picomite does) and overhaul the terminal emulation such that it doesn’t use the LCD controller’s scroll register, and instead use the extra frame memory in the LCD controller itself as a “free” framebuffer by exploiting the scroll register (this was a misunderstanding of how the LCD controller uses its own memory - there’s not enough to have two full screens and flip through them)

i also plan on making an editor, i was trying to implement enough of computercraft’s API to use its editor written in lua rather than having to implement one on the C side from scratch, unfortunately as soon as i got enough in place for it to run it just blows through the entire free memory, probably because of all the lexer and autocomplete stuff it does :^)

5 Likes

Really nice, I’m glad you find time to improve this project.

here’s a new release, the ComputerCraft editor (slightly modified) now mostly works! Release 0.2 · Lana-chan/picocalc_lua · GitHub

currently there’s a weird crash that happens if you try to interrupt a program, though, and i think it might have to do with my use of timers, but i’m not having much luck tracking it down yet. once that’s sorted my next two main goals are adding a framebuffer for smoother motion drawing and wifi functionality

10 Likes

i think i’ve ironed out most issues with hangs and spurious drawing glitches, now we also have a PSRAM framebuffer (it’s not fast to use it realtime, but it’s faster to draw to it than to the screen, and then one slow draw to blit it to the screen, which is what the bubble universe demo uses)

i’ve been toying with a few demos, an interactive mandelbrot render, bubble universe and a game of asteroids (very similar to the one i wrote for MMBasic, but runs faster :))

i still want to add wifi and sound modules before i call it truly done but since there’s many fixes to make it usable and that’s going to take a while, i’m releasing a new version here: Release PicoCalc Lua 0.3 · Lana-chan/picocalc_lua · GitHub

6 Likes

Excellent!!!

Good job. I like it!

1 Like

Awesome work!! I added that to the Awesome PicoCalc list:

1 Like

Im trying out/reviewing this project, the blink example isn’t working on my Pico 2W, any tips?

lua> LED=25
lua> sys.setOutput(LED, true)
lua> sys.setPin(LED, true)

Also, how do I run files? (like the examples)

The LED on the W-series Pico is no longer directly attached to the microcontroller. Attempting this in any language would not work.

2 Likes

Using dofile("lua/bubble.lua") worked

1 Like

you can also launch the editor with edit(filename), i’ll put those instructions somewhere easier to find in the future, the readme needs a rewrite

2 Likes

Here’s an upcoming review/tutorial :fire:

3 Likes

I can’t seem to get this to run on my RP2040 based Pico. I just get a blank screen. I’ve tried the uf2 from your published release, as well as building from source. In the debugger it seems to be stuck in the multicore init code:

#0  0x10006c98 in multicore_fifo_push_blocking_inline (data=<optimized out>) at /usr/share/pico-sdk/src/rp2_common/pico_multicore/include/pico/multicore.h:186
#1  multicore_fifo_push_blocking (data=<optimized out>) at /usr/share/pico-sdk/src/rp2_common/pico_multicore/multicore.c:34
#2  multicore_launch_core1_raw (entry=entry@entry=0x10006c25 <core1_trampoline>, sp=<optimized out>, vector_table=536870912)
    at /usr/share/pico-sdk/src/rp2_common/pico_multicore/multicore.c:201
#3  0x10006d06 in multicore_launch_core1_with_stack (entry=<optimized out>, stack_bottom=0x20040800, stack_size_bytes=2048)
    at /usr/share/pico-sdk/src/rp2_common/pico_multicore/multicore.c:161
#4  multicore_launch_core1 (entry=<optimized out>) at /usr/share/pico-sdk/src/rp2_common/pico_multicore/multicore.c:170
#5  0x100002ee in main () at /home/wmoore/Source/github.com/Lana-chan/picocalc_lua/picolua.c:66

I noticed this line in picolua.c, which seems to be overclocking the MCU:

set_system_clk(220000);

The weird thing is that it did work once, but only that one time, I wondered if it was this clock speed. I’m not sure if other parts of the system are dependant on that clock speed but I also tried commenting out that line and adding set(PICO_USE_FASTEST_SUPPORTED_CLOCK 1) to CMakeLists.txt, but that didn’t change anything.

Any other suggestions would be appreciated.

it’s true that i haven’t been testing my releases on RP2040 since swapping picos in the picocalc is such a pain and i’ve been developing mainly on a plus2w now

i just tried the 0.3 UF2 release without modification on a 2022-silkscreened Pico 1 W, it loaded without issue and i was able to run the bubble universe demo. this showed some screen glitches (possibly due to the overclock affectting PSRAM read/writes) but the image was correct aside from some garbage on the left side of the screen, so it seemed to be working mostly as intended

as i previously suspected, because the editor gobbles up around 200k of RAM (remember, it’s written in Lua and was not at all designed for microcontrollers) it immediately panics out of memory

the second core is used to offload all drawing functions from the first core, and since i can see things on screen i’m assuming that it’s working fine on my RP2040 at least… i’ve yet to build a debugger probe into my picocalc

i’m wondering if your pico1 is crashing due to not enough voltage for the requested clock speed. i definitely shouldn’t have included that line indiscriminately for all models, i needed some per-model settings once i start including wifi functions anyway

Thanks for looking into it. My Pico board has never had anything bad happen to it, but it seems to have strange compatibility issues. I’m able to run all the things clockworkpi published, but have had the issues described above with picolua as well as issues with uf2loader. uf2loader is flakey, sometimes not showing the menu at all, other times showing the menu but then hanging. Since the PicoMite and co work fine it seems like it’s not an issue with my hardware, but I don’t know for sure.

i’m wondering if your pico1 is crashing due to not enough voltage for the requested clock speed. i definitely shouldn’t have included that line indiscriminately for all models, i needed some per-model settings once i start including wifi functions anyway

Even when I set a compatible clock speed it still didn’t seem to work, so it might not be the issue.