-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallNMEA2000.cpp
749 lines (658 loc) · 23.1 KB
/
SmallNMEA2000.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
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
#include <Arduino.h>
#include <SPI.h>
#include <mcp_can.h>
#include "SmallNMEA2000.h"
bool SNMEA2000::open(byte clockSet) {
if ( canIsOpen ) {
return true;
}
uint8_t res = CAN.begin(CAN_250KBPS, clockSet);
if (res == CAN_OK ) {
canIsOpen = true;
delay(200);
claimAddress();
return true;
} else {
console->print(F("CAN Fail code:"));
console->println(res);
}
return false;
}
void SNMEA2000::processMessages() {
if ( ! canIsOpen ) {
return;
}
unsigned char len = 0;
int frames = 0;
unsigned char buf[8];
hasClaimedAddress();
while(frames < 20 && CAN_MSGAVAIL == CAN.checkReceive()){
frames++;
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
MessageHeader messageHeader(CAN.getCanId());
messagesRecieved++;
switch (messageHeader.pgn) {
case 59392L: /*ISO Acknowledgement*/
if ( diagnostics ) {
console->print(F("can: <ack"));
messageHeader.print(console, buf,len);
}
break;
case 59904L: /*ISO Request*/
if ( diagnostics ) {
console->print(F("can: <req"));
messageHeader.print(console, buf,len);
}
handleISORequest(&messageHeader, buf, len);
break;
case 60928L: /*ISO Address Claim*/
if ( diagnostics ) {
console->print(F("can: <claim"));
messageHeader.print(console, buf,len);
}
handleISOAddressClaim(&messageHeader, buf, len);
break;
default:
if ( diagnostics ) {
console->print(F("can: <unknown"));
messageHeader.print(console, buf,len);
}
}
}
}
void SNMEA2000::print_uint64_t(uint64_t num) {
char rev[128];
char *p = rev+1;
while (num > 0) {
*p++ = '0' + ( num % 10);
num/= 10;
}
p--;
/*Print the number which is now in reverse*/
while (p > rev) {
console->print(*p--);
}
}
void SNMEA2000::handleISOAddressClaim(MessageHeader *messageHeader, byte * buffer, int len) {
if ( messageHeader->source == 254 ) return; // annother node cannot claim an address, ignore this.
tUnionDeviceInformation * remoteDeviceInfo = (tUnionDeviceInformation *)(&buffer[0]);
if ( messageHeader->source == deviceAddress ) {
// annother device is claiming this address
console->print(F("can: 2 node claiming this address. local:"));
print_uint64_t(devInfo->getName());
console->print(F(" remote:"));
print_uint64_t(remoteDeviceInfo->name);
console->print(F(" msg:"));
messageHeader->print(console, buffer, len);
if (devInfo->getName() == remoteDeviceInfo->name ) {
// should not happen. This means that we have a collision of 2 of the same devices on the same bus
// with the same device instance number in the name.
// rather than go into an infinite loop both should randomly choose a new instance number.
// and try again. For some devices, the instance number is used in the messages eg BatteryInstance but for
// most of devices based on this library thats not the case, and its not currently possible to
// set the instance number using NMEA2000 group functions.
console->println(F("can: Claim from remote is identical, duplicate device names "));
uint8_t newInstance = 0xfe & millis();
devInfo->setDeviceInstanceNumber(newInstance);
console->print(F("can: Device Instances set to "));
console->println(newInstance, DEC);
} else if (devInfo->getName() > remoteDeviceInfo->name ) {
// but our name is > callers so we must increment address and claim
//
console->println(F("can: Claim from remote has higher precidence"));
deviceAddress++;
} else {
console->println(F("can: Claim from remote has lower precidence"));
}
claimAddress();
}
}
void SNMEA2000::claimAddress() {
clearRXFilter();
rxFiltersSet = true;
sendIsoAddressClaim();
addressClaimStarted = millis();
console->print(F("Send Address claim for:"));
console->print(deviceAddress);
console->print(F(" at "));
console->println(addressClaimStarted);
}
bool SNMEA2000::hasClaimedAddress() {
if ( (addressClaimStarted != 0) &&
(millis()-addressClaimStarted > 250) ) {
console->print(F("Address claimed as "));
console->print(deviceAddress);
console->print(F(" at "));
console->println(millis());
rxFiltersSet = setupRXFilter();
addressClaimStarted = 0;
}
return (addressClaimStarted==0);
}
void SNMEA2000::handleISORequest(MessageHeader *messageHeader, byte * buffer, int len) {
if ( len < 3 || len > 8) {
return; // ISO requests are expected to be between 3 and 8 bytes.
}
if (messageHeader->destination != 0xff && messageHeader->destination != deviceAddress ) {
return;
}
if (!hasClaimedAddress()) {
return; // dont respond to queries until address is claimed.
}
unsigned long requestedPGN = (((unsigned long )buffer[2])<<16)|(((unsigned long )buffer[1])<<8)|(buffer[0]);
switch(requestedPGN) {
case 60928L: /*ISO Address Claim*/ // Someone is asking others to claim their addresses
sendIsoAddressClaim();
break;
case 126464L:
sendPGNLists(messageHeader);
break;
case 126996L: /* Product information */
sendProductInformation(messageHeader);
break;
case 126998L: /* Configuration information */
sendConfigurationInformation(messageHeader);
break;
default:
if ( isoRequestHandler == NULL || !isoRequestHandler(requestedPGN, messageHeader, buffer, len) ) {
console->print(F("Not Known"));
console->println(requestedPGN);
sendIsoAcknowlegement(messageHeader, 1, 0xff);
}
}
}
// the PDN lists are
void SNMEA2000::sendPGNLists(MessageHeader *requestMessageHeader) {
MessageHeader messageHeader(126464L, 6, deviceAddress, requestMessageHeader->source);
// 126464L structure is a fast packet sequence with the
// total length is 1+npgns*3
sendPGNList(&messageHeader, 0, txPGNList);
sendPGNList(&messageHeader, 1, rxPGNList);
}
void SNMEA2000::sendPGNList(MessageHeader *messageHeader, int listType, const unsigned long *pgnList) {
int ipgn = 0; // pgn length
while(pgm_read_dword(&pgnList[ipgn++]) !=0 );
startFastPacket(messageHeader, 1+ipgn*3);
outputByte(listType); // RX PGN List
for(int i = 0; i < ipgn; i++) {
output3ByteInt(pgm_read_dword(&pgnList[i]));
}
finishFastPacket();
}
void SNMEA2000::sendIsoAddressClaim() {
MessageHeader messageHeader(60928L, 6, deviceAddress, 0xff);
// CAN and AVR are little endian, so this is ok.
sendMessage(&messageHeader,devInfo->getDeviceNameBuffer(), 8);
}
void SNMEA2000::sendProductInformation(MessageHeader *requestMessageHeader) {
MessageHeader messageHeader(126996L, 6, deviceAddress, requestMessageHeader->source);
// this is a fast packet, has to be constructed from the struc on the fly.
startFastPacket(&messageHeader, 4+32*4+2);
output2ByteUInt(pgm_read_word(&productInfo->nk2version));
output2ByteUInt(pgm_read_word(&productInfo->productCode));
outputFixedString(pgm_read_ptr(&productInfo->modelID), 32, 0xff);
outputFixedString(pgm_read_ptr(&productInfo->softwareVersion), 32, 0xff);
outputFixedString(pgm_read_ptr(&productInfo->modelVersion), 32, 0xff);
outputFixedString(pgm_read_ptr(&productInfo->serialNumber), 32, 0xff);
outputByte(pgm_read_byte(&productInfo->certificationLevel));
outputByte(pgm_read_byte(&productInfo->loadEquivalency));
finishFastPacket();
}
void SNMEA2000::sendConfigurationInformation(MessageHeader *requestMessageHeader) {
MessageHeader messageHeader(126998L, 6, deviceAddress, requestMessageHeader->source);
// this is a fast packet.
char * manufacturerInfo = pgm_read_ptr(&configInfo->manufacturerInfo);
char * installDesc1 = pgm_read_ptr(&configInfo->installDesc1);
char * installDesc2 = pgm_read_ptr(&configInfo->installDesc2);
int manufacturerInfoLen = strnlen(manufacturerInfo,70);
int installDesc1Len = strnlen(installDesc1,70);
int installDesc2Len = strnlen(installDesc2,70);
startFastPacket(&messageHeader, manufacturerInfoLen+installDesc1Len+installDesc2Len+6 );
outputVarString(manufacturerInfo, manufacturerInfoLen);
outputVarString(installDesc1, installDesc1Len);
outputVarString(installDesc2, installDesc2Len);
finishFastPacket();
}
void SNMEA2000::outputVarString(const char * str, uint8_t strLen) {
outputByte(strLen+2);
outputByte(0x01);
for(int i = 0; i < strLen; i++) {
outputByte(str[i]);
}
}
void SNMEA2000::outputFixedString(const char * str, int maxLen, byte padding) {
int ib = 0;
while( ib < maxLen ) {
byte bs = str[ib++];
outputByte(bs);
if (bs == '\0') break;
}
while (ib < maxLen) {
outputByte(padding);
ib++;
}
}
void SNMEA2000::output2ByteUInt(uint16_t i) {
outputByte(i&0xff);
outputByte((i>>8)&0xff);
}
void SNMEA2000::output2ByteInt(uint16_t i) {
outputByte(i&0xff);
outputByte((i>>8)&0xff);
}
void SNMEA2000::output3ByteInt(int32_t i) {
outputByte(i&0xff);
outputByte((i>>8)&0xff);
outputByte((i>>16)&0xff);
}
void SNMEA2000::output2ByteDouble(double value, double precision) {
if (value == SNMEA2000::n2kDoubleNA ) {
// udefined is 32767 = 0x7FFF
outputByte(0xff);
outputByte(0x7f);
} else {
double vd=value/precision;
vd = round(vd);
int16_t i = (vd>=-32768 && vd<0x7fee)?(int16_t)vd:0x7fee;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
}
}
void SNMEA2000::output2ByteUDouble(double value, double precision) {
// indef is 65535U = 0xFFFF
if (value == SNMEA2000::n2kDoubleNA) {
outputByte(0xff);
outputByte(0xff);
} else {
double vd=value/precision;
vd = round(vd);
uint16_t i = (vd>=0 && vd<0xfffe)?(int16_t)vd:0xfffe;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
}
}
void SNMEA2000::output3ByteDouble(double value, double precision) {
if (value == SNMEA2000::n2kDoubleNA ) {
// undef is 2147483647 = 7FFFFF
outputByte(0xff);
outputByte(0xff);
outputByte(0x7f);
} else {
double vd=value/precision;
vd = round(vd);
int32_t i = (vd>=-8388608L && vd<8388606L)?(int32_t)vd:8388606L;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
outputByte((i>>16)&0xff);
}
}
void SNMEA2000::output3ByteUDouble(double value, double precision) {
if (value == SNMEA2000::n2kDoubleNA ) {
// undef is 2147483647 = FFFFFF
outputByte(0xff);
outputByte(0xff);
outputByte(0xff);
} else {
double vd=value/precision;
vd = round(vd);
int32_t i = (vd>=0 && vd<16777214L)?(int32_t)vd:16777214L;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
outputByte((i>>16)&0xff);
}
}
void SNMEA2000::output4ByteDouble(double value, double precision) {
if (value == SNMEA2000::n2kDoubleNA ) {
// undef is 2147483647 = 7FFFFFFF
outputByte(0xff);
outputByte(0xff);
outputByte(0xff);
outputByte(0x7f);
} else {
double vd=value/precision;
vd = round(vd);
int32_t i = (vd>=-2147483648L && vd<2147483647L)?(int32_t)vd:2147483647L;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
outputByte((i>>16)&0xff);
outputByte((i>>24)&0xff);
}
}
void SNMEA2000::output4ByteUDouble(double value, double precision) {
if (value == SNMEA2000::n2kDoubleNA ) {
// undef is 4294967295U = FFFFFFFF
outputByte(0xff);
outputByte(0xff);
outputByte(0xff);
outputByte(0xff);
} else {
double vd=value/precision;
vd = round(vd);
uint32_t i = (vd>=0 && vd<0xfffffffe)?(uint32_t)vd:0xfffffffe;
outputByte(i&0xff);
outputByte((i>>8)&0xff);
outputByte((i>>16)&0xff);
outputByte((i>>24)&0xff);
}
}
bool SNMEA2000::setupRXFilter() {
// CAN ID is 29 bits long
// 111 1 1 11111111 11111111 11111111
// -------- Source Addresss 8 bits @0
// -------- PDU specific 8 bits @ 8
// -------- PDU Format 8 bits @ 16
// - Data Page 1 bit @24
// - reserved 1 bit @25
// --- priority (3 bits starting @26)
// PDU Format < 240 is transmitted with a destination address, which is in 8 bits 8
// PDU Format 240 > 255 is a broadcast with the Group extension in 8 bits @8.
// For all PDU < 240 we are only interested destination addresses
// of our destination or 0xff.
// For all PDU's >= 240 then we are only interested in packets matching
// the recieved PGNS.
// since we interested in 0xff messages, then that makes the mask and filter for the PSU specific part as 0xff 0xff which
//
// 59392L == 11101000 00000000 232 PDU1
// 59904L == 11101010 00000000 234 PDU1
// 60928L == 11101110 00000000 238 PDU1
//
// Accept for our device and 0xfff
// Mask 11111001 11111111 00000000 = 0xf9ff00
// Filter 59392L to us 11101000 <our device> 00000000
// Filter 59392L to 0xff 11101000 11111111 00000000
// Filter 59904L to 0xff 11101010 11111111 00000000
// Filter 60928L to 0xff 11101110 11111111 00000000
bool ret = true;
if (CAN.init_Mask(0,1,0xf9ff00) != MCP2515_OK) {
ret = false;
}
if (CAN.init_Mask(1,1,0xf9ff00) != MCP2515_OK ) {
ret = false;
}
// broadcasts
if (CAN.init_Filt(0,1,0xE8FF00) != MCP2515_OK ) {
ret = false;
}
// thisDevice
if (CAN.init_Filt(1,1,0xE80000 | (((uint16_t)deviceAddress<<8)&0xff00)) != MCP2515_OK ) {
ret = false;
}
return ret;
}
bool SNMEA2000::clearRXFilter() {
bool ret = true;
if ( CAN.init_Mask(0,1,0x0) != MCP2515_OK ) {
ret = false;
}
if ( CAN.init_Mask(1,1,0x0) != MCP2515_OK ) {
ret = false;
}
return ret;
}
void SNMEA2000::startPacket(MessageHeader *messageHeader) {
packetMessageHeader = messageHeader;
ob = 0;
fastPacket = false;
}
void SNMEA2000::finishPacket() {
if ( !fastPacket ) {
if ( ob > 0) {
sendMessage(packetMessageHeader, buffer, ob);
}
} else {
packetErrors++;
console->println(F("Error: Not a Single Packet"));
}
}
void SNMEA2000::startFastPacket(MessageHeader *messageHeader, int length) {
packetMessageHeader = messageHeader;
fastPacket = true;
fastPacketSequence++;
if (fastPacketSequence > 7 ) {
fastPacketSequence = 0;
}
frame = 0;
ob = 0;
buffer[ob++] = (fastPacketSequence << 5) | frame++;
buffer[ob++] = length;
fastPacketLength = length;
fastPacketSent = 0;
}
void SNMEA2000::checkFastPacket() {
if ( fastPacket ) {
if ( fastPacketLength > 223 ) {
console->print(F("Error: fastpacket too long:"));
console->println(fastPacketLength);
}
if ( fastPacketSent != fastPacketLength ) {
console->print(F("Error: FastPacket length wrong wanted:"));
console->print(fastPacketLength);
console->print(F(" got:"));
console->println(fastPacketSent);
}
} else {
console->println(F("not fast packet"));
}
}
void SNMEA2000::finishFastPacket() {
if (fastPacket ) {
if (ob > 1) {
// send remaining frame
sendMessage(packetMessageHeader, buffer, ob);
}
} else {
packetErrors++;
console->println(F("Error: Not a FastPacket"));
}
}
void SNMEA2000::outputByte(byte opb) {
if ( ob < 8 ) {
buffer[ob++] = opb;
fastPacketLength--;
if( fastPacket && ob == 8) {
sendMessage(packetMessageHeader, &buffer[0], 8);
ob = 0;
buffer[ob++] = (fastPacketSequence << 5) | frame++;
}
} else {
frameErrors++;
console->println(F("Error: Frame > 8 bytes"));
}
}
/*
void SNMEA2000::sendFastPacket(MessageHeader *messageHeader, byte *message, int len, bool progmem) {
startFastPacket(messageHeader, len);
int ib = 0;
while(ib < len) {
if ( progmem ) {
outputByte(pgm_read_byte(&message[ib++]));
} else {
outputByte(message[ib++]);
}
}
finishFastPacket();
}
*/
void SNMEA2000::sendIsoAcknowlegement(MessageHeader *requestMessageHeader, unsigned char control, unsigned char groupFunction) {
MessageHeader messageHeader(59392L, 6, deviceAddress, requestMessageHeader->source);
startPacket(&messageHeader);
outputByte(control);
outputByte(groupFunction);
outputByte(0xff);
outputByte(0xff);
outputByte(0xff);
output3ByteInt(requestMessageHeader->pgn);
finishPacket();
}
void SNMEA2000::sendMessage(MessageHeader *messageHeader, byte * message, int length) {
if ( ! canIsOpen ) {
return;
}
uint8_t res = CAN.sendMsgBuf(messageHeader->id, 1, length, message);
if ( res != CAN_OK ) {
console->print(F("can: err"));
console->println(res);
}
if ( diagnostics ) {
console->print(F("can: out>"));
messageHeader->print(console, message,length);
}
messagesSent++;
}
void EngineMonitor::sendRapidEngineDataMessage(
byte engineInstance,
double engineSpeed,
double engineBoostPressure,
byte engineTiltTrim) {
MessageHeader messageHeader(127488L, 2, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(engineInstance);
output2ByteUDouble(engineSpeed,0.25);
output2ByteUDouble(engineBoostPressure,100);
outputByte(engineTiltTrim);
outputByte(0xff);
outputByte(0xff);
finishPacket();
}
void EngineMonitor::sendEngineDynamicParamMessage(
byte engineInstance,
double engineHours,
double engineCoolantTemperature,
double alternatorVoltage,
uint16_t status1,
uint16_t status2,
double engineOilPressure,
double engineOilTemperature,
double fuelRate,
double engineCoolantPressure,
double engineFuelPressure,
byte engineLoad,
byte engineTorque
) {
MessageHeader messageHeader(127489L, 2, getAddress(), SNMEA2000::broadcastAddress);
startFastPacket(&messageHeader, 26);
outputByte(engineInstance);
output2ByteUDouble(engineOilPressure,100);
output2ByteUDouble(engineOilTemperature,0.1);
output2ByteUDouble(engineCoolantTemperature,0.01);
output2ByteDouble(alternatorVoltage,0.01);
output2ByteDouble(fuelRate,0.1);
output4ByteUDouble(engineHours,1);
output2ByteUDouble(engineCoolantPressure,100);
output2ByteUDouble(engineFuelPressure,1000);
outputByte(0xff); // reserved
output2ByteUInt(status1);
output2ByteUInt(status2);
outputByte(engineLoad);
outputByte(engineTorque);
finishFastPacket();
}
void EngineMonitor::sendDCBatterStatusMessage(
byte batteryInstance,
byte sid,
double batteryVoltage,
double batteryTemperature,
double batteryCurrent
) {
MessageHeader messageHeader(127508L, 6, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(batteryInstance);
output2ByteDouble(batteryVoltage,0.01);
output2ByteDouble(batteryCurrent,0.1);
output2ByteUDouble(batteryTemperature,0.01);
outputByte(sid);
finishPacket();
}
void EngineMonitor::sendFluidLevelMessage(
byte type,
byte instance,
double level,
double capacity) {
MessageHeader messageHeader(127505L, 6, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
byte b = ((type<<4)&0xff)|(instance&0xff);
outputByte(b);
output2ByteDouble(level,0.004);
output4ByteUDouble(capacity,0.1);
outputByte(0xff);
finishPacket();
}
void EngineMonitor::sendTemperatureMessage(
byte sid,
byte instance,
byte source,
double actual,
double requested) {
MessageHeader messageHeader(130312L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
outputByte(instance);
outputByte(source);
output2ByteUDouble(actual,0.01);
output2ByteUDouble(requested,0.01);
outputByte(0xff);
finishPacket();
}
void PressureMonitor::sendOutsideEnvironmentParameters(
byte sid,
double waterTemperature,
double outsideAirTemperature,
double atmospheicPressure
) {
MessageHeader messageHeader(130310L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
output2ByteUDouble(waterTemperature,0.01);
output2ByteUDouble(outsideAirTemperature,0.01);
output2ByteUDouble(atmospheicPressure,100);
outputByte(0xff);
finishPacket();
}
void PressureMonitor::sendEnvironmentParameters(
byte sid,
double atmosphericPressure, // Atomspheric Pressure
byte tempSource,
double temperature, // temperature
byte humiditySource,
double humidity // humidity
) {
MessageHeader messageHeader(130311L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
outputByte(((humiditySource) & 0x03)<<6 | (tempSource & 0x3f));
output2ByteUDouble(temperature,0.01);
output2ByteDouble(humidity,0.004);
output2ByteUDouble(atmosphericPressure,100);
finishPacket();
}
void PressureMonitor::sendHumidity(byte sid, byte humiditySource, byte humidityInstance, double humidity) {
MessageHeader messageHeader(130313L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
outputByte(humidityInstance);
outputByte(humiditySource);
output2ByteDouble(humidity,0.004);
output2ByteDouble(SNMEA2000::n2kDoubleNA,0.004);
outputByte(0xff);
finishPacket();
}
void PressureMonitor::sendPressure(byte sid, byte pressureSource, byte pressureInstance, double pressure ) {
MessageHeader messageHeader(130314L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
outputByte(pressureInstance);
outputByte(pressureSource);
output4ByteDouble(pressure,0.1);
outputByte(0xff);
finishPacket();
}
void PressureMonitor::sendTemperature(byte sid, byte temperatureSource, byte temperatureInstance, double tremperature ) {
MessageHeader messageHeader(130316L, 5, getAddress(), SNMEA2000::broadcastAddress);
startPacket(&messageHeader);
outputByte(sid);
outputByte(temperatureInstance);
outputByte(temperatureSource);
output3ByteUDouble(tremperature,0.001);
output2ByteUDouble(SNMEA2000::n2kDoubleNA,0.1);
finishPacket();
}