-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathd1-helsinki-tram-times.ino
1788 lines (1476 loc) · 45.4 KB
/
d1-helsinki-tram-times.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Helsinki Tram-Departure Display - https://steve.fi/Hardware/
//
// This is a simple program which uses WiFi & an LCD display
// to show useful information.
//
// The top line of the display will default to showing the current time,
// and date. But it might display a fixed message, or the temperature
// instead.
//
// Each additional line of the display will show the departure
// of the next tram from the chosen stop. For example we'd
// we would see something like this on a 4x20 LCD display:
//
// #######################
// # 11:12:13 Fri 21 Apr #
// # Line 4B @ 11:17 #
// # Line 7B @ 11:35 #
// # Line 10 @ 11:45 #
// #######################
//
//
// There is a simple HTTP-server which is running on port 80, which
// will mirror the LCD-contents, and allow changes to be made to the
// device.
//
// For example the user can change the time-zone offset, the tram-stop,
// toggle the backlight, or even change the remote URL being polled.
//
// The HTTP-server also allows changing the display-mode too.
//
// The user-selectable-changes will be persisted to flash memory, so
// they survive reboots.
//
// Because the Helsinki Tram API is a complex GraphQL-beast we're getting
// data via a simple helper:
//
// https://api.steve.fi/Helsinki-Transport/
//
// We use a similar site to get the temperature, if that is selected as
// a display mode (via the web-UI):
//
// https://api.steve.fi/Helsinki-Temperature/
//
//
// Optional Button
// --------------
//
// If you wire a button between D0 & D8 you can control the device
// using it:
//
// Single press & release
// Toggle the backlight.
//
// Double-Click
// Show IP and other data.
//
// Long-press & release
// Resync time & departure-data.
//
//
// Steve
// --
//
//
// The name of this project.
//
// Used for the Access-Point name, and for OTA-identification.
//
#define PROJECT_NAME "TRAM-TIMES"
//
// The number of rows/columns of our display.
//
#define NUM_ROWS 4
#define NUM_COLS 20
//
// The URL to fetch our tram-data from.
//
// The string `__ID__` in this URL will be replaced by the ID of the stop.
//
#define DEFAULT_API_ENDPOINT "http://api.steve.fi/Helsinki-Transport/data/__ID__"
//
// The temperature API URL
//
#define DEFAULT_WEATHER_ENDPOINT "http://api.steve.fi/Helsinki-Temperature/data/"
//
// The default stop to monitor.
//
#define DEFAULT_TRAM_STOP "1160404"
//
// For WiFi setup.
//
#include "WiFiManager.h"
//
// We persist our settings to flash, so we need this.
//
#include <FS.h>
//
// WiFi & over the air updates
//
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
//
// For driving the LCD
//
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
//
// For dealing with NTP & the clock.
//
#include "NTPClient.h"
//
// Debug messages over the serial console.
//
#include "debug.h"
//
// The button handler
//
#include "OneButton.h"
//
// For fetching URLS & handling URL-parameters
//
#include "url_fetcher.h"
#include "url_parameters.h"
//
// Forward-declaration of function-prototypes.
//
String read_file(const char *path);
void draw_line(int row, const char *txt);
void access_point_callback(WiFiManager* myWiFiManager);
void on_before_ntp();
void on_after_ntp();
void fetch_tram_times();
void handlePendingButtons();
void on_short_click();
void on_long_click();
void on_double_click();
void processHTTPRequest(WiFiClient client);
void set_display_mode(const char *mode);
//
// NTP client, and UDP socket it uses.
//
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
//
// Timezone offset from GMT
//
int time_zone_offset = 0;
//
// The HTTP-server we present runs on port 80.
//
WiFiServer server(80);
//
// The purpose of our project is to display tram/bus departures from
// a given stop. However we also show the time/date upon the first
// screen.
//
// There are a couple of other display-modes which may be selected:
//
// Time & date.
// Time & temperature.
// Time & date/temperature - swapping every ten seconds.
// A fixed message.
//
// Here we describe those possible states
//
typedef enum {DATE, DATE_OR_TEMP, MESSAGE, TEMPERATURE} state;
//
// Our currently-selected display-mode.
//
state g_state = DATE;
//
// Our current temperature, as fetched via the remote API.
//
char g_temp[10] = {'\0'};
//
// Our current message, if any, which has been set by a HTTP-client
//
char g_msg[128] = { '\0' };
//
// The ID of the bus/tram stop we're going to display departures for.
//
char tram_stop[12] = { '\0' };
//
// The API end-point to return departure-data from.
//
char api_end_point[256] = { '\0' };
//
// The API end-point for getting temperature.
//
char temp_end_point[256] = { '\0' };
//
// This two-dimensional array holds the text that we're
// going to display upon our LCD.
//
// The first line will ALWAYS be the time/date, or the other
// data depending upon the display-mode.
//
// Any additional lines will contain the departure
// time of the next tram(s).
//
char screen[NUM_ROWS][NUM_COLS];
//
// Set the LCD address to 0x27, and define the number
// of rows & columns.
//
LiquidCrystal_I2C lcd(0x27, NUM_COLS, NUM_ROWS);
//
// Is the backlight lit? Defaults to true.
//
bool backlight = true;
//
// Time for the backlight to go on/off automatically
//
// This is set via the user-interface, and is optional.
//
int backlight_on = -1;
int backlight_off = -1;
//
// Setup a new OneButton on pin D8.
//
OneButton button(D8, false);
//
// Are there pending clicks to process?
//
volatile bool short_click = false;
volatile bool double_click = false;
volatile bool long_click = false;
//
// This function is called when the device is powered-on.
//
void setup()
{
//
// Enable our serial port.
//
#ifdef DEBUG
Serial.begin(115200);
#endif
//
// Enable access to the filesystem.
//
SPIFFS.begin();
//
// Load the tram-stop if we can
//
String tram_stop_str = read_file("/tram.stop");
if (tram_stop_str.length() > 0)
strncpy(tram_stop, tram_stop_str.c_str(), sizeof(tram_stop) - 1);
else
strncpy(tram_stop, DEFAULT_TRAM_STOP, sizeof(tram_stop) - 1);
//
// Load the API end-point if we can
//
String api_end_str = read_file("/tram.api");
if (api_end_str.length() > 0)
strncpy(api_end_point, api_end_str.c_str(), sizeof(api_end_point) - 1);
else
strncpy(api_end_point, DEFAULT_API_ENDPOINT, sizeof(api_end_point) - 1);
//
// Load the temperature-endpoint, if we can.
//
String weather_end = read_file("/temp.api");
if (weather_end.length() > 0)
strncpy(temp_end_point, weather_end.c_str(), sizeof(temp_end_point) - 1);
else
strncpy(temp_end_point, DEFAULT_WEATHER_ENDPOINT, sizeof(temp_end_point) - 1);
//
// Load the time-zone offset, if we can
//
String tz_str = read_file("/time.zone");
if (tz_str.length() > 0)
time_zone_offset = tz_str.toInt();
//
// Load our display-mode, if we can
//
String md = read_file("/display.mode");
if (md.length() > 0)
set_display_mode(md.c_str());
//
// Load our message, if we can.
//
String msg = read_file("/text.msg");
if (msg.length() > 0)
{
strncpy(g_msg, msg.c_str(), sizeof(g_msg) - 1);
}
//
// If we have a schedule for the backlight then load
// that here too.
//
// That involves two times; one to go off and one to
// go on.
//
String bon = read_file("/b.on");
if (bon.length() > 0)
{
backlight_on = atoi(bon.c_str());
DEBUG_LOG("Backlight schedule turns on at %d\n", backlight_on);
}
String boff = read_file("/b.off");
if (boff.length() > 0)
{
backlight_off = atoi(boff.c_str());
DEBUG_LOG("Backlight schedule turns off at %d\n", backlight_off);
}
//
// initialize the LCD
//
lcd.begin();
lcd.setBacklight(true);
//
// Show a message.
//
draw_line(0, "Starting up ..");
//
// Handle the connection to the local WiFi network.
//
WiFiManager wifiManager;
wifiManager.setAPCallback(access_point_callback);
wifiManager.autoConnect(PROJECT_NAME);
//
// Now we're connected show the local IP address.
//
draw_line(0, "WiFi Connected");
draw_line(1, WiFi.localIP().toString().c_str());
//
// Allow the IP to be visible.
//
delay(1500);
//
// Ensure our NTP-client is ready.
//
timeClient.begin();
//
// Configure the NTP-callbacks.
//
timeClient.on_before_update(on_before_ntp);
timeClient.on_after_update(on_after_ntp);
//
// Setup the timezone & update-interval.
//
timeClient.setTimeOffset(time_zone_offset * (60 * 60));
timeClient.setUpdateInterval(300 * 1000);
//
// Now we can start our HTTP server
//
server.begin();
DEBUG_LOG("HTTP-Server started on http://%s/\n",
WiFi.localIP().toString().c_str());
//
// Allow over the air updates.
//
ArduinoOTA.setHostname(PROJECT_NAME);
ArduinoOTA.onStart([]()
{
draw_line(0, "OTA Start");
});
ArduinoOTA.onEnd([]()
{
draw_line(0, "OTA Ended");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
char buf[NUM_COLS + 1];
memset(buf, '\0', sizeof(buf));
snprintf(buf, NUM_COLS, "Upgrade - %02u%%", (progress / (total / 100)));
draw_line(0, buf);
});
ArduinoOTA.onError([](ota_error_t error)
{
if (error == OTA_AUTH_ERROR)
draw_line(0, "Auth Failed");
else if (error == OTA_BEGIN_ERROR)
draw_line(0, "Begin Failed");
else if (error == OTA_CONNECT_ERROR)
draw_line(0, "Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
draw_line(0, "Receive Failed");
else if (error == OTA_END_ERROR)
draw_line(0, "End Failed");
});
//
// Ensure the OTA process is running & listening.
//
ArduinoOTA.begin();
//
// We support the use of an optional switch between D8 & D0.
//
// Configure that here.
//
pinMode(D8, INPUT_PULLUP);
pinMode(D0, OUTPUT);
digitalWrite(D0, HIGH);
//
// Configure the button-action(s).
//
button.attachClick(on_short_click);
button.attachDoubleClick(on_double_click);
button.attachLongPressStop(on_long_click);
}
//
// Record that a short-click happened.
//
void on_short_click()
{
short_click = true;
}
//
// Record that a double-click happened.
//
void on_double_click()
{
double_click = true;
}
//
// Record that a long-click happened.
//
void on_long_click()
{
long_click = true;
}
//
// Called just before the date/time is updated via NTP
//
void on_before_ntp()
{
draw_line(NUM_ROWS - 1, "Updating date & time");
}
//
// Called just after the date/time is updated via NTP
//
void on_after_ntp()
{
draw_line(NUM_ROWS - 1, "Updated date & time");
}
//
// If we're unconfigured we run an access-point.
//
// Show that, explicitly.
//
void access_point_callback(WiFiManager* myWiFiManager)
{
draw_line(0, "AccessPoint Mode");
draw_line(1, PROJECT_NAME);
if (NUM_ROWS > 2)
{
draw_line(2, "IP Address:");
draw_line(3, WiFi.localIP().toString().c_str());
}
}
//
// This function is called continuously.
//
void loop()
{
//
// Keep the previous time, to avoid needless re-draws
//
static char prev_time[NUM_COLS] = { '\0'};
//
// If we're in the date/temperature mode we need to keep track
// of the last time we swapped state, and what state we're currently
// in.
//
static long last_change = millis();
static state g_temp_date = DATE;
//
// If we're displaying a fixed-message, instead of the time/similar
// then we might need to handle scrolling it - if it is longer than
// our display-width.
//
// Keep track of the offset here.
//
static int msg_offset = 0;
//
// Handle any pending over the air updates.
//
ArduinoOTA.handle();
//
// Resync the clock.
//
timeClient.update();
//
// Process our button - looking for clicks, double-clicks, & long-clicks
//
button.tick();
//
// Handle any pending clicks here.
//
handlePendingButtons();
//
// Get the current time.
//
// We save this, so that we can trigger actions such as updating
// the departure-data, and reloading the temperature data.
//
int hour = timeClient.getHours();
int min = timeClient.getMinutes();
int sec = timeClient.getSeconds();
int year = timeClient.getYear();
String d_name = timeClient.getWeekDay();
String m_name = timeClient.getMonth();
int day = timeClient.getDayOfMonth();
// Prefix for the debug-output will have our time in it.
char tmp_debug_date[11];
snprintf(tmp_debug_date, sizeof(tmp_debug_date)-1, "%02d:%02d:%02d ",
hour, min, sec);
debug_prefix = String(tmp_debug_date);
//
// Every two minutes we'll update the departure times.
//
// We also do it immediately the first time we're run,
// when there is no pending time available.
//
if ((strlen(screen[1]) == 0) || ((min % 2 == 0) && (sec == 0)))
fetch_tram_times();
//
// Every half hour we'll update the temperature, or initially if empty.
//
// Note that we don't bother unless we're in a mode where the
// temperature might be displayed.
//
if (g_state == TEMPERATURE || g_state == DATE_OR_TEMP)
if ((strlen(g_temp) == 0) || ((min % 30 == 0) && (sec == 0)))
fetch_temperature();
//
// Every 10 seconds we swap between date + temp if either is displayed.
//
// NOTE: We have to record the time of the last change here
// because otherwise this loop might come around while the
// second value hasn't changed. That would result in the
// mode being changed N times in a second, with much confusion
// and hilarity.
//
// Of course this only makes sense if we have a temperatur-end-point
// setup, so ignore this if we don't.
//
if ((g_state == DATE_OR_TEMP) && (strlen(temp_end_point) > 0))
{
if (((sec % 10) == 0) && ((millis() - last_change) > 1000))
{
switch (g_temp_date)
{
case DATE:
g_temp_date = TEMPERATURE;
break;
case TEMPERATURE:
g_temp_date = DATE;
break;
}
// Record the last time we transitioned state.
last_change = millis();
}
}
//
// Clear the first row of the display.
//
memset(screen[0], '\0', NUM_COLS);
//
// Decide what to draw.
//
switch (g_state)
{
case DATE:
snprintf(screen[0], NUM_COLS, "%02d:%02d:%02d %s %02d %s %04d",
hour, min, sec, d_name.c_str(), day, m_name.c_str(), year);
break;
case TEMPERATURE:
snprintf(screen[0], NUM_COLS, "%02d:%02d:%02d %s",
hour, min, sec, g_temp);
break;
case DATE_OR_TEMP:
// repetition here is bad
switch (g_temp_date)
{
case DATE:
snprintf(screen[0], NUM_COLS, "%02d:%02d:%02d %s %02d %s %04d",
hour, min, sec, d_name.c_str(), day, m_name.c_str(), year);
break;
case TEMPERATURE:
snprintf(screen[0], NUM_COLS, "%02d:%02d:%02d %s",
hour, min, sec, g_temp);
break;
}
break;
case MESSAGE:
// Drawing a fixed message. There are two cases, where
// the text is "short" and fits, and when the text must be
// scrolled.
int len = strlen(g_msg);
if (len < NUM_COLS)
{
snprintf(screen[0], NUM_COLS - 1, "%s", g_msg);
}
else
{
// Draw the message.
memset(screen[0], '\0', NUM_COLS);
snprintf(screen[0], NUM_COLS - 1, "%s", g_msg + msg_offset);
// Ensure that we absolutely, definitely, null-terminate it.
screen[0][NUM_COLS - 1] = '\0';
// Bump the offset for next time we redraw the text.
if ((millis() - last_change) > 333)
{
msg_offset += 1;
// but don't walk off the end of the string.
if (msg_offset >= len)
msg_offset = 0;
last_change = millis();
}
}
break;
}
//
// Now draw all the rows - correctly doing this
// after the previous steps might have updated
// the display.
//
// That avoids showing outdated information, albeit
// information that is only outdated for <1 second!
//
// NOTE: We delay() for less than a second in this function
// so we cheat here, only updating the LCD if the currently
// formatted time is different to that we set in the past.
//
// i.e. We don't re-draw the screen unless the first-line has changed.
//
// If the real-time hasn't changed then the tram-arrival times
// haven't changed either, since that only updates every two minutes.
//
if (strcmp(prev_time, screen[0]) != 0)
{
for (int i = 0; i < NUM_ROWS; i++)
draw_line(i, screen[i]);
//
// Keep the current time in our local/static buffer.
//
strcpy(prev_time, screen[0]);
}
//
// Optionally we allow disabling the backlight on a schedule.
//
// We test for this every hour, on the hour. If a schedule hasn't
// been setup then the `on` and `off` times will be `-1`, so
// they will never match and we're safe.
//
if ((min == 0) && (sec == 0))
{
// The time for it to go on.
if (backlight_on == hour)
{
if (! backlight)
{
backlight = true;
lcd.setBacklight(backlight);
}
}
// The time for it to go off
if (backlight_off == hour)
{
if (backlight)
{
backlight = false;
lcd.setBacklight(backlight);
}
}
}
//
// Check if a client has connected to our HTTP-server.
//
// If so handle it.
//
// (This allows changing the stop, timezone, backlight, etc.)
//
WiFiClient client = server.available();
if (client)
processHTTPRequest(client);
//
// Now sleep a little.
//
delay(10);
}
//
// Set the display-mode appropriately, given a string describing
// the desired state.
// This function might be called as a result of a HTTP-POST, or
// by reading the mode on-startup.
//
void set_display_mode(const char *mode)
{
if (strcmp(mode, "date") == 0)
{
g_state = DATE;
}
if (strcmp(mode, "temp") == 0)
{
g_state = TEMPERATURE;
}
if (strcmp(mode, "dt") == 0)
{
g_state = DATE_OR_TEMP;
}
if (strcmp(mode, "msg") == 0)
{
g_state = MESSAGE;
}
}
//
// We bind our button such that short-clicks, long-clicks,
// and double-clicks will invoke a call-back.
//
// The callbacks just record the pending state, and here we
// process any of them that were raised.
//
void handlePendingButtons()
{
//
// If we have a pending-short-click then handle it
//
if (short_click)
{
short_click = false;
DEBUG_LOG("Short Click\n");
// Toggle the state of the backlight
backlight = !backlight;
lcd.setBacklight(backlight);
}
//
// If we have a pending long-click then handle it.
//
if (long_click)
{
long_click = false;
DEBUG_LOG("Long Click\n");
// Update the date/time
timeClient.forceUpdate();
// Refresh the tram data
fetch_tram_times();
// Refresh the temperature
if (g_state == TEMPERATURE || g_state == DATE_OR_TEMP)
fetch_temperature();
}
//
// If we have a pending double-click then handle it
//
if (double_click)
{
double_click = false;
DEBUG_LOG("Double Click\n");
char line[NUM_COLS + 1];
// Line 0 - About
draw_line(0, "Steve Kemp - Trams");
// Line 1 - IP
snprintf(line, NUM_COLS, "IP: %s", WiFi.localIP().toString().c_str());
draw_line(1, line);
// Line 2 - Timezone
if (time_zone_offset > 0)
snprintf(line, NUM_COLS, "Timezone: +%d", time_zone_offset);
else if (time_zone_offset < 0)
snprintf(line, NUM_COLS, "Timezone: -%d", time_zone_offset);
else
snprintf(line, NUM_COLS, "Timezone: %d", time_zone_offset);
draw_line(2, line);
// Line 3 - Tram ID
snprintf(line, NUM_COLS, "Tram ID : %s", tram_stop);
draw_line(3, line);
delay(2500);
}
}
//
// Given a bunch of CSV, containing departures, we parse this and
// update the screen-array.
//
// We handle CSV of the form:
//
// NNNNN,HH:MM:SS,Random-Text
//
// The first field is the tram/bus ID, the second is the time in
// full hour-minute-second format, and the third is the name of the
// route.
//
// We'll cope with a bus/tram-ID of up to five characters, and we'll
// keep the display neat by truncating the time at HH:MM.
//
// The name of the route is not displayed.
//
void update_tram_times(const char *txt)
{
char* pch = NULL;
int line = 1;
pch = strtok((char *)txt, "\r\n");
while (pch != NULL)
{
//
// If we got a line, and it is at least ten characters
// long then it is probably valid.
//
// The line will be:
//
// NN,HH:MM:SS,NAME
//
// So if we assume a two-digit ID such as "10", "7A", "4B",
// and the six digits of the time, then ten is a reasonable
// bound on the minimum-length of a valid-line.
//
if ((strlen(pch) > 10) && (line < NUM_ROWS))
{
//
// Skip newlines / carriage returns
//
while (pch[0] == '\n' || pch[0] == '\r')
pch += 1;
//
// Look for the first comma, which seperates
// the tram/bus-number and the time.
//
// We don't know how long that bus/tram ID
// will be. But we'll assume <=6 characters
// later on.
//
char *comma = strchr(pch, ',');
//
// If we found a comma then proceed.
//
if (comma != NULL)