PicoCalc Lua - now with sound!

starting a new thread for the PicoCalc Lua firmware for a huge new release! here’s what version 0.5 brings:

  • Replaces SPI with PIO in LCD driver for a small speed boost without the need to change clk_peri
  • Sound module
  • Sprites support
  • Some API changes
  • Kilo text editor:
    • Improvements in Page Up/Down, Shift-Left/Right cursor movement
    • Selection mode with clipboard
    • Line numbers (CTRL-L) (thanks @wkjagt)
    • Less flicker when moving cursor
    • Adding newline keeps previous’ indentation
  • Demos:
    • New demo: boxworld.lua to showcase music, sprites, and contains 100 classic Sokoban levels to keep you busy :slight_smile:
    • New demo: speedtest.lua is a very simple tool to determine screen-writing speeds
    • New demo: piano.lua to showcase the sound driver in real-time
    • New demo: browser.lua as a very simple but easy way to browse and edit Lua files
    • asteroids.lua: Added sound effects
    • bubble.lua: Speed up with look-up tables
  • Added a credits() command

download here!

with this update i’ve included a conversion of a beloved Windows 3.11 game of mine, Box World, to showcase the new graphics and sound drivers :]

14 Likes

Great work!

In case if anyone wants to try this lua with sound on Pico 1 for PicoCalc

here is the pre-compiled version based on the commit hash

847aa1778607de66a3533b2ed62a2259816f1b1f

uf2 file:

multi boot bin file:

and here is the patch I’ve made:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fcc867f..cbe27c1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,7 +12,7 @@ if (EXISTS ${picoVscode})
     include(${picoVscode})
 endif()
 # ====================================================================================
-set(PICO_BOARD pico2 CACHE STRING "Board type")
+set(PICO_BOARD pico CACHE STRING "Board type")
 
 cmake_minimum_required(VERSION 3.13)
 
diff --git a/drivers/keyboard.c b/drivers/keyboard.c
index 344755a..2044924 100644
--- a/drivers/keyboard.c
+++ b/drivers/keyboard.c
@@ -11,7 +11,7 @@
 #define KBD_MOD    i2c1
 #define KBD_SDA    6
 #define KBD_SCL    7
-#define KBD_SPEED  20000 // if dual i2c, then the speed of keyboard i2c should be 10khz
+#define KBD_SPEED  10000 // if dual i2c, then the speed of keyboard i2c should be 10khz
 #define KBD_ADDR   0x1F
 
 // Commands defined by the keyboard driver
@@ -31,7 +31,7 @@ enum {
 	REG_ID_C64_JS = 0x0d,  // joystick io bits
 };
 
-#define I2C_TIMEOUT 10000
+#define I2C_TIMEOUT 50000
 volatile atomic_bool i2c_in_use = false;
 
 #define KEY_COUNT 256
@@ -54,7 +54,7 @@ static int keyboard_modifiers;
 static int i2c_kbd_write(unsigned char* data, int size) {
 	if (atomic_load(&i2c_in_use) == true) return 0;
 	atomic_store(&i2c_in_use, true);
-	int retval = i2c_write_timeout_us(KBD_MOD, KBD_ADDR, data, size, false, I2C_TIMEOUT * size);
+	int retval = i2c_write_timeout_us(KBD_MOD, KBD_ADDR, data, size, false, I2C_TIMEOUT );
 	atomic_store(&i2c_in_use, false);
 	if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
 		printf("i2c_kbd_write: i2c write error\n");
@@ -66,7 +66,7 @@ static int i2c_kbd_write(unsigned char* data, int size) {
 static int i2c_kbd_read(unsigned char* data, int size) {
 	if (atomic_load(&i2c_in_use) == true) return 0;
 	atomic_store(&i2c_in_use, true);
-	int retval = i2c_read_timeout_us(KBD_MOD, KBD_ADDR, data, size, false, I2C_TIMEOUT * size);
+	int retval = i2c_read_timeout_us(KBD_MOD, KBD_ADDR, data, size, false, I2C_TIMEOUT);
 	atomic_store(&i2c_in_use, false);
 	if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
 		printf("i2c_kbd_read: i2c read error\n");
@@ -216,4 +216,4 @@ int get_battery(bool* charging) {
 	result >>= 8;
 	if (charging) *charging = (result & 1<<7) != 0;
 	return result & ~(1<<7);
-}
\ No newline at end of file
+}

1 Like

hmm, i did try the 2040 binary i generated with a 1w, since i don’t have a way to wire my 1h for debugging , and didn’t find any issues with the keyboard

i’ll try those patches on my end and if the keyboard works just as well on my 2 and 1w with those values i’ll just apply that patch upstream anyway

thanks!

This is beyond my dreams, bravo!

Do you think you could extend the PIO display driver to do palette lookups from the framebuffer (instead of doing it on the CPU)? Also, sprites could be accelerated by PIO as well…

likely! i thought about it, but i know aaabsolutely nothing about how to properly interface with and program the PIO yet, i only aped the LCD example here with some adaptation: pico-examples/pio/st7789_lcd/st7789_lcd.c at master · raspberrypi/pico-examples · GitHub

i would love to make it better, polpo’s psram library is also very limiting because it measures transfer length in bits and uses an 8-bit value to do so, so you max out at asking for about 20 bytes or so before transfers just stop working, and i couldn’t figure out how to make it use a 16-bit value instead…

i think increasing the PSRAM transfer length (maybe even blit from PSRAM to LCD without CPU involvement at all!) and palettized sprites and framebuffer would make this killer for programming games :] i’ll study the PIO more and maybe one day!

1 Like

Hello, I am having problems with versions 0.4 and 0.5. With these versions, the SD card is not recognized and the text “card mounted” does not appear. However, it works with version 0.3. Has anyone else experienced this?

Is the Lua project still being maintained?

Thank you!

i can’t say i had those issues, but i admit it’s been a while since i last looked at the code… i’m not sure what could affect the mounting at boot between those versions

i’m still around and want to continue it! i just (un)fortunately have a full time job now :sweat_smile:

have you tried reinserting the card after boot? there’s a hot swapping system in place that should try to remount the card every time it’s inserted

Hi, I’ve been playing with v0.5-6-gdaf20a2 on my PicoCalc with a pico2 for a day and I’m liking it! I’ve run into a couple of isses though:

  1. The kilo editor gets i2c read and write errors the first time I hit a key, and seems to hang the keyboard processor. Could this be a conflict with the multicore use?
  2. The cc editor gets out of sync between the visual cursor position and the actual input point, such that when I insert or delete characters, the insertion/deletion occurs several characters away from the cursor. I think this may have to do with lines longer than the screen width, somehow causing screenX or scrollX to get out of sync with x.

Still, this is a promising effort! I’m having fun tinkering with it.

1 Like

this is something that happens with the stock keyboard firmware i believe, but i’m not entirely sure why. since i use Jack Carter Smith’s custom one, i never caught it. i will put this in my backlog to investigate

yeaahh, so that editor i aped from a Minecraft mod, ComputerCraft because i thought it would be the quickest way to get a working editor without having to write one from scratch. i don’t remember that i ever actually got it fully working because of how much RAM it uses, once i found kilo i immediately abandoned it

Thanks for your answer, when I eject and insert the card it just say:

SD unplugged! Unmounted!
SD inserted, mounting… Failed to mount!

then something is going wrong at the mount process and that’s why it won’t say anything at boot either.. I’d have to take a look if anything changed between the versions you mentioned since i don’t have trouble mounting any of my cards, weird

i’ve been away from the PicoCalc game for a while due to being busy with other things, so it might be a bit until i can chase up on this

Thanks! I flashed the Jack Carter Smith firmware and kilo works now. There’s still a minor display glitch where arrowing over a character deletes a few of its pixels, but it’s quite usable.

Now to do some benchmarks comparing picocalc lua to ulisp!

1 Like

this is known.. it’s more to do with the fact that i cannot easily (since the display driver has no memory of it) replace the glyph once the cursor draws over it

the solution is to use a font (the getting started guide on GitHub should show you how) which doesn’t have pixels in the column that the cursor is drawn onto. i believe using one of the HP fonts as your default should solve this

i need to change the default font so it suffices that as well

Same for me (setup: Pico 2 W with UF2 Loader).

But I think I found a solution for this and created a GitHub PR with the fix.

1 Like

thank you! curious that setting the CPU frequency like that affects it, probably because of what i was doing with clk_peri? i’ll investigate a bit now that i have a lead and i’ll pull that in for the next release :]