Sunday, March 28, 2010

The TDA7056 amplifier

The TDA7056 is this integrated circuit that basically provides you with an entire amplifier. A colleague of mine has pointed me in its direction once, saying that it's a good quality amplifier and that it's almost complete. And I always wanted to give it a go. Being the klotz that I am with electronics this took a lot of time and effort.

The datasheet for the TDA7056 actually shows how to apply the integrated circuit. I also found some other application somewhere else (I forgot where though) and modified the original application with a few little details. I bought the parts and a project box and started working on this stuff.


The Vcc can be anywhere between 3V and 12V, according to the datasheet - I used 9V, 5V, and 3V, and felt little meaningful difference in the output; I finally settled for two 1.5V AAA batteries because I had the box for them at hand, and I have a lot of those batteries. I also added an on-off switch that completely cuts off the Vcc and the ground, but did not include it in the schematic, because it's a bit crowded as it is.

... that was actually in the summer. I failed miserably. First, I broke off two legs of my IC and decided to get a new one. Then, I worked on it all day and when I was done it didn't work, and I had completely no idea why. So I took a couple of weeks away from the project. I returned to it later on, only to measure some stuff, change some wires, get really frustrated and quit for a little longer. This repeated several times. Also with each time I inadvertently messed up the box a little, e.g. by sticking a soldering iron to the side by accident, or by spilling some glue on it.

Alas, this week I sat down, took everything apart, then reworked the schematics and assembled the thing again with some different parts. I tried to get every little element neatly into the box, painstakingly checking each connection to see that the solder was done ok. This worked mostly. And at the end I got tired and confused and a lot of little problems ensued with connections. At one point the circuit picked up radio even. And I screwed up the box again too.

Still, I managed. Here are some pictures:


My plans for using it include... nothing very much. This was a point of honour rather than a practical venture.

Sunday, March 21, 2010

The turner off-er

Unconventionally, this is an entry about a hardware hack I perpetrated, and not a script as such. There is a script at the end, but it's trivial.

For a long, long time I wanted to make this device. It started such a long time ago that I don't even remember what I wanted to use it for... The idea is that you have some sort of an appliance that you plug into the wall, like a refrigerator or a lamp or something, and you want to control when it is turned off and on from your computer.

I approached the subject several times now. Each of the previous times my lousy electronic and electronics-related skills had caused me either (a) to get too frustrated too continue at the planning stage, (b) not to be able to obtain the proper components, or (c) to almost annihilate the world due to a short-circuit. And each time I put it off until I got more drive.

And finally, this week (mostly due to the confidence that playing with the Arduino has given me) I've done it! It works, and I am not unhappy.

Þiſ herewiþ iſ þe chronicle of my victory.

Bright was the morning and high ovr hearts when I proceeded (again) to think about the problem of turning things on and off with my computer. I started out by searching for relevant information (because I know there are schematics available out there) and found somebody's blog post about how this can be done with an Arduino. This was an excellent point to start, because I own an Arduino, and those are easier to come to grips with than hooking up a bunch of wires to the back of the computer. And it's basically a ready solution.

Except that it isn't for me. That there is done for 120V AC and I live in a place where we have 220V AC in the wall, so I had to redesign.

So I had to find out about relays. In a nutshell these are like switches which use electromagnetic magic to turn a switch on and off. That's all I need to know. I require the relay to be turned on when a 5V DC signal appears on one of its magic electromagnetic coils, and allows 220V AC to pass through it. A longer while spent searching and a couple of questions this guy one I know allowed me to figure out that the relay I need is a JQX115F 005. A further 30 minutes with a datasheet helped me figure out that I want something like a JQX115F 005/2ZS2... which they didn't have at the store, so I finally acquired the JQX115F 005/2ZS4 instead.

Here's a sketch of what I came up with:


I used my breadboard to put the thing together and test it out, first with my Arduino.


When that worked, I soldered all the bits together and put them in a socket-like box, and then tested it out. It stopped working so well, because I soldered a few elements the wrong way (specifically, the diode was upside down)... and then I tried again and it worked fine again.


The thing left to do now was to interface with a computer. Happily, I several of those things. Having visited electronics stores, I also obtained the necessary parts to make a cable connection between the computer and the turner off-er.

There is a snag, however. I decided to use the parallel port to control the device. My main reason behind this is that using any (e.g., USB) serial port would require an additional chip between the computer and the device that would translate the values from the serial protocol to some usable form. This is expensive to put together. So I stuck to the parallel port, since I have plenty of old computers that sport it. The snag is though, that the parallel port is not designed to provide power, and may fail to give enough current to work the relay (it's not powerful enough to turn the switch on), which would not have been a problem with the USB.

I decided to do a nasty hack then, and use the USB to get power and simultaneously use the printer port to control the device. This is not pretty, I know, but it's easier, and it's enough for the moment. I'll probably try to use only USB in the future, if I ever get round to redesigning the device.

So, the cable has two endings that go into the computer. In the USB cable I tap into the Vcc 5V (usually red) and the Ground (usually black). In the parallel port, I tap into the Ground pin and the first input pin - Data0 (see image below). Then I connect ground from the USB cable and the ground the parallel port together.


As for the device, the Vcc goes into the Vcc of the turner off-er (red socket), the grounds go into the Ground of the turner off-er (black socket), and the Data0 goes into the Input of the turner off-er (yellow socket). And all is well. Although the actual cable I made looks kinda manky.


Last thing was a computer program to control the thing. I decided to go with the pyParallel library, so the script to turn the thing on or off would be something like:

 
1 #!/usr/bin/python
2 from parallel import Parallel
3 from sys import argv
4
5 def usage():
6 print "Usage: %s on|off" % argv[0]
7
8 if len(argv) != 2:
9 usage()
10 elif not argv[1] in ['on', 'off']:
11 usage()
12 else:
13 port = Parallel()
14 data = 1 if argv[1] == 'on' else 0
15 port.setData(data)


And I had to remove lp and install the ppdev kernel module on my Debian Sid box where I was testing it:

1 rmmod lp
2 modprobe ppdev

And that's it.

Update (April 19th): I later made another unit. It came out slightly neater (and much quicker) now that I knew exactly what I was doing. Thus, I give you, Mk. II:

And with the Mk. II cable:

Thursday, March 18, 2010

PDF shorthand


There are periods of time when I have to do a lot of article editing or book writing, and I usually do that in LaTeX. I find that writing dockets in LaTeX helps me focus on the content, maintain coherent style throughout the document, and produce better-looking results... and I lost my patience with office suites in general.

There is however, one thing that annoys me. Since I tend to just use a text editor (vim, or kate, or gedit) I usually need to go into terminal and type in the four commands to compile the document into a pdf:

pdflatex meh.tex && \
bibtex meh && \
pdflatex meh.tex && \
pdflatex meh.tex

And since I'm kinda compulsive I do that kind of often. And when it's not in the history of commands (because e.g., I'd been writing a script during a break in editing) I get annoyed.

So I figured I could do a simple script that would take care of those commands. A simple script that just takes the name of the tex file and returns a pdf. And I did just that.

And then I started fiddling with it and adding silly features: making it quiet, interactive, whatever; and then a getopt interface on top of it all. So it got kind of convoluted by the end. Feature-wise it probably lays smack in the middle between lightweight and feature-rich. I.e., it is slightly too complex but doesn't have the feature you want.

But I still like it.

If you're looking for fun stuff, the usage function is interesting, even if I say so myself. Instead of writing out a sequence of echo commands explaining what the program does, I decided to use the comments I write on the top of the scripts always. So the usage command finds and parses the script file, and then, using awk yanks the large comment, strips it of the hash character in front, and prints it out to the user. It saved time, and I should remember to sue it in the future in this context.

Some examples of usage now. First, make pdf from tex, bibliography and all:

pdfshorthand top.tex

To make it not produce all those pesky temporary documents (ask before removing each file or not):

pdfshorthand -c top.tex
pdfshorthand -cf top.tex

And if you have no bibliography in your docket, you need to use the b flag:

pdfshorthand -b top.tex
pdfshorthand -bc top.tex
pdfshorthand -bcf top.tex

The code:
 
1 #!/bin/bash
2 #
3 # My shorthand for pdflatex
4 #
5 # Because I'm sick and tired of always putting in the same command over and
6 # over again just to compile a file into a PDF. This script does it for me.
7 # It's basically the equivalent for writing:
8 # pdflatex top.tex && bibtex top && pdflatex top.tex && pdflatex top.tex and
9 # then cursing because I'd have much preferred it to halt on error but that is
10 # way too much writing.
11 #
12 # Usage:
13 # shorthand [OPTION ...] FILE [FILE ...]
14 #
15 # The FILE can but doesn't have to end with the extension 'tex'. If it isn't,
16 # one will be added before passing the file to pdflatex; if it is, it will be
17 # removed before passing the file to bibtex.
18 #
19 # Options:
20 # -n|--no-colors Do not use colors in output. Normally, colors are used
21 # for key messages: on success, on failure, and when
22 # compilation starts.
23 #
24 # -b|--no-biblio Do not attempt to generate a bibliography using bibtex.
25 #
26 # -i|--interactive Run pdflatex in interactive mode (halt on error).
27 # Otherwise the compilation is run in non-stop mode.
28 #
29 # -q|--quiet Do not show any output from the command whatsoever.
30 # Else, compilation produces a lot of output (be advised).
31 #
32 # -c|--clean-up Clean up after the compilation - remove aux, blg, bbl,
33 # and log files from the working directory. The files will
34 # be backed up in /tmp/ec.XXX/ just in case though, and
35 # you will be asked before the actual removal of each file
36 # takes place.
37 #
38 # -f|--force When used with -c or --clean-up, the script will not ask
39 # before removing each temporary file from the working
40 # directory.
41 #
42 # -h|--help Print usage information.
43 #
44 # --latex-command Specify a command to run instead of pdflatex. It will be
45 # given some arguments that pdflatex would have been given
46 # i.e., -interaction MODE and -non-stop-mode.
47 #
48 # --bibtex-command Specify a command to run instead of bibtex.
49 #
50 # Example:
51 # shorthand top.tex
52 #
53 # Requires:
54 # pdflatex
55 # bibtex
56 #
57 # Author:
58 # Konrad Siek <konrad.siek@gmail.com>
59 #
60 # License:
61 # Copyright 2010 Konrad Siek
62 #
63 # This program is free software: you can redistribute it and/or modify it
64 # under the terms of the GNU General Public License as published by the Free
65 # Software Foundation, either version 3 of the License, or (at your option)
66 # any later version. See <http://www.gnu.org/licenses/> for details.
67 #
68
69 texcommand=pdflatex # name or path to command
70 bibcommand=bibtex # name or path to command
71 quiet=$false # true or $false
72 interactive=$false # true or $false
73 colors=true # true or $false
74 cleanup=$false # true or $false
75 force=$false # true or $false
76 no_biblio=$false # true or $false
77
78 # Print usage information.
79 usage () {
80 gawk '/^#/ && NR>1 {sub(/^#[ \t]?/,""); print; next} NR>1 {exit}' "$0"
81 }
82
83 # Print stuff to standard error.
84 stderr() {
85 echo $@ &> 2
86 }
87
88 # Parse options.
89 options=$(\
90 getopt \
91 -o ciqnhfb \
92 --long clean-up,interactive,quiet,latex-command:,bibtex-command:,no-colors,force,help,no-biblio \
93 -n $0 -- "$@" \
94 )
95
96 # Stop if there's some sort of problem.
97 if [ $? != 0 ]
98 then
99 [ $quiet ] && syserr "Argh! Parsing went pear-shaped!"
100 exit 1
101 fi
102
103 # Set the parsed command options and work with the settings.
104 eval set -- "$options"
105 while true
106 do
107 case "$1" in
108 --latex-command) texcommand="$2"; shift 2;;
109 --bibtex-command) bibcommand="$2"; shift 2;;
110 -c|--clean-up) cleanup=true; shift;;
111 -n|--no-colors) colors=$false; shift;;
112 -f|--force) force=true; shift;;
113 -i|--interactive) interactive=true; shift;;
114 -q|--quiet) quiet=true; shift;;
115 -h|--help) usage; exit 1;;
116 -b|--no-biblio) no_biblio=true; shift;;
117 --) shift; break;;
118 *) syserr "Ack! She cannae take it anymore, captain!"; exit 3;;
119 esac
120 done
121
122 # Various messages printed out by the script.
123 SUCCESS="Huzzah!"
124 FAIL="Your shipment of fail has arrived."
125 PROCESSING="Processing file <{}>"
126
127 # If no params given, print usage and quit with an error.
128 [ $# -lt '1' ] && (usage; exit 1)
129
130 # Specify the params for pdflatex for whatever level of interactiveness the
131 # user expects. We do this beforehand.
132 if [ $interactive ]
133 then
134 iparams="-interaction errorstopmode -halt-on-error"
135 else
136 iparams="-interaction nonstopmode"
137 fi
138
139 clean_up () {
140 if [ $cleanup ]
141 then
142 dir=`mktemp --tmpdir -d "$1.XXX"` && \
143 for ext in bbl blg log aux out nav snm
144 do
145 f="$1.$ext"; cp -v "$f" "$dir/$f"
146 if [ -f "$f" ]
147 then
148 [ $force ] && rm -vf "$f" || rm -vi "$f"
149 else
150 "Omitting file: $f"
151 fi
152 done
153 cp -v "$1.tex" "$dir/"
154 fi
155 }
156
157 # If the user wants colors, the messages can be prepared beforehand.
158 if [ $colors ]
159 then
160 # Add coloring control characters to the message strings:
161 # success, green; fail, red; processing, white; all bold.
162 SUCCESS="\033[32m\033[1m$SUCCESS\033[m"
163 FAIL="\033[31m\033[1m$FAIL\033[m"
164 PROCESSING="\033[37m\033[1m$PROCESSING\033[m"
165 fi
166
167 # If bibtex is not supposed to be run at all, run the 'true' command instead.
168 # The 'true' command will take whatever parameters and do absolutely nothing,
169 # then it'll report a success. So it fits perfectly with the other commands.
170 if [ $no_biblio ]
171 then
172 bibcommand=true
173 fi
174
175 # The shorthand method for the pdflatex/bibtex combination.
176 shorthand () {
177 # Cut off the extension, if any to create the bibtex argument,
178 dir=`dirname "$1"`
179 base=`basename "$1" .tex`
180 stump="$dir/$base"
181
182 # And now stick the extension back on to create the pdflatex argument.
183 full="$stump.tex"
184
185 # Print out what file is being processed.
186 echo -e ${PROCESSING//\{\}/$full}
187
188 # The pipe, where all the magic happens:
189 $texcommand $iparams "$full" && \
190 $bibcommand "$stump" && \
191 $texcommand $iparams "$full" && \
192 $texcommand $iparams "$full" && \
193 (clean_up "$base"; echo -e $SUCCESS) || echo -e $FAIL
194
195 # Finally print out an information whether the processing was a success or
196 # a failure.
197 }
198
199 # Do the thing.
200 for arg
201 do
202 # If quiet mode is on, send all results to the black hole.
203 [ $quiet ] && shorthand "$arg" 1> /dev/null 2>/dev/null || shorthand "$arg"
204 done


The code is also available at GitHub as bash/pdfshorthand.