Makefiles¶
OS autodetection¶
Note that not all copies of the Makefile include this at the moment (see section "Use" below).
Motivation¶
The name of the serial port depends on the operating system:- Windows:
COM4:
- All others (?):
/dev/ttyUSB0
The port to use is selected automatically to avoid having to modify the Makefile every time a different machine is used.
Use¶
The Makefile contains a line starting with AVRDUDE_PORT =
. Replace that line with:
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
Requirements¶
- uname
- sh
Both are standard on unix machines and included in the mingw32 distribution.
Principle of operation¶
Algorithm¶
The OS is determined by looking at the output of the uname -s
(kernel name) command. If the output contains, case insensitively, the string "w32", a Windows system is assumed and COM4:
is used. If not, /dev/ttyUSB0
is used.
Implementation¶
The switch is not implemented by means of make
but relies on a shellout. The output from grep
is redirected to /dev/null
to avoid printing the port name to stdout.
Alternative methods¶
- Instead of looking at the output of
uname
, the existence of/dev/ttyUSB0
could be checked. However, it would try to accessCOM4:
on Linux when the robot is not plugged in (which is not a real problem, but might lead to confusion). - The system wide AVRdude configuration file (
/etc/avrdude.conf
) could be used to set the default port for a given machine (where is this file located on Windows systems?). However, this would have to be set up for every machine used for programming.
Extensions¶
A more complex selection scheme can be implemented in case the name of the port may be different on a given machine (for example COM1:
or /dev/ttyS0
).
Enhancements¶
- Multi-user main function (problems with the current solution: preprocessor black magic, main function not called main, main file not recompiled when different makefile chosen)
- Single Makefile (included)
- Change target name "main"
- Output files not clean up?