Skip to content

Commit bdd3904

Browse files
committed
Add serial module and two simple demo apps
- Add lib/serial/ with modernized Web Serial API wrapper (serial.js) - String-based events (replaces Symbol), connection state, off(), write guards - Robust line splitting (\r\n, \n, \r), improved close() cleanup - Better error messages for common failures (port in use, unsupported browser) - Add rgbToHex() to lib/graphics/color-utils.js - Export hsvToRgb, rgbToHsv, rgbToHex, and other color utils from graphics index - Add apps/serial/SerialTest (basic connect/send/receive test app) - Add apps/serial/SerialColorTest (bidirectional HSV color picker) - Update README with serial module docs
1 parent 29d07d1 commit bdd3904

24 files changed

Lines changed: 3690 additions & 11 deletions

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The library is organized into three main modules:
88

99
- **Math** - Vector operations and mathematical utilities (conversions, interpolation, random numbers)
1010
- **Graphics** - Color manipulation and geometric primitives (line segments, color utilities)
11+
- **Serial** - Web Serial API wrapper for communication with microcontrollers (Arduino, ESP32)
1112
- **Logo** - Interactive Makeability Lab logo components with animation support
1213

1314
Each module can be imported independently or as a complete bundle, allowing for optimized loading based on your needs.
@@ -22,6 +23,7 @@ The easiest way to use this library is via CDN. No installation required - just
2223
// Import specific modules
2324
import { Vector, lerp } from 'https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.math.js';
2425
import { lerpColor } from 'https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.graphics.js';
26+
import { Serial, SerialEvents } from 'https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.serial.js';
2527
import { MakeabilityLabLogo } from 'https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.logo.js';
2628
2729
// Or import everything
@@ -46,12 +48,14 @@ https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/{filename}
4648
// Specific modules
4749
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.math.js
4850
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.graphics.js
51+
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.serial.js
4952
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.logo.js
5053
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.all.js
5154

5255
// Minified versions (recommended for production)
5356
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.math.min.js
5457
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.graphics.min.js
58+
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.serial.min.js
5559
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.logo.min.js
5660
https://cdn.jsdelivr.net/gh/makeabilitylab/js@main/dist/makelab.all.min.js
5761
```
@@ -130,6 +134,7 @@ js/
130134
│ ├── lib/
131135
│ │ ├── math/ # Math utilities and Vector class
132136
│ │ ├── graphics/ # Graphics utilities (colors, line segments)
137+
│ │ ├── serial/ # Web Serial API wrapper
133138
│ │ └── logo/ # Makeability Lab logo components
134139
│ └── apps/ # Example applications
135140
├── dist/ # Built output files
@@ -150,6 +155,18 @@ js/
150155
- `LineSegment` - Line segment operations and intersections
151156
- `lerpColor()` - Interpolate between colors
152157
- `convertColorStringToObject()` - Parse color strings to RGB objects
158+
- `hsvToRgb()` - Convert HSV to RGB
159+
- `rgbToHsv()` - Convert RGB to HSV
160+
- `rgbToHex()` - Convert RGB to hex string
161+
- `hexStringToRgb()` - Convert hex string to RGB
162+
- `changeColorBrightness()` - Adjust color brightness
163+
- `changeColorSaturationAndBrightness()` - Adjust color saturation and brightness
164+
165+
#### Serial Module
166+
- `Serial` - Event-driven Web Serial API wrapper for text-based communication
167+
- `SerialEvents` - Event type constants (`CONNECTION_OPENED`, `CONNECTION_CLOSED`, `DATA_RECEIVED`, `ERROR_OCCURRED`)
168+
- `SerialState` - Connection state constants (`CLOSED`, `OPENING`, `OPEN`, `CLOSING`)
169+
- `LineBreakTransformer` - Stream transformer for line-delimited serial data
153170

154171
#### Logo Module
155172
- `MakeabilityLabLogo` - Main logo component
@@ -173,6 +190,7 @@ This command generates the following files in the `dist/` directory:
173190
|--------|----------|----------|
174191
| Math | `makelab.math.js` | `makelab.math.min.js` |
175192
| Graphics | `makelab.graphics.js` | `makelab.graphics.min.js` |
193+
| Serial | `makelab.serial.js` | `makelab.serial.min.js` |
176194
| Logo | `makelab.logo.js` | `makelab.logo.min.js` |
177195
| All (Complete) | `makelab.all.js` | `makelab.all.min.js` |
178196

@@ -397,6 +415,38 @@ line.getIntersection(otherLine) // Get intersection point
397415
```javascript
398416
lerpColor(color1, color2, amount) // Blend two colors
399417
convertColorStringToObject(colorString) // Parse color to RGB
418+
hsvToRgb(h, s, v) // HSV (0–1) to RGB object
419+
rgbToHsv(r, g, b) // RGB (0–255) to HSV object
420+
rgbToHex(r, g, b) // RGB to hex string
421+
hexStringToRgb(hex) // Hex string to RGB object
422+
changeColorBrightness(color, percent) // Adjust brightness
423+
```
424+
425+
### Serial Module
426+
427+
```javascript
428+
const serial = new Serial()
429+
430+
// Events
431+
serial.on(SerialEvents.DATA_RECEIVED, (sender, line) => { ... })
432+
serial.on(SerialEvents.CONNECTION_OPENED, (sender) => { ... })
433+
serial.on(SerialEvents.CONNECTION_CLOSED, (sender) => { ... })
434+
serial.on(SerialEvents.ERROR_OCCURRED, (sender, error) => { ... })
435+
serial.off(event, callback) // Remove a listener
436+
437+
// Connection
438+
serial.connectAndOpen(filters, options) // Prompt user + open (requires user gesture)
439+
serial.autoConnectAndOpenPreviouslyApprovedPort(options) // Reconnect without gesture
440+
serial.close() // Close and release resources
441+
442+
// I/O
443+
serial.writeLine(text) // Send text + newline
444+
serial.write(text) // Send text
445+
446+
// State
447+
serial.isOpen() // Boolean
448+
serial.state // "closed" | "opening" | "open" | "closing"
449+
Serial.isWebSerialSupported() // Static browser check
400450
```
401451

402452
### Logo Module
@@ -431,4 +481,4 @@ For questions, issues, or contributions, please visit the project repository.
431481

432482
---
433483

434-
Built with Rollup | Powered by JavaScript ES6+ Modules
484+
Built with Rollup | Powered by JavaScript ES6+ Modules

0 commit comments

Comments
 (0)