seveibar/keyboard-sample

A tscircuit package created by seveibar.

Usage: For "@tsci/seveibar.keyboard-sample": 1. Import the component: ```tsx import KeyboardSample from "@tsci/seveibar.keyboard-sample" ``` 2. Use the component in a React-like circuit design: ```tsx <board> <KeyboardSample sampleNumber={3} /> </board> ``` The component takes an optional `sampleNumber` parameter to define the grid size of the keyboard matrix (defaults to 2). It automatically generates a keyboard matrix with keys, diodes, and traces connecting to a Pico2 microcontroller.

Version
0.0.1
License
unset
Stars
0

index.ts

PCBPCB preview for index.ts
SchematicSchematic preview for index.ts
import { SmdDiode } from "@tsci/seveibar.SmdDiode"
import { Key } from "@tsci/seveibar.Key"
import { Pico2 } from "@tsci/seveibar.pico2"

function grid(opts: {
  sizeX: number
  sizeY: number
  pitch: number
  offset?: { x: number; y: number }
}): Array<{ x: number; y: number; row: number; col: number }> {
  const { sizeX, sizeY, pitch, offset = { x: 0, y: 0 } } = opts
  const points: Array<{ x: number; y: number; row: number; col: number }> = []
  const startX = (-(sizeX - 1) * pitch) / 2
  const startY = (-(sizeY - 1) * pitch) / 2
  for (let row = 0; row < sizeY; row++) {
    for (let col = 0; col < sizeX; col++) {
      points.push({
        x: startX + col * pitch + offset.x,
        y: startY + row * pitch + offset.y,
        row,
        col,
      })
    }
  }
  return points
}

const rowToMicroPin = {
  0: "GP0",
  1: "GP1",
  2: "GP10",
}
const colToMicroPin = {
  0: "GP19",
  1: "GP17",
  2: "GP5",
}

export default ({ sampleNumber = 2 }: { sampleNumber: number }) => {
  return (
    <board width="100mm" height="100mm" routingDisabled>
      <Pico2 name="U1" pcbX={-30} />
      {grid({ sizeX: sampleNumber, sizeY: sampleNumber, pitch: 19.05, offset: { x: 20, y: 0 } }).map(
        ({ x, y, row, col }, index) => (
          <group key={`Kgroup${index}`}>
            <Key pcbX={x} pcbY={y} name={`K${index + 1}`} />
            <SmdDiode
              pcbX={x}
              pcbY={y - 13}
              layer="bottom"
              name={`D${index + 1}`}
            />
            <trace from={`.D${index + 1} .pin1`} to={`.K${index + 1} .pin1`} />
            <trace
              from={`.D${index + 1} .pin2`}
              to={`.U1 .${rowToMicroPin[row]}`}
            />
            <trace
              from={`.K${index + 1} .pin2`}
              to={`.U1 .${colToMicroPin[col]}`}
            />
          </group>
        )
      )}
    </board>
  )
}