Skip to content

Commit

Permalink
Update wiki for 2.x (#76)
Browse files Browse the repository at this point in the history
* Create Palette.md

* Update Palette.md

* Update HydraMenu.md

* Update Playing-Sound.md

* Update popup.md

* Update userinput.md

* Update Home.md
  • Loading branch information
echo-lalia authored Aug 19, 2024
1 parent df99773 commit ef69a9d
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 156 deletions.
6 changes: 3 additions & 3 deletions wiki/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ MicroHydra includes a built-in library, intended to help you easily make apps. C
├── $lib$
      ├── [audio](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Playing-Sound)
      ├── [display](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Display)
           ├── palette
           └── named_palette
           ├── [palette](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Palette)
           └── [namedpalette](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Palette#libdisplaynamedpalettenamedpalette)
     
      ├── $hydra$
           ├── beeper
           ├── [beeper](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Playing-Sound#beeper)
           ├── color
           ├── [config](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/Accessing-config-files)
           ├── [menu](https://github.com/echo-lalia/Cardputer-MicroHydra/wiki/HydraMenu)
Expand Down
38 changes: 19 additions & 19 deletions wiki/HydraMenu.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
[HydraMenu.py](https://github.com/Gabriel-F-Sousa/HydraMenu/blob/main/HydraMenu.py) is a module contributed by [Gabriel-F-Sousa](https://github.com/echo-lalia/Cardputer-MicroHydra/commits?author=Gabriel-F-Sousa), which is designed to make it easy to create menu screens for MicroHydra apps.
[HydraMenu.py](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/wikiimprovements/src/lib/hydra/menu.py) is a module contributed by [Gabriel-F-Sousa](https://github.com/echo-lalia/Cardputer-MicroHydra/commits?author=Gabriel-F-Sousa), which is designed to make it easy to create menu screens for MicroHydra apps.

HydraMenu is being utilized heavily by the (newly refurbished) inbuilt [settings app](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/main/MicroHydra/launcher/settings.py). Please take a look at that file for some practical examples of what can be done with the module.
HydraMenu is being utilized heavily by the (newly refurbished) inbuilt [settings app](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/wikiimprovements/src/launcher/settings.py). Please take a look at that file for some practical examples of what can be done with the module.

And for a simplified example app utilizing HydraMenu, check out the [example app](https://github.com/Gabriel-F-Sousa/HydraMenu/blob/main/examplebuf.py).

Here's a trimmed excerpt from that example, for your reference:
Here's a simplified example, for your reference:
``` Python
from lib import st7789fbuf, keyboard, mhconfig, HydraMenu
from lib.display import Display
from lib.userinput import UserInput
from lib.hydra.config import Config
from lib.hydra import menu as HydraMenu

# ...
userinput = UserInput()
display = Display()
config = Config()
# ...

...

""" Create our HydraMenu.Menu:
"""
menu = HydraMenu.Menu(
# display_fbuf is passed to signal that we are using the st7789fbuf driver.
# we would use display_py if we were using st7789py.
display_fbuf=display,
# pass our config object, which was created above.
# if omitted, HydraMenu will create it's own config object.
config=config,
)
menu = HydraMenu.Menu()


"""Add menu items to the menu:
Expand All @@ -36,10 +35,10 @@ menu.append(HydraMenu.IntItem(
min_int=0, max_int=10,
# callbacks are what makes this all functional.
# Pass the function you want to be called every time a value is confirmed.
callback=print_menu_callback,
callback=lambda item, val: print(f"Confirmed val: {val}"),
# You can also use an 'instant_callback' if you want to track the value as it changes.
# this is what the main settings app uses to update the volume, and ui colors as they're changed.
instant_callback=print_instant_callback
instant_callback=lambda item, val: print(val)
))

...
Expand All @@ -51,7 +50,7 @@ redraw = True
while True:

# get our newly pressed keys
keys = kb.get_new_keys()
keys = userinput.get_new_keys()

# pass each key to the handle_input method of our menu.
for key in keys:
Expand All @@ -69,4 +68,5 @@ while True:
# and False when the animation is done (therefore, does not need to be redrawn until another key is pressed)
redraw = menu.draw()
display.show()
```

```
85 changes: 85 additions & 0 deletions wiki/Palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## lib.display.palette.Palette

This Palette class is designed to be used for storing a list of RGB565 colors,
and for returning the apropriate colors by index to be used with MicroHydra's display module.

When `Config` from `lib.hydra.config` is initialized, it reads the user set color data from `config.json` and creates a 16-color palette from it.

<br />

Key notes on Palette:
- Has a static length of 16 colors

- Is used by both `lib.hydra.config.Config` and `lib.display.Display` (it is the same Palette in both)

- uses a bytearray to store color information,
this is intended for fast/easy use with Viper's ptr16.

- Returns an RGB565 color when using normal framebuffer, or an index when Display is initialized with `use_tiny_buf`.
(This makes it so that you can pass a `Palette[i]` to the Display class in either mode.)

- Palette is a singleton, which is important so that different MH classes can modify and share it's data
(without initializing the Display).


Retrieving a color from the Palette is fairly fast, but if you want to maximize your speed, it's probably smart to read and store the colors you need as local variables (after initializing the `Display` and `Config`).

<br />

For your reference, here is a complete list of the colors, by index, contained in the palette:
<ol start="0">
<li>Black</li>
<li>Darker bg_color</li>
<li>bg_color</li>
</ol>

<ol start="3">
<li>84% bg_color 16% ui_color</li>
<li>67% bg_color 33% ui_color</li>
<li>50% bg_color 50% ui_color (mid_color)</li>
<li>33% bg_color 67% ui_color</li>
<li>16% bg_color 84% ui_color</li>
</ol>

<ol start="8">
<li>ui_color</li>
<li>Lighter ui_color</li>
<li>white</li>
</ol>

<ol start="11">
<li>reddish bg_color</li>
<li>greenish mid_color</li>
<li>bluish ui_color</li>
</ol>

<ol start="14">
<li>compliment bg_color (opposite hue)</li>
<li>compliment ui_color</li>
</ol>

<br /><br /><br /><br />


## lib.display.namedpalette.NamedPalette

As an alternative, for improved readability and simplicity, you can import the optional `NamedPalette` class, which works similarly to the normal `Palette`, but also accepts strings containing color names.

``` Py
names = {
'black':0,
'bg_dark':1,
'bg_color':2,
'mid_color':5,
'ui_color':8,
'ui_light':9,
'white':10,
'red':11,
'green':12,
'blue':13,
'bg_complement':14,
'ui_complement':15,
}
```

<br />
41 changes: 25 additions & 16 deletions wiki/Playing-Sound.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ There are two main modules built-in to MicroHydra currently, which can be used f

<br />

### M5Sound.py
[M5Sound](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/main/MicroHydra/lib/M5Sound.py) is a module contributed by [Lana-chan](https://github.com/echo-lalia/Cardputer-MicroHydra/commits?author=Lana-chan) for playing high-quality sound on the Cardputer.
### lib.audio.Audio
MicroHydra's [audio](https://github.com/echo-lalia/Cardputer-MicroHydra/tree/wikiimprovements/src/lib/audio) module subclasses the apropriate audio module for the current device (Currently this is only `i2ssound`, but may be expanded in the future), and initializes it with the apropriate values for the device.

It can play samples stored in a variable, or read large samples from storage using little memory.
It also can change the pitch of those samples, and even play several at the same time, overlapping. Unlike the beeper module, this one is non-blocking (your code can continue to execute while sound is playing).
Note:
> *`i2ssound` was previously named `M5Sound`, and was contributed by [Lana-chan](https://github.com/echo-lalia/Cardputer-MicroHydra/commits?author=Lana-chan), for playing high-quality sound on the Cardputer.
> It has been renamed for consistency with MicroHydras other modules.*
It can play samples stored in a variable, or read large samples from storage using little memory.
It also can change the pitch of those samples, and even play several at the same time, overlapping.

<br />

Expand All @@ -16,9 +20,9 @@ It also can change the pitch of those samples, and even play several at the same

``` Python
import time
from lib import M5Sound
from lib.audio import Audio

sound = M5Sound.M5Sound()
audio = Audio()

_SQUARE = const(\
b'\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80\x00\x80'\
Expand All @@ -32,17 +36,17 @@ _SQUARE = const(\
SQUARE = memoryview(_SQUARE)

for note in range(24,60): # note values, C-3 to B-5
sound.play(SQUARE, note, 0, 14, 0, True)
audio.play(SQUARE, note, 0, 14, 0, True)
for i in range(13,1,-1): # fade out volume
sound.setvolume(i)
audio.setvolume(i)
time.sleep(0.05)
sound.stop(0)
audio.stop(0)
```

Samples can also be loaded from a 'raw' file on the flash or SDCard.

``` Python
M5Sound.Sample(source, buffer_size=1024)
i2ssound.Sample(source, buffer_size=1024)
"""Initialize a sample for playback
- source: If string, filename. Otherwise, use MemoryView.
Expand All @@ -55,26 +59,31 @@ M5Sound.Sample(source, buffer_size=1024)
<br /><br /><br />

### beeper
[beeper.py](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/main/MicroHydra/lib/beeper.py) is a module for playing simple UI beeps.
[beeper.py](https://github.com/echo-lalia/Cardputer-MicroHydra/blob/wikiimprovements/src/lib/hydra/beeper.py) is a module for playing simple UI beeps.

This module is very imperfect. It is somewhat limited in its use, and it uses more memory than what is probably needed. However, it *is* simple to use, and it thankfully does work decent for short UI tones.
This module is very imperfect. It is somewhat limited in its use, however, it *is* simple to use.
> In previous versions, this module used its own, blocking, implementation for sending audio to the speaker. However, the current version is just a wrapper for the `Audio` class above. The audio is now much higher quality and more consistent, however there is a noticable delay in it. I would like to fix this in the future, but I'm not sure how to do that.


To use it:

``` Python
from lib import beeper
from lib.hydra import beeper
```

``` Python
#init the beeper!
beep = beeper.Beeper()

beep.play(
notes=('C4',('D4','D4')), # a tuple of strings containing notes to play. a nested tuple can be used to play multiple notes together.
time_ms=120, # how long to play each note
volume=4) # an integer from 0-10 representing the volume of the playback. Default is 2, 0 is almost inaudible, 10 is loud.
# a tuple of strings containing notes to play. a nested tuple can be used to play multiple notes together.
notes=('C4',('D4','D4')),
# how long to play each note
time_ms=120,
# an integer from 0-10 representing the volume of the playback. Default is 2, 0 is almost inaudible, 10 is loud.
volume=10,
)
```

<br /><br /><br />
Expand Down
Loading

0 comments on commit ef69a9d

Please sign in to comment.