diff --git a/plugins/superstack/skills/s2-lua/references/lua-api.md b/plugins/superstack/skills/s2-lua/references/lua-api.md index 85a37a7..f3b7fac 100644 --- a/plugins/superstack/skills/s2-lua/references/lua-api.md +++ b/plugins/superstack/skills/s2-lua/references/lua-api.md @@ -29,6 +29,8 @@ Complete, ready-to-run code is in the example repository. If you write code with - [I2C communication](#i2c-communication) - [SPI communication](#spi-communication) - [UART communication](#uart-communication) + - [Microphone (PDM)](#microphone-pdm) + - [Addressable LEDs (WS2812)](#addressable-leds-ws2812) - [Networking libraries](#networking-libraries) - [Networking (LTE)](#networking-lte) - [Location (GPS)](#location-gps) @@ -67,6 +69,10 @@ Every function and constant on this page, in page order. Each links to its full | [`device.uart.write()`](#write-uart-data) | Write UART data | | [`device.uart.assign_read_event()`](#assign-an-event-handler-for-uart-data) | Assign an event handler for UART data | | [`device.uart.unassign_read_event()`](#disable-the-uart-read-event-handler) | Disable the UART read event handler | +| [`device.pdm.read()`](#record-audio-from-a-pdm-microphone) | Record audio from a PDM microphone | +| [`device.pdm.assign_read_event()`](#assign-an-event-handler-for-microphone-audio) | Assign an event handler for microphone audio | +| [`device.pdm.unassign_read_event()`](#disable-the-microphone-read-event-handler) | Disable the microphone read event handler | +| [`device.ws2812.write()`](#set-the-colors-of-addressable-leds) | Set the colors of addressable LEDs | | [`network.get()`](#send-a-get-or-delete-request-to-a-web-service) | Send a GET or DELETE request to a web service | | [`network.delete()`](#send-a-get-or-delete-request-to-a-web-service) | Send a GET or DELETE request to a web service | | [`network.post()`](#send-a-post-put-or-patch-request-to-a-web-service) | Send a POST, PUT or PATCH request to a web service | @@ -690,6 +696,155 @@ device.uart.unassign_read_event() --- +### Microphone (PDM) + +#### Record audio from a PDM microphone + +```lua +device.pdm.read(samples, { port="PORTA", channel="LEFT", sample_rate=16000, gain=0 }) +``` + +**Parameters** + +- `samples` - **integer** - The number of samples to record from each channel. E.g. `16000` for one second at the default sample rate + +**Optional parameters** + +- `port` - **string** - The 4-pin port the microphone is connected to. Can be `"PORTA"`, `"PORTB"`, `"PORTE"`, or `"PORTF"`. Defaults to `"PORTA"`. The clock is on pin 4 and the data on pin 3, the pins that carry SCL and SDA in the [Stemma QT](https://learn.adafruit.com/introducing-adafruit-stemma-qt/what-is-stemma)/[Qwiic](https://www.sparkfun.com/qwiic) pinout. Cannot be used with `clk_pin` and `data_pin` +- `clk_pin` - **string** - The pin name for the clock. E.g. `"C0"`. Must be specified with `data_pin` +- `data_pin` - **string** - The pin name for the data. E.g. `"C1"`. Must be specified with `clk_pin` +- `channel` - **string** - The channel to record. Can be `"LEFT"`, `"RIGHT"`, or `"STEREO"`. A PDM microphone sends the left or the right channel, which a pin or jumper on the microphone selects. `"STEREO"` records two microphones that share the clock and data pins, one set to each channel. Defaults to `"LEFT"` +- `sample_rate` - **integer** - The samples per second. Can be `12500`, `16000`, or `20000`. Defaults to `16000` +- `gain` - **number** - The gain in dB. From `-20` to `20` in steps of `0.5`. Defaults to `0` + +**Returns** + +- **string** - The samples, each a signed 16-bit little-endian integer. Read one with `string.unpack(" 10 then + table.remove(recent, 1) + end +end) + +-- Later, such as when a button is pressed +local audio = table.concat(recent) +``` + +--- + +#### Disable the microphone read event handler + +```lua +device.pdm.unassign_read_event() +``` + +**Returns** + +- **nil** + +**Example** + +```lua +-- Stop recording +device.pdm.unassign_read_event() +``` + +--- + +### Addressable LEDs (WS2812) + +#### Set the colors of addressable LEDs + +```lua +device.ws2812.write(pin, colors, { order="GRB", frequency=800000 }) +``` + +**Parameters** + +- `pin` - **string** - The pin name connected to the data input of the first LED. E.g. `"B0"` +- `colors` - **table** - One color for each LED, in the order the LEDs are chained. Each color is a table of red, green and blue values, then white when `order` includes `W`, each an integer from `0` to `255`. E.g. `{ { 255, 0, 0 }, { 0, 0, 255 } }` + +**Optional parameters** + +- `order` - **string** - The order in which the LEDs take each color's values, as the letters `R`, `G`, `B`, and optionally `W`, each once. E.g. `"GRB"` for most WS2812 and SK6812 LEDs, or `"GRBW"` for most RGBW LEDs. Check the LEDs' datasheet. Defaults to `"GRB"` +- `frequency` - **integer** - The data rate in Hz. Can be `800000`, or `400000` for LEDs such as the WS2811 in its slow mode. Defaults to `800000` + +**Returns** + +- **nil** - Returns once every color is sent + +**Note:** One write can set up to 1365 LEDs, or 1023 when `order` includes `W`. The LEDs keep their colors until the next write. A write uses one of the four PWM instances while it sends. When [PWM](#output-a-pwm-signal-on-a-pin) uses all four, the pin that started PWM first stops. + +The data swings to the IO voltage of the port, which starts at 1.8V, so call [`device.power.set_vout(3.3)`](#set-the-io-voltage-for-all-ports) first. PORTA to PORTC share 100mA, as do PORTD to PORTF, and a single LED at full brightness can draw tens of milliamps. Power more LEDs than that allows from a separate supply. + +**Example** + +```lua +-- Light a ring of 12 RGBW LEDs dim red +device.power.set_vout(3.3) + +local colors = {} + +for led = 1, 12 do + colors[led] = { 32, 0, 0, 0 } +end + +device.ws2812.write("B0", colors, { order="GRBW" }) +``` + +--- + ## Networking libraries ### Networking (LTE)