Time and date parsing, I decided to use the definition from Erlangs calendar module which are:
date() = {Year, Month, Day}
Year = int()
Month = 1..12
Day = 1..31
time() = {Hour, Minute, Second}
Hour = 0..23
Minute = Second = 0..59
Only I need millisecond precision for the time so i decided on this compatible format
{{Hour, Minute, Second}, Millis} Code for generating these is easy using
iolib:fread/2
{ok,[Day,Month,Year],[]} = io_lib:fread("~2d~2d~2d",Date),
{ok,[Hour,Minute,Second,Millis],[]} =io_lib:fread("~2d~2d~2d.~3d",Time),
I get a list of the parsed values which can be passed directly into the relevant tuples.
{fixtime,{{Hour,Minute,Second},Millis}},
{date,{Year+2000,Month,Day}}
Since last I noticed that the GPS device prior to getting a satellite fix returned empty lists in places where the code expected integers, floats or atoms, leading to errors in the call to list_to_integer for example, so I had to replace these with functions like these:
safe_to_float([]) ->
-1.0;
safe_to_float(F) ->
list_to_float(F).
Now to the client process. I simple spawn a new process which will receieve messages from the driver and react only to
gga messages as they are carrying the position information, then they format these in a google maps friendly way. The entire code goes like this:
start() ->
spawn(fun() ->
gpsdriver:start(self()),
loop()
end).
loop() ->
receive
{gga,
{fixtime,_},
{lattitude,[Lattitude,NS]},
{longitude,[Longitude,EW]},
{quality,_},
{numberofsatellites,_},
{horizdi,_},
{altitude,_},
{geoidheight,_},
{timesincedgps,_},
{dgpsstation,_}} ->
io:format("~p~s,~p~s~n",[Lattitude,NS,Longitude,EW]);
_ -> true
end,
loop().
Note that in the
start/0 function I pass the Pid (process id) of the newly created process to the gpsdriver, so that was changed to accept this and instead of outputting directly to standard output sends the tuples to this process using Erlang message sending mechanism Pid ! RecordThe final code outputs messages like:
29.9792N,31.1343E
29.9792N,31.1343E
29.9792N,31.1343E
So this completes the gps project for me, only that I will make some performance measures to get an idea of the best way to parse the incoming messages.
However this has to be postponed to the new year, as a major holiday is approaching fast.
Best wishes for 2008 to everyone who bothers reading this.