Skip to content

Instantly share code, notes, and snippets.

@kasbah
Created October 17, 2017 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasbah/1ab5e25ab27805b40fba8b69397fa4fc to your computer and use it in GitHub Desktop.
Save kasbah/1ab5e25ab27805b40fba8b69397fa4fc to your computer and use it in GitHub Desktop.
Experiment of combining Electro Grammar with SKiDL
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
" -->\n",
"<!-- Title: %3 Pages: 1 -->\n",
"<svg width=\"292pt\" height=\"270pt\"\n",
" viewBox=\"0.00 0.00 291.67 270.38\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(154.804 142.585)\">\n",
"<title>%3</title>\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-154.804,127.797 -154.804,-142.585 136.867,-142.585 136.867,127.797 -154.804,127.797\"/>\n",
"<!-- GND -->\n",
"<g id=\"node1\" class=\"node\"><title>GND</title>\n",
"<ellipse fill=\"black\" stroke=\"black\" cx=\"-76.2899\" cy=\"-73.5018\" rx=\"1.8\" ry=\"1.8\"/>\n",
"<text text-anchor=\"middle\" x=\"-93.0899\" y=\"-79.1018\" font-family=\"Times,serif\" font-size=\"14.00\">GND</text>\n",
"</g>\n",
"<!-- C1 -->\n",
"<g id=\"node2\" class=\"node\"><title>C1</title>\n",
"<polygon fill=\"none\" stroke=\"black\" points=\"25.1187,-123.585 -28.8813,-123.585 -28.8813,-87.5854 25.1187,-87.5854 25.1187,-123.585\"/>\n",
"<text text-anchor=\"middle\" x=\"-1.88135\" y=\"-101.885\" font-family=\"Times,serif\" font-size=\"14.00\">C1</text>\n",
"<text text-anchor=\"middle\" x=\"-42.8813\" y=\"-127.385\" font-family=\"Times,serif\" font-size=\"14.00\">100n</text>\n",
"</g>\n",
"<!-- C1&#45;&gt;GND -->\n",
"<g id=\"edge1\" class=\"edge\"><title>C1&#45;&gt;GND</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M-29.1658,-93.8209C-47.0838,-86.0949 -68.3135,-76.9411 -74.5103,-74.2692\"/>\n",
"</g>\n",
"<!-- N$1 -->\n",
"<g id=\"node4\" class=\"node\"><title>N$1</title>\n",
"<ellipse fill=\"black\" stroke=\"black\" cx=\"73.7115\" cy=\"-76.4052\" rx=\"1.8\" ry=\"1.8\"/>\n",
"</g>\n",
"<!-- C1&#45;&gt;N$1 -->\n",
"<g id=\"edge4\" class=\"edge\"><title>C1&#45;&gt;N$1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M25.4094,-95.0507C43.6789,-87.9984 65.5056,-79.5728 71.8804,-77.1121\"/>\n",
"</g>\n",
"<!-- D1 -->\n",
"<g id=\"node3\" class=\"node\"><title>D1</title>\n",
"<polygon fill=\"none\" stroke=\"black\" points=\"-78.8044,-16.0233 -132.804,-16.0233 -132.804,19.9767 -78.8044,19.9767 -78.8044,-16.0233\"/>\n",
"<text text-anchor=\"middle\" x=\"-105.804\" y=\"5.67673\" font-family=\"Times,serif\" font-size=\"14.00\">D1</text>\n",
"<text text-anchor=\"middle\" x=\"-141.804\" y=\"-19.8233\" font-family=\"Times,serif\" font-size=\"14.00\">red</text>\n",
"</g>\n",
"<!-- D1&#45;&gt;GND -->\n",
"<g id=\"edge2\" class=\"edge\"><title>D1&#45;&gt;GND</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M-98.6597,-16.2945C-91.1532,-35.4913 -80.0278,-63.9427 -77.0472,-71.565\"/>\n",
"</g>\n",
"<!-- N$2 -->\n",
"<g id=\"node6\" class=\"node\"><title>N$2</title>\n",
"<ellipse fill=\"black\" stroke=\"black\" cx=\"-73.6232\" cy=\"76.3514\" rx=\"1.8\" ry=\"1.8\"/>\n",
"</g>\n",
"<!-- D1&#45;&gt;N$2 -->\n",
"<g id=\"edge5\" class=\"edge\"><title>D1&#45;&gt;N$2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M-98.0142,19.9808C-89.8294,38.8968 -77.6989,66.932 -74.449,74.4429\"/>\n",
"</g>\n",
"<!-- R2 -->\n",
"<g id=\"node5\" class=\"node\"><title>R2</title>\n",
"<polygon fill=\"none\" stroke=\"black\" points=\"132.867,-20.0105 78.8674,-20.0105 78.8674,15.9895 132.867,15.9895 132.867,-20.0105\"/>\n",
"<text text-anchor=\"middle\" x=\"105.867\" y=\"1.68947\" font-family=\"Times,serif\" font-size=\"14.00\">R2</text>\n",
"<text text-anchor=\"middle\" x=\"71.8674\" y=\"-23.8105\" font-family=\"Times,serif\" font-size=\"14.00\">1k</text>\n",
"</g>\n",
"<!-- R2&#45;&gt;N$1 -->\n",
"<g id=\"edge3\" class=\"edge\"><title>R2&#45;&gt;N$1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M98.0833,-20.0194C89.905,-38.9405 77.784,-66.9833 74.5367,-74.4962\"/>\n",
"</g>\n",
"<!-- VIN -->\n",
"<g id=\"node8\" class=\"node\"><title>VIN</title>\n",
"<ellipse fill=\"black\" stroke=\"black\" cx=\"76.1446\" cy=\"73.3774\" rx=\"1.8\" ry=\"1.8\"/>\n",
"<text text-anchor=\"middle\" x=\"61.8446\" y=\"67.7774\" font-family=\"Times,serif\" font-size=\"14.00\">VIN</text>\n",
"</g>\n",
"<!-- R2&#45;&gt;VIN -->\n",
"<g id=\"edge8\" class=\"edge\"><title>R2&#45;&gt;VIN</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M98.6723,16.2388C91.1128,35.4125 79.9089,63.8297 76.9073,71.4429\"/>\n",
"</g>\n",
"<!-- R1 -->\n",
"<g id=\"node7\" class=\"node\"><title>R1</title>\n",
"<polygon fill=\"none\" stroke=\"black\" points=\"28.8753,87.7975 -25.1247,87.7975 -25.1247,123.797 28.8753,123.797 28.8753,87.7975\"/>\n",
"<text text-anchor=\"middle\" x=\"1.87525\" y=\"109.497\" font-family=\"Times,serif\" font-size=\"14.00\">R1</text>\n",
"<text text-anchor=\"middle\" x=\"-32.1247\" y=\"83.9975\" font-family=\"Times,serif\" font-size=\"14.00\">1k</text>\n",
"</g>\n",
"<!-- R1&#45;&gt;N$2 -->\n",
"<g id=\"edge6\" class=\"edge\"><title>R1&#45;&gt;N$2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M-25.3814,95.1668C-43.628,88.0502 -65.4275,79.5479 -71.7943,77.0647\"/>\n",
"</g>\n",
"<!-- R1&#45;&gt;VIN -->\n",
"<g id=\"edge7\" class=\"edge\"><title>R1&#45;&gt;VIN</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M29.1087,93.9095C46.9932,86.1026 68.1831,76.8527 74.3684,74.1527\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.dot.Digraph at 0x7f9670dd7b00>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import skidl\n",
"from easy_skidl import *\n",
"\n",
"default_circuit.mini_reset()\n",
"\n",
"r1 = resistor('1k 0603')\n",
"r2 = r1.copy()\n",
"\n",
"c1 = capacitor('0.1uF 0805')\n",
"\n",
"led1 = led('0805 red')\n",
"\n",
"vin = skidl.Net('VIN')\n",
"gnd = skidl.Net('GND')\n",
"\n",
"vin += r1[1], r2[1]\n",
"r2[2] += c1[1]\n",
"c1[2] += gnd\n",
"led1[1] += r1[2]\n",
"led1[2] += gnd\n",
"\n",
"skidl.generate_graph()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#easy_skidl.py\n",
"import skidl\n",
"import electro_grammar\n",
"from quantiphy import Quantity\n",
"\n",
"def component(description):\n",
" c = electro_grammar.parse(description)\n",
" if c['type'] == 'resistor':\n",
" p = skidl.Part('device', 'R', skidl.TEMPLATE).copy()\n",
" p.value = Quantity(c['resistance'])\n",
" if 'size' in c:\n",
" p.footprint = 'Resistors_SMD:R_{}'.format(c['size'])\n",
" return p\n",
" elif c['type'] == 'capacitor':\n",
" p = skidl.Part('device', 'C', skidl.TEMPLATE).copy()\n",
" p.value = Quantity(c['capacitance'])\n",
" if 'size' in c:\n",
" p.footprint = 'Capacitors_SMD:C_{}'.format(c['size'])\n",
" return p\n",
" elif c['type'] == 'led':\n",
" p = skidl.Part('device', 'LED', skidl.TEMPLATE).copy()\n",
" p.value = c['color']\n",
" if 'size' in c:\n",
" p.footprint = 'LEDS:LED_{}'.format(c['size'])\n",
" return p\n",
" raise Exception('could not determine component type from \"{}\"'.format(description)\n",
"\n",
"def resistor(description=''):\n",
" return component(description)\n",
"\n",
"def capacitor(description=''):\n",
" return component(description)\n",
"\n",
"def led(description=''):\n",
" return component('led ' + description)\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment