-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHapcanDevice.cpp
764 lines (649 loc) · 19.9 KB
/
HapcanDevice.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "Arduino.h"
#include "HapcanDevice.h"
#include <avr/wdt.h>
#include <EEPROM.h>
using namespace Onixarts::HomeAutomationCore::Hapcan;
HapcanDevice* pHapcanDevice = NULL;
// Default constructor, initializes empty HapcanMessage
HapcanMessage::HapcanMessage()
: m_id(0)
{
// reset all data fields
memset(m_data, 0xFF, 8);
}
// Constructs HapcanMessage object from CAN id and buffer. Buffer must have 8 bytes length
HapcanMessage::HapcanMessage(unsigned long id, byte* buffer)
: m_id(id)
{
memcpy(m_data, buffer, 8);
}
// Constructs HapcanMessage object, Used to send data
HapcanMessage::HapcanMessage(unsigned int frameType, bool isAnswer, byte node, byte group)
{
InitMessageId(frameType, isAnswer, node, group);
// reset all data fields
memset(m_data, 0xFF, 8);
}
// Constructs HapcanMessage object, Used to send data. Node number and group is filled by device's data
HapcanMessage::HapcanMessage(unsigned int frameType, bool isAnswer)
{
InitMessageId(frameType, isAnswer, 0, 0);
// reset all data fields
memset(m_data, 0xFF, 8);
}
void HapcanMessage::PrintToSerial()
{
#ifdef OA_DEBUG
//Serial.print("Raw Frame type: ");
//Serial.println(m_id, HEX);
Serial.print(F("Frame: 0x"));
unsigned int temp1 = GetFrameType();
if (temp1 < 0x100)
Serial.print(F("0"));
Serial.print(temp1, HEX);
Serial.print(IsAnswer() ? F(" A") : F(" "));
Serial.print(F("\tNode ("));
Serial.print(GetNode());
Serial.print(F(","));
Serial.print(GetGroup());
Serial.print(F(")"));
Serial.print(F("\t\tdata: "));
for (int i = 0; i<8; i++) // Print each byte of the data
{
if (m_data[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print(F("0"));
}
Serial.print(m_data[i], HEX);
Serial.print(F(" "));
}
Serial.println();
#endif
}
HapcanDevice::HapcanDevice()
:CAN(Config::MCP::CSPin)
, m_RxBufferIndex(0)
, m_TxBufferIndex(0)
, m_RxBufferReadIndex(0)
, m_TxBufferReadIndex(0)
, m_rxBufferOverflowCount(0)
, m_txBufferOverflowCount(0)
, m_group(Hapcan::Config::Node::SerialNumber3)
, m_node(Hapcan::Config::Node::SerialNumber2)
, m_processOwnMessages(false)
, m_receiveAnswerMessages(false)
, m_isInProgrammingMode(false)
, m_isInitialized(false)
, m_memoryAddress(0)
, m_memoryCommand(Programming::Command::Undefined)
, m_uptime(0UL)
, m_lastMillis(0UL)
, m_executeInstructionDelegate(NULL)
, m_statusRequestDelegate(NULL)
{
pHapcanDevice = this;
}
// Initiate Hapcan Device. Call this method in Arduino setup() function
void HapcanDevice::Begin()
{
//Hapcan bus speed is 125Kbps
CAN.begin(MCP_ANY, CAN_125KBPS, Config::MCP::OscillatorFrequency);
CAN.setMode(MCP_NORMAL);
pinMode(Config::MCP::InterruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(Config::MCP::InterruptPin), OnCanReceivedDispatcher, FALLING);
OnInit();
ReadEEPROMConfig();
m_isInitialized = true;
}
void HapcanDevice::ReadEEPROMConfig()
{
// node reading
m_node = EEPROM[0x26];
// save initial node value to EEPROM if not set
if (m_node == 0xFF)
{
EEPROM.update(0x26, Config::Node::SerialNumber2);
m_node = Config::Node::SerialNumber2;
}
m_group = EEPROM[0x27];
// save initial group value to EEPROM if not set
if (m_group == 0xFF)
{
EEPROM.update(0x27, Config::Node::SerialNumber3);
m_node = Config::Node::SerialNumber3;
}
OnReadEEPROMConfig();
}
// Add message to RX FIFO buffer, with overflow check
void HapcanDevice::AddMessageToRxBuffer(HapcanMessage& message)
{
m_RxBuffer[m_RxBufferIndex] = message;
m_RxBufferIndex++;
if (m_RxBufferIndex >= Config::RxFifoQueueSize)
m_RxBufferIndex = 0;
// rx buffer overflow check
if (m_RxBufferIndex == m_RxBufferReadIndex)
{
m_rxBufferOverflowCount++;
//OA_LOG_LINE("RX Buffer overflow. Count = ");
//OA_LOG(m_rxBufferOverflowCount);
}
}
// Add message to TX FIFO buffer, with overflow check
void HapcanDevice::AddMessageToTxBuffer(HapcanMessage& message)
{
m_TxBuffer[m_TxBufferIndex] = message;
m_TxBufferIndex++;
if (m_TxBufferIndex >= Config::TxFifoQueueSize)
m_TxBufferIndex = 0;
// tx buffer overflow check
if (m_TxBufferIndex == m_TxBufferReadIndex)
{
m_txBufferOverflowCount++;
OA_LOG_LINE(F("TX Buffer overflow. Count = "));
OA_LOG(m_txBufferOverflowCount);
}
}
// Read one message from RX FIFO buffer.
// Returns true if there is any message to read
bool HapcanDevice::ReadRxBuffer(HapcanMessage ** message)
{
// no messages waiting
if (m_RxBufferReadIndex == m_RxBufferIndex)
return false;
*message = &m_RxBuffer[m_RxBufferReadIndex];
m_RxBufferReadIndex++;
if (m_RxBufferReadIndex >= Config::RxFifoQueueSize)
m_RxBufferReadIndex = 0;
return true;
}
// Read one message from TX FIFO buffer.
// Returns true if there is any message to read
bool HapcanDevice::ReadTxBuffer(HapcanMessage ** message)
{
// no messages waiting
if (m_TxBufferReadIndex == m_TxBufferIndex)
return false;
*message = &m_TxBuffer[m_TxBufferReadIndex];
m_TxBufferReadIndex++;
if (m_TxBufferReadIndex >= Config::TxFifoQueueSize)
m_TxBufferReadIndex = 0;
return true;
}
// static CAN MCP interupt callback function
void HapcanDevice::OnCanReceivedDispatcher()
{
if (pHapcanDevice != NULL)
pHapcanDevice->OnCanReceived();
}
// Method is called on CAN MCP Interrupt. It reads message and put it into RX FIFO buffer
void HapcanDevice::OnCanReceived()
{
// don't process any message until device is full initialized
if (!m_isInitialized)
return;
byte len = 0;
byte rxBuffer[8];
long unsigned int rxId;
byte ext;
CAN.readMsgBuf(&rxId, &ext, &len, rxBuffer);
HapcanMessage hapcanMessage(rxId, rxBuffer);
if (!m_receiveAnswerMessages && hapcanMessage.IsAnswer()) // ignore answer messages
return;
AddMessageToRxBuffer(hapcanMessage);
}
// Perform rx buffer reading and processing. Call this methon in loop() function.
void HapcanDevice::Update()
{
ProcessRxBuffer();
UpdateUptime();
OnUpdate();
ProcessTxBuffer();
}
// Checks if there is any new message to process and perform processing in this case
// Returns true if message processed
bool HapcanDevice::ProcessRxBuffer()
{
Hapcan::HapcanMessage* message = NULL;
if (ReadRxBuffer(&message))
{
message->PrintToSerial();
byte frameTypeCategory = message->GetFrameTypeCategory();
if (m_isInProgrammingMode)
{
ProcessProgrammingMessage(message);
return true;
}
if (frameTypeCategory < Hapcan::Message::NormalMessageCategory)
ProcessSystemMessage(message);
else
ProcessNormalMessage(message);
return true;
}
return false;
}
// Checks if there is any message to send
// Returns true if message processed
bool HapcanDevice::ProcessTxBuffer()
{
Hapcan::HapcanMessage* message = NULL;
if (ReadTxBuffer(&message))
{
CAN.sendMsgBuf(message->m_id, 1, 8, message->m_data);
if (m_processOwnMessages && !message->IsAnswer())
AddMessageToRxBuffer(*message);
return true;
}
return false;
}
// Updates uptime counter
void HapcanDevice::UpdateUptime()
{
unsigned long currentMillis = millis();
unsigned long timeSpan = currentMillis - m_lastMillis;
if (timeSpan >= 1000)
{
m_uptime += timeSpan / 1000;
m_lastMillis = currentMillis;
}
}
// Checks if message is for nodes in current group or for all groups
bool HapcanDevice::MatchGroup(HapcanMessage* message)
{
if (message->m_data[2] == 0 && (message->m_data[3] == m_group || message->m_data[3] == 0x00))
return true;
return false;
}
// Checks if message is for this node
bool HapcanDevice::MatchNode(HapcanMessage* message)
{
if (message->m_data[2] == m_node && message->m_data[3] == m_group)
return true;
return false;
}
void HapcanDevice::ProcessProgrammingMessage(HapcanMessage* message)
{
unsigned int frameType = message->GetFrameType();
switch (frameType)
{
case Message::System::AddressFrame:
AddressFrameAction(message);
break;
case Message::System::DataFrame:
DataFrameAction(message);
break;
case Message::System::ExitAllFromBootloaderProgrammingMode:
ProgrammingModeAction(frameType);
break;
case Message::System::ExitOneNodeFromBootloaderProgrammingMode:
if (message->GetNode() == m_node && message->GetGroup() == m_group)
ProgrammingModeAction(frameType);
break;
}
}
// Process normal message type. Checks box enable bits and test HAPCAN message conditions.
bool HapcanDevice::ProcessNormalMessage(HapcanMessage* message)
{
//unsigned int boxConfigAddress = CoreConfig::EEPROM::BoxConfigAddress;
for (byte i = 0; i < CoreConfig::BoxCount/8; i++)
{
byte boxEnableFlags = EEPROM[CoreConfig::EEPROM::BoxEnableAddress + i];
for (byte boxBit = 0; boxBit < 8; boxBit++)
{
if (boxEnableFlags & 0x01)
{
BoxConfigStruct boxConfig;
EEPROM.get(CoreConfig::EEPROM::BoxConfigAddress + sizeof(BoxConfigStruct)*boxBit + i*8*sizeof(BoxConfigStruct), boxConfig);
if (boxConfig.Accept(message))
{
OA_LOG_LINE(F("> Accepted box: "));
OA_LOG_LINE((boxBit + i * 8)+1);
OA_LOG_LINE(F(" instr: "));
OA_LOG(boxConfig.Instruction());
//InstructionStruct* exec = &boxConfig;
OnExecuteInstruction(*(&boxConfig), *message);
if (m_executeInstructionDelegate != NULL)
m_executeInstructionDelegate(*(&boxConfig), *message);
}
}
boxEnableFlags = boxEnableFlags >> 1;
}
}
return false;
}
// Process system massage type
bool HapcanDevice::ProcessSystemMessage(HapcanMessage* message)
{
unsigned int frameType = message->GetFrameType();
switch(frameType)
{
case Message::System::EnterProgrammingMode:
if (MatchNode(message))
EnterProgrammingModeAction(frameType);
break;
case Message::System::RebootRequestToGroup:
if (MatchGroup(message))
RebootAction();
break;
case Message::System::RebootRequestToNode:
if (MatchNode(message))
RebootAction();
break;
case Message::System::HardwareTypeRequestToGroup:
if(MatchGroup(message))
CanNodeIdAction(frameType);
break;
case Message::System::HardwareTypeRequestToNode:
if (MatchNode(message))
CanNodeIdAction(frameType);
break;
case Message::System::FirmwareTypeRequestToGroup:
if (MatchGroup(message))
CanFirmwareIdAction(frameType);
break;
case Message::System::FirmwareTypeRequestToNode:
if (MatchNode(message))
CanFirmwareIdAction(frameType);
break;
case Message::System::SetDefaultNodeAndGroupRequestToNode:
if (MatchNode(message))
SetDefaultNodeAndGroupAction(frameType);
break;
case Message::System::StatusRequestToGroup:
if (MatchGroup(message))
StatusRequestAction(message);
break;
case Message::System::StatusRequestToNode:
if (MatchNode(message))
StatusRequestAction(message);
break;
case Message::System::ControlMessage:
if (MatchNode(message))
ControlAction(message);
break;
case Message::System::SupplyVoltageRequestToGroup:
if (MatchGroup(message))
SupplyVoltageAction(frameType);
break;
case Message::System::SupplyVoltageRequestToNode:
if (MatchNode(message))
SupplyVoltageAction(frameType);
break;
case Message::System::DescriptionRequestToGroup:
if (MatchGroup(message))
NodeDescriptionAction(frameType);
break;
case Message::System::DescriptionRequestToNode:
if (MatchNode(message))
NodeDescriptionAction(frameType);
break;
case Message::System::DeviceIDRequestToGroup:
if (MatchGroup(message))
DeviceIDAction(frameType);
break;
case Message::System::DeviceIDRequestToNode:
if (MatchNode(message))
DeviceIDAction(frameType);
break;
case Message::System::UptimeRequestToGroup:
if (MatchGroup(message))
UptimeAction(frameType);
break;
case Message::System::UptimeRequestToNode:
if (MatchNode(message))
UptimeAction(frameType);
break;
//const unsigned int HealthCheckRequestToGroup = 0x114;
//const unsigned int HealthCheckRequestToNode = 0x115;
default:
return false;
}
return true;
}
// Returns RX FIFO buffer overflow count
unsigned long HapcanDevice::GetRxBufferOverflowCount()
{
return m_rxBufferOverflowCount;
}
// Send HapcanMessage to the CAN BUS
void HapcanDevice::Send(HapcanMessage& message, bool sendImmediately)
{
message.Prepare(m_node, m_group);
message.PrintToSerial();
if (sendImmediately)
{
CAN.sendMsgBuf(message.m_id, 1, 8, message.m_data);
if (m_processOwnMessages && !message.IsAnswer())
AddMessageToRxBuffer(message);
}
else
AddMessageToTxBuffer(message);
}
// Returns byte from one of the EEPROM's config bank
bool HapcanDevice::GetConfigByte(byte configBank, byte byteNumber, byte& value)
{
switch (configBank)
{
case Hapcan::ConfigBank::NodeConfig:
if (byteNumber > Hapcan::ConfigBank::NodeConfigCapacity)
return false;
value = EEPROM[Hapcan::CoreConfig::EEPROM::NodeConfigAddress + byteNumber];
break;
case Hapcan::ConfigBank::ExtendedConfig:
if (byteNumber > Hapcan::ConfigBank::ExtendedConfigCapacity)
return false;
value = EEPROM[Hapcan::CoreConfig::EEPROM::ExtendedConfigAddress + byteNumber];
break;
default:
return false;
}
return true;
}
// Set byte in one of the EEPROM's config bank.
bool HapcanDevice::SetConfigByte(byte configBank, byte byteNumber, byte value)
{
switch (configBank)
{
case Hapcan::ConfigBank::NodeConfig:
if (byteNumber > Hapcan::ConfigBank::NodeConfigCapacity)
return false;
EEPROM.update(Hapcan::CoreConfig::EEPROM::NodeConfigAddress + byteNumber, value);
break;
case Hapcan::ConfigBank::ExtendedConfig:
if (byteNumber > Hapcan::ConfigBank::ExtendedConfigCapacity)
return false;
EEPROM.update(Hapcan::CoreConfig::EEPROM::ExtendedConfigAddress + byteNumber, value);
break;
default:
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------
//-- BOOTLOADER ACTIONS ----------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------
// Enter programming mode
void HapcanDevice::EnterProgrammingModeAction(unsigned int frameType)
{
m_isInProgrammingMode = true;
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[2] = Config::BootLoader::BootloaderVersion;
message.m_data[3] = Config::BootLoader::BootloaderRevision;
OA_LOG(F("> Entering programming mode"));
Send(message);
}
// Address frame handling in programming mode
void HapcanDevice::AddressFrameAction(HapcanMessage* inputMessage)
{
//Currently only EEPROM programming is supported. Hapcan EEPROM is on 0xF00000 - 0xF003FF (1kB)
if (inputMessage->m_data[0] != 0xF0)
{
ErrorFrameAction(inputMessage);
return;
}
m_memoryAddress = ((int) inputMessage->m_data[1] << 8) + inputMessage->m_data[2];
m_memoryCommand = inputMessage->m_data[5];
if (m_memoryAddress > EEPROM.length())
{
ErrorFrameAction(inputMessage);
return;
}
inputMessage->SetAnswer();
OA_LOG(F("> Address frame"));
Send(*inputMessage);
}
// Data frame handling in programming mode
void HapcanDevice::DataFrameAction(HapcanMessage* inputMessage)
{
switch (m_memoryCommand)
{
case Programming::Command::Undefined:
ErrorFrameAction(inputMessage);
return;
case Programming::Command::Read:
for (byte i = 0; i < 8; i++)
inputMessage->m_data[i] = EEPROM[m_memoryAddress+i];
break;
case Programming::Command::Write:
for (byte i = 0; i < 8; i++)
{
EEPROM[m_memoryAddress + i] = inputMessage->m_data[i];
inputMessage->m_data[i] = EEPROM[m_memoryAddress + i]; // read EEPROM and send it back, it should be the same
}
break;
}
inputMessage->SetAnswer();
OA_LOG(F("> Data frame"));
Send(*inputMessage);
}
// Send error frame
void HapcanDevice::ErrorFrameAction(HapcanMessage* inputMessage)
{
HapcanMessage message(Message::System::ErrorFrame, true, m_node, m_group);
message.m_data[2] = Config::BootLoader::BootloaderVersion;
message.m_data[3] = Config::BootLoader::BootloaderRevision;
OA_LOG(F("> Error Frame"));
Send(message);
}
// Handle programming mode
void HapcanDevice::ProgrammingModeAction(unsigned int frameType)
{
switch (frameType)
{
case Message::System::ExitAllFromBootloaderProgrammingMode:
case Message::System::ExitOneNodeFromBootloaderProgrammingMode:
OA_LOG(F("> Exiting programming mode"));
if (m_isInProgrammingMode)
RebootAction();
break;
}
}
// Reboots the device. No Message is sent to CAN BUS.
void HapcanDevice::RebootAction()
{
OA_LOG(F("> Rebooting..."));
wdt_enable(WDTO_15MS);
_delay_ms(20);
}
// Send Can Node ID
void HapcanDevice::CanNodeIdAction(unsigned int frameType)
{
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[0] = Config::Hardware::HardwareType1;
message.m_data[1] = Config::Hardware::HardwareType2;
message.m_data[2] = Config::Hardware::HardwareVersion;
message.m_data[4] = Config::Node::SerialNumber0;
message.m_data[5] = Config::Node::SerialNumber1;
message.m_data[6] = Config::Node::SerialNumber2;
message.m_data[7] = Config::Node::SerialNumber3;
OA_LOG(F("> CanNodeId"));
Send(message);
}
// Send Can Firmware ID or error frame if no firmware
void HapcanDevice::CanFirmwareIdAction(unsigned int frameType)
{
//TODO: test if firmware is OK, return error frame then
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[0] = Config::Hardware::HardwareType1;
message.m_data[1] = Config::Hardware::HardwareType2;
message.m_data[2] = Config::Hardware::HardwareVersion;
message.m_data[3] = Config::Firmware::ApplicationType;
message.m_data[4] = Config::Firmware::ApplicationVersion;
message.m_data[5] = Config::Firmware::FirmwareVersion;
message.m_data[6] = Config::BootLoader::BootloaderVersion;
message.m_data[7] = Config::BootLoader::BootloaderRevision;
OA_LOG(F("> CanFirmwareId"));
Send(message);
}
// Send supply voltage information (currently not supported, returns 0V)
void HapcanDevice::SupplyVoltageAction(unsigned int frameType)
{
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[0] = 0;
message.m_data[1] = 0;
message.m_data[2] = 0;
message.m_data[3] = 0;
OA_LOG(F("> SupplyVoltage"));
Send(message);
}
void HapcanDevice::NodeDescriptionAction(unsigned int frameType)
{
HapcanMessage message(frameType, true, m_node, m_group);
for (byte i = 0; i < 8; i++)
message.m_data[i] = EEPROM[Hapcan::CoreConfig::EEPROM::DescriptionAddress + i];
OA_LOG(F("> Description 1"));
Send(message);
for (byte i = 0; i < 8; i++)
message.m_data[i] = EEPROM[Hapcan::CoreConfig::EEPROM::DescriptionAddress + i + 8];
OA_LOG(F("> Description 2"));
Send(message);
}
// Reset node and group to default values
void HapcanDevice::SetDefaultNodeAndGroupAction(unsigned int frameType)
{
EEPROM.update(0x26, Config::Node::SerialNumber2);
EEPROM.update(0x27, Config::Node::SerialNumber3);
m_node = Config::Node::SerialNumber2;
m_group = Config::Node::SerialNumber3;
HapcanMessage message(frameType, true, m_node, m_group);
OA_LOG(F("> SetDefaultNodeAndGroup"));
Send(message);
}
// Status request
void HapcanDevice::StatusRequestAction(HapcanMessage* message)
{
OA_LOG(F("> StatusRequest"));
OnStatusRequest(Hapcan::Message::System::StatusRequestType::SendAll, true);
if (m_statusRequestDelegate != NULL)
m_statusRequestDelegate(Hapcan::Message::System::StatusRequestType::SendAll, true);
}
// Control action
void HapcanDevice::ControlAction(HapcanMessage* message)
{
OA_LOG(F("> Direct Control"));
InstructionStruct exec;
exec.InitFromBytes(message->m_data[0], message->m_data[1], message->m_data[4], message->m_data[5], message->m_data[6], message->m_data[7]);
OnExecuteInstruction(exec, *message);
if (m_executeInstructionDelegate != NULL)
m_executeInstructionDelegate(exec, *message);
}
// Send device (chip) ID
void HapcanDevice::DeviceIDAction(unsigned int frameType)
{
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[0] = Config::Hardware::DeviceId1;
message.m_data[1] = Config::Hardware::DeviceId2;
OA_LOG(F("> DeviceID"));
Send(message);
}
// Send uptime info
void HapcanDevice::UptimeAction(unsigned int frameType)
{
HapcanMessage message(frameType, true, m_node, m_group);
message.m_data[4] = (byte)(m_uptime >> 24);
message.m_data[5] = (byte)(m_uptime >> 16);
message.m_data[6] = (byte)(m_uptime >> 8);
message.m_data[7] = (byte)m_uptime;
OA_LOG(F("> Uptime"));
Send(message);
}