-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtikz-timing-counters.sty
329 lines (328 loc) · 10.8 KB
/
tikz-timing-counters.sty
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
% \begin{macrocode}
%<!COPYRIGHT>
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{tikz-timing-counters}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
2099/01/01 develop
%</DRIVER>
tikz-timing library for counter values]
% \end{macrocode}
%
%
% \begin{macrocode}
\newif\if@tikztiming@counter@wraps
\tikzset{%
timing/new counter/.style={timing/counter/new={#1}},
timing/counter/.cd,
new/.code={\tikztiming@counter@new{#1}},
new/.value required,
opt/.cd,
char/.initial=,
char/.value required,
reset char/.initial=,
reset char/.value required,
reset type/.is choice,
reset type/width/.code={\def\tikztiming@resettype{0}},
reset type/arg/.code={\def\tikztiming@resettype{1}},
reset type/both/.code={\def\tikztiming@resettype{2}},
reset type/Both/.code={\def\tikztiming@resettype{3}},
half width char/.initial=,
half width char/.value required,
base/.initial=10,
base/.value required,
increment/.initial=1,
increment/.value required,
max value/.initial=,
max value/.value required,
min value/.initial=,
min value/.value required,
wraps/.is if=@tikztiming@counter@wraps,
digits/.initial=,
digits/.value required,
fg style/.initial=,
bg style/.initial=,
text style/.initial=,
text format/.initial=,
fg style/.value required,
bg style/.value required,
text style/.value required,
text format/.value required,
}
\def\tikztiming@counter@textformat{}%
% \end{macrocode}
%
% \begin{macro}{\tikztimingcounter}[1]{Counter Character}
% \begin{macrocode}
\def\tikztimingcounter#1{%
\csname tikztiming@counter@#1\endcsname
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztimingsetcounter}[2]{Counter Character}{pgfmath expression}
% \begin{macrocode}
\def\tikztimingsetcounter#1#2{%
\begingroup
\pgfmathtruncatemacro\@tempa{#2}%
\expandafter\global\expandafter\let\csname tikztiming@counter@#1\endcsname\@tempa
\endgroup
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@new}[1]{Styles}
% Most parts of this macro is enclosed in a group to keep most changes locally.
% \begin{macrocode}
\def\tikztiming@counter@new#1{%
\begingroup
\def\tikztiming@resettype{0}%
% \end{macrocode}
% The argument is taken as pgf keys relative to the "/tikz/timing/counter/opt" path.
% The counter character is mandatory and an error is raised if it is not defined.
% \begin{macrocode}
\pgfqkeys{/tikz/timing/counter/opt}{#1}%
\tikzset{timing/counter/opt/char/.get=\counterchar}%
\ifx\counterchar\empty
\PackageError{tikz-timing-counters}{Counter character must be given using
the 'char' key!}{}%
\endgroup
\else
% \end{macrocode}
% The counter boundary test code is now assembled in |\@tempb|.
% If the "wraps" option was used the \cs{tikztiming@counter@wrap} macro is used with the
% "min value" and "max value" as (expanded) arguments.
% \begin{macrocode}
\pgfkeys{/tikz/timing/counter/opt/min value/.get=\min}%
\pgfkeys{/tikz/timing/counter/opt/max value/.get=\max}%
\if@tikztiming@counter@wraps
\ifx\min\empty
\def\min{0}%
\fi
\ifx\max\empty
\PackageError{tikz-timing-counters}{The option 'wraps' needs the option 'max value' to be set!}{}%
\fi
\def\@tempb{%
\noexpand\tikztiming@counter@wrap%
{\expandafter\unexpanded\expandafter{\min}}%
{\expandafter\unexpanded\expandafter{\max}}%
}%
\else
% \end{macrocode}
% Otherwise either the \cs{tikztiming@counter@maxsat}, \cs{tikztiming@counter@minsat} or \cs{tikztiming@counter@minmaxsat}
% macro is used if "max value", "min value" or both are set, respectively.
% \begin{macrocode}
\ifx\min\empty
\ifx\max\empty
\let\@tempb\empty
\else
\def\@tempb{%
\noexpand\tikztiming@counter@maxsat%
{\expandafter\unexpanded\expandafter{\max}}%
}%
\fi
\else
\ifx\max\empty
\def\@tempb{%
\noexpand\tikztiming@counter@minsat%
{\expandafter\unexpanded\expandafter{\min}}%
}%
\else
\def\@tempb{%
\noexpand\tikztiming@counter@minmaxsat%
{\expandafter\unexpanded\expandafter{\min}}%
{\expandafter\unexpanded\expandafter{\max}}%
}%
\fi
\fi
\fi
% \end{macrocode}
% The counter styles are set up.
% \begin{macrocode}
\pgfkeys{/tikz/timing/counter/opt/fg style/.get=\fgstyle}%
\pgfkeys{/tikz/timing/counter/opt/bg style/.get=\bgstyle}%
\pgfkeys{/tikz/timing/counter/opt/text style/.get=\textstyle}%
\pgfkeys{/tikz/timing/counter/opt/text format/.get=\textformat}%
% \end{macrocode}
% Finally the \cs{tikztiming@counter@new@} macro is called with most of the
% settings as expanded arguments.
% \begin{macrocode}
\pgfkeys{/tikz/timing/counter/opt/increment/.get=\increment}%
\edef\@tempa{%
\noexpand\tikztiming@counter@new@
{\pgfkeysvalueof{/tikz/timing/counter/opt/char}}%
{\pgfkeysvalueof{/tikz/timing/counter/opt/base}}%
{\pgfkeysvalueof{/tikz/timing/counter/opt/half width char}}%
{\pgfkeysvalueof{/tikz/timing/counter/opt/reset char}}%
{\pgfkeysvalueof{/tikz/timing/counter/opt/digits}}%
{\expandafter\unexpanded\expandafter{\increment}}%
{\@tempb}%
{\min}%
{\tikztiming@resettype}%
{\expandafter\unexpanded\expandafter{\fgstyle}}%
{\expandafter\unexpanded\expandafter{\bgstyle}}%
{\expandafter\unexpanded\expandafter{\textstyle}}%
{\expandafter\unexpanded\expandafter{\textformat}}%
}%
\expandafter\expandafter\expandafter\endgroup
\expandafter\@tempa
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@new@}[9]{Character}{Base}{Half Width
% Character}{Reset Character}{Number of Digits}{Increment}{Boundary Code}{Min Value}{Reset Type}
% \begin{macrocode}
\def\tikztiming@counter@new@#1#2#3#4#5#6#7#8#9{%
\ifx\empty#8\empty
\@namedef{tikztiming@counter@#1}{0}%
\else
\expandafter
\pgfmathtruncatemacro\csname tikztiming@counter@#1\endcsname{#8}%
\fi
\tikztimingmetachar{#1}[1]{ { [timing/counter/#1] ##1D{[timing/counter/#1/text]\tikztiming@counter@vs{#1}{#2}{#5}{#6}{#7}} } }%
\ifx\empty#3\empty
\tikztiming@iflower{#1}{}%
{\lowercase{\tikztimingmetachar{#1}}[1]%
{ { [timing/counter/#1] ##1d{[timing/counter/#1/text]\tikztiming@counter@vs{#1}{#2}{#5}{#6}{#7}}} }%
}%
\else
\tikztimingmetachar{#3}[1]{%
{ [timing/counter/#1] ##1d{[timing/counter/#1/text]\tikztiming@counter@vs{#1}{#2}{#5}{#6}{#7}} }%
}%
\fi
\ifx\empty#4\empty\else
\ifcase0#9\relax
\tikztimingmetachar{#4}[1]{[|/utils/exec=\tikztiming@counter@reset{#1}{##1}|]}%
\or
\tikztimingmetachar{#4}[2]{[|/utils/exec=\tikztimingsetcounter{#1}{##2}|]}%
\or
\uppercase{\tikztimingmetachar{#4}}[1]{[|/utils/exec=\tikztiming@counter@reset{#1}{##1}|]}%
\lowercase{\tikztimingmetachar{#4}}[2]{[|/utils/exec=\tikztimingsetcounter{#1}{##2}|]}%
\or
\lowercase{\tikztimingmetachar{#4}}[1]{[|/utils/exec=\tikztiming@counter@reset{#1}{##1}|]}%
\uppercase{\tikztimingmetachar{#4}}[2]{[|/utils/exec=\tikztimingsetcounter{#1}{##2}|]}%
\fi
\fi
\tikztiming@counter@new@@{#1}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@new@@}[5]{Character}{Foreground Style}{Background Style}{Text Style}
% {Text format}
% The counter styles are set up.
% \begin{macrocode}
\def\tikztiming@counter@new@@#1#2#3#4#5{%
\tikzset{timing/counter/#1/.style={}}%
\ifx\empty#2\empty\else
\tikzset{timing/counter/#1/.append style={%
/tikz/timing/d/.style={#2}%
}}%
\fi
\ifx\empty#3\empty\else
\tikzset{timing/counter/#1/.append style={%
/tikz/timing/d/background/.style={#3}%
}}%
\fi
\tikzset{timing/counter/#1/text/.style={#4}}%
\ifx\empty#5\empty\else
\tikzset{timing/counter/#1/.append style={%
/utils/exec={\def\tikztiming@counter@textformat{#5}}%
}}%
\fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@reset}[2]{Character to Reset}{Reset value}
% The counter is set first using pgf to round the value correctly and then again
% with \cs{setcounter} to make it global.
% \begin{macrocode}
\def\tikztiming@counter@reset#1#2{%
\begingroup
\pgfmathtruncatemacro\@tempa{round((#2)/\tikztiming@wscale)}%
\expandafter\global\expandafter\let\csname tikztiming@counter@#1\endcsname\@tempa
\endgroup
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@vs}[5]{Character}{Base}{Number of Digits}{Increment}{Boundary Code}
% Note: \cs{pgfmathdectoBase} is only used for a base different to 10 to allow
% negative number for decimal base.
% \begin{macrocode}
\def\tikztiming@counter@vs#1#2#3#4#5{%
\begingroup
\ifx\empty#3\empty\else
\pgfmathsetbasenumberlength{#3}%
\fi
\ifnum#2=10
\expandafter\expandafter\expandafter\tikztiming@counter@textformat
\expandafter\expandafter\expandafter{\csname tikztiming@counter@#1\endcsname}%
\else
\pgfmathdectoBase\pgfmathresult{\@nameuse{tikztiming@counter@#1}}{#2}%
\expandafter\tikztiming@counter@textformat\expandafter{\pgfmathresult}%
\fi
\edef\N{\@nameuse{tikztiming@counter@#1}}%
\pgfmathtruncatemacro\N{\N + #4}%
#5%
\expandafter\global\expandafter\let\csname tikztiming@counter@#1\endcsname\N
\endgroup
}
% \end{macrocode}
% \end{macro}
%
% The following macros checks the boundaries of the counter value. The value is stored in the
% macro \cs{N}, the boundaries are provided as arguments. If necessary the value can be
% adjusted by changing \cs{N}. All values are taken as \emph{integers}.
%
% \begin{macro}{\tikztiming@counter@minsat}[1]{Minimum Value}
% \begin{macrocode}
\def\tikztiming@counter@minsat#1{%
\pgfmathtruncatemacro\MIN{#1}%
\ifnum\N<\MIN \let\N\MIN \fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@maxsat}[1]{Maximum Value}
% \begin{macrocode}
\def\tikztiming@counter@maxsat#1{%
\pgfmathtruncatemacro\MAX{#1}%
\ifnum\N>\MAX \let\N\MAX \fi
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@minmaxsat}[2]{Minimum Value}{Maximum Value}
% \begin{macrocode}
\def\tikztiming@counter@minmaxsat#1#2{%
\tikztiming@counter@minsat{#1}%
\tikztiming@counter@maxsat{#2}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tikztiming@counter@wrap}[2]{Minimum Value}{Maximum Value}
% \begin{macrocode}
\def\tikztiming@counter@wrap#1#2{%
\pgfmathtruncatemacro\MIN{#1}%
\pgfmathtruncatemacro\MAX{#2}%
\ifnum\N>\MAX
\pgfmathtruncatemacro\N{\MIN + (\N-\MAX-1)}%
\fi
\ifnum\N<\MIN
\pgfmathtruncatemacro\N{\MAX - (\MIN-\N+1)}%
\fi
}
% \end{macrocode}
% \end{macro}
%
% Use the \pkg{pgfopts} to handle the package options.
% \begin{macrocode}
\RequirePackage{pgfopts}
\ProcessPgfOptions{/tikz/timing/counter}
% \end{macrocode}