Browse Source

add blog post about github actions docs

Thomas Buck 7 months ago
parent
commit
78724503f2

+ 222
- 0
input/blog/2024/2024_05_05_auto_project_docs.md View File

@@ -0,0 +1,222 @@
1
+title: Blog
2
+post: Auto-generated project docs with 3D preview
3
+description: For KiCad PCBs and STL files, made with mdBook
4
+date: 2024-05-05
5
+---
6
+
7
+I recurring problem I ran into with my projects is documentation.
8
+Especially with 3D printed parts or PCBs it's often required to generate preview images or PDFs so someone interested can take a look without having to clone the repo and install some development tools to open the original files.
9
+Doing this manually is time-consuming and repetitive.
10
+So it should be automated.
11
+
12
+I've come across a couple of similar projects recently, like [this](https://hackaday.com/2024/05/04/giving-your-kicad-pcb-repository-pretty-pictures/) or [these](https://hackaday.com/2024/03/11/share-your-projects-kicad-automations-and-pretty-renders/).
13
+But they mostly spit out non-interactive images.
14
+So I've decided to go a slightly different and more interactive route.
15
+
16
+<!--%
17
+lightgallery([
18
+    [ "img/github_pages_sch.png", "2D KiCad board visualization" ],
19
+    [ "img/github_pages_pcb.png", "3D KiCad board visualization" ],
20
+    [ "img/github_pages_stl.png", "3D STL visualization" ],
21
+])
22
+%-->
23
+
24
+Using [GitHub Actions](https://docs.github.com/en/actions) and [GitHub Pages](https://docs.github.com/en/pages), I'm creating a static documentation website using [mdBook](https://rust-lang.github.io/mdBook/).
25
+To visualize KiCad schematics and 2D board designs I'm exporting them as svg images and display them interactively using [svg-pan-zoom](https://github.com/bumbu/svg-pan-zoom).
26
+KiCad PCBs are also exported in 3D as VRML files and displayed using [three.js](https://threejs.org/).
27
+
28
+To generate these files I'm mostly using shell scripts in the project repository.
29
+
30
+* `generate_stls.sh` is creating STL files from OpenSCAD sources
31
+* `generate_fab.sh` is creating Gerber files to order PCBs
32
+* `generate_plot.sh` is creating the 2D and 3D files for KiCad projects
33
+* `generate_docs.sh` is creating the static website using the output from the previous scripts
34
+
35
+Then the workflows can be kept relatively short, just installing the required dependencies, running the proper script and putting the resulting files where they are needed.
36
+This way the coupling to the proprietary GitHub features is not that thight and can hopefully be ported to other providers with relative ease.
37
+
38
+* `cmake.yml` is creating firmware archives (also usable for OTA updates)
39
+* `scad.yml` to create the STL files
40
+* `kicad.yml` to generate board gerber files
41
+* `docs.yml` to create the static documentation pages
42
+
43
+The whole workflow is relatively customizable.
44
+So for some projects I'm for example creating special board previews for etching PCBs at home.
45
+
46
+I've implemented this workflow in a couple of projects now:
47
+
48
+* [OpenAutoLab](https://kauzerei.github.io/openautolab/) ([Repo](https://github.com/kauzerei/openautolab))
49
+* [LARS](https://xythobuz.github.io/lars/) ([Repo](https://github.com/xythobuz/lars))
50
+* [Dispensy](https://drinkrobotics.github.io/dispensy/) ([Repo](https://github.com/drinkrobotics/dispensy))
51
+* [Tritonmischer](https://kauzerei.github.io/tritonmischer/) ([Repo](https://github.com/kauzerei/tritonmischer))
52
+
53
+Take a look there for the implementation details.
54
+
55
+## Example Implementation
56
+
57
+Here are the basics to get you started.
58
+
59
+#### 1) mdBook dependency
60
+
61
+Fetch the [pre-built mdBook binary](https://github.com/rust-lang/mdBook/releases) and place it in your PATH.
62
+
63
+#### 2) mdBook initialization
64
+
65
+Add a [docs](https://github.com/xythobuz/lars/tree/master/docs) directory to your project, either by copying it from one of my repos, or creating a new one and adapting the resulting `book.toml` config.
66
+
67
+    mdbook init docs
68
+
69
+#### 3) svg-pan-zoom dependency
70
+
71
+If you want to visualize SVG images for KiCad schematics and boards, add svg-pan-zoom as a subrepo.
72
+
73
+    cd docs
74
+    git submodule add https://github.com/bumbu/svg-pan-zoom
75
+    mkdir -p src/js
76
+    cd src/js
77
+    ln -s ../../svg-pan-zoom/dist/svg-pan-zoom.js svg-pan-zoom.js
78
+    ln -s ../../svg-pan-zoom/dist/svg-pan-zoom.min.js svg-pan-zoom.min.js
79
+
80
+#### 4) Generating docs
81
+
82
+Add the `docs/generate_docs.sh` script and adapt the config and calls to other script at the beginning, as needed for your project:
83
+
84
+<!-- https://clay-atlas.com/us/blog/2021/06/30/html-en-copy-text-button/ -->
85
+<script>
86
+function copyEvent(id) {
87
+    var str = document.getElementById(id);
88
+    window.getSelection().selectAllChildren(str);
89
+    document.execCommand("Copy")
90
+}
91
+</script>
92
+
93
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/docs/generate_docs.sh) is an example `generate_docs.sh` file.
94
+<button type="button" onclick="copyEvent('generatedocs')" class="clip-btn">Copy 'generate_docs.sh' to clipboard</button>
95
+
96
+<pre id="generatedocs" class="sh_sh">
97
+<!--%
98
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/docs/generate_docs.sh")
99
+%-->
100
+</pre>
101
+
102
+#### 5) Auto-generating and publishing docs
103
+
104
+Add the `.github/workflows/docs.yml` script:
105
+
106
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/docs.yml) is an example `docs.yml` file.
107
+<button type="button" onclick="copyEvent('docsyml')" class="clip-btn">Copy 'docs.yml' to clipboard</button>
108
+
109
+<pre id="docsyml" class="sh_yaml">
110
+<!--%
111
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/docs.yml")
112
+%-->
113
+</pre>
114
+
115
+And don't forget to set the GitHub Pages source in the repo settings to GitHub Actions:
116
+
117
+<!--%
118
+lightgallery([
119
+    [ "img/github_pages_actions.png", "What to change in the GitHub repo settings" ],
120
+])
121
+%-->
122
+
123
+#### 6) STL 3D visualization
124
+
125
+If you want to visualize 3D print files, do the same with `3dprint/generate_stls.sh` and `.github/workflows/scad.yml`:
126
+
127
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/3dprint/generate_stls.sh) is an example `generate_stls.sh` file.
128
+<button type="button" onclick="copyEvent('generatestls')" class="clip-btn">Copy 'generate_stls.sh' to clipboard</button>
129
+
130
+<pre id="generatestls" class="sh_sh">
131
+<!--%
132
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/3dprint/generate_stls.sh")
133
+%-->
134
+</pre>
135
+
136
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/scad.yml) is an example `scad.yml` file.
137
+<button type="button" onclick="copyEvent('scadyml')" class="clip-btn">Copy 'scad.yml' to clipboard</button>
138
+
139
+<pre id="scadyml" class="sh_yaml">
140
+<!--%
141
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/scad.yml")
142
+%-->
143
+</pre>
144
+
145
+And then add something like this to the mdBook sources where you want the visualization to appear:
146
+
147
+    \{{#include inc_enclosure_bottom.stl.md}}
148
+
149
+    [Direct link to this file](./stl/enclosure_bottom.stl).
150
+
151
+#### 7) PCB gerber files
152
+
153
+If you want to generate gerber files from PCBs, do something similar with `pcb/generate_fab.sh` and `.github/workflows/kicad.yml`:
154
+
155
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/pcb2/generate_fab.sh) is an example `generate_fab.sh` file.
156
+<button type="button" onclick="copyEvent('generatefab')" class="clip-btn">Copy 'generate_fab.sh' to clipboard</button>
157
+
158
+<pre id="generatefab" class="sh_sh">
159
+<!--%
160
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/pcb2/generate_fab.sh")
161
+%-->
162
+</pre>
163
+
164
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/kicad.yml) is an example `kicad.yml` file.
165
+<button type="button" onclick="copyEvent('kicadyml')" class="clip-btn">Copy 'kicad.yml' to clipboard</button>
166
+
167
+<pre id="kicadyml" class="sh_yaml">
168
+<!--%
169
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/.github/workflows/kicad.yml")
170
+%-->
171
+</pre>
172
+
173
+#### 8) PCB visualization
174
+
175
+If you want to visualize KiCad schematics and PCBs in 2D and 3D, add `pcb/generate_plot.sh`:
176
+
177
+[Here](https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/pcb2/generate_plot.sh) is an example `generate_plot.sh` file.
178
+<button type="button" onclick="copyEvent('generateplot')" class="clip-btn">Copy 'generate_plot.sh' to clipboard</button>
179
+
180
+<pre id="generateplot" class="sh_sh">
181
+<!--%
182
+include_url("https://git.xythobuz.de/thomas/drumkit/raw/commit/314bf218ca5e958d6ffa825d92d702cb5431abf6/pcb2/generate_plot.sh")
183
+%-->
184
+</pre>
185
+
186
+Then include this where you want the 2D schematic visualization to appear:
187
+
188
+    You can also view the [schematics as PDF](./plot/lars2.kicad_sch.pdf).
189
+
190
+    \{{#include inc_lars2.kicad_sch.md}}
191
+
192
+And this where you want to visualize the 2D PCB layout:
193
+
194
+    You can also view the [2D PCB layout as PDF](./plot/lars2.kicad_pcb.pdf).
195
+
196
+    <script src="js/svg-pan-zoom.js" charset="UTF-8"></script>
197
+    <div style="background-color: white; border: 1px solid black;">
198
+        <embed type="image/svg+xml" src="./plot/lars2.kicad_pcb.svg" id="pz_drumkit0" style="width: 100%;"/>
199
+        <script>
200
+            document.getElementById('pz_drumkit0').addEventListener('load', function(){
201
+                svgPanZoom(document.getElementById('pz_drumkit0'), {controlIconsEnabled: true, minZoom: 1.0});
202
+            })
203
+        </script>
204
+    </div>
205
+
206
+    [Direct link to this file](./plot/lars2.kicad_pcb.svg).
207
+
208
+And add this where you want to visualize the 3D PCB layout:
209
+
210
+    \{{#include inc_lars2.kicad_pcb.md}}
211
+
212
+That should more or less be all that's required.
213
+
214
+Of course, adapt paths as needed to match your project layout.
215
+And test locally by calling the scripts manually on your machine:
216
+
217
+    ./pcb/generate_plot.sh
218
+    ./3dprint/generate_stls.sh
219
+
220
+    ./docs/generate_docs.sh serve
221
+
222
+Hope this helps.

BIN
static/img/github_pages_actions.png View File


BIN
static/img/github_pages_actions_small.png View File


BIN
static/img/github_pages_pcb.png View File


BIN
static/img/github_pages_pcb_small.png View File


BIN
static/img/github_pages_sch.png View File


BIN
static/img/github_pages_sch_small.png View File


BIN
static/img/github_pages_stl.png View File


BIN
static/img/github_pages_stl_small.png View File


Loading…
Cancel
Save