-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pde
231 lines (174 loc) · 6.5 KB
/
main.pde
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Example code for connecting a Parallax GPS module to the Arduino
Igor Gonzalez Martin. 05-04-2007
igor.gonzalez.martin@gmail.com
English translation by djmatic 19-05-2007
Listen for the $GPRMC string and extract the GPS location data from this.
Display the result in the Arduino's serial monitor.
*/
#include <ctype.h>
#include <math.h>
#include "common.h"
#include "SHARPInterface.h"
//#include "LCDInterface.h"
#include "GPSInterface.h"
using namespace std;
void updateIGTV();
char messageForDisplay[126];
// ########################################################################
void setup()
{
//Serial.begin(9600);
//DEBUG(F("Starting Serial debug..."));
initGPSInterface();
initDisplay();
messageForDisplay[0] = '\0';
delay(1000);
// printMessageToScreen("Hello from the screen !");
SHARPdisplay.clearDisplay();
SHARPdisplay.setTextSize(1);
SHARPdisplay.setTextColor(BLACK);
SHARPdisplay.setCursor(0,0);
SHARPdisplay.println("Hello from the screen !");
SHARPdisplay.refresh();
delay(1000);
}
void loop()
{
bool newData = readGPS();
if (newData)
{
DEBUG(F("Received stuff !"));
updateIGTV();
printMessageToScreen(messageForDisplay);
resetGPSInterface();
}
}
void updateIGTV()
{
DEBUG(F("in parse"));
char* dataFromGPS = gpsInterface.dataFromGPS;
int* indices = gpsInterface.indices;
// Parse shit and stuff
int j;
// Time
j = indices[0];
int hour = 10 * (dataFromGPS[j+1]-48) + (dataFromGPS[j+2]-48);
int minutes = 10 * (dataFromGPS[j+3]-48) + (dataFromGPS[j+4]-48);
int seconds = 10 * (dataFromGPS[j+5]-48) + (dataFromGPS[j+6]-48);
float current_time = hour + (minutes / 60.0) + (seconds / 3600.0);
//DEBUG_NOLN(F("Hour (UTC +0) : "));
//DEBUG(hour);
//DEBUG(F(":"));
//DEBUG(minutes);
//DEBUG(F(":"));
//DEBUG(seconds);
//DEBUG_NOLN(F("calc : "));
// Date
j = indices[8];
int day = 10 * (dataFromGPS[j+1]-48) + (dataFromGPS[j+2]-48);
int month = 10 * (dataFromGPS[j+3]-48) + (dataFromGPS[j+4]-48);
int year = 10 * (dataFromGPS[j+5]-48) + (dataFromGPS[j+6]-48);
// Latitude
j = indices[2];
float latitude = 10 * (dataFromGPS[j+1]-48)
+ 1 * (dataFromGPS[j+2]-48)
+ 0.1 * (dataFromGPS[j+3]-48)
+ 0.01 * (dataFromGPS[j+4]-48);
// Longitude
j = indices[4];
float longitude = 100 * (dataFromGPS[j+1]-48)
+ 10 * (dataFromGPS[j+2]-48)
+ 1 * (dataFromGPS[j+3]-48)
+ 0.1 * (dataFromGPS[j+4]-48)
+ 0.01 * (dataFromGPS[j+5]-48);
// Use longitude/latitude information to compute sunrise and sunset time
int N1 = month * 275 / 9;
int N2 = (month + 9) / 12;
int K = 1 + (year - 4 * ( year / 4 ) + 2 ) / 3;
int rank = N1 - N2 * K + day - 30;
float DegToRad = M_PI / 180.0;
float M = (357 + 0.9856 * rank) * DegToRad;
float C = (1.914 * sin(M) + 0.02 * sin(2*M));
float L = (280 + C + 0.9856 * rank) * DegToRad;
float R = -2.465 * sin(2*L) + 0.053 * sin(4*L);
float ET = (C+R) * 4;
float dec = asin(0.3978 * sin(L));
float H0 = acos( (-0.01454 - sin(dec) * sin(latitude*DegToRad))
/ (cos(dec) * cos(latitude*DegToRad) )
)
/ (DegToRad * 15);
float Hlever = 12 - H0 + ET / 60 + longitude / 15 - 1;
float Hcoucher = 12 + H0 + ET / 60 + longitude / 15 - 1;
int Hlever_hours = int(Hlever);
int Hlever_minutes = int((Hlever - Hlever_hours) * 60);
int Hcoucher_hours = int(Hcoucher);
int Hcoucher_minutes = int((Hcoucher - Hcoucher_hours) * 60);
//DEBUG_NOLN(F("H_lever : "));
//DEBUG_NOLN(Hlever_hours);
//DEBUG_NOLN(F(":"));
//DEBUG(Hlever_minutes);
//DEBUG_NOLN(F("H_coucher : "));
//DEBUG_NOLN(Hcoucher_hours);
//DEBUG_NOLN(F(":"));
//DEBUG(Hcoucher_minutes);
float delta_lever = current_time - Hlever;
float delta_coucher = current_time - Hcoucher;
float Sol_duree = Hcoucher - Hlever;
float Night_duree = 24 - Sol_duree;
int Sol_duree_hours = int(Sol_duree);
int Sol_duree_minutes = int((Sol_duree - Sol_duree_hours) * 60);
//DEBUG_NOLN(F("Sol_duree : "));
//DEBUG_NOLN(Sol_duree_hours);
//DEBUG_NOLN(F(":"));
//DEBUG(Sol_duree_minutes);
//DEBUG(F("Delta lever : "));
//printFloatln(delta_lever);
//DEBUG(F("Delta coucher : "));
//printFloatln(delta_coucher);
float S_var_mean;
float current_S_var;
// TODO : properly manage case between hour = 0 and hour = Hlever (check night's hour of previous day)
if ((current_time > Hlever) && (current_time < Hcoucher))
{
S_var_mean = Sol_duree / 12;
current_S_var = 1.0 + (S_var_mean - 1.0) * M_PI / 2.0 * sin(delta_lever * M_PI / Sol_duree);
}
else
{
S_var_mean = Night_duree / 12;
current_S_var = 1.0 + (S_var_mean - 1.0) * M_PI / 2.0 * sin(delta_coucher * M_PI / Night_duree);
}
//DEBUG_NOLN(F("Moyenne de S-var : "));
//printFloatln(S_var_mean);
//DEBUG_NOLN(F("Current_S-var : "));
//printFloatln(current_S_var);
float H_var;
if ((current_time > Hlever) && (current_time < Hcoucher))
{
H_var = delta_lever + (1.0/S_var_mean-1) * Sol_duree / 2.0 * (1.0 - cos(delta_lever * M_PI / Sol_duree));
}
else
{
H_var = delta_coucher + (1.0/S_var_mean-1) * Night_duree / 2.0 * (1.0 - cos(delta_coucher * M_PI / Night_duree));
}
int H_var_hours = int(H_var);
int H_var_minutes = (H_var - H_var_hours) * 60;
//DEBUG_NOLN(F("Current H-var : "));
//DEBUG(H_var_hours);
//DEBUG_NOLN(F(";"));
//DEBUG(H_var_minutes);
messageForDisplay[0] = '\0';
appendString(messageForDisplay,"Hstand: ");
//hour + 1 for Hstand = UTC+1
printHourInString(messageForDisplay+8, hour + 1, minutes, ':');
appendString(messageForDisplay," ");
appendString(messageForDisplay, "S_var: ");
printFloatInString(messageForDisplay+28, current_S_var);
appendString(messageForDisplay," ");
appendString(messageForDisplay,"H_var: ");
printHourInString(messageForDisplay+49, H_var_hours,H_var_minutes, ';');
DEBUG(F("Printing on screen"));
DEBUG(messageForDisplay);
//gotoXY(0, 0);
}