Debian Codegraph @

texmf-dist/tex/latex/tikz-osci/tikz-osci.sty

raw
1 % -----------------------------------------------------------
2 % This is TikZ Osci package
3 %
4 % It allows you to produce oscilloscope "screen shots".
5 %
6 % It is based on TikZ and pgfplots and is a replacement can-
7 % didate to the pstricks pst-osci package which is not main-
8 % tained anymore.
9 %
10 % Author: Thibault Giauffret
11 % Date: 2023/10/24
12 % Version: 0.4.0
13 % Licence : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
14 % Not an expert... Just doing it for fun.
15 % -----------------------------------------------------------
16
17 \NeedsTeXFormat{LaTeX2e}
18 \ProvidesPackage{tikz-osci}[2023/10/24 0.4.0 Tikz Osci package]
19
20 % Required packages
21 \RequirePackage[dvipsnames]{xcolor}
22 \RequirePackage{pgfkeys}
23 \RequirePackage{pgfplots}
24 \RequirePackage{tikz}
25 \usetikzlibrary{arrows,shapes,positioning,decorations.markings,decorations.pathmorphing,
26 decorations.pathreplacing,calc,patterns,shapes.geometric}
27
28 % Raised text
29 \newcommand*{\raisedtext}[1]{%
30 \begingroup
31 \leavevmode
32 \rlap{\kern-.2pt\raise.2pt\hbox{\color{white}#1}}%
33 \rlap{\kern.2pt\raise-.2pt\hbox{\color{black!30}#1}}%
34 \hbox{#1}%
35 \endgroup
36 }
37
38 % The main command
39 \newcommand\osci[1][]{
40
41 % Init the keys and get the values and add the user's values
42 \pgfkeys{
43 /osci/.cd,
44 scale/.store in=\scale,
45 scale=1,
46 rounded corners/.store in=\roundedCorners,
47 rounded corners=10,
48 second channel/.store in=\secondChannel,
49 second channel=0,
50 screen offset one/.store in=\screenOffsetOne,
51 screen offset one=0,
52 screen offset two/.store in=\screenOffsetTwo,
53 screen offset two=-0,
54 time div/.store in=\timeDiv,
55 time div=20,
56 voltage div one/.store in=\voltageDivOne,
57 voltage div one=1,
58 voltage div two/.store in=\voltageDivTwo,
59 voltage div two=1,
60 sample rate/.store in=\sampleRate,
61 sample rate=200,
62 xy mode/.store in=\xyMode,
63 xy mode=0,
64 math mode/.store in=\mathMode,
65 math mode=0,
66 math mode hide source/.store in=\mathModeHideSource,
67 math mode hide source=0,
68 func one/.store in=\funcOne,
69 func one=2*sin(2*180/0.020*x),
70 func two/.store in=\funcTwo,
71 func two=0.6*sin(2*180/0.020*x)+0.2*sin(2*180/0.040*x),
72 indicators/.store in=\indicators,
73 indicators=1,
74 vertical cursor one/.store in=\verticalCursorOne,
75 vertical cursor one=false,
76 vertical cursor two/.store in=\verticalCursorTwo,
77 vertical cursor two=false,
78 horizontal cursor one/.store in=\horizontalCursorOne,
79 horizontal cursor one=false,
80 horizontal cursor two/.store in=\horizontalCursorTwo,
81 horizontal cursor two=false,
82 cursor precision/.store in=\cursorPrecision,
83 cursor precision=1,
84 color one/.store in=\colorOne,
85 color one=D62626,
86 color text one/.store in=\colorTextOne,
87 color text one=FFFFFF,
88 color two/.store in=\colorTwo,
89 color two=1053AF,
90 color text two/.store in=\colorTextTwo,
91 color text two=FFFFFF,
92 color three/.store in=\colorThree,
93 color three=2E8B73,
94 color text three/.store in=\colorTextThree,
95 color text three=FFFFFF,
96 graph back color/.store in=\graphBackColor,
97 graph back color=FFFFFF,
98 info back color/.store in=\infoBackColor,
99 info back color=D6D6D6,
100 info text color/.store in=\infoTextColor,
101 info text color=000000,
102 main axis color/.store in=\mainAxisColor,
103 main axis color=000000,
104 grid color/.store in=\gridColor,
105 grid color=AAAAAA,
106 horizontal cursor color/.store in=\horizontalCursorColor,
107 horizontal cursor color=FFA500,
108 vertical cursor color/.store in=\verticalCursorColor,
109 vertical cursor color=800080,
110 smooth one/.store in=\smoothOne,
111 smooth one=0,
112 smooth two/.store in=\smoothTwo,
113 smooth two=0,
114 smooth three/.store in=\smoothThree,
115 smooth three=0,
116 #1
117 }
118
119
120 % Set the scale of the picture
121 \scalebox{\scale}{
122 % Begin the picture
123 \begin{tikzpicture}
124
125 % Set the colors
126 \definecolor{firstcolor}{HTML}{\colorOne}
127 \definecolor{secondcolor}{HTML}{\colorTwo}
128 \definecolor{thirdcolor}{HTML}{\colorThree}
129 \definecolor{graphbackcolor}{HTML}{\graphBackColor}
130 \definecolor{infoBackColor}{HTML}{\infoBackColor}
131 \definecolor{infoTextColor}{HTML}{\infoTextColor}
132 \definecolor{mainAxisColor}{HTML}{\mainAxisColor}
133 \definecolor{gridColor}{HTML}{\gridColor}
134 \definecolor{colorTextOne}{HTML}{\colorTextOne}
135 \definecolor{colorTextTwo}{HTML}{\colorTextTwo}
136 \definecolor{colorTextThree}{HTML}{\colorTextThree}
137 \definecolor{verticalCursorColor}{HTML}{\verticalCursorColor}
138 \definecolor{horizontalCursorColor}{HTML}{\horizontalCursorColor}
139
140 % Draw the info box
141 \draw[black,thick, fill= infoBackColor]
142 (0,0) --
143 ++(10,0) {[rounded corners=\roundedCorners] --
144 ++(0,-1.5) --
145 ++(-10,0)} --
146 cycle
147 {};
148
149 % Add time/div label
150 \draw (5,-0.15) node[anchor=north, color= infoTextColor] {\timeDiv\ ms/div};
151
152 % Test if smoothOne is enabled
153 \ifnum\smoothOne=1
154 % set \smoothOne to smooth
155 \def\smoothOne{smooth}
156 \else
157 % set \smoothOne to empty
158 \def\smoothOne{}
159 \fi
160
161 % Test if smoothTwo is enabled
162 \ifnum\smoothTwo=1
163 % set \smoothTwo to smooth
164 \def\smoothTwo{smooth}
165 \else
166 % set \smoothTwo to empty
167 \def\smoothTwo{}
168 \fi
169
170 % Test if smoothThree is enabled
171 \ifnum\smoothThree=1
172 % set \smoothThree to smooth
173 \def\smoothThree{smooth}
174 \else
175 % set \smoothThree to empty
176 \def\smoothThree{}
177 \fi
178
179 % Add XY mode indicator
180 % Lissajou mode
181 \ifnum\xyMode=1
182
183 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
184 \draw (5,-0.75) node[anchor=north, color=colorTextThree] {XY mode};
185
186 \else
187
188 % Add mode
189 \ifnum\mathMode=1
190
191 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
192 \draw (5,-0.75) node[anchor=north, color=colorTextThree] {Add};
193
194 \else
195
196 % Substract mode
197 \ifnum\mathMode=2
198
199 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
200 \draw (5,-0.75) node[anchor=north, color=colorTextThree] {Substract};
201
202 \else
203
204 % Multiply mode
205 \ifnum\mathMode=3
206
207 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
208 \draw (5,-0.75) node[anchor=north, color=colorTextThree] {Multiply};
209
210 \else
211
212 % Divide mode
213 \ifnum\mathMode=4
214
215 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
216 \draw (5,-0.75) node[anchor=north, color=colorTextThree] {Divide};
217
218 \fi
219 \fi
220 \fi
221 \fi
222 \fi
223
224 % Add voltage/div label for the first channel
225 \draw[firstcolor,fill=firstcolor] (0.5,-0.15) rectangle (1.5,-0.6);
226 \draw (1,-0.1) node[anchor=north, color=colorTextOne] {CH1};
227 \draw (1,-0.75) node[anchor=north, color= infoTextColor] {\voltageDivOne\ V/div};
228
229 % Add voltage/div label for the second channel
230 \ifnum\secondChannel=1
231 \draw[secondcolor,fill=secondcolor] (8.5,-0.15) rectangle (9.5,-0.6);
232 \draw (9,-0.1) node[anchor=north, color=colorTextTwo]{CH2};
233 \draw (9,-0.75) node[anchor=north, color= infoTextColor] {\voltageDivTwo\ V/div};
234 \fi
235
236 % Draw the oscilloscope screen border
237 \draw[black,thick, fill= graphbackcolor]
238 (0,0) --
239 ++(10,0) {[rounded corners=\roundedCorners] --
240 ++(0,8) --
241 ++(-10,0)} --
242 cycle
243 {};
244
245 % Draw the oscilloscope screen graduations
246 \begin{scope}
247
248 % Remove the border
249 \clip (0.05,0.05) rectangle (9.95,7.95);
250
251 \begin{axis}[xmin=-5*(\timeDiv/1000), xmax=5*(\timeDiv/1000), ymin=-4, ymax=4, xtick distance = \timeDiv/1000,
252 axis x line=middle, axis y line=middle,
253 axis line style={-},
254 color=mainAxisColor,
255 % Draw major grid
256 grid=both,
257 xticklabels={,,},
258 yticklabels={,,},
259 % Add sub ticks for the x axis
260 minor tick num=4,
261 % Remove minor grid
262 minor grid style={line width=0pt, draw=white, draw opacity=0},
263 major grid style={thick,dashed,draw=gridColor},
264 every major tick/.append style={very thick, major tick length=6pt, color=mainAxisColor},
265 every minor tick/.append style={thick, minor tick length=4pt, color=mainAxisColor},
266 ]
267
268 % Change plot width
269 \pgfplotsset{width=11.58cm, height=9.58cm}
270 % Change tick label style
271 \pgfplotsset{ every non boxed x axis/.append style={x axis line style=-},
272 every non boxed y axis/.append style={y axis line style=-}}
273
274 % Lissajous mode
275 \ifnum\xyMode=1
276 \addplot
277 [domain=0:360,variable=\x,
278 samples=200,very thick,color=thirdcolor, \smoothThree]
279 ({(\funcOne)/\voltageDivOne},{(\funcTwo)/\voltageDivTwo});
280 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
281 \draw (5,-0.75) node[anchor=north, color=white] {XY mode};
282
283 \else
284
285 % Show source signals
286 \ifnum\mathModeHideSource=0
287 % Draw each channel separately
288 % First channel plot
289 \addplot[color=firstcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000),samples=\sampleRate,\smoothOne]{(\funcOne)/\voltageDivOne+\screenOffsetOne};
290
291 % If the second channel is enabled, draw it
292 \ifnum\secondChannel=1
293 % Draw the second channel
294 \addplot[color=secondcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000), samples=\sampleRate,\smoothTwo]{(\funcTwo)/\voltageDivTwo+\screenOffsetTwo};
295
296 \fi
297 \fi
298
299 % Add mode
300 \ifnum\mathMode=1
301 \addplot[color=thirdcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000),
302 smooth, samples=\sampleRate, \smoothThree]{(\funcOne)/\voltageDivOne + (\funcTwo)/\voltageDivTwo};
303
304 \else
305
306 % Substract mode
307 \ifnum\mathMode=2
308 \addplot[color=thirdcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000), samples=\sampleRate, \smoothThree]{(\funcOne)/\voltageDivOne - (\funcTwo)/\voltageDivTwo};
309 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
310 \draw (5,-0.75) node[anchor=north, color=white] {Substract};
311
312 \else
313
314 % Multiply mode
315 \ifnum\mathMode=3
316 \addplot[color=thirdcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000) ,samples=\sampleRate, \smoothThree]{(\funcOne)/\voltageDivOne * (\funcTwo)/\voltageDivTwo};
317 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
318 \draw (5,-0.75) node[anchor=north, color=white] {Multiply};
319
320 \else
321
322 % Divide mode
323 \ifnum\mathMode=4
324 \addplot[color=thirdcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000), samples=\sampleRate, \smoothThree]{(\funcOne)/\voltageDivOne / (\funcTwo)/\voltageDivTwo};
325 \draw[thirdcolor,fill=thirdcolor] (4,-0.78) rectangle (6,-1.28);
326 \draw (5,-0.75) node[anchor=north, color=white] {Divide};
327 \fi
328 \fi
329 \fi
330 \fi
331 \fi
332
333 \end{axis}
334
335 \ifnum\xyMode=1
336 \else
337 % Show the channel indicators
338 \ifnum\indicators=1
339 % Draw the first channel's screen marker
340 \draw[firstcolor,fill=firstcolor] (0.4, \screenOffsetOne+4) -- (0, \screenOffsetOne+4+0.3) -- (0, \screenOffsetOne+4-0.3) -- cycle;
341 \draw (-0.04, \screenOffsetOne+4) node[anchor=west, color=colorTextOne,font=\fontsize{8}{10}\selectfont] {1};
342
343
344 % Draw the second channel's screen marker
345 \ifnum\secondChannel=1
346 \draw[secondcolor,fill=secondcolor] (0.4, \screenOffsetTwo+4) -- (0, \screenOffsetTwo+4+0.3) -- (0, \screenOffsetTwo+4-0.3) -- cycle;
347 \draw (-0.04, \screenOffsetTwo+4) node[anchor=west, color=colorTextTwo,font=\fontsize{8}{10}\selectfont] {2};
348 \fi
349 \fi
350
351
352 % Draw the vertical cursors
353
354 % Define the false value
355 \def\false{false}
356
357 % Set the value of \verticalCursorOne
358 \pgfmathsetmacro{\verticalCursorOneValue}{\verticalCursorOne*\timeDiv}
359
360
361 % If the first vertical cursor is enabled
362 \ifx\verticalCursorOne\false
363 \else
364 % Draw the first vertical cursor with \verticalCursorOne value
365 \draw[verticalCursorColor,very thick,dashed] (\verticalCursorOne+5,0) -- (\verticalCursorOne+5,8);
366 \draw[verticalCursorColor,very thick,fill=verticalCursorColor] (\verticalCursorOne+5-0.25,7.5) rectangle (\verticalCursorOne+5+0.25,8);
367
368 % Draw the indicator
369 \draw[white,very thick] (\verticalCursorOne+5,7.45) node[anchor=south, color=white,font=\fontsize{10}{12}\selectfont] {A};
370
371 % Draw the legend
372 \draw[verticalCursorColor,very thick] (0.25,0.5) node[anchor=west, color=verticalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurA \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\verticalCursorOneValue}\ ms}};
373
374 % If the second vertical cursor is enabled
375 \ifx\verticalCursorTwo\false
376 \else
377 % Draw the time difference between the two vertical cursors
378 \pgfmathsetmacro{\verticalCursorDiff}{\verticalCursorTwo-\verticalCursorOne}
379 \pgfmathsetmacro{\verticalCursorDiffValue}{\verticalCursorDiff*\timeDiv}
380 \draw[verticalCursorColor,very thick] (0.25,1.5) node[anchor=west, color=verticalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurB - CurA \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\verticalCursorDiffValue}\ ms}};
381 \fi
382
383 \fi
384
385 % Set the value of \verticalCursorTwo
386 \pgfmathsetmacro{\verticalCursorTwoValue}{\verticalCursorTwo*\timeDiv}
387
388 % If the second vertical cursor is enabled
389 \ifx\verticalCursorTwo\false
390 \else
391 % Draw the second vertical cursor with \verticalCursorTwo value
392 \draw[verticalCursorColor,very thick,dashed] (\verticalCursorTwo+5,0) -- (\verticalCursorTwo+5,8);
393 \draw[verticalCursorColor,very thick,fill=verticalCursorColor] (\verticalCursorTwo+5-0.25,7.5) rectangle (\verticalCursorTwo+5+0.25,8);
394
395 % Draw the indicator
396 \draw[white,very thick] (\verticalCursorTwo+5,7.45) node[anchor=south, color=white,font=\fontsize{10}{12}\selectfont] {B};
397
398 % Draw the legend
399 \draw[verticalCursorColor,very thick] (0.25,1) node[anchor=west, color=verticalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurB \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\verticalCursorTwoValue}\ ms}};
400
401 \fi
402
403
404 % Draw the horizontal cursors
405
406 % Set the value of \horizontalCursorOne
407 \pgfmathsetmacro{\horizontalCursorOneValue}{\horizontalCursorOne*\voltageDivOne}
408
409 % If the first horizontal cursor is enabled
410 \ifx\horizontalCursorOne\false
411 \else
412 % Draw the first horizontal cursor with \horizontalCursorOne value
413 \draw[horizontalCursorColor,very thick,dashed] (0,\horizontalCursorOne+4) -- (10,\horizontalCursorOne+4);
414 \draw[horizontalCursorColor,very thick,fill=horizontalCursorColor] (9.5,\horizontalCursorOne+4-0.25) rectangle (10.5,\horizontalCursorOne+4+0.25);
415
416 % Draw the indicator
417 \draw[white,very thick] (10,\horizontalCursorOne+4) node[anchor=east, color=white,font=\fontsize{10}{12}\selectfont] {A};
418
419 % Draw the legend
420 \draw[horizontalCursorColor,very thick] (5.25,0.5) node[anchor=west, color=horizontalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurA \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\horizontalCursorOneValue}\ V}};
421
422 % If the second horizontal cursor is enabled
423 \ifx\horizontalCursorTwo\false
424 \else
425 % Draw the voltage difference between the two horizontal cursors
426 \pgfmathsetmacro{\horizontalCursorDiff}{\horizontalCursorTwo-\horizontalCursorOne}
427 \pgfmathsetmacro{\horizontalCursorDiffValue}{\horizontalCursorDiff*\voltageDivOne}
428 \draw[horizontalCursorColor,very thick] (5.25,1.5) node[anchor=west, color=horizontalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurB - CurA \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\horizontalCursorDiffValue}\ V}};
429 \fi
430 \fi
431
432 % Set the value of \horizontalCursorTwo
433 \pgfmathsetmacro{\horizontalCursorTwoValue}{\horizontalCursorTwo*\voltageDivOne}
434
435 % If the second horizontal cursor is enabled
436 \ifx\horizontalCursorTwo\false
437 \else
438 % Draw the second horizontal cursor with \horizontalCursorTwo value
439 \draw[horizontalCursorColor,very thick,dashed] (0,\horizontalCursorTwo+4) -- (10,\horizontalCursorTwo+4);
440 \draw[horizontalCursorColor,very thick,fill=horizontalCursorColor] (9.5,\horizontalCursorTwo+4-0.25) rectangle (10.5,\horizontalCursorTwo+4+0.25);
441
442 % Draw the indicator
443 \draw[white,very thick] (10,\horizontalCursorTwo+4) node[anchor=east, color=white,font=\fontsize{10}{12}\selectfont] {B};
444
445 % Draw the legend
446 \draw[horizontalCursorColor,very thick] (5.25,1) node[anchor=west, color=horizontalCursorColor,font=\fontsize{10}{12}\selectfont] {\raisedtext{CurB \pgfmathprintnumber[precision=\cursorPrecision, assume math mode=true,fixed zerofill]{\horizontalCursorTwoValue}\ V}};
447
448 \fi
449
450 % End xyMode test
451 \fi
452
453 \end{scope}
454
455 \end{tikzpicture}
456 }
457 }
458
459
460