|
| 1 | +import type { Meta } from "@storybook/react" |
| 2 | +import { Circuit } from "@tscircuit/core" |
| 3 | +import type React from "react" |
| 4 | +import { PCBViewer } from "../../../PCBViewer" |
| 5 | +import { usePICO_W } from "./usePICO" |
| 6 | +import { useHS91L02W2C01 } from "./useHS91L02W2C01" |
| 7 | +import { WS2812B_2020 as LedWithIc } from "./useWS2812B_2020" |
| 8 | + |
| 9 | +export const contributionBoard: React.FC = () => { |
| 10 | + const circuit = new Circuit() |
| 11 | + type Point = { x: number; y: number } |
| 12 | + |
| 13 | + type GridCellPositions = { |
| 14 | + index: number |
| 15 | + center: Point |
| 16 | + topLeft: Point |
| 17 | + bottomRight: Point |
| 18 | + } |
| 19 | + |
| 20 | + type GridOptions = { |
| 21 | + rows: number |
| 22 | + cols: number |
| 23 | + xSpacing?: number |
| 24 | + ySpacing?: number |
| 25 | + width?: number |
| 26 | + height?: number |
| 27 | + offsetX?: number |
| 28 | + offsetY?: number |
| 29 | + yDirection?: "cartesian" | "up-is-negative" |
| 30 | + } |
| 31 | + |
| 32 | + // ToDO import from tscircuit utils in the future |
| 33 | + function grid({ |
| 34 | + rows, |
| 35 | + cols, |
| 36 | + xSpacing, |
| 37 | + ySpacing, |
| 38 | + width, |
| 39 | + height, |
| 40 | + offsetX = 0, |
| 41 | + offsetY = 0, |
| 42 | + yDirection = "cartesian", |
| 43 | + }: GridOptions): GridCellPositions[] { |
| 44 | + // Calculate cell dimensions |
| 45 | + const cellWidth = width ? width / cols : (xSpacing ?? 1) |
| 46 | + const cellHeight = height ? height / rows : (ySpacing ?? 1) |
| 47 | + |
| 48 | + const cells: GridCellPositions[] = [] |
| 49 | + |
| 50 | + for (let row = 0; row < rows; row++) { |
| 51 | + for (let col = 0; col < cols; col++) { |
| 52 | + const index = row * cols + col |
| 53 | + |
| 54 | + // Calculate center position |
| 55 | + const centerX = offsetX + col * cellWidth + cellWidth / 2 |
| 56 | + const rawCenterY = offsetY + row * cellHeight + cellHeight / 2 |
| 57 | + |
| 58 | + // Adjust Y coordinate based on yDirection |
| 59 | + const centerY = |
| 60 | + yDirection === "cartesian" |
| 61 | + ? offsetY + (rows - 1 - row) * cellHeight + cellHeight / 2 |
| 62 | + : rawCenterY |
| 63 | + |
| 64 | + cells.push({ |
| 65 | + row, |
| 66 | + col, |
| 67 | + index, |
| 68 | + center: { x: centerX, y: centerY }, |
| 69 | + topLeft: { |
| 70 | + x: centerX - cellWidth / 2, |
| 71 | + y: centerY + cellHeight / 2, |
| 72 | + }, |
| 73 | + bottomRight: { |
| 74 | + x: centerX + cellWidth / 2, |
| 75 | + y: centerY - cellHeight / 2, |
| 76 | + }, |
| 77 | + } as any) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return cells |
| 82 | + } |
| 83 | + |
| 84 | + const U1 = usePICO_W("U1") |
| 85 | + const U2 = useHS91L02W2C01("U2") |
| 86 | + circuit.add( |
| 87 | + <board width="316mm" height="52mm" routingDisabled> |
| 88 | + <U1 pcbRotation="90deg" pcbX={-122 - 15} pcbY={0} /> |
| 89 | + <U2 |
| 90 | + GND="net.GND" |
| 91 | + VCC={"net.V5"} |
| 92 | + SDA={U1.GP26_ADC0_I2C1SDA} |
| 93 | + SCL={U1.GP27_ADC1_I2C1SCL} |
| 94 | + schX={-7} |
| 95 | + schY={0} |
| 96 | + pcbX={-122 + 5} |
| 97 | + pcbY={19} |
| 98 | + /> |
| 99 | + {grid({ |
| 100 | + cols: 53, |
| 101 | + rows: 7, |
| 102 | + xSpacing: 5, |
| 103 | + ySpacing: 5, |
| 104 | + offsetX: 3 - 122, |
| 105 | + offsetY: -32 / 2 - 7.5, |
| 106 | + }).map(({ center, index }) => { |
| 107 | + const ledName = `LED${index + 1}` |
| 108 | + const prevLedName = index > 0 ? `LED${index}` : null |
| 109 | + const capName = `C_${ledName}` |
| 110 | + const ledSchX = ((center.x / 2) * 8) / 5 + 2 + 101 |
| 111 | + const ledSchY = 5 + center.y / 1.5 |
| 112 | + return ( |
| 113 | + <> |
| 114 | + <LedWithIc |
| 115 | + schX={ledSchX} |
| 116 | + schY={ledSchY} |
| 117 | + name={ledName} |
| 118 | + pcbX={center.x} |
| 119 | + pcbY={center.y} |
| 120 | + /> |
| 121 | + <trace from={`.${ledName} .GND`} to="net.GND" /> |
| 122 | + <trace from={`.${ledName} .VDD`} to="net.V5" /> |
| 123 | + {prevLedName && ( |
| 124 | + <trace from={`.${prevLedName} .DO`} to={`.${ledName} .DI`} /> |
| 125 | + )} |
| 126 | + <capacitor |
| 127 | + name={capName} |
| 128 | + footprint="0402" |
| 129 | + capacitance="100nF" |
| 130 | + pcbX={center.x} |
| 131 | + pcbY={center.y - 2.2} |
| 132 | + pcbRotation="180deg" |
| 133 | + schX={ledSchX} |
| 134 | + schY={ledSchY - 1.1} |
| 135 | + schRotation="180deg" |
| 136 | + /> |
| 137 | + <trace from={`.${capName} .neg`} to="net.GND" /> |
| 138 | + <trace from={`.${capName} .pos`} to="net.V5" /> |
| 139 | + </> |
| 140 | + ) |
| 141 | + })} |
| 142 | + |
| 143 | + <capacitor |
| 144 | + name="C1" |
| 145 | + capacitance="100uF" |
| 146 | + footprint="1206" |
| 147 | + schX={5} |
| 148 | + pcbX={-122} |
| 149 | + pcbRotation="90deg" |
| 150 | + schRotation="270deg" |
| 151 | + /> |
| 152 | + <trace from=".C1 .neg" to="net.GND" /> |
| 153 | + <trace from=".C1 .pos" to="net.V5" /> |
| 154 | + |
| 155 | + <trace from=".LED1 .DI" to={U1.GP11_SPI1TX_I2C1SCL} /> |
| 156 | + <trace from={U1.GND1} to="net.GND" /> |
| 157 | + <trace from={U1.GND2} to="net.GND" /> |
| 158 | + <trace from={U1.GND3} to="net.GND" /> |
| 159 | + <trace from={U1.GND4} to="net.GND" /> |
| 160 | + <trace from={U1.GND5} to="net.GND" /> |
| 161 | + <trace from={U1.GND6} to="net.GND" /> |
| 162 | + <trace from={U1.GND7} to="net.GND" /> |
| 163 | + |
| 164 | + <trace from={U1.VBUS} to="net.V5" /> |
| 165 | + <footprint> |
| 166 | + <hole pcbX={-32 - 122} pcbY={20} diameter="4.8mm" /> |
| 167 | + <hole pcbX={-32 - 122} pcbY={-20} diameter="4.8mm" /> |
| 168 | + <hole pcbX={32 + 122} pcbY={20} diameter="4.8mm" /> |
| 169 | + <hole pcbX={32 + 122} pcbY={-20} diameter="4.8mm" /> |
| 170 | + </footprint> |
| 171 | + </board>, |
| 172 | + ) |
| 173 | + |
| 174 | + const soup = circuit.getCircuitJson() |
| 175 | + |
| 176 | + return ( |
| 177 | + <div style={{ backgroundColor: "black" }}> |
| 178 | + <PCBViewer soup={soup} /> |
| 179 | + </div> |
| 180 | + ) |
| 181 | +} |
| 182 | + |
| 183 | +const meta: Meta<typeof contributionBoard> = { |
| 184 | + title: "contribution-board-performance-test/contribution-board", |
| 185 | + component: contributionBoard, |
| 186 | +} |
| 187 | + |
| 188 | +export default meta |
0 commit comments