Skip to content

Commit 4dde9b0

Browse files
Merge pull request #104 from maejima-fumika/feature/add-gpio-to-website
Add guides and library reference to the website.
2 parents 4ab4855 + 0f8ee66 commit 4dde9b0

File tree

24 files changed

+1057
-51
lines changed

24 files changed

+1057
-51
lines changed

cli/src/commands/project/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as fs from '../../core/fs';
1010
import { CommandHandler } from "../command";
1111

1212

13-
const MAIN_FILE_CONTENTS = `print('Hello world!')\n`;
13+
const MAIN_FILE_CONTENTS = `console.log('Hello world!')\n`;
1414
const GIT_IGNORE_CONTENTS = `**/dist/\n`;
1515

1616
class CreateHandler extends CommandHandler {

lang/src/compiler/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class ESPIDFComponents {
353353
public readonly commonArchiveFilePaths: string[];
354354

355355
private readonly COMPONENTS_PATH_PREFIX = /^.*microcontroller/;
356-
private readonly COMMON_COMPONENTS = ["cxx", "newlib", "freertos", "esp_hw_support", "heap", "log", "soc", "hal", "esp_rom", "esp_common", "esp_system"];
356+
private readonly COMMON_COMPONENTS = ["cxx", "newlib", "freertos", "esp_hw_support", "heap", "log", "soc", "hal", "esp_rom", "esp_common", "esp_system", "xtensa"];
357357
private readonly RUNTIME_DIR: string;
358358
private readonly SDK_CONFIG_DIR: string;
359359

website/docs/reference/libraries/gpio.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Standard Libraries
2+
3+
BlueScript follows a "Battery-included but removable" philosophy.
4+
Core features are kept minimal, while hardware drivers are provided as external standard libraries hosted on GitHub.
5+
6+
To install any of these libraries, use the command:
7+
`bscript project install <git-url>`
8+
9+
## Available Libraries
10+
11+
Currently, the following libraries are available for stable use.
12+
13+
### Digital I/O
14+
* **GPIO (General Purpose Input/Output)**
15+
* Control pins, read digital states, and handle interrupts.
16+
* **Repository:** [https://github.com/bluescript-lang/pkg-gpio-esp32.git](https://github.com/bluescript-lang/pkg-gpio-esp32.git)
17+
* **Usage:** `import { GPIO } from "gpio";`
18+
19+
---
20+
21+
## Roadmap (Planned Libraries)
22+
23+
We are actively developing drivers for the following peripherals.
24+
Support for these features will be rolled out in upcoming updates.
25+
26+
| Category | Library | Status | Description |
27+
| :--- | :--- | :--- | :--- |
28+
| **Analog** | **ADC** | 🚧 Planned | Read analog sensor values. |
29+
| **Analog** | **DAC** | 🚧 Planned | Output analog voltage signals. |
30+
| **Control** | **PWM** | 🚧 Planned | Pulse Width Modulation for LEDs and Servos. |
31+
| **Comms** | **UART** | 🚧 Planned | Serial communication with other devices. |
32+
| **Comms** | **I2C** | 🚧 Planned | Interface with sensors and displays (Two-wire). |
33+
| **Comms** | **SPI** | 🚧 Planned | High-speed serial communication. |
34+
| **Audio** | **I2S** | 🚧 Planned | Digital audio data transfer. |
35+
| **Wireless** | **WiFi** | 🚧 Planned | Connect to the internet, make HTTP requests. |
36+
| **Wireless** | **Bluetooth** | 🚧 Planned | BLE communication (Central/Peripheral). |
37+
38+
<!-- :::note Contribute?
39+
BlueScript is an open ecosystem. If you have wrapped an ESP-IDF component into a BlueScript package, feel free to share it with the community!
40+
::: -->

website/docs/tutorial/examples/blink-led.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Blink LED
2+
3+
Now that you have verified your environment, let's move on to the "Real" Hello World of embedded systems: **Blinking an LED**.
4+
5+
In this tutorial, you will learn:
6+
1. How to install external packages.
7+
2. How to wire an LED to the ESP32.
8+
3. How to write code to control GPIO pins.
9+
10+
## Step 1: Hardware Setup
11+
12+
Depending on your hardware, choose one of the following setups.
13+
14+
### Option A: Using the Onboard LED
15+
Most ESP32 development boards (like the ESP32-DevKitC) have a small blue LED built into the board.
16+
* **Pin:** Typically **GPIO 2**.
17+
* **Wiring:** No external wiring required.
18+
19+
### Option B: Using an External LED
20+
If your board does not have an onboard LED, or if you prefer to build a circuit, connect an LED to **GPIO 23**.
21+
22+
**Required Parts:**
23+
* 1x LED
24+
* 1x Resistor (220Ω - 1kΩ)
25+
* Breadboard and Jumper wires
26+
27+
:::tip Polarity Check
28+
Make sure to connect the **longer leg** (Anode) of the LED towards the GPIO pin (through the resistor), and the **shorter leg** (Cathode) to GND.
29+
:::
30+
31+
## Step 2: Install GPIO Package
32+
33+
BlueScript keeps the core runtime small. To use hardware features like GPIO, we need to install the driver package.
34+
35+
Run the following command in your project directory:
36+
37+
```bash
38+
bscript project install https://github.com/bluescript-lang/pkg-gpio-esp32.git
39+
```
40+
41+
## Step 3: Write Code
42+
43+
Open `index.bs` and replace its content with the code below.
44+
45+
This program will blink the LED 10 times with a 1-second interval.
46+
47+
:::warning Select your Pin
48+
The code below uses **GPIO 2** (Onboard LED).
49+
If you are using the **External LED**, change `2` to `23` in the `new GPIO(...)` line.
50+
:::
51+
52+
```typescript title="index.bs"
53+
// Import GPIO class and Enums from the installed package
54+
import { GPIO, PinMode, PinLevel } from "gpio";
55+
56+
// Initialize GPIO 2 as Input/Output mode
57+
// CHANGE THIS TO '23' IF USING EXTERNAL LED
58+
const led = new GPIO(2, PinMode.InputOutput);
59+
60+
console.log("Starting Blink Loop...");
61+
62+
// Blink the LED 10 times
63+
for (let i = 0; i < 10; i++) {
64+
// Turn LED ON
65+
led.write(PinLevel.High);
66+
time.delay(1000); // Wait for 1000ms (1 second)
67+
68+
// Turn LED OFF
69+
led.write(PinLevel.Low);
70+
time.delay(1000);
71+
}
72+
73+
led.close();
74+
console.log("Finished!");
75+
```
76+
77+
## Step 4: Run
78+
79+
Make sure your device is powered on and run:
80+
81+
```bash
82+
bscript project run
83+
```
84+
85+
You should see the LED turn on and off every second!

website/docs/tutorial/get-started/create-project-and-run.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ bscript project run
4949
Please note that programs uploaded via `bscript project run` are **not persisted** after a reboot.
5050

5151
If you restart or power off the device, the program will be lost. You will need to execute `bscript project run` again to re-upload your code.
52+
53+
**Future Roadmap:**
54+
We are planning to introduce a **`bscript project deploy`** command. This command will permanently install your application, allowing it to start automatically when the device powers on.
5255
:::
5356

5457
:::tip Try the REPL
@@ -58,10 +61,3 @@ Run **`bscript repl`** in your terminal. You can type commands like `console.log
5861
:::
5962

6063
---
61-
62-
## Next Steps
63-
64-
Now that you have the basics down, explore what else BlueScript can do:
65-
66-
* **[Language Reference](../../reference/language/intro)**: Learn about supported syntax, Inline C, and hardware APIs.
67-
* **[CLI Reference](../../reference/cli)**: Discover advanced commands.

website/docs/tutorial/get-started/setup-environment.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Set up your environment
22

3+
:::danger macOS Only
4+
Currently, BlueScript strictly requires **macOS**. Windows and Linux support is under development.
5+
:::
6+
37
In this guide, we will install the BlueScript CLI and flash the runtime environment to your microcontroller.
48

59
Currently, only **ESP32 development boards** are supported.
@@ -12,9 +16,8 @@ Before we begin, ensure you have the following:
1216
- **Host PC:** A laptop running **macOS** (Windows and Linux are currently **not** supported).
1317
- **Micocontroller:** An ESP32 development board (e.g., ESP32-DevKitC)
1418
- **USB cable** to connect your host PC and the microcontroller
15-
- **Software**
19+
- **Software:**
1620
- [Node.js](https://nodejs.org/) (v20 or later) installed on your host PC.
17-
1821
---
1922

2023
## Step 1: Install the CLI
@@ -58,12 +61,12 @@ Connect your ESP32 to your computer via USB and flash the runtime:
5861
bscript board flash-runtime esp32
5962
```
6063

61-
If the flash is successful, your device is now ready to receive BlueScript code wirelessly!
62-
63-
---
64+
The CLI will display a list of detected serial ports. Use the arrow keys to select the one corresponding to your ESP32 (e.g., /dev/tty.usbserial-xxxx).
6465

65-
## Next Steps
66+
:::info Device not found?
67+
If your device does not appear in the list, you may need to install USB-to-UART drivers (e.g., [CP210x](https://www.silabs.com/software-and-tools/usb-to-uart-bridge-vcp-drivers) or [FTDI](https://ftdichip.com/drivers/vcp-drivers/)).
6668

67-
Now that your device is set up, let's create your first project.
69+
See also [Establish Serial Connection with ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/get-started/establish-serial-connection.html).
70+
:::
6871

69-
👉 **[Go to: Create project and Run](./create-project-and-run)**
72+
If the flash is successful, your device is now ready to receive BlueScript code wirelessly!
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Imports & Includes
2+
3+
As your project grows, you will want to split your code into multiple files.
4+
BlueScript handles dependencies differently depending on whether you are loading **BlueScript Modules** or raw **C Source Files**.
5+
6+
## Importing BlueScript Modules
7+
8+
You can create reusable BlueScript code (`.bs` files) and import them into other files.
9+
10+
### 1. Local Modules
11+
To import a module from your own project, use the **relative path** (starting with `./` or `../`).
12+
13+
**`math-utils.bs`** (The library)
14+
```typescript
15+
// Named export
16+
export function add(a: integer, b: integer): integer {
17+
return a + b;
18+
}
19+
20+
// ❌ Default export is NOT supported
21+
// export default function ...
22+
```
23+
24+
**`index.bs`** (The importer)
25+
```typescript
26+
// Import using relative path
27+
import { add } from "./math-utils";
28+
29+
console.log(add(10, 20));
30+
```
31+
32+
:::warning No Default Exports
33+
BlueScript currently supports only **Named Exports**.
34+
You must use `import { name }` syntax. `import name from ...` will not work.
35+
:::
36+
37+
### 2. Package Modules
38+
When you install an external library (like a driver), you import it by its **Package Name**, not a file path.
39+
40+
```typescript
41+
// Import from an installed package
42+
// The compiler resolves "gpio" from your project config
43+
import { GPIO } from "gpio";
44+
```
45+
46+
## Including C Files
47+
48+
If you have standalone C source files (`.c`) in your project, you can include them using **Inline C**.
49+
50+
Unlike standard C compilers, BlueScript's `code` block treats `#include` paths relative to the current file when using quotes.
51+
52+
**`native-lib.c`**
53+
```c
54+
// A pure C function
55+
int native_multiply(int a, int b) {
56+
return a * b;
57+
}
58+
```
59+
60+
**`index.bs`**
61+
```typescript
62+
// Include the local C file
63+
code`#include "./native-lib.c"`
64+
65+
export function multiply(a: integer, b: integer): integer {
66+
let result = 0;
67+
// Call the function defined in the included C file
68+
code`${result} = native_multiply(${a}, ${b});`
69+
return result;
70+
}
71+
72+
console.log(multiply(3, 4));
73+
```
74+
75+
### Summary table
76+
77+
| Source Type | Syntax | Path Style | Example |
78+
| :--- | :--- | :--- | :--- |
79+
| **Local BS Module** | `import { ... }` | Relative | `"./utils"` |
80+
| **Package BS Module** | `import { ... }` | Package Name | `"gpio"` |
81+
| **Local C File** | `code`\`#include ...\` | Relative | `"./driver.c"` |

0 commit comments

Comments
 (0)