forked from liquidware/LibGeoShield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibGeoShield.cpp
More file actions
195 lines (162 loc) · 5.78 KB
/
LibGeoShield.cpp
File metadata and controls
195 lines (162 loc) · 5.78 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
LibGeoShield.cpp - Liquidware GeoShield Sensor library for Arduino
GeoShield - The Liquidware GeoShield is an integrated Arduino module with built-in
GPS, 3-Axis Accelerometer, and Digital Compass sensors
Learn more at http://www.liquidware.com/category/Sensors
Copyright (c) 2010 Christopher Ladden All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* Includes
******************************************************************************/
#include <string.h>
#include <LibCompass.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "LibGeoShield.h"
/******************************************************************************
* Definitions
******************************************************************************/
#define GPS_RX_PIN 4
#define GPS_TX_PIN 5
#define GPS_BUFF_SIZE 528
#define G_SLOPE_5V 10.18
#define G_OFFSET_5V 3437
LibCompass compass = LibCompass(COMPASS_HW_GEOSHIELD);
SoftwareSerial gps = SoftwareSerial(GPS_RX_PIN, GPS_TX_PIN);
static char GPS_buff[GPS_BUFF_SIZE];
/******************************************************************************
* Constructors
******************************************************************************/
LibGeoShield::LibGeoShield(uint8_t GeoShieldType) {
gps.begin(9600);
_AccelGSlope = G_SLOPE_5V; //If different supply, please calibrate
_AccelGOffset = G_OFFSET_5V;
}
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
* readGPS
* Used to read the entire GPS sensor output into a local buffer
*
* @return char* - A pointer to the GPS data
**********************************************************/
char* LibGeoShield::readGPS(void) {
char c;
GPS_buffIndex = 0;
memset(GPS_buff,0,GPS_BUFF_SIZE);
while (1) {
c = gps.read();
if (c == -1) {
break;
}
GPS_buff[GPS_buffIndex] = (char)c;
GPS_buffIndex++;
}
return &GPS_buff[0]; //pass back a pointer to the entire buffer
}
/**********************************************************
* readCompass
* Reads the GeoShield's on-board compass
*
* @return int - The compass heading in degrees (0-360)
**********************************************************/
int LibGeoShield::readCompass(void) {
return (int)compass.GetHeading();
}
/**********************************************************
* setSupply
* Set the Arduino's voltage supply. Used
* for accelerometer volts to mg's conversion accuracy
*
* supplyVolts - The measured voltage of
* the AREF pin, for example, 4.97
**********************************************************/
void LibGeoShield::setSupply(float supplyVolts) {
_AccelGSlope = (supplyVolts / 5.00) * G_SLOPE_5V;
}
/**********************************************************
* readAccelX
* Reads the GeoShield's on-board accelerometer
*
* @return int - The accelerometer position data
* in mg (thousands of a g)
**********************************************************/
int LibGeoShield::readAccelX(void) {
float val;
val = _AccelGSlope * analogRead(0) - _AccelGOffset;
return (int)val;
}
/**********************************************************
* readAccelY
* Reads the GeoShield's on-board accelerometer
*
* @return int - The accelerometer position data
* in mg (thousands of a g)
**********************************************************/
int LibGeoShield::readAccelY(void) {
float val;
val = _AccelGSlope * analogRead(1) - _AccelGOffset;
return (int)val;
}
/**********************************************************
* readAccelZ
* Reads the GeoShield's on-board accelerometer
*
* @return int - The accelerometer position data
* in mg (thousands of a g)
**********************************************************/
int LibGeoShield::readAccelZ(void) {
float val;
val = _AccelGSlope * analogRead(2) - _AccelGOffset;
return (int)val;
}
//Export the compass functions
bool LibGeoShield::CalibrateCompass(void) {
return compass.Calibrate();
}
void LibGeoShield::SleepCompass(void) {
compass.Sleep();
}
void LibGeoShield::WakeCompass(void) {
compass.Wake();
}
/**********************************************************
* ledOn
* The GeoShield has four onboard LEDs. Use this to
* control the GeoShield's LED.
*
* ledNum - values are 0 - 3
**********************************************************/
void LibGeoShield::ledOn(uint8_t ledNum) {
if ((ledNum < 0) || (ledNum > 3)) {
return;
}
pinMode(ledNum+10, OUTPUT);
digitalWrite(ledNum+10, LOW);
}
/**********************************************************
* ledOff
* The GeoShield has four onboard LEDs. Use this to
* control the GeoShield's LED.
*
* ledNum - values are 0 - 3
**********************************************************/
void LibGeoShield::ledOff(uint8_t ledNum) {
if ((ledNum < 0) || (ledNum > 3)) {
return;
}
pinMode(ledNum+10, OUTPUT);
digitalWrite(ledNum+10, HIGH);
}