Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaking main scripts and lib #42

Merged
merged 35 commits into from Jan 9, 2023
Merged

Conversation

ace-dent
Copy link
Contributor

@ace-dent ace-dent commented Dec 13, 2022

The main.py and menu.py should be lean. 🗜💪

Small tweaks to clean things up. Mainly trimming unused imports.
Tested and looks ok. (Thanks @fuglaro for extensive review and testing).

Speed

Benchmarked with ticks_ms:
main.py boot reduced from ~754ms to ~246ms (now takes ~1/3rd of the time!).
menu.py up to main loop, reduced from ~1766ms to ~1581ms.

Memory

Memory savings using print(gc.mem_free()):
Vanilla
Before GC: mem_free=81376
After GC: mem_free=122112
This PR
Before GC: mem_free=84288 (+2,912 bytes free) ~2.8kb!!
After GC: mem_free=122144 (+32 bytes free) - the GC was managing to remove all the unused imports pretty well.

- Remove old code for faster parsing. (Exists in git history if needed).
- Only import what is used.
- Reformat `freq(X_000_000)` to clarify MHz.
- Replace `time.sleep()` with `machine.lightsleep()`, for small power saving; reduces `time` imports.
- Fix `reset()` at end of ‘main.py’.
- Remove extra setting of FPS. No noticeable difference for user.
- Actually remove the `time` import.
- Make sure `lightsleep` is available when needed.
- Remove printing `audioSetting`.
- Comment out `gc` debugging stats for now (delete in future?).
- Make import overclock consistent 250 MHz (as used in `thumby.py`)
- Do not overclock in `main.py`.
ThumbyGames/menu.py Outdated Show resolved Hide resolved
ThumbyGames/main.py Outdated Show resolved Hide resolved
ThumbyGames/menu.py Outdated Show resolved Hide resolved
@ace-dent ace-dent changed the title Tweaking main scripts Tweaking main scripts and lib Dec 14, 2022
- Remove commented out code.
- Format `freq(X_000_000)` to clarify MHz.
- Only import what is required.
- Only import necessary functions from `os` and `gc`.
- Reinstate garbage collection (thanks @fuglaro).
- Reduce imports from `sys`.
- Fix typo for import of gc.
- Revert use of `machine.lightsleep` for welll tested `time.sleep_ms`. Power saving would be small. (Thanks @fuglaro).
- Run at 133MHz base speed on startup; no need to underclock to 125MHz.
- Not overclocking for stability.
- Change copyright / legal date for international audience, and to distinguish between code edit dates.
- Add `# -Emulation edition` to the emu files, for clarity while editing.
- Sync changes back to the emulator lib copies.
- Add date of last update to main.py and menu.py; Matches lib files and helps debugging.
ThumbyGames/main.py Show resolved Hide resolved
ThumbyGames/menu.py Show resolved Hide resolved
ThumbyGames/menu.py Show resolved Hide resolved
- Fix hot patching in `play.js` emulator. (Thanks @fuglaro).
- Remove debug print of config from `main.py`.
- Remove debug print from `thumbyGraphics.py`.
- Remove HWID and do minimum required to display splash screen.
- Avoids duplicate code in `thumbyHardware.py` (HWID is already determined by thumby lib, executed from menu.py).
- Remove import of `machine.Timer` - unused.
- Use `if else` statement; Can only be one connection or the other.
- Group blocks of code for easier understanding: init screen, then set brightness.
- Conditionally import `I2C` if needed.
- Remove unused `Timer` import
- Remove debug code. … last one… I hope!
- Copy across recent tweaks to emulator lib files.
- Update Changelog.
- Only import the `SSD1306_SPI` class by default.
- Conditionally import `SSD1306_I2C` if required.
@fuglaro
Copy link
Contributor

fuglaro commented Dec 17, 2022

Looking awesome. I love these changes. I can do some more testing of the newer stuff tomorrow.

- Change ‘low’ brightness to `1`, as `0` is too dim on newer OLED models (thanks agovtman) .
- Make the default brightness setting consistently `mid`in all places.
- Restore Thumby frequency back to 125MHz when launching a game. (~6% underclock).
- Do faster import for `mem32()` and `soft_reset()` during startup, rather than at launch time.
- Precalculate watchdog timer scratch register `0x4005800C` (readability).
- Bonus: uniquely name `gc_collect()`.
- Remove unused `micropython const` from `ssd1306.py`.
- Minimize `os` imports for `thumbySaves.py`.
@fuglaro
Copy link
Contributor

fuglaro commented Dec 21, 2022

Note for testing:
I think only three games on the Arcade currently use the thumby saves API:
AstroJumper
BurnsCharade
PicoPac

ThumbyGames/menu.py Outdated Show resolved Hide resolved
js/play.js Outdated Show resolved Hide resolved
- Fix detecting later revisions of the PCB (HWID=2) in `main.py` (thanks @fuglaro).
- Avoid importing `ssd1306.SSD1306_I2C` unless necessary.
- Update `thumbyGraphics` to set pins to `PULL_DOWN’ earlier.
- Sync the emulator to replace the `mem32` string (thanks @fuglaro).
- Remove unused `tick_us` import and `start` timer variable.
- Remove font setting (it is the default already).
- Remove unused banner bitmap.
- Restore faster scrolling of the splashscreen (thanks @fuglaro).
- Combine 2 `machine` imports for `thumbyHardware.py`.
- Small improvements to text of some comment lines.
- Use the correct `thumby.reset()` at the end of file for menu.py (`machine.reset()` is no longer imported).
- Note: This is redundant code; it will never execute. It is kept to indicate intent/ current practice.
Copy link
Collaborator

@masonova1 masonova1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the WDT scratch address as a named constant? It's unclear to me how this improves readability, and the code only needs to run once on-startup, so lookup overhead shouldn't be a problem here. Other than that, everything looks good!

@ace-dent
Copy link
Contributor Author

ace-dent commented Jan 9, 2023

@masonova1 -
Thanks for taking the time to review this PR (and many commits!)...
Readability is perhaps subjective. May I ask you: Is there a need to communicate to the casual user, that the base address is at 0x40058000 and the first register '0' is at 0x0C? I kinda think that's great for a data sheet, but practically, I want to just glance at this 'magic' address where we set and read a flag ...IMHO. I also think with using the next 3 registers, it starts to look cluttered. (See here and here). If you feel strongly on this, I can add back in the extra variables. Many thanks,
Andrew

@masonova1
Copy link
Collaborator

@masonova1 - Thanks for taking the time to review this PR (and many commits!)... Readability is perhaps subjective. May I ask you: Is there a need to communicate to the casual user, that the base address is at 0x40058000 and the first register '0' is at 0x0C? I kinda think that's great for a data sheet, but practically, I want to just glance at this 'magic' address where we set and read a flag ...IMHO. I also think with using the next 3 registers, it starts to look cluttered. (See here and here). If you feel strongly on this, I can add back in the extra variables. Many thanks, Andrew

I'm honestly fine with less words. If somebody wants the exposition, they would need to go back to the definition to see what it means anyways. If somebody complains, we'll bring it back. We'll get this merged soon.

@masonova1 masonova1 merged commit cbec6d8 into TinyCircuits:master Jan 9, 2023
@ace-dent ace-dent deleted the PR1-Tweaks branch January 9, 2023 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants