|
| 1 | +[简体中文 (Chinese)](./README.ZH-CN.md) |
1 | 2 | # micropython-easymenu |
2 | | -这是一个测试版本:适用于 micropython 的菜单库,可以在屏幕上显示菜单 |
3 | | - |
4 | | -3月再来看这个仓库吧,还在写 |
5 | | - |
6 | | -### 支持的芯片 |
7 | | -#### 已测试 |
8 | | -- st7735 |
9 | | -#### 待测试 |
10 | | -- st1306 |
11 | | -- 其他的待定 |
12 | | - |
13 | | -### 软件依赖 |
14 | | -#### 必须 |
15 | | -- https://github.com/funnygeeker/micropython-easydisplay |
16 | | -#### 推荐 |
17 | | -- https://github.com/funnygeeker/micropython-easybutton |
| 3 | + |
| 4 | +A simple menu library for Micropython that allows you to construct a menu through simple configuration. |
| 5 | + |
| 6 | +### Tested Development Boards |
| 7 | +- esp32c3 |
| 8 | + |
| 9 | +#### Tested Screens |
| 10 | +- st7789 |
| 11 | + |
| 12 | +### Software Dependencies |
| 13 | + |
| 14 | +#### Mandatory |
| 15 | +- [micropython-easydisplay](https://github.com/funnygeeker/micropython-easydisplay) |
| 16 | + |
| 17 | +#### Optional |
| 18 | +- [micropython-easybutton](https://github.com/funnygeeker/micropython-easybutton) |
| 19 | + |
| 20 | +### Usage Example |
| 21 | +```python |
| 22 | +# This is an example of usage |
| 23 | +import time |
| 24 | +import framebuf |
| 25 | +from machine import SPI, Pin |
| 26 | +from drivers import st7789_spi |
| 27 | +from libs.easydisplay import EasyDisplay |
| 28 | +from libs.easymenu import EasyMenu |
| 29 | + |
| 30 | +# ESP32C3 & ST7789 |
| 31 | +spi = SPI(1, baudrate=20000000, polarity=0, phase=0, sck=Pin(19), mosi=Pin(18)) |
| 32 | +dp = st7789_spi.ST7789(width=240, height=240, spi=spi, cs=0, dc=1, rst=11, rotation=1) |
| 33 | +ed = EasyDisplay(display=dp, font="/font/text_lite_16px_2311.v3.bmf", show=True, color=0xFFFF, clear=True, |
| 34 | + color_type=framebuf.RGB565) |
| 35 | +menu = { |
| 36 | + 'offset':(16,16), |
| 37 | + 'title': ('Test Menu', 'center', 0), |
| 38 | + 'start': (0, 22), |
| 39 | + 'layout': (2, 2), |
| 40 | + 'spacing': (60, 20), |
| 41 | + 0: {'text': 'TEXT0'}, |
| 42 | + 1: {'text': 'TEXT1', |
| 43 | + 0: {'text': ('TEXT1-0', 0, 0)}, |
| 44 | + 1: {'text': ('TEXT1-1', 0, 0)} |
| 45 | + }, |
| 46 | + 2: {'text': ('TEXT2', 0, 0)}, |
| 47 | + 3: {'text': ('TEXT3', 0, 0)}, |
| 48 | + 4: {'text': ('TEXT4', 0, 0)}, |
| 49 | + 5: {'text': ('TEXT5', 0, 0)} |
| 50 | +} |
| 51 | +em = EasyMenu(ed, menu) |
| 52 | +time.sleep(3) |
| 53 | +em.previous() # Previous option |
| 54 | +time.sleep(3) |
| 55 | +em.next() # Next option |
| 56 | +print(em.select()) # Outputs the content of the current option |
| 57 | +``` |
| 58 | + |
| 59 | +For examples and explanations about EasyDisplay, please visit: [micropython-easydisplay](https://github.com/funnygeeker/micropython-easydisplay) |
| 60 | + |
| 61 | +### Configuration Explanation |
| 62 | +- Note: You can add extra data in the standard menu specification and use the `enter()` and `select()` functions to read the extended data to achieve certain functionalities. |
| 63 | +- The detailed explanation and an example of the `menu` parameter configuration are as follows: |
| 64 | +```python |
| 65 | +menu = {'_len': 1, |
| 66 | + # Current page menu length (automatically managed by the program, please do not fill in if unnecessary) |
| 67 | + 'image': ('/img/text.pbm', 0, 0), |
| 68 | + # Menu page image: If not filled in, it will not be displayed. Tuple ('image file path', x, y) |
| 69 | + # is supported, either as a tuple or as a list. Images can be dat, pbm, or bmp format. |
| 70 | + # The tuple can also be replaced with a list. Images need to include the file extension. |
| 71 | + # For detailed image instructions, please refer to micropython-easydisplay. |
| 72 | + 'title': ('Main Menu', 'center', 0), |
| 73 | + # Menu page title: If not filled in, it will not be displayed. There are two formats: |
| 74 | + # Tuple ('title', x, y) or a string 'title' (default centered text, y-coordinate is 0). |
| 75 | + # The tuple can also be replaced with a list. The x-coordinate of the title supports using |
| 76 | + # 'center', 'left', 'right' instead, and the y-coordinate supports using 'center', 'top', |
| 77 | + # 'bottom' instead. |
| 78 | + 'start': (0, 0), |
| 79 | + # Starting point of options: If not filled in, it will be automatically calculated. Starting |
| 80 | + # from (x, y), each option is displayed at a length equal to the spacing. |
| 81 | + 'clear': (0, 0, 239, 239), |
| 82 | + # Clearing the screen area: (x_start, y_start, x_end, y_end) If not filled in, default to full screen. |
| 83 | + # This option can be used to implement multiple menus on the same screen simultaneously. |
| 84 | + 'style': {'border': 0, 'title_line': 1, 'img_reversion': 0}, |
| 85 | + # Style: If not filled in, default values will be used. Please fill in a dictionary: |
| 86 | + # border: Style of the outer border. In the current version, 0 means no outer border |
| 87 | + # (the selected option will be highlighted), and 1 means having an outer border |
| 88 | + # (the selected option will be highlighted). |
| 89 | + # title_line: Add a line below the title, 0 for disable and 1 for enable. |
| 90 | + # img_reversion: Images will be displayed with reversed colors. Only applicable to bmp and pbm images. |
| 91 | + 'layout': (4, 1), |
| 92 | + # Layout: If not filled in, it will be automatically calculated, but it is recommended to fill in. |
| 93 | + # (x, y), x * y options per page. |
| 94 | + 'offset': (0, 0), |
| 95 | + # Offset: Default is (0, 0). Adds an offset to all images and texts in the options that have not |
| 96 | + # specified an offset. |
| 97 | + 'spacing': (100, 20), |
| 98 | + # Spacing: If not filled in, it will be automatically calculated, but it is recommended to fill in. |
| 99 | + # Coordinates (x, y) for the starting point interval of each option. |
| 100 | + 0: {'text': ('option-1', 0, 0), |
| 101 | + # If not filled in, it will not be displayed. There are two formats: |
| 102 | + # Tuple ('text', x, y) or a string 'text' (default offset is (0, 0)). |
| 103 | + # The tuple can also be replaced with a list. The offset of text can only be expressed as a number. |
| 104 | + 'img': ('/img/text.dat', 0, 0), |
| 105 | + # Option image: If not filled in, it will not be displayed. Tuple ('image file path', x, y) |
| 106 | + # or a string 'image file path' (default offset is (0, 0)) is supported. |
| 107 | + # Images can be dat, pbm, or bmp format. The tuple can also be replaced with a list. |
| 108 | + # Images need to include the file extension. For detailed image instructions, please refer |
| 109 | + # to micropython-easydisplay. |
| 110 | + 'select': True, |
| 111 | + # Whether it can be selected: If not filled in, default to False. When select is True, |
| 112 | + # the option or menu will be displayed, but the cursor will automatically skip this option and |
| 113 | + # cannot be selected. |
| 114 | + }, |
| 115 | + # This is an example of a regular option. If an option contains a key with the number 0, |
| 116 | + # the option will be recognized as a menu, and when the enter() function is used, |
| 117 | + # it will enter the next level menu. |
| 118 | + 1: {'title': 'Sub Menu', |
| 119 | + 'text': 'option-2', |
| 120 | + 'img': '/img/text.dat', |
| 121 | + 'image': ('/img/text.pbm', 0, 0), |
| 122 | + 0: { |
| 123 | + 'text': 'option-2-1' |
| 124 | + }, |
| 125 | + 1: { |
| 126 | + 'text': 'option-2-2' |
| 127 | + } |
| 128 | + }, |
| 129 | + # This is an example of a menu containing menu options. |
| 130 | + } |
| 131 | +``` |
| 132 | + |
| 133 | +- A typical example is as follows: |
| 134 | +```python |
| 135 | +menu = { |
| 136 | + 'offset':(16,16), |
| 137 | + 'title': ('Test Menu', 'center', 0), |
| 138 | + 'start': (0, 22), |
| 139 | + 'layout': (2, 2), |
| 140 | + 'spacing': (60, 20), |
| 141 | + 0: {'text': 'TEXT0'}, |
| 142 | + 1: {'text': 'TEXT1', |
| 143 | + 0: {'text': ('TEXT1-0', 0, 0)}, |
| 144 | + 1: {'text': ('TEXT1-1', 0, 0)} |
| 145 | + }, |
| 146 | + 2: {'text': ('TEXT2', 0, 0)}, |
| 147 | + 3: {'text': ('TEXT3', 0, 0)}, |
| 148 | + 4: {'text': ('TEXT4', 0, 0)}, |
| 149 | + 5: {'text': ('TEXT5', 0, 0)} |
| 150 | +} |
| 151 | +``` |
0 commit comments