Skip to content

Commit c65252a

Browse files
committed
Add serial components
1 parent 1701168 commit c65252a

File tree

6 files changed

+166
-5
lines changed

6 files changed

+166
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Web Components library for micro:bit
3636
- [x] Read/Write LED Matrix
3737

3838
# Serial
39-
- [ ] Send Message
40-
- [ ] Receive Message
39+
- [x] Send Message
40+
- [x] Receive Message
4141

4242
### Events
4343
- [ ] Send Event

src/components.d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,36 @@ export namespace Components {
287287
'setServices'?: (services: Services) => void;
288288
}
289289

290+
interface MicrobitReceive {
291+
'services': Services;
292+
}
293+
interface MicrobitReceiveAttributes extends StencilHTMLAttributes {
294+
'services'?: Services;
295+
}
296+
297+
interface MicrobitSend {
298+
/**
299+
* The text shown on the button
300+
*/
301+
'buttonLabel': string;
302+
/**
303+
* The delimiter to use
304+
*/
305+
'delimiter': string;
306+
'services': Services;
307+
}
308+
interface MicrobitSendAttributes extends StencilHTMLAttributes {
309+
/**
310+
* The text shown on the button
311+
*/
312+
'buttonLabel'?: string;
313+
/**
314+
* The delimiter to use
315+
*/
316+
'delimiter'?: string;
317+
'services'?: Services;
318+
}
319+
290320
interface MicrobitStateButtonA {
291321
/**
292322
* The css class to use when long-pressed
@@ -427,6 +457,8 @@ declare global {
427457
'MicrobitApp': Components.MicrobitApp;
428458
'MicrobitCalibrate': Components.MicrobitCalibrate;
429459
'MicrobitConnect': Components.MicrobitConnect;
460+
'MicrobitReceive': Components.MicrobitReceive;
461+
'MicrobitSend': Components.MicrobitSend;
430462
'MicrobitStateButtonA': Components.MicrobitStateButtonA;
431463
'MicrobitStateButtonB': Components.MicrobitStateButtonB;
432464
'MicrobitStateConnection': Components.MicrobitStateConnection;
@@ -447,6 +479,8 @@ declare global {
447479
'microbit-app': Components.MicrobitAppAttributes;
448480
'microbit-calibrate': Components.MicrobitCalibrateAttributes;
449481
'microbit-connect': Components.MicrobitConnectAttributes;
482+
'microbit-receive': Components.MicrobitReceiveAttributes;
483+
'microbit-send': Components.MicrobitSendAttributes;
450484
'microbit-state-button-a': Components.MicrobitStateButtonAAttributes;
451485
'microbit-state-button-b': Components.MicrobitStateButtonBAttributes;
452486
'microbit-state-connection': Components.MicrobitStateConnectionAttributes;
@@ -532,6 +566,18 @@ declare global {
532566
new (): HTMLMicrobitConnectElement;
533567
};
534568

569+
interface HTMLMicrobitReceiveElement extends Components.MicrobitReceive, HTMLStencilElement {}
570+
var HTMLMicrobitReceiveElement: {
571+
prototype: HTMLMicrobitReceiveElement;
572+
new (): HTMLMicrobitReceiveElement;
573+
};
574+
575+
interface HTMLMicrobitSendElement extends Components.MicrobitSend, HTMLStencilElement {}
576+
var HTMLMicrobitSendElement: {
577+
prototype: HTMLMicrobitSendElement;
578+
new (): HTMLMicrobitSendElement;
579+
};
580+
535581
interface HTMLMicrobitStateButtonAElement extends Components.MicrobitStateButtonA, HTMLStencilElement {}
536582
var HTMLMicrobitStateButtonAElement: {
537583
prototype: HTMLMicrobitStateButtonAElement;
@@ -570,6 +616,8 @@ declare global {
570616
'microbit-app': HTMLMicrobitAppElement
571617
'microbit-calibrate': HTMLMicrobitCalibrateElement
572618
'microbit-connect': HTMLMicrobitConnectElement
619+
'microbit-receive': HTMLMicrobitReceiveElement
620+
'microbit-send': HTMLMicrobitSendElement
573621
'microbit-state-button-a': HTMLMicrobitStateButtonAElement
574622
'microbit-state-button-b': HTMLMicrobitStateButtonBElement
575623
'microbit-state-connection': HTMLMicrobitStateConnectionElement
@@ -590,6 +638,8 @@ declare global {
590638
'microbit-app': HTMLMicrobitAppElement;
591639
'microbit-calibrate': HTMLMicrobitCalibrateElement;
592640
'microbit-connect': HTMLMicrobitConnectElement;
641+
'microbit-receive': HTMLMicrobitReceiveElement;
642+
'microbit-send': HTMLMicrobitSendElement;
593643
'microbit-state-button-a': HTMLMicrobitStateButtonAElement;
594644
'microbit-state-button-b': HTMLMicrobitStateButtonBElement;
595645
'microbit-state-connection': HTMLMicrobitStateConnectionElement;

src/components/led/microbit-text.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export class MicrobitText {
4949
type="input"
5050
disabled={this.disabled}
5151
maxLength={20}
52-
onKeyPress={e => this.handleKeyPress(e)}></input>
52+
onKeyUp={e => this.handleKey(e)}></input>
5353
{button}
5454
</span>
5555
);
5656
}
5757

58-
private handleKeyPress(event) {
58+
private handleKey(event) {
5959
if (event.keyCode == 13) {
6060
this.writeText();
6161
} else {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Component, Prop, Element, State, Watch } from "@stencil/core";
2+
import { Services } from "microbit-web-bluetooth";
3+
import DeviceTunnel from '../../device-tunnel';
4+
5+
@Component({
6+
tag: 'microbit-receive'
7+
})
8+
export class MicrobitReceive {
9+
@Element() el;
10+
@Prop() services: Services = undefined;
11+
12+
@State() disabled = true;
13+
@State() data: string = "";
14+
15+
@Watch('services')
16+
async watchHandler() {
17+
this.disabled = !this.services || !this.services.uartService;
18+
19+
const service = this.services.uartService;
20+
21+
if (!service) {
22+
this.data = "";
23+
return;
24+
}
25+
26+
await service.addEventListener("receiveString", event => this.data += event.detail);
27+
}
28+
29+
render() {
30+
const style = {
31+
whiteSpace: 'pre'
32+
};
33+
return (
34+
<span style={style}>
35+
{this.data}
36+
</span>
37+
);
38+
}
39+
}
40+
41+
DeviceTunnel.injectProps(MicrobitReceive, ['services']);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Component, Prop, Element, State, Watch } from "@stencil/core";
2+
import { Services } from "microbit-web-bluetooth";
3+
import DeviceTunnel from '../../device-tunnel';
4+
5+
@Component({
6+
tag: 'microbit-send'
7+
})
8+
export class MicrobitSend {
9+
@Element() el;
10+
@Prop() services: Services = undefined;
11+
12+
/**
13+
* The text shown on the button
14+
*/
15+
@Prop() buttonLabel: string = "";
16+
17+
/**
18+
* The delimiter to use
19+
*/
20+
@Prop() delimiter: string = "";
21+
22+
@State() disabled = true;
23+
24+
private text = "";
25+
26+
@Watch('services')
27+
async watchHandler() {
28+
this.disabled = !this.services || !this.services.uartService;
29+
}
30+
31+
render() {
32+
let button: JSX.Element;
33+
34+
if (this.buttonLabel) {
35+
button = <input
36+
type="submit"
37+
disabled={this.disabled}
38+
value={this.buttonLabel}
39+
onClick={() => this.sendText()}></input>;
40+
}
41+
42+
return (
43+
<span>
44+
<input
45+
type="input"
46+
disabled={this.disabled}
47+
maxLength={20}
48+
onKeyUp={e => this.handleKey(e)}></input>
49+
{button}
50+
</span>
51+
);
52+
}
53+
54+
private handleKey(event) {
55+
if (event.keyCode == 13) {
56+
this.sendText();
57+
} else {
58+
this.text = event.target.value;
59+
}
60+
}
61+
62+
private async sendText() {
63+
const text = `${this.text}${this.delimiter}`;
64+
await this.services.uartService.sendString(text);
65+
}
66+
}
67+
68+
DeviceTunnel.injectProps(MicrobitSend, ['services']);

src/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
<microbit-firmware></microbit-firmware>
6969
<microbit-hardware></microbit-hardware>
7070
<microbit-temperature></microbit-temperature>
71-
<microbit-text button-label="Write"></microbit-text>
7271
<microbit-state-connection>
7372
<span class="state">Connected</span>
7473
</microbit-state-connection>
@@ -108,6 +107,9 @@
108107
<div id="matrix-4-3" class="microbit-matrix-led"></div>
109108
<div id="matrix-4-4" class="microbit-matrix-led"></div>
110109
</microbit-matrix>
110+
<microbit-text button-label="Write"></microbit-text>
111+
<microbit-send delimiter=":" button-label="Send"></microbit-send>
112+
<microbit-receive></microbit-receive>
111113
<microbit-calibrate></microbit-calibrate>
112114
<microbit-compass>
113115
<div id="compass"></div>

0 commit comments

Comments
 (0)