forked from Integreight/1Sheeld-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPSShield.cpp
105 lines (85 loc) · 2.32 KB
/
GPSShield.cpp
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
/*
Project: 1Sheeld Library
File: GPSShield.cpp
Version: 1.0
Compiler: Arduino avr-gcc 4.3.2
Author: Integreight
Date: 2014.5
*/
#define FROM_ONESHEELD_LIBRARY
#include "OneSheeld.h"
#include "GPSShield.h"
//Class Constructor
GPSShieldClass::GPSShieldClass () : ShieldParent(GPS_ID)
{
LatValue=0;
LonValue=0;
isInit=false;
isCallBackAssigned=false;
}
//GPS Input Data Processing
void GPSShieldClass::processData ()
{
//Checking Function-ID
byte functionId=getOneSheeldInstance().getFunctionId();
if(functionId==GPS_VALUE)
{
LatValue=getOneSheeldInstance().convertBytesToFloat(getOneSheeldInstance().getArgumentData(0));
LonValue=getOneSheeldInstance().convertBytesToFloat(getOneSheeldInstance().getArgumentData(1));
isInit=true; //setting a flag
}
//Users Function Invoked
if (isCallBackAssigned && !isInACallback())
{
enteringACallback();
(*changeCallBack)(LatValue,LonValue);
exitingACallback();
}
}
//Getter
float GPSShieldClass::getLatitude()
{
return LatValue;
}
//Getter
float GPSShieldClass::getLongitude()
{
return LonValue;
}
//Helper
bool GPSShieldClass::isInRange(float usersValue1 , float usersValue2,float range)
{
if(!isInit)return false;
float x= getDistance(usersValue1 , usersValue2);
if( x >=0 && x < range)
{
return true;
}
else
{
return false;
}
}
//Helper
float GPSShieldClass::getDistance(float x , float y) //x and y is the lattitude and the longitude inserted by the user
{
if(!isInit)return 0;
float dLat = radian(x-LatValue); //difference betwwen the two lattitude point
float dLon = radian(y-LonValue); //difference betwwen the two longitude point
float chordProcess = sin(dLat/2)*sin(dLat/2)+sin(dLon/2)*sin(dLon/2)*cos(radian(LatValue))*cos(radian(x));
float angularDistance = 2*atan2(sqrt(chordProcess),sqrt(1-chordProcess));
float actualDsitance = (RADIUS_OF_EARTH*angularDistance)*1000; //getting the actuall distance in meters
return actualDsitance;
}
//Helper
float GPSShieldClass::radian(float value)
{
float radianValue = value*(PI/180);
return radianValue;
}
//User Function Setter
void GPSShieldClass::setOnValueChange(void (*userFunction)(float lattitude ,float longitude))
{
changeCallBack=userFunction;
isCallBackAssigned=true;
}