Files
python-editor/LED_TUTORIAL.md
Jimmy 7ee15f8eac Stop tracking workspace/; bundled-demos/ is the canonical demo source
`workspace/` is runtime state (per-user folders, no-auth dev's `code/`)
and shouldn't be in git. The same files were previously committed under
both `workspace/code/` and `src/static/bundled-demos/`, which forced a
Docker `diff -q` sync check and leaked user-scoped paths into version
control.

- /workspace/ added to .gitignore; all previously tracked files removed
  via `git rm --cached`.
- src/static/bundled-demos/ becomes the single source of truth: panel16
  demos, led_tutorial, led_patterns, neopixel demos, and main.py move
  here alongside the existing canonical demos.
- New BUNDLED_DEMOS_DIR config; user_workspace seeders read from it.
- main.py lifespan seeds WORKSPACE_ROOT/code/ on startup so a fresh
  clone running `pipenv run dev` still gets the full sample set
  (existing files never overwritten — user edits survive restarts).
- Dockerfile drops `COPY workspace` and the diff sanity check.
- README/LED_TUTORIAL repointed at the new canonical paths.
- test_led_patterns loads led_patterns.py from bundled-demos.
- test_api uses mkdir(exist_ok=True) for `code/` (startup pre-creates).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-10 06:55:59 +12:00

76 lines
1.5 KiB
Markdown

# Python LED Tutorial (NeoPixel Focus)
This tutorial is for the browser editor's ESP32-style mocks:
- `machine.Pin`
- `neopixel.NeoPixel`
Open `code/led_tutorial.py` in the editor while reading this guide. (Source of truth: `src/static/bundled-demos/led_tutorial.py` — the editor's `code/` folder is seeded from there on first run.)
## 1) Basic setup
```python
from machine import Pin
import neopixel
np = neopixel.NeoPixel(Pin(4), 12)
```
- `Pin(4)` means data pin 4 (matching common ESP32 examples).
- `12` is the number of LEDs in the strip/ring.
- `np` is your LED strip object.
## 2) Set one LED color
```python
np[0] = (255, 0, 0) # red
np.write()
```
- Colors are `(red, green, blue)` from `0` to `255`.
- Nothing updates visually until `np.write()`.
## 3) Fill all LEDs
```python
np.fill((0, 0, 255)) # all blue
np.write()
```
## 4) Clear LEDs (turn off)
```python
np.fill((0, 0, 0))
np.write()
```
## 5) Animate over time
```python
import time
for step in range(20):
np.fill((step * 10, 0, 255 - step * 10))
np.write()
time.sleep(0.08)
```
`time.sleep(...)` controls animation speed.
## 6) Moving pixel example
```python
for i in range(len(np)):
np.fill((0, 0, 0))
np[i] = (255, 120, 0)
np.write()
time.sleep(0.06)
```
## 7) Tips
- Keep color values in `0..255`.
- Use helper functions for repeated color logic.
- Start with short loops, then increase frames once behavior looks good.
- If the simulator is closed, run your script again to show updates.