File: electronics.gle

package info (click to toggle)
gle-graphics-library 4.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,164 kB
  • sloc: python: 183; makefile: 2
file content (1340 lines) | stat: -rw-r--r-- 19,223 bytes parent folder | download | duplicates (8)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
! Electronic Circuit diagrams with GLE
! Author: A. S. Budden <abudden@NOSPAMgataki.co.uk>

! Date:	 17th August 2007
! Version: 1.3

! Copyright:	 Copyright (C) 2005 A. S. Budden
!					 Permission is hereby granted to use and distribute this code,
!					 with or without modifications, provided that this copyright
!					 notice is copied with it. Like anything else that's free,
!					 electronics.gle is provided *as is* and comes with no warranty
!					 of any kind, either expressed or implied. By using this
!					 plugin, you agree that in no event will the copyright
!					 holder be liable for any damages resulting from the use
!					 of this software.

! Documentation ** gle_electronics.pdf **

! A simple function to draw a grid for reference
sub drawgrid gridscale
	default gridscale 1
	gsave
	set lwidth 0.005
	set lstyle 2
	for i = 0 to pageheight()/gridscale
		amove 0 i
		rline pagewidth()/gridscale 0
	next i

	for i = 0 to pagewidth()/gridscale
		amove i 0
		rline 0 pageheight()/gridscale
	next i

	grestore
end sub

! Connection point
sub connection
	circle 0.1 fill black
end sub

!===================================
! Resistors
!===================================

! Resistor base device
sub resistor
	gsave
	rline 1 0
	rline 0 0.5
	rline 2 0
	rline 0 -1
	rline -2 0
	rline 0 0.5
	rmove 2 0
	rline 1 0
	grestore
end sub

! Resistor base device
sub rresistor
	gsave
	rline 1 0
	rline 0.125 0.5
	rline 0.25 -1.0
	rline 0.25 1.0
	rline 0.25 -1.0
	rline 0.25 1.0
	rline 0.25 -1.0
	rline 0.25 1.0
	rline 0.25 -1.0
	rline 0.125 0.5
	rline 1 0
	grestore
end sub


! Horizontal resistor
sub rresistor_h rlabel$
	gsave
	@rresistor
	rmove 2 0.75
	set just BC
	set hei 0.8
	write rlabel$
	grestore
	rmove 4 0
end sub

! Vertical resistor
sub rresistor_v rlabel$
	gsave
	begin rotate -90
		@rresistor
	end rotate
	rmove 0.75 -2
	set just LC
	set hei 0.8
	write rlabel$
	grestore
	rmove 0 -4
end sub


! Horizontal resistor
sub resistor_h rlabel$
	gsave
	@resistor
	rmove 2 0.75
	set just BC
	set hei 0.8
	write rlabel$
	grestore
	rmove 4 0
end sub

! Vertical resistor
sub resistor_v rlabel$
	gsave
	begin rotate -90
		@resistor
	end rotate
	rmove 0.75 -2
	set just LC
	set hei 0.8
	write rlabel$
	grestore
	rmove 0 -4
end sub

! Horizontal potentiometer
sub potentiometer_hb rlabel$
	gsave
	@resistor
	rmove 2 -2
	rline 0 1.5 arrow end
	rmove 0 1.5
	set just BC
	set hei 0.8
	write rlabel$
	grestore
	rmove 4 0
end sub

! Vertical potentiometer
sub potentiometer_vr rlabel$
	gsave
	begin rotate -90
		@resistor
	end rotate
	rmove 2 -2
	rline -1.5 0 arrow end
	rmove -1.5 0
	set just RC
	set hei 0.8
	write rlabel$
	grestore
	rmove 0 -4
end sub

! Horizontal variable resistor
sub vresistor_h rlabel$
	gsave
	@resistor
	rmove 1 -1
	rline 2 2 arrow end
	rmove -1 0
	set just BC
	set hei 0.8
	write rlabel$
	grestore
	rmove 4 0
end sub

! Vertical Variable resistor
sub vresistor_v rlabel$
	gsave
	begin rotate -90
		@resistor
	end rotate
	rmove -1 -3
	rline 2 2 arrow end
	rmove 0 -1
	set just LC
	set hei 0.8
	write rlabel$
	grestore
	rmove 0 -4
end sub

!===================================
! Transistors
!===================================

! BJT Base
sub bjt leg direction
	gsave
	rline 1 0
	rmove 0 1
	rline 0 -2
	rmove 0 0.5
	if leg = 1 then
		if direction = 1 then
			rline 1 -0.5 arrow end
		else
			rline 1 -0.5 arrow start
		end if
		rmove -1 1.5
		rline 1 0.5
	else
		rline 1 -0.5
		rmove -1 1.5
		if direction = 1 then
			rline 1 0.5 arrow end
		else
			rline 1 0.5 arrow start
		end if
	end if
	grestore
end sub

! NPN BJT
sub npn_bjt bjtlabel$
	gsave
	bjt 1 1
	rmove 2 0
	set just LC
	set hei 0.8
	write bjtlabel$
	grestore
end sub

! PNP BJT
sub pnp_bjt bjtlabel$
	gsave
	bjt 0 0
	rmove 2 0
	set just LC
	set hei 0.8
	write bjtlabel$
	grestore
end sub

! Base MOSFET
sub mosfet channel
	! 0 is N channel, 1 is P channel
	gsave
	rline 1 0
	rmove 0 1
	rline 0 -2
	rmove 0.25 0
	rline 0 2/4
	rmove 0 1/4
	rline 0 2/4
	rmove 0 1/4
	rline 0 2/4
	rmove 0 -1/4
	rline 0.75 0
	rline 0 1/4
	rmove 0 -2
	rline 0 1/4
	rline -0.75 0
	rmove 0.75 0
	@connection
	rline 0 3/4
	if channel = 0 then
		rline -0.75 0 arrow end
	else
		rline -0.75 0 arrow start
	end if
	grestore
end sub

! N-Channel MOSFET
sub n_mosfet mlabel$
	gsave
	mosfet 0
	rmove 2.5 0
	set hei 0.8
	set just LC
	write mlabel$
	grestore
end sub

! P-Channel MOSFET
sub p_mosfet mlabel$
	gsave
	mosfet 1
	rmove 2.5 0
	set hei 0.8
	set just LC
	write mlabel$
	grestore
end sub

! IGBT
sub igbt igbtlabel$
	gsave
	rline 0.75 0
	rline 0 0.75
	rmove 0 -0.75
	rline 0 -0.75
	rmove 0.25 0.75
	rline 0 1
	rmove 0 -1
	rline 0 -1
	rmove 0 0.5
	rline 1 -0.5 arrow end
	rmove -1 1.5
	rline 1 0.5
	rmove 0 -1
	set hei 0.8
	set just LC
	write igbtlabel$
	grestore
end sub

!===================================
! Inductors
!===================================

! Single coil
sub coil
	gsave
	rmove 0.25 0
	arc 0.25 0 180
	grestore
	rmove 0.5 0
end sub

! Single double size coil
sub large_coil dot
	gsave
	rmove 0.5 0
	arc 0.5 0 180
	if dot = 1 then
		circle 0.1 fill black
	end if
	grestore
	rmove 1 0
end sub

! Base inductor
sub inductor core
	gsave
	rline 1 0
	for i = 1 to 4
		@coil
	next i
	rline 1 0
	if core = 1 then
		rmove -1 0.5
		rline -2 0
		rmove 0 0.2
		rline 2 0
	end if
	grestore
end sub

sub large_inductor dot_position
	gsave
	if dot_position = 1 then ! Dot at start
		@large_coil 1
	else
		@large_coil 0
	end if
	@large_coil 0
	@large_coil 0
	if dot_position = 2 then ! Dot at end
		@large_coil 1
	else
		@large_coil 0
	end if
	grestore
end sub

! Horizontal Inductor
sub inductor_h core ilabel$
	gsave
	@inductor core
	if core = 1 then
		rmove 2 1
	else
		rmove 2 0.5
	end if
	set hei 0.8
	set just BC
	write ilabel$
	grestore
	rmove 4 0
end sub

! Vertical Inductor
sub inductor_v core ilabel$
	gsave
	begin rotate -90
		@inductor core
	end rotate
	if core = 1 then
		rmove 1 -2
	else
		rmove 0.5 -2
	end if
	set hei 0.8
	set just LC
	write ilabel$
	grestore
	rmove 0 -4
end sub

! Transformer Base
sub xformer core type n1$ n2$
	gsave
	! Select type
	! 0: no dots
	! 1: no invert
	! 2: invert
	if type = 0 then
		leftdot = 0
		rightdot = 0
	end if
	if type = 1 then
		leftdot = 1
		rightdot = 2
	end if
	if type = 2 then
		leftdot = 1
		rightdot = 1
	end if
	begin rotate -90
		@large_inductor leftdot
	end rotate
	rmove 2 -4
	begin rotate 90
		@large_inductor rightdot
	end rotate
	if core = 1 then
		rmove -1.2 0
		rline 0 4
		rmove 0.4 0
		rline 0 -4
	end if
	grestore

	! Number of turns
	gsave
	rmove 0.25 0.25
!	rmove 0.5 0.5
	set just BC
	set hei 0.5
	write n1$
	rmove 1.5 0
	write n2$
	grestore
end sub

! Transformer with a double secondary
sub xformer_dblsecondary type location
	gsave
	! Select type (as xformer)
	if type = 0 then
		leftdot = 0
		rightdot = 0
	end if
	if type = 1 then
		leftdot = 1
		rightdot = 2
	end if
	if type = 2 then
		leftdot = 1
		rightdot = 1
	end if
	begin rotate -90
		@large_inductor leftdot
	end rotate

	! If location = 0, then balance
	! If location = 1, then draw above
	! If location = 2, then draw below
	if location = 0 then
		rmove 2 -1
	end if
	if location = 1 then
		rmove 2 2
	end if
	if location = 2 then
		rmove 2 -4
	end if

	! We are now at the bottom of the top coil
	begin rotate 90
		@large_inductor rightdot
	end rotate
	rmove 0 -6
	begin rotate 90
		@large_inductor rightdot
	end rotate
	rmove -1.2 0
	rline 0 10
	rmove 0.4 0
	rline 0 -10
	grestore
end sub




!===================================
! Capacitors
!===================================

! Base capacitor
sub capacitor
	gsave
	rline 1.75 0
	rline 0 1
	rmove 0 -2
	rline 0 1
	rmove 0.5 0
	rline 0 1
	rmove 0 -2
	rline 0 1
	rline 1.75 0
	grestore
end sub

! Base polarised capacitor
sub p_capacitor polarity
	! Polarity gives location of + side
	! polarity = 0: on left (and therefore top)
	! polarity = 1: on right
	gsave
	rline 1.5+1/8 0
	rmove 0 -1
	if polarity = 1 then
		box 0.25 2 fill black
	else
		box 0.25 2
	end if
	rmove 0.5 0
	if polarity = 1 then
		box 0.25 2
	else
		box 0.25 2 fill black
	end if
	rmove 0.25 1
	rline 1.5+1/8 0
	grestore
end sub

! Horizontal L Polarised Capacitor
sub p_capacitor_hl clabel$
	gsave
	@p_capacitor 0
	rmove 1 0.75
	rline 0.5 0
	rmove -0.25 -0.25
	rline 0 0.5
	rmove -1.25 -1

	rmove 2 1.25
	set hei 0.8
	set just BC
	write clabel$
	grestore
	rmove 4 0
end sub

! Horizontal R Polarised Capacitor
sub p_capacitor_hr clabel$
	gsave
	@p_capacitor 1
	rmove 2.75 0.75
	rline 0.5 0
	rmove -0.25 -0.25
	rline 0 0.5
	rmove -3 -1

	rmove 2 1.25
	set hei 0.8
	set just BC
	write clabel$
	grestore
	rmove 4 0
end sub

! Vertical Top Polarised Capacitor
sub p_capacitor_vt clabel$
	gsave
	begin rotate -90
		@p_capacitor 0
	end rotate
	rmove -1 -1.25
	rline 0.5 0
	rmove -0.25 -0.25
	rline 0 0.5
	rmove 0.75 1

	rmove 1.25 -2
	set hei 0.8
	set just LC
	write clabel$
	grestore
	rmove 0 -4
end sub


! Vertical Bottom Polarised Capacitor
sub p_capacitor_vb clabel$
	gsave
	begin rotate -90
		@p_capacitor 1
	end rotate
	rmove -1 -3
	rline 0.5 0
	rmove -0.25 -0.25
	rline 0 0.5
	rmove 0.75 2.75

	rmove 1.25 -2
	set hei 0.8
	set just LC
	write clabel$
	grestore
	rmove 0 -4
end sub



! Horizontal capacitor
sub capacitor_h clabel$
	gsave
	@capacitor
	rmove 2 1.25
	set hei 0.8
	set just BC
	write clabel$
	grestore
	rmove 4 0
end sub

! Vertical capacitor
sub capacitor_v clabel$
	gsave
	begin rotate -90
		@capacitor
	end rotate
	rmove 1.25 -2
	set hei 0.8
	set just LC
	write clabel$
	grestore
	rmove 0 -4
end sub

! Horizontal varicap
sub vcapacitor_h clabel$
	gsave
	@capacitor
	rmove 1 -1
	rline 2 2 arrow end
	rmove -3 -1
	rmove 2 1.25
	set hei 0.8
	set just BC
	write clabel$
	grestore
	rmove 4 0
end sub

! Vertical varicap
sub vcapacitor_v clabel$
	gsave
	begin rotate -90
		@capacitor
	end rotate
	rmove -1 -3
	rline 2 2 arrow end
	rmove -1 1
	rmove 1.25 -2
	set hei 0.8
	set just LC
	write clabel$
	grestore
	rmove 0 -4
end sub

!===================================
! Crystals
!===================================
sub crystal
	crystalgap = 0.25
	crystalboxw = 0.5
	crystalleglength = (4-(crystalgap*2 + crystalboxw))/2
	rline crystalleglength 0
	rmove 0 1
	rline 0 -2
	rmove crystalgap 0
	box crystalboxw 2
	rmove crystalboxw+crystalgap 0
	rline 0 2
	rmove 0 -1
	rline crystalleglength 0
end sub

sub crystal_h label$
	@crystal
	gsave
	rmove -2 1.2
	set just BC
	set hei 0.8
	write label$
	grestore
end sub

sub crystal_v label$
	gsave
	gsave
	begin rotate -90
		@crystal
	end rotate
	grestore
	rmove 1.2 -2
	set just LC
	set hei 0.8
	write label$
	grestore
	rmove 0 -4
end sub


!===================================
! Diodes
!===================================

DIODE_NORMAL = 0
DIODE_SCHOTTKY = 1
DIODE_ZENER = 2
DIODE_THYRISTOR = 3

! Base diode
sub diode type
	default type DIODE_NORMAL
	gsave
	rline 1.5 0
	rline 0 0.5
	rline 1 -0.5
	rline -1 -0.5
	rline 0 0.5
	rmove 1 -0.5
	rline 0 1
	rmove 0 -0.5
	gsave
	if type = DIODE_SCHOTTKY then
		schottky_depth = 0.15
		schottky_width = 0.25
		gsave
		rmove 0 0.5
		rline schottky_depth 0
		rline 0 -schottky_width
		grestore
		rmove 0 -0.5
		rline -schottky_depth 0
		rline 0 schottky_width
	else if type = DIODE_ZENER then
		zener_depth = 0.15
		gsave
		rmove 0 0.5
		rline zener_depth 0
		grestore
		rmove 0 -0.5
		rline -zener_depth 0
	else if type = DIODE_THYRISTOR then
		rline 0.5 -0.5
		rline 0 -0.5
	end if
	grestore
	rline 1.5 0
	grestore
end sub

! Horizontal right-facing Diode
sub diode_hr dlabel$ type
	default type DIODE_NORMAL
	gsave
	@diode type
	rmove 2 1
	set hei 0.8
	set just BC
	write dlabel$
	grestore
	rmove 4 0
end sub

! Horizontal left-facing Diode
sub diode_hl dlabel$ type
	default type DIODE_NORMAL
	gsave
	rmove 4 0
	begin rotate 180
		@diode type
	end rotate
	rmove -2 1
	set hei 0.8
	set just BC
	write dlabel$
	grestore
	rmove 4 0
end sub

! Vertical downward Diode
sub diode_vd dlabel$ type
	default type DIODE_NORMAL
	gsave
	begin rotate -90
		@diode type
	end rotate
	rmove 1 -2
	set hei 0.8
	set just LC
	write dlabel$
	grestore
	rmove 0 -4
end sub

! Vertical upward Diode
sub diode_vu dlabel$ type
	default type DIODE_NORMAL
	gsave
	rmove 0 -4
	begin rotate 90
		@diode type
	end rotate
	rmove 1 2
	set hei 0.8
	set just LC
	write dlabel$
	grestore
	rmove 0 -4
end sub

! Vertical downward LED
sub led_vd label$
	@diode_vd label$ DIODE_NORMAL
	gsave
	arroffset = 0.15
	set arrowsize 0.3
	set arrowangle 20
	!	rmove 0.65 1.85
	rmove 0.6 1.75
	rmove 0 arroffset
	rline 0.5 -0.5 arrow end

	rmove 0 -2*arroffset
	rline -0.5 0.5 arrow start
	grestore
end sub


sub bridge_rectifier blabel$
	gsave
	d_offset = (3 - sqrt(8)) / 2
	d_length = sqrt(8)
	@connection
	rline d_offset d_offset
	begin rotate 45
		@diode DIODE_NORMAL
	end rotate
	rmove d_length d_length
	rline d_offset d_offset
	@connection
	rline d_offset -d_offset
	begin rotate -45
		@diode DIODE_NORMAL
	end rotate
	rmove d_length -d_length
	rline d_offset -d_offset
	@connection
	rmove -6 0
	rline d_offset -d_offset
	begin rotate -45
		@diode DIODE_NORMAL
	end rotate
	rmove d_length -d_length
	rline d_offset -d_offset
	@connection
	rline d_offset d_offset
	begin rotate 45
		@diode DIODE_NORMAL
	end rotate
	rmove d_length d_length
	rline d_offset d_offset
	rmove -3 0
	set hei 0.8
	set just CC
	write blabel$
	grestore
end sub

! Opto Isolator
sub optocoupler optolabel$
	@diode_vd "" DIODE_NORMAL
	gsave
	rmove 3 0
	rline 0 1
	rline -1 0.5 arrow start
	rmove 0 -0.5
	rline 0 2
	rmove 0 -0.5
	rline 1 0.5
	rline 0 1
	set just LC
	rmove 0 -2
	set hei 0.8
	write optolabel$
	optoarrowoffset = 0.25
	optoarrowlength = 1
	set arrowsize 0.4
	set arrowangle 20
	rmove -1.2 optoarrowoffset
	rline -optoarrowlength 0 arrow start
	rmove 0 -optoarrowoffset*2
	rline optoarrowlength 0 arrow end
	grestore
end sub


! Need to add triacs, thyristors etc

!===================================
! Op-amps
!===================================
sub opamp supply ulabel$
	gsave
	rline 1 0
	rline 0 1
	rline 3 -2
	rline -3 -2
	rline 0 1
	rline -1 0
	rmove 1 0
	rline 0 2
	! Draw the "-"
	rmove 0.2 0
	rline 0.5 0

	! Draw the "+"
	rmove -0.5 -2
	rline 0.5 0
	rmove -0.25 -0.25
	rline 0 0.5
	rmove -0.25 -0.25
	rmove 2.8 1
	rline 1 0
	rmove -3 0
	set just CC
	set hei 0.8
	write ulabel$
	if supply = 1 then
		rmove 2 0
		rmove -2 4/3
		rline 0 2/3
		rmove 0 -4
		rline 0 2/3
	end if
	grestore
end sub

!===================================
! Power supplies etc
!===================================

! Ground
sub ground
	gsave
	rline 0 -1
	rmove -0.75 0
	rline 1.5 0
	rmove -0.25 -0.25
	rline -1 0
	rmove 0.25 -0.25
	rline 0.5 0
	grestore
end sub

! Power connection
sub power label$
	gsave
	rline 0 1
	rline -0.25 -0.25
	rmove 0.5 0
	rline -0.25 0.25
	rmove 0.1 0.1
	set just BL
	write label$
	grestore
end sub

sub supply_centre type
	if type = 1 then
		! Supply with voltage
		rmove 0 -0.6
		rline 0 1.2 arrow end
		rmove 0 -0.6
	end if
	if type = 2 then
		! +/- Supply
		rmove 0 0.5
		rline 0.25 0
		rmove -0.25 0
		rline -0.25 0
		rmove 0.25 0
		rline 0 0.25
		rmove 0 -0.25
		rline 0 -0.25
		rmove 0 0.25

		rmove 0 -1
		rmove -0.25 0
		rline 0.5 0
		rmove -0.25 0.5
	end if
	if type = 3 then
		! Sine source
		rmove -0.5 0
		rbezier 0.25 0.5 0.25 0.5 0.5 0
		rbezier 0.25 -0.5 0.25 -0.5 0.5 0
		rmove -0.5 0
	end if
end sub

! Horizontal Power supply
sub supply_h type
	! Type gives central drawing
	! 0: Nothing
	! 1: Voltage Arrow
	! 2: +/-
	! 3: Crude Sinusoid
	gsave
	rline 1 0
	rmove 1 0
	circle 1
	begin rotate -90
		@supply_centre type
	end rotate
	rmove 1 0
	rline 1 0
	grestore
	rmove 4 0
end sub

! Vertical Power supply (probably more usual)
sub supply_v type
	! Type gives central drawing
	! 0: Nothing
	! 1: Voltage Arrow
	! 2: +/-
	! 3: Crude Sinusoid
	gsave
	rline 0 -1
	rmove 0 -1
	circle 1
	@supply_centre type
	rmove 0 -1
	rline 0 -1
	grestore
	rmove 0 -4
end sub

!===================================
! Cells
!===================================

! Base cell
sub cell
	gsave
	rline 1.8 0
	rmove 0 -1
	rline 0 2
	rmove 0.4 -1
	gsave
	set lwidth 0.2
	rmove 0 -0.5
	rline 0 1
	rmove 0 -0.5
	grestore
!	set lwidth 0.02
	rline 1.8 0
	grestore
end sub

! Horizontal cell (perhaps should have hr & hl?)
sub cell_h elabel$
	gsave
	@cell
	rmove 2 1.25
	set just BC
	set hei 0.8
	write elabel$
	grestore
	rmove 4 0
end sub

! Vertical cell
sub cell_v elabel$
	gsave
	begin rotate -90
		@cell
	end rotate
	rmove 1.25 -2
	set just LC
	set hei 0.8
	write elabel$
	grestore
	rmove 0 -4
end sub

!===================================
! Logic Gates
!===================================

! Left side of gate (for and/nand)
sub and_left_gate
	gsave
	rmove 0 1
	rline 1 0
	rmove 0 1
	rline 0 -4
	rmove 0 1
	rline -1 0
	grestore
end sub

! Left side of gate (for not/buffer)
sub not_left_gate
	gsave
	rline 1 0
	rmove 0 -2
	rline 0 4
	grestore
end sub

! Curved left gate (for OR)
sub or_left_gate
	gsave
	! Draw the connection lines
	linelength = 1.59
	rmove 0 -1
	rline linelength 0
	rmove 0 2
	rline -linelength 0
	rmove 1 1

	! Draw the curve
	cx = xpos()
	cy = ypos()
	bezier cx+1 cy-1 cx+1 cy-3 cx cy-4
	grestore
end sub

! Double Curved left gate (for XOR)
sub xor_left_gate
	gsave
	! Draw the connection lines
	linelength = 1.09
	rmove 0 -1
	rline linelength 0
	rmove 0 2
	rline -linelength 0
	rmove 1 1

	! Draw the curves
	for i = 0 to 0.5 step 0.5
		rmove -i 0
		cx = xpos()
		cy = ypos()
		bezier cx+1 cy-1 cx+1 cy-3 cx cy-4
		amove cx cy
	next i
	grestore
end sub

! Non-inverted output
sub non_inverted_output
	gsave
	rmove 5 0
	rline 1 0
	grestore
end sub

! Inverted output
sub inverted_output
	gsave
	rmove 5 0
	invrad = 0.25
	rmove invrad 0
	circle invrad
	rmove invrad 0
	rline 1-2*invrad 0
	grestore
end sub

! Curved edges for AND/OR
sub curved_edges
	gsave
	rmove 1 2
	cx = xpos()
	cy = ypos()
	! Draw the top curve
	bezier cx+3 cy cx+4 cy-1 cx+4 cy-2
	! Draw the bottom curve
	cx = xpos()
	cy = ypos()
	bezier cx cy-1 cx-1 cy-2 cx-4 cy-2
	grestore
end sub

! Straight edges for Buffer/Not
sub straight_edges
	gsave
	rmove 1 2
	rline 4 -2
	rline -4 -2
	grestore
end sub

! XOR Gate
sub xor
	gsave
	rmove 0 -1
	@xor_left_gate
	@curved_edges
	@non_inverted_output
	grestore
end sub

! XNOR Gate
sub xnor
	gsave
	rmove 0 -1
	@xor_left_gate
	@curved_edges
	@inverted_output
	grestore
end sub

! OR Gate
sub or
	gsave
	rmove 0 -1
	@or_left_gate
	@curved_edges
	@non_inverted_output
	grestore
end sub

! NOR Gate
sub nor
	gsave
	rmove 0 -1
	@or_left_gate
	@curved_edges
	@inverted_output
	grestore
end sub

! AND Gate
sub and
	gsave
	rmove 0 -1
	@and_left_gate
	@curved_edges
	@non_inverted_output
	grestore
end sub

! NAND Gate
sub nand
	gsave
	rmove 0 -1
	@and_left_gate
	@curved_edges
	@inverted_output
	grestore
end sub

! NOT Gate
sub not
	gsave
	@not_left_gate
	@straight_edges
	@inverted_output
	grestore
	rmove 6 0
end sub

! BUFFER
sub buffer
	gsave
	@not_left_gate
	@straight_edges
	@non_inverted_output
	grestore
	rmove 6 0
end sub

! Schmitt Trigger symbol
sub schmitt
	gsave
	sw = 0.5
	sh = 1
	h = 0.5
	rmove -(sw/2+h) sh/2
	rline h+sw 0
	rline 0 -sh
	rmove h 0
	rline -(h+sw) 0
	rline 0 sh
	grestore
end sub

! NAND Schmitt Trigger
sub nand_schmitt
	gsave
	rmove 2.5 -1
	@schmitt
	grestore
	@nand
end sub

! Inverter Schmitt Trigger
sub not_schmitt
	gsave
	rmove 2.25 0
	@schmitt
	grestore
	@not
end sub