Skip to content

Commit 9c6f183

Browse files
authored
Merge pull request #74 from pimoroni/helgibbons-patch-1
PicoVector and EzWifi documentation tweaks
2 parents a55f759 + b821018 commit 9c6f183

File tree

3 files changed

+202
-27
lines changed

3 files changed

+202
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ There are two choices, a regular build that just updates the MicroPython firmwar
3636
* [Function Reference](docs/presto.md)
3737
* [Pico Graphics documentation](https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics/README.md)
3838
* [Pico Vector documentation](docs/picovector.md)
39+
* [EzWiFi documentation](docs/wifi.md)
3940

4041
## Other Resources
4142

docs/picovector.md

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,46 +158,27 @@ Under the hood PicoVector uses [Alright Fonts](https://github.com/lowfatcode/alr
158158
a font-format for embedded and low resource platforms.
159159

160160
Alright Fonts supports converting TTF and OTF fonts into .af format which can
161-
then be displayed using PicoVector. Most of your favourite fonts should work
162-
- including silly fonts like [Jokerman](https://en.wikipedia.org/wiki/Jokerman_(typeface)) - but there are some limitations to their complexity.
161+
then be displayed using PicoVector. Most of your favourite fonts should work, including silly fonts like [Jokerman](https://en.wikipedia.org/wiki/Jokerman_(typeface)) - but there are some limitations to their complexity.
163162

164163
### Converting
165164

166165
Converting from an OTF or TTF font is done with the `afinate` utility. It's a
167166
Python script that handles decomposing the font into a simple list of points.
168167

169-
Right now you'll need the `port-to-c17` branch:
168+
The latest version of this conversion utility can be found at the repo below, along with installation instructions and some pre-converted sample fonts:
170169

171-
```
172-
git clone https://github.com/lowfatcode/alright-fonts --branch port-to-c17
173-
```
174-
175-
And you'll need to set up/activate a virtual env and install some dependencies:
176-
177-
```
178-
cd alright-fonts
179-
python3 -m venv .venv
180-
source .venv/bin/activate
181-
pip install freetype.py simplification
182-
```
183-
184-
And, finally, convert a font with `afinate`:
185-
186-
```
187-
./afinate --font jokerman.ttf --quality medium jokerman.af
188-
```
189-
190-
This will output two things- a wall of text detailing which characters have
191-
been converted and how many points/contours they consist of, and the font
192-
file itself. You'll then need to upload the font to your board, this could
193-
be via the file explorer in Thonny or your preferred method.
170+
- [PicoVector Fonts / Alright Fonts](https://github.com/pimoroni/picovector-fonts)
194171

195172
### Loading & Configuring
196173

197174
```python
198175
vector.set_font("jokerman.af", 24)
199176
```
200177

178+
`set_font` specifies the font and size.
179+
180+
Your *.af font file will need to be present on Presto's file system, you can upload it using Thonny's Files window or another means like `mpremote`.
181+
201182
### Spacing & Alignment
202183

203184
* `set_font_size()`

docs/wifi.md

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,194 @@
1-
# Presto WiFi
1+
# EzWiFi <!--omit in toc-->
2+
3+
- [EzWiFi ](#ezwifi-)
4+
- [Easy EzWiFi](#easy-ezwifi)
5+
- [EzWiFi Class](#ezwifi-class)
6+
- [Asyncio](#asyncio)
7+
- [Connect Options](#connect-options)
8+
- [Handlers](#handlers)
9+
- [Other Functions](#other-functions)
10+
11+
EzWiFi, or Easy WiFi, is a helper module to get you connected to wireless networks.
12+
13+
It's based around the use of `secrets.py`, a Python file that very simply tucks
14+
your wireless SSID and password away for use across multiple scripts.
15+
16+
`secrets.py` looks like this:
17+
18+
```python
19+
WIFI_SSID = "your_ssid"
20+
WIFI_PASSWORD = "your_password"
21+
```
22+
23+
## Easy EzWiFi
24+
25+
The easiest way to use EzWiFi is with the blocking `connect` method, like so:
26+
27+
```python
28+
import ezwifi
29+
30+
ezwifi.connect()
31+
```
32+
33+
This will load login details from `secrets.py` and quietly connect to your
34+
wireless network. It will try ten times by default with an overall timeout
35+
of 60 seconds.
36+
37+
If you need a little more debugging information you can
38+
supply a log handler like so:
39+
40+
```python
41+
import ezwifi
42+
43+
ezwifi.connect(verbose=True)
44+
```
45+
46+
If you need specific log messages, want to perform an action or display a message
47+
on screen when connected/failed then you can supply:
48+
49+
* `connected` - Called when a connection is established (with no message).
50+
* `failed` - Called when the connection fails.
51+
* `info` - Called with basic info messages.
52+
* `warning` - Called when a connection attempt fails (and in other cases in future).
53+
* `error` - Called when a connection totally fails (and in other cases in future).
54+
* `failed` - Called when a connection fails (with no message).
55+
56+
For example, this will call a function if/when the connection succeeds or fails:
57+
58+
```python
59+
import ezwifi
60+
61+
62+
def connect_handler(wifi):
63+
pass
64+
65+
66+
def failed_handler(wifi):
67+
pass
68+
69+
70+
ezwifi.connect(connected=connect_handler, failed=failed_handler)
71+
```
72+
73+
## EzWiFi Class
74+
75+
EzWiFi is also available as a class if you need to integrate with async.
76+
77+
Create an instance with:
78+
79+
```python
80+
from ezwifi import EzWiFi
81+
82+
wifi = EzWiFi()
83+
```
84+
85+
You can then use the async `connect()` method, if you're using the class and
86+
want this to run in synchronous code you'll need to:
87+
88+
```python
89+
import asyncio
90+
91+
asyncio.get_event_loop().run_until_complete(wifi.connect())
92+
```
93+
94+
### Asyncio
95+
96+
With asyncio you can do other things while waiting for WiFi to connect, like so:
97+
98+
```python
99+
import asyncio
100+
from ezwifi import EzWiFi
101+
102+
wifi = EzWiFi()
103+
104+
105+
@wifi.on("connected")
106+
async def handle_connect(wifi):
107+
print("Connected!")
108+
109+
110+
@wifi.on("failed")
111+
async def handle_connect(wifi):
112+
print("Failed!")
113+
114+
115+
async def main():
116+
wifi_task = asyncio.create_task(wifi.connect())
117+
while True:
118+
print("Main loop...")
119+
await asyncio.sleep_ms(1000)
120+
121+
122+
asyncio.run(main())
123+
```
124+
125+
### Connect Options
126+
127+
You can supply an optional `ssid`, `password`, `timeout` and number of retries to
128+
`connect()`:
129+
130+
### Handlers
131+
132+
If you need specific log messages, want to perform an action or display a message
133+
on screen when connected/failed then you use the following handlers:
134+
135+
* `connected` - Called when a connection is established (with no message).
136+
* `failed` - Called when the connection fails.
137+
* `info` - Called with basic info messages.
138+
* `warning` - Called when a connection attempt fails (and in other cases in future).
139+
* `error` - Called when a connection totally fails (and in other cases in future).
140+
* `failed` - Called when a connection fails (with no message).
141+
142+
These can be supplied to EzWiFi as an argument, eg:
143+
144+
```python
145+
import asyncio
146+
from ezwifi import EzWiFi
147+
148+
149+
async def info_handler(wifi, message):
150+
print(message)
151+
152+
153+
wifi = EzWiFi(info=info_handler)
154+
155+
156+
async def main():
157+
wifi_task = asyncio.create_task(wifi.connect())
158+
while True:
159+
print("Main loop...")
160+
await asyncio.sleep_ms(1000)
161+
162+
163+
asyncio.run(main())
164+
```
165+
166+
Or by using the `wifi.on` decorator:
167+
168+
```python
169+
import asyncio
170+
from ezwifi import EzWiFi
171+
172+
wifi = EzWiFi()
173+
174+
175+
@wifi.on("info")
176+
async def info_handler(wifi, message):
177+
print(message)
178+
179+
180+
async def main():
181+
wifi_task = asyncio.create_task(wifi.connect())
182+
while True:
183+
print("Main loop...")
184+
await asyncio.sleep_ms(1000)
185+
186+
187+
asyncio.run(main())
188+
```
189+
190+
### Other Functions
191+
192+
* `ipv4` - returns the ipv4 address, shortcut for `if.ipconfig("addr4")[0]`
193+
* `ipvv6` - returns the ipv4 address, shortcut for `if.ipconfig("addr6")[0]`
194+
* `isconnected` - returns the connection status

0 commit comments

Comments
 (0)