Skip to content

Commit bd51974

Browse files
committed
feat: helper functions for spi/i2c/usart signals
1 parent 6ce606f commit bd51974

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

src/arduino-uno-element.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { customElement, html, LitElement, property, svg } from 'lit-element';
22
import { pinsFemalePattern } from './patterns/pins-female';
3-
import { analog, ElementPin } from './pin';
3+
import { analog, ElementPin, i2c, spi, usart } from './pin';
44

55
@customElement('wokwi-arduino-uno')
66
export class ArduinoUnoElement extends LitElement {
@@ -10,14 +10,14 @@ export class ArduinoUnoElement extends LitElement {
1010
@property() ledPower = false;
1111

1212
readonly pinInfo: ElementPin[] = [
13-
{ name: 'A5.2', x: 87, y: 9, signals: [analog(5), { type: 'i2c', signal: 'SCL' }] },
14-
{ name: 'A4.2', x: 97, y: 9, signals: [analog(4), { type: 'i2c', signal: 'SDA' }] },
13+
{ name: 'A5.2', x: 87, y: 9, signals: [analog(5), i2c('SCL')] },
14+
{ name: 'A4.2', x: 97, y: 9, signals: [analog(4), i2c('SDA')] },
1515
{ name: 'AREF', x: 106, y: 9, signals: [] },
1616
{ name: 'GND.1', x: 115.5, y: 9, signals: [{ type: 'power', signal: 'GND' }] },
17-
{ name: '13', x: 125, y: 9, signals: [{ type: 'spi', signal: 'SCK' }] },
18-
{ name: '12', x: 134.5, y: 9, signals: [{ type: 'spi', signal: 'MISO' }] },
19-
{ name: '11', x: 144, y: 9, signals: [{ type: 'spi', signal: 'MOSI' }, { type: 'pwm' }] },
20-
{ name: '10', x: 153.5, y: 9, signals: [{ type: 'spi', signal: 'SS' }, { type: 'pwm' }] },
17+
{ name: '13', x: 125, y: 9, signals: [spi('SCK')] },
18+
{ name: '12', x: 134.5, y: 9, signals: [spi('MISO')] },
19+
{ name: '11', x: 144, y: 9, signals: [spi('MOSI'), { type: 'pwm' }] },
20+
{ name: '10', x: 153.5, y: 9, signals: [spi('SS'), { type: 'pwm' }] },
2121
{ name: '9', x: 163, y: 9, signals: [{ type: 'pwm' }] },
2222
{ name: '8', x: 173, y: 9, signals: [] },
2323
{ name: '7', x: 189, y: 9, signals: [] },
@@ -26,8 +26,8 @@ export class ArduinoUnoElement extends LitElement {
2626
{ name: '4', x: 217.5, y: 9, signals: [] },
2727
{ name: '3', x: 227, y: 9, signals: [{ type: 'pwm' }] },
2828
{ name: '2', x: 236.5, y: 9, signals: [] },
29-
{ name: '1', x: 246, y: 9, signals: [{ type: 'usart', signal: 'TX' }] },
30-
{ name: '0', x: 255.5, y: 9, signals: [{ type: 'usart', signal: 'RX' }] },
29+
{ name: '1', x: 246, y: 9, signals: [usart('TX')] },
30+
{ name: '0', x: 255.5, y: 9, signals: [usart('RX')] },
3131
{ name: 'IOREF', x: 131, y: 191.5, signals: [] },
3232
{ name: 'RESET', x: 140.5, y: 191.5, signals: [] },
3333
{ name: '3.3V', x: 150, y: 191.5, signals: [{ type: 'power', signal: 'VCC', voltage: 3.3 }] },
@@ -39,8 +39,8 @@ export class ArduinoUnoElement extends LitElement {
3939
{ name: 'A1', x: 217.5, y: 191.5, signals: [analog(1)] },
4040
{ name: 'A2', x: 227, y: 191.5, signals: [analog(2)] },
4141
{ name: 'A3', x: 236.5, y: 191.5, signals: [analog(3)] },
42-
{ name: 'A4', x: 246, y: 191.5, signals: [analog(4), { type: 'i2c', signal: 'SCL' }] },
43-
{ name: 'A5', x: 255.5, y: 191.5, signals: [analog(5), { type: 'i2c', signal: 'SDA' }] },
42+
{ name: 'A4', x: 246, y: 191.5, signals: [analog(4), i2c('SCL')] },
43+
{ name: 'A5', x: 255.5, y: 191.5, signals: [analog(5), i2c('SDA')] },
4444
];
4545

4646
render() {

src/pin.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ export type PinSignalInfo =
22
| {
33
type: 'i2c';
44
signal: 'SDA' | 'SCL';
5-
bus?: number;
5+
bus: number;
66
}
77
| {
88
type: 'spi';
99
signal: 'SCK' | 'MOSI' | 'MISO' | 'SS';
10-
bus?: number;
10+
bus: number;
1111
}
1212
| {
1313
type: 'usart';
1414
signal: 'RX' | 'TX';
15-
bus?: number;
15+
bus: number;
1616
}
1717
| {
1818
type: 'power';
@@ -50,5 +50,23 @@ export interface ElementPin {
5050
description?: string;
5151
}
5252

53-
/** Helper function for creating analog PinSignalInfo objects */
54-
export const analog = (channel: number) => ({ type: 'analog', channel } as PinSignalInfo);
53+
/** Helper function for creating PinSignalInfo objects */
54+
export const analog = (channel: number): PinSignalInfo => ({ type: 'analog', channel });
55+
56+
export const i2c = (signal: 'SCL' | 'SDA', bus = 0): PinSignalInfo => ({
57+
type: 'i2c',
58+
signal,
59+
bus,
60+
});
61+
62+
export const spi = (signal: 'SCK' | 'MOSI' | 'MISO' | 'SS', bus = 0): PinSignalInfo => ({
63+
type: 'spi',
64+
signal,
65+
bus,
66+
});
67+
68+
export const usart = (signal: 'RX' | 'TX', bus = 0): PinSignalInfo => ({
69+
type: 'usart',
70+
signal,
71+
bus,
72+
});

src/pushbutton-element.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css, customElement, html, LitElement, property } from 'lit-element';
2+
import { ElementPin } from './pin';
23

34
const SPACE_KEY = 32;
45

@@ -7,6 +8,13 @@ export class PushbuttonElement extends LitElement {
78
@property() color = 'red';
89
@property() pressed = false;
910

11+
readonly pinInfo: ElementPin[] = [
12+
{ name: '1.l', x: 2, y: 9, signals: [] },
13+
{ name: '2.l', x: 2, y: 36, signals: [] },
14+
{ name: '1.r', x: 65, y: 36, signals: [] },
15+
{ name: '2.r', x: 65, y: 9, signals: [] },
16+
];
17+
1018
static get styles() {
1119
return css`
1220
button {

src/ssd1306-element.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Reference: https://cdn-learn.adafruit.com/assets/assets/000/036/494/original/lcds___displays_fabprint.png?1476374574
22
import { customElement, html, LitElement, property, SVGTemplateResult } from 'lit-element';
3+
import { ElementPin, i2c } from './pin';
34

45
type CanvasContext = CanvasRenderingContext2D | null | undefined;
56
@customElement('wokwi-ssd1306')
@@ -19,6 +20,17 @@ export class SSD1306Element extends LitElement {
1920
private canvas: HTMLCanvasElement | null | undefined = void 0;
2021
private ctx: CanvasContext = null;
2122

23+
readonly pinInfo: ElementPin[] = [
24+
{ name: 'DATA', x: 36.5, y: 12.5, signals: [i2c('SDA')] },
25+
{ name: 'CLK', x: 45.5, y: 12.5, signals: [i2c('SCL')] },
26+
{ name: 'DC', x: 54.5, y: 12.5, signals: [] },
27+
{ name: 'RST', x: 64.5, y: 12.5, signals: [] },
28+
{ name: 'CS', x: 74.5, y: 12.5, signals: [] },
29+
{ name: '3V3', x: 83.5, y: 12.5, signals: [{ type: 'power', signal: 'VCC', voltage: 3.3 }] },
30+
{ name: 'VIN', x: 93.5, y: 12.5, signals: [{ type: 'power', signal: 'VCC' }] },
31+
{ name: 'GND', x: 103.5, y: 12, signals: [{ type: 'power', signal: 'GND' }] },
32+
];
33+
2234
constructor() {
2335
super();
2436
this.imageData = new ImageData(this.screenWidth, this.screenHeight);

0 commit comments

Comments
 (0)