summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikz-osci/tikz-osci.sty
blob: d2d59cd525571b73d990e00cc61b654778e7fcb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
% -----------------------------------------------------------
% This is TikZ Osci package
%
% It provides some macros to draw oscilloscope screen captures 
% from math functions provided by the user.
%
% It is based on TikZ and pgfplots and is a replacement can-
% didate to the pstricks pst-osci package which is not main-
% tained anymore.
%
% Author: Thibault Giauffret
% Date: 2013/10/06
% Version: 0.1.0
% Licence : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
% Not an expert... Just doing it for fun.
% -----------------------------------------------------------

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tikz-osci}[2023/09/30 0.1.0 Tikz Osci package]

% Required packages
\RequirePackage[dvipsnames]{xcolor}
\RequirePackage{pgfkeys}
\RequirePackage{pgfplots}
\RequirePackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\usetikzlibrary{decorations.markings,decorations.pathmorphing,
  decorations.pathreplacing}
\usetikzlibrary{calc,patterns,shapes.geometric}


% Init the keys and get the values
\pgfkeys{
  /osci/.cd,
  scale/.store in=\scale,
  scale=1,
  second channel/.store in=\secondChannel,
  second channel=0,
  screen offset one/.store in=\screenOffsetOne,
  screen offset one=0,
  screen offset two/.store in=\screenOffsetTwo,
  screen offset two=-0,
  time div/.store in=\timeDiv,
  time div=20,
  voltage div one/.store in=\voltageDivOne,
  voltage div one=1,
  voltage div two/.store in=\voltageDivTwo,
  voltage div two=1,
  sample rate/.store in=\sampleRate,
  sample rate=200,
  xy mode/.store in=\xyMode,
  xy mode=0,
  expr one/.store in=\exprOne,
  expr one=2*sin(2*180/0.020*x),
  expr two/.store in=\exprTwo,
  expr two=0.6*sin(2*180/0.020*x)+0.2*sin(2*180/0.040*x),
  color one/.store in=\colorOne,
  color one=D62626,
  color two/.store in=\colorTwo,
  color two=1053AF,
  color xy/.store in=\colorXY,
  color xy=2E8B73,
  graph back color/.store in=\graphBackColor,
  graph back color=FFFFFF,
  info back color/.store in=\infoBackColor,
  info back color=D6D6D6,
  info text color/.store in=\infoTextColor,
  info text color=000000,
  main axis color/.store in=\mainAxisColor,
  main axis color=000000,
  sub axis color/.store in=\subAxisColor,
  sub axis color=AAAAAA,
}

% The main command
\newcommand\osci[1][]{

  % Get the values
  \pgfkeys{
    /osci/.cd,
    #1,
  }

  \scalebox{\scale}{
    % Begin the picture
    \begin{tikzpicture}

      % Set the colors
      \definecolor{firstcolor}{HTML}{\colorOne}
      \definecolor{secondcolor}{HTML}{\colorTwo}
      \definecolor{thirdcolor}{HTML}{\colorXY}
      \definecolor{graphbackcolor}{HTML}{\graphBackColor}
      \definecolor{infoBackColor}{HTML}{\infoBackColor}
      \definecolor{infoTextColor}{HTML}{\infoTextColor}
      \definecolor{mainAxisColor}{HTML}{\mainAxisColor}
      \definecolor{subAxisColor}{HTML}{\subAxisColor}

      % Draw the info box
      \draw[black,thick, fill= infoBackColor]
      (0,0) --
      ++(10,0) {[rounded corners=10] --
        ++(0,-1.5) --
        ++(-10,0)} --
      cycle
        {};

      % Add time/div label
      \draw (5,-0.15) node[anchor=north, color= infoTextColor] {\timeDiv\ ms/div};

      % Add voltage/div label for the first channel
      \draw[firstcolor,fill=firstcolor] (0.5,-0.15) rectangle (1.5,-0.6);
      \draw (1,-0.1) node[anchor=north, color=white] {CH1};
      \draw (1,-0.75) node[anchor=north, color= infoTextColor] {\voltageDivOne\ V/div};

      % Add voltage/div label for the second channel
      \ifnum\secondChannel=1
        \draw[secondcolor,fill=secondcolor] (8.5,-0.15) rectangle (9.5,-0.6);
        \draw (9,-0.1) node[anchor=north, color=white] {CH2};
        \draw (9,-0.75) node[anchor=north, color= infoTextColor] {\voltageDivTwo\ V/div};
      \fi

      % Add the XY mode label
      \ifnum\xyMode=1
        % \draw (5,-0.75) node[anchor=north, color=thirdcolor] {XY mode};
        % Coming soon...
      \fi

      % Draw the oscilloscope screen border
      \draw[black,thick, fill= graphbackcolor]
      (0,0) --
      ++(10,0) {[rounded corners=10] --
        ++(0,8) --
        ++(-10,0)} --
      cycle
        {};

      % Draw the oscilloscope screen graduations
      \begin{scope}

        % Remove the border
        \clip (0.05,0.05) rectangle (9.95,7.95);

        \begin{axis}[xmin=-5*(\timeDiv/1000), xmax=5*(\timeDiv/1000), ymin=-4, ymax=4, xtick distance = \timeDiv/1000,
            axis x line=middle, axis y line=middle,
            axis line style={-},
            % Draw major grid
            grid=both,
            xticklabels={,,},
            yticklabels={,,},
            % Add sub ticks for the x axis
            minor tick num=4,
            % Remove minor grid
            minor grid style={line width=0pt, draw=white, draw opacity=0},
            major grid style={thick,dashed,draw=subAxisColor},
            every major tick/.append style={very thick, major tick length=6pt, color=mainAxisColor},
            every minor tick/.append style={thick, minor tick length=4pt, color=mainAxisColor},
          ]

          % Change plot width
          \pgfplotsset{width=11.58cm, height=9.58cm}
          % Change tick label style
          \pgfplotsset{ every non boxed x axis/.append style={x axis line style=-},
            every non boxed y axis/.append style={y axis line style=-}}

          \ifnum\xyMode=1

            % Coming soon...

          \else

            % Draw each channel separately
            % First channel plot
            \addplot[color=firstcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000),
              smooth, samples=\sampleRate]{(\exprOne)/\voltageDivOne+\screenOffsetOne};

            % If the second channel is enabled, draw it
            \ifnum\secondChannel=1
              % Draw the second channel
              \addplot[color=secondcolor,very thick,domain=-5*(\timeDiv/1000):5*(\timeDiv/1000), smooth, samples=\sampleRate]{(\exprTwo)/\voltageDivTwo+\screenOffsetTwo};
            \fi
          \fi

        \end{axis}

        \ifnum\xyMode=0
          % Draw the first channel's screen marker
          \draw[firstcolor,fill=firstcolor] (0.4, \screenOffsetOne+4) -- (0, \screenOffsetOne+4+0.3) -- (0, \screenOffsetOne+4-0.3) -- cycle;
          \draw (-0.04, \screenOffsetOne+4) node[anchor=west, color=white,font=\fontsize{8}{10}\selectfont] {1};

          % Draw the second channel's screen marker
          \ifnum\secondChannel=1
            \draw[secondcolor,fill=secondcolor] (0.4, \screenOffsetTwo+4) -- (0, \screenOffsetTwo+4+0.3) -- (0, \screenOffsetTwo+4-0.3) -- cycle;
            \draw (-0.04, \screenOffsetTwo+4) node[anchor=west, color=white,font=\fontsize{8}{10}\selectfont] {2};
          \fi
        \fi

      \end{scope}

    \end{tikzpicture}
  }
}