-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alexandrin.ino
197 lines (167 loc) · 3.59 KB
/
Alexandrin.ino
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
#include "SoftwareSerialZero.h"
#include <TinyScreen.h>
#include <TinyGPS++.h>
#include "upheaval30pt.h"
// GPS pins
const int GPS_ONOFFPin = A3;
const int GPS_SYSONPin = A2;
const int GPS_RXPin = A1;
const int GPS_TXPin = A0;
const int GPSBaud = 4800;
// Display dimensions
const int screenWidth = 96;
const int screenHeight = 64;
// GPS vars
TinyGPSPlus gps;
SoftwareSerial ss(GPS_RXPin, GPS_TXPin);
// Display
TinyScreen display = TinyScreen(TinyScreenPlus);
// Currently waiting for a GPS fix?
bool locating = false;
// display Y offset of current "new" line
int nextHeight = 0;
//=======================================
// Utils
//=======================================
/**
* Display centered text
* @param str Text to display
* @param y optionnal Y coord on screen, will use current "free" Y
* if not specified
*/
void centerText(String str, int y = -1)
{
int size = display.getPrintWidth(&str[0]);
if (y == -1) {
y = nextHeight;
}
if (size > screenWidth) {
display.setCursor(0, y);
} else {
display.setCursor((screenWidth - size)/2, y);
}
display.print(str);
nextHeight = display.getFontHeight() + y;
}
/**
* Well, setup.
*/
void setup() {
// Init display
initDisplay(15);
// Say Hello
splashScreen();
// Init GPS
initGPS();
}
/**
* while true;
*/
void loop() {
checkLocating();
if (!locating)
{
displaySpeed();
}
readGPS(1000);
}
//=======================================
// GPS related Stuff
//=======================================
/**
* Read and encode GPS data
* @param ms reading time
*/
static void readGPS(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
/**
* Handle GPS signal. We won't do anything unless we have a fix.
*/
void checkLocating() {
if (!gps.location.isValid()) {
// Service message displayed?
if (!locating) {
display.clearScreen();
display.setFont(liberationSans_8ptFontInfo);
centerText("Need more signal...", 0);
locating = true;
}
} else {
if (locating) {
display.clearScreen();
locating = false;
}
}
}
/**
* Init GPS Routine
*/
void initGPS()
{
display.clearScreen();
ss.begin(GPSBaud);
pinMode(GPS_SYSONPin, INPUT);
digitalWrite(GPS_ONOFFPin, LOW);
pinMode(GPS_ONOFFPin, OUTPUT);
delay(100);
display.setCursor(0,0);
display.print("Waking up GPS...");
while (digitalRead(GPS_SYSONPin) == LOW)
{
// Need to wake the module
digitalWrite(GPS_ONOFFPin, HIGH);
delay(5);
digitalWrite(GPS_ONOFFPin, LOW);
delay(100);
}
centerText("Done!");
delay(200);
}
//=======================================
// Display
//=======================================
/**
* Init display routine
* @param brightness desired brightness 0-15
*/
void initDisplay(int brightness)
{
display.begin();
display.setFlip(1);
display.on();
display.setBrightness(brightness);
display.fontColor(TS_16b_White, TS_16b_Black);
display.setFont(liberationSans_12ptFontInfo);
}
/**
* Welcome text/image/whatever
*/
void splashScreen()
{
int nextHeight;
display.clearScreen();
display.fontColor(TS_16b_White, TS_16b_Black);
display.setFont(liberationSans_12ptFontInfo);
centerText("Je suis mon", 0);
centerText("cher ami,");
centerText("tres heureux");
centerText("de te voir.");
delay(2000);
}
/**
* Display current speed
* @return [description]
*/
void displaySpeed() {
display.clearScreen();
display.setFont(upheavalTTBRK_30ptFontInfo);
String groundSpeed = String(gps.speed.kmph(), 1);
centerText(groundSpeed, 24);
}