-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.cpp
186 lines (151 loc) · 6.55 KB
/
main.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
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
/*
The MIT License
Copyright (c) 2016 Thomas Sarlandie thomas@sarlandie.net
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <KBoxHardware.h>
#include <KBoxLoggerMultiplexer.h>
#include "common/signalk/SKHub.h"
#include "common/time/WallClock.h"
#include "host/config/KBoxConfig.h"
#include "host/config/KBoxConfigParser.h"
#include "host/os/TaskManager.h"
#include "host/drivers/ILI9341GC.h"
#include "host/pages/BatteryMonitorPage.h"
#include "host/pages/StatsPage.h"
#include "host/services/MFD.h"
#include "host/services/ADCService.h"
#include "host/services/BarometerService.h"
#include "host/services/IMUService.h"
#include "host/pages/IMUMonitorPage.h"
#include "host/services/NMEA2000Service.h"
#include "host/services/SerialService.h"
#include "host/services/RunningLightService.h"
#include "host/services/SDLoggingService.h"
#include "host/services/TimeService.h"
#include "host/services/USBService.h"
#include "host/services/WiFiService.h"
static const char *configFilename = "kbox-config.json";
ILI9341GC gc(KBox.getDisplay(), Size(320, 240));
MFD mfd(gc, KBox.getEncoder(), KBox.getButton());
TaskManager taskManager;
SKHub skHub;
KBoxConfig config;
USBService usbService(gc, skHub);
SDLoggingService sdLoggingService(config.sdLoggingConfig, skHub);
KBoxLoggerMultiplexer loggerMultiplexer(usbService, sdLoggingService);
void setup() {
// Enable float in printf:
// https://forum.pjrc.com/threads/27827-Float-in-sscanf-on-Teensy-3-1
asm(".global _printf_float");
Serial.begin(115200);
KBoxLogging.setLogger(&loggerMultiplexer);
KBox.setup();
// Clears the screen
mfd.setup();
delay(200);
DEBUG("Starting - reboot: %s", KBox.rebootReason().c_str());
digitalWrite(led_pin, 1);
// Generate a default URN based on MCU serial number.
const char *vesselURLFormat = "urn:mrn:signalk:uuid:%08lX-%04lX-4%03lX-%04lX-%04lX%08lX";
char defaultVesselURN[80];
tKBoxSerialNumber kboxSN;
KBox.readKBoxSerialNumber(kboxSN);
snprintf(defaultVesselURN, sizeof(defaultVesselURN), vesselURLFormat,
kboxSN.dwords[0], (uint16_t)(kboxSN.dwords[1] >> 16), (uint16_t)(kboxSN.dwords[1] & 0x0fff),
(uint16_t)(kboxSN.dwords[2] >> 16), (uint16_t)(kboxSN.dwords[2] & 0xffff), kboxSN.dwords[3]);
// Load configuration if available
KBoxConfigParser configParser(defaultVesselURN);
configParser.defaultConfig(config);
if (KBox.getSdFat().exists(configFilename)) {
File configFile = KBox.getSdFat().open(configFilename);
// We can afford to allocate a lot of memory on the stack for this because we have not started doing
// anything real yet.
StaticJsonBuffer<4096> jsonBuffer;
JsonObject &root =jsonBuffer.parseObject(configFile);
if (root.success()) {
DEBUG("Loading configuration from SDCard");
configParser.parseKBoxConfig(root, config);
}
else {
ERROR("Failed to parse configuration file");
}
}
else {
DEBUG("No configuration file found. Using defaults.");
}
// Instantiate all our services
WiFiService *wifi = new WiFiService(config.wifiConfig, skHub, gc);
ADCService *adcService = new ADCService(skHub, KBox.getADC());
BarometerService *baroService = new BarometerService(skHub);
IMUService *imuService = new IMUService(config.imuConfig, skHub);
NMEA2000Service *n2kService = new NMEA2000Service(config.nmea2000Config,
skHub);
n2kService->addSentenceRepeater(*wifi);
n2kService->addSentenceRepeater(usbService);
SerialService *reader1 = new SerialService(config.serial1Config, skHub, NMEA1_SERIAL);
SerialService *reader2 = new SerialService(config.serial2Config, skHub, NMEA2_SERIAL);
reader1->addRepeater(*wifi);
reader2->addRepeater(*wifi);
reader1->addRepeater(usbService);
reader2->addRepeater(usbService);
reader1->addRepeater(sdLoggingService);
reader2->addRepeater(sdLoggingService);
n2kService->addSentenceRepeater(sdLoggingService);
// Tell the wallClock how to get the number of ms elapsed since boot.
wallClock.setMillisecondsProvider(millis);
// Create a new instance of TimeService - We do not need a pointer to it.
// It will subscribe to the hub and update the wallClock.
new TimeService(skHub);
// Add all the tasks
taskManager.addTask(&mfd);
taskManager.addTask(new IntervalTask(new RunningLightService(), 250));
taskManager.addTask(new IntervalTask(adcService, 1000));
if (config.imuConfig.enabled) {
taskManager.addTask(new IntervalTask(imuService, 1000 / config.imuConfig.frequency));
}
if (config.barometerConfig.enabled) {
taskManager.addTask(new IntervalTask(baroService, 1000 / config.barometerConfig.frequency));
}
taskManager.addTask(n2kService);
taskManager.addTask(reader1);
taskManager.addTask(reader2);
taskManager.addTask(wifi);
taskManager.addTask(&sdLoggingService);
taskManager.addTask(&usbService);
StatsPage *statsPage = new StatsPage();
statsPage->setSDLoggingService(&sdLoggingService);
statsPage->setWiFiService(wifi);
mfd.addPage(statsPage);
if (config.imuConfig.enabled) {
// At the moment the IMUMonitorPage is working with built-in sensor only
IMUMonitorPage *imuPage = new IMUMonitorPage(config.imuConfig, skHub, *imuService);
mfd.addPage(imuPage);
}
BatteryMonitorPage *batPage = new BatteryMonitorPage(skHub);
mfd.addPage(batPage);
taskManager.setup();
KBox.watchdogSetup();
// Reinitialize debug here because in some configurations
// (like logging to nmea2 output), the kbox setup might have messed
// up the debug configuration.
DEBUG("setup done");
}
void loop() {
taskManager.loop();
KBox.watchdogRefresh();
}