Skip to content

Commit

Permalink
adding new hid keyboard sample
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Mar 31, 2023
1 parent 885e08e commit 9fc249e
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/sampleprj/src/consoledata.ts
@@ -0,0 +1,6 @@
setInterval(() => {
console.data({
temp: 20 + Math.random() / 10,
humi: 80 + Math.random() / 100,
})
}, 500)
42 changes: 42 additions & 0 deletions packages/sampleprj/src/copypastebutton.ts
@@ -0,0 +1,42 @@
import { pins, board } from "@dsboard/pico"
import {
startButton,
startHidKeyboard,
startLightBulb,
} from "@devicescript/servers"
import {
HidKeyboardAction,
HidKeyboardModifiers,
HidKeyboardSelector,
} from "@devicescript/core"

// the keyboard button mounted on GP14
const button = startButton({
pin: pins.P14,
})
// a status indicator led mounted on GP1
const led = startLightBulb({
pin: pins.P1,
})
// the HID keyboard driver that will send keystrokes
const keyboard = startHidKeyboard({})

// true: ctrl+c, false: ctrl+v
let copy = true
// use leftgui on mac or leftcontrol on windows
let modifier = HidKeyboardModifiers.LeftGUI

// copy and paste on button click
button.down.subscribe(async () => {
// when copy is true, send ctrl+c
const selector = copy ? HidKeyboardSelector.C : HidKeyboardSelector.V
// when copy is true, turn on the led to represent a "full clipboard"
const brightness = copy ? 1 : 0

// a bit of logging
console.log(copy ? "ctrl+c" : "ctrl+v")
await keyboard.key(selector, modifier, HidKeyboardAction.Press)
await led.brightness.write(brightness)
// toggle for next round
copy = !copy
})
2 changes: 1 addition & 1 deletion website/docs/getting-started/cli.mdx
@@ -1,5 +1,5 @@
---
sidebar_position: 20
sidebar_position: 2
---

# Command Line
Expand Down
4 changes: 4 additions & 0 deletions website/docs/getting-started/index.mdx
Expand Up @@ -18,3 +18,7 @@ tracing, device monitoring and other developer productivity features.
The command line is IDE agnostic and will let you script your own developer experience.

- [Getting started with command line](/getting-started/cli)

## Samples

Once you are ready with your setup, you can start with the samples.
4 changes: 4 additions & 0 deletions website/docs/getting-started/samples/_category_.json
@@ -0,0 +1,4 @@
{
"label": "Samples",
"position": 3
}
4 changes: 4 additions & 0 deletions website/docs/getting-started/samples/rp2040/_category_.json
@@ -0,0 +1,4 @@
{
"label": "RP2040",
"position": 3
}
8 changes: 5 additions & 3 deletions website/docs/getting-started/samples/rp2040/blinky.mdx
@@ -1,6 +1,8 @@
# Blinky

The classic LED blinking program on a Raspberry Pi Pico. The LED is connected to the Pico's pin GP1 (2 on the silk)
The classic LED blinking program on a [Raspberry Pi Pico](/devices/rp2040). The LED is connected to the Pico's pin GP1 (2 on the silk)

- **[4 minute blinky demo](/devicescript/videos/blinky.mp4)**

```ts
import { pins } from "@dsboard/pico"
Expand All @@ -15,10 +17,10 @@ const led = startLightBulb({
// start interval timer every 1000ms
setInterval(async () => {
// read current brightness
const brightness = await led.brigthness.read()
const brightness = await led.brightness.read()
// toggle on/off
const newbrightness = brightness > 0 ? 0 : 1
// apply new brightness
await led.brigthness.write(newbrightness)
await led.brightness.write(newbrightness)
}, 1000)
```
40 changes: 32 additions & 8 deletions website/docs/getting-started/samples/rp2040/copy-paste-button.mdx
Expand Up @@ -4,8 +4,20 @@ hide_table_of_contents: true

# Copy Paste Button

In this example, we use a single button to create a `copy-paste` micro-keyboard.

- The button is connected to the Pico's GP14 pin. When the button is pressed, the Pico will send a `ctrl+c` or `ctrl+v` keystroke to the computer using a [HID keyboard](/api/servers/hidkeyboard) server.
The `ctrl+c` keystroke will copy the selected text, and the `ctrl+v` keystroke will paste the copied text.
- The status of the clipboard is indicated by a status LED connected to the Pico's GP1 pin. When the LED is on, the clipboard is full, and when the LED is off, the clipboard is empty.

:::note

On MacOS, we use `LeftGUI`. To update for Windows, replace `LeftGuid` with `LeftControl`.

:::

```ts
import { pins } from "@dsboard/pico"
import { pins, board } from "@dsboard/pico"
import {
startButton,
startHidKeyboard,
Expand All @@ -17,24 +29,36 @@ import {
HidKeyboardSelector,
} from "@devicescript/core"

// the keyboard button mounted on GP14
const button = startButton({
pin: pins.P14,
})
// a status indicator led mounted on GP1
const led = startLightBulb({
pin: pins.P1,
})
// the HID keyboard driver that will send keystrokes
const keyboard = startHidKeyboard({})

// true: ctrl+c, false: ctrl+v
let copy = true
// use leftgui on mac or leftcontrol on windows
let modifier = HidKeyboardModifiers.LeftGUI
// uncomment for windows
// let modifier = HidKeyboardModifiers.LeftControl

// copy and paste on button click
button.down.subscribe(async () => {
// when copy is true, send ctrl+c
const selector = copy ? HidKeyboardSelector.C : HidKeyboardSelector.V
// when copy is true, turn on the led to represent a "full clipboard"
const brightness = copy ? 1 : 0
console.log(copy ? 'ctrl+c' : 'ctrl+v')
await keyboard.key(
selector,
HidKeyboardModifiers.LeftControl,
HidKeyboardAction.Press
)

// a bit of logging
console.log(copy ? "ctrl+c" : "ctrl+v")
await keyboard.key(selector, modifier, HidKeyboardAction.Press)
await led.brightness.write(brightness)
// toggle for next round
copy = !copy
})
```
```
2 changes: 1 addition & 1 deletion website/docs/getting-started/vscode/_category_.json
@@ -1,4 +1,4 @@
{
"label": "Visual Studio Code Extension",
"position": 19
"position": 1
}

0 comments on commit 9fc249e

Please sign in to comment.