INCLUDE rtcu.inc
VAR_INPUT
dipEjectBOOLR_EDGE
END_VAR;
VAR_OUTPUT
gw_conn : BOOL;
gps_pos : BOOL;
END_VAR;
VAR
log : logWrite;
clock : clockLinsecToTime;
gps : gpsFix;
gps_linsec : DINT;
gps_pwr : BOOL;
gsm_pwr : BOOL;
media_openBOOL
fd : FILE;
rc : INT;
str : STRING;
tSleep : TON;
END_VAR;
PROGRAM MX2GPSLog;
pmPowerFail(bat:=TRUE);
tSleep.pt := 300000;
fsMediaOpen(media:=0);
fsStatusLEDEnable(enable:=ON)
IFNOTlogIsInitialized(key:=4712)THEN
logInitialize(key:=4712, numlogvalues:=2, numlogrecords:=0)
END_IF
BEGIN
gw_conn := gwConnected();
IF fsMediaPresent(media:=0) AND NOT media_open THEN
media_open:=TRUE
ELSIFNOTfsMediaPresent(media:=0)ANDmedia_openTHEN
media_open:=FALSE
END_IF;
IF dipEject THEN
fsMediaEject(media:=0);
media_open := FALSE;
END_IF;
IF media_open AND fsFileStatus(fd:=fd) <> 0 THEN
IF fsFileExists(name:="\datalog.txt") THEN
DebugMsg(message:="Open File");
fd := fsFileOpen(name:="\datalog.txt");
ELSE
DebugMsg(message:="File Create");
fd := fsFileCreate(name:="\datalog.txt");
END_IF;
END_IF;
IF NOT gps_pwr THEN
DebugMsg(message:="Start GPS");
gpsPower(power:=ON);
gps_pwr := ON;
END_IF;
IF NOT gsm_pwr THEN
DebugMsg(message:="Start GSM");
gsmPower(power:=ON);
gprsOpen();
gsm_pwr := ON;
END_IF;
gps();
IF gps.mode > 1 THEN
gps_pos := NOT gps_pos;
IF clockNow() > gps_linsec THEN
log(value[1]:=gps.latitude,value[2]:=gps.longitude);
IF fsFileStatus(fd:=fd) = 0 THEN
clock(linsec:=gps.linsec);
str := strFormat(format:="\1.\2.\3, ",v1:=clock.year,v2:=clock.month,v3:=clock.day) +
strFormat(format:="\1:\2:\3, ",v1:=clock.hour,v2:=clock.minute,v3:=clock.second);
IF gps.latsouth THEN str := str + "-"; END_IF;
str := str + strFormat(format:="\1*\2.\3, ",v1:=gps.latdeg,v2:=gps.latmin,v3:=gps.latdecmin);
IF gps.lonwest THEN str := str + "-"; END_IF;
str := str + strFormat(format:="\1*\2.\3, ",v1:=gps.londeg,v2:=gps.lonmin,v3:=gps.londecmin);
fsFileWriteStringNL(fd:=fd,str:=str);
END_IF;
gps_linsec := clockNow() + 15;
END_IF;
END_IF;
IF pmVibration() THEN
tSleep(trig:=FALSE);
ELSE
tSleep(trig:=TRUE);
END_IF;
IF tSleep.q THEN
IF NOT batIsCharging() THEN
IF fsFileStatus(fd:=fd) = 0 THEN
fsFileWriteStringNL(fd:=fd,str:="--- Enter pmWaitEvent ---");
END_IF;
// Stop GPS and/or GSM
IF gps_pwr THEN
DebugMsg(message:="Stop GPS");
gpsPower(power:=OFF);
gps_pwr := OFF;
END_IF;
IF gsm_pwr THEN
DebugMsg(message:="Stop GSM");
gprsClose();
gsmPower(power:=OFF);
gsm_pwr := OFF;
END_IF;
rc := pmWaitEvent(vibration:=ON);
DebugMsg(message:="pmWaitEvent - Movement");
IF fsFileStatus(fd:=fd) = 0 THEN
str := strFormat(format:="--- Exit pmWaitEvent (rc=\1) ---",v1:=rc);
fsFileWriteStringNL(fd:=fd,str:=str);
END_IF;
tSleep(trig:=FALSE);
END_IF;
END_IF;
END;
END_PROGRAM;
|