Skip to content

Commit fb7ba24

Browse files
authored
Merge pull request #38 from wokwi/pin-info
add `pinInfo` getter to elements
2 parents efacd99 + bd51974 commit fb7ba24

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

src/arduino-uno-element.ts

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

45
@customElement('wokwi-arduino-uno')
56
export class ArduinoUnoElement extends LitElement {
@@ -8,6 +9,40 @@ export class ArduinoUnoElement extends LitElement {
89
@property() ledTX = false;
910
@property() ledPower = false;
1011

12+
readonly pinInfo: ElementPin[] = [
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')] },
15+
{ name: 'AREF', x: 106, y: 9, signals: [] },
16+
{ name: 'GND.1', x: 115.5, y: 9, signals: [{ type: 'power', signal: 'GND' }] },
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' }] },
21+
{ name: '9', x: 163, y: 9, signals: [{ type: 'pwm' }] },
22+
{ name: '8', x: 173, y: 9, signals: [] },
23+
{ name: '7', x: 189, y: 9, signals: [] },
24+
{ name: '6', x: 198.5, y: 9, signals: [{ type: 'pwm' }] },
25+
{ name: '5', x: 208, y: 9, signals: [{ type: 'pwm' }] },
26+
{ name: '4', x: 217.5, y: 9, signals: [] },
27+
{ name: '3', x: 227, y: 9, signals: [{ type: 'pwm' }] },
28+
{ name: '2', x: 236.5, y: 9, signals: [] },
29+
{ name: '1', x: 246, y: 9, signals: [usart('TX')] },
30+
{ name: '0', x: 255.5, y: 9, signals: [usart('RX')] },
31+
{ name: 'IOREF', x: 131, y: 191.5, signals: [] },
32+
{ name: 'RESET', x: 140.5, y: 191.5, signals: [] },
33+
{ name: '3.3V', x: 150, y: 191.5, signals: [{ type: 'power', signal: 'VCC', voltage: 3.3 }] },
34+
{ name: '5V', x: 160, y: 191.5, signals: [{ type: 'power', signal: 'VCC', voltage: 5 }] },
35+
{ name: 'GND.2', x: 169.5, y: 191.5, signals: [{ type: 'power', signal: 'GND' }] },
36+
{ name: 'GND.3', x: 179, y: 191.5, signals: [{ type: 'power', signal: 'GND' }] },
37+
{ name: 'VIN', x: 188.5, y: 191.5, signals: [{ type: 'power', signal: 'VCC' }] },
38+
{ name: 'A0', x: 208, y: 191.5, signals: [analog(0)] },
39+
{ name: 'A1', x: 217.5, y: 191.5, signals: [analog(1)] },
40+
{ name: 'A2', x: 227, y: 191.5, signals: [analog(2)] },
41+
{ name: 'A3', x: 236.5, y: 191.5, signals: [analog(3)] },
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')] },
44+
];
45+
1146
render() {
1247
const { ledPower, led13, ledRX, ledTX } = this;
1348
return html`

src/led-element.ts

Lines changed: 6 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 lightColors: { [key: string]: string } = {
45
red: '#ff8080',
@@ -17,6 +18,11 @@ export class LEDElement extends LitElement {
1718
@property() lightColor: string | null = null;
1819
@property() label = '';
1920

21+
readonly pinInfo: ElementPin[] = [
22+
{ name: 'A', x: 24, y: 42, signals: [], description: 'Anode' },
23+
{ name: 'C', x: 16, y: 42, signals: [], description: 'Cathode' },
24+
];
25+
2026
static get styles() {
2127
return css`
2228
:host {

src/pin.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export type PinSignalInfo =
2+
| {
3+
type: 'i2c';
4+
signal: 'SDA' | 'SCL';
5+
bus: number;
6+
}
7+
| {
8+
type: 'spi';
9+
signal: 'SCK' | 'MOSI' | 'MISO' | 'SS';
10+
bus: number;
11+
}
12+
| {
13+
type: 'usart';
14+
signal: 'RX' | 'TX';
15+
bus: number;
16+
}
17+
| {
18+
type: 'power';
19+
signal: 'GND' | 'VCC';
20+
voltage?: number;
21+
}
22+
| {
23+
type: 'pwm';
24+
}
25+
| {
26+
type: 'analog';
27+
channel?: number;
28+
};
29+
30+
export interface ElementPin {
31+
/**
32+
* A name that uniquely identifies the pin, e.g. `GND` or `VCC`.
33+
* Pins that are connected internally must have the same name prefix, followed by a single dot,
34+
* and a unique suffix (e.g. `GND.1` and `GND.2`).
35+
*/
36+
name: string;
37+
38+
/** The x-coordinate of the pin, relative to the element's origin */
39+
x: number;
40+
41+
/** The y-coordinate of the pin, relative to the element's origin */
42+
y: number;
43+
44+
/** The signals for this pin. Leave empty for generic pins without a designated signals. **/
45+
signals: PinSignalInfo[];
46+
47+
/**
48+
* Optional pin description
49+
*/
50+
description?: string;
51+
}
52+
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)