-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathPacketCommander.cpp
121 lines (93 loc) · 2.98 KB
/
PacketCommander.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
#include "PacketCommander.h"
PacketCommander::PacketCommander(bool echo) : echo(echo) {
// nothing to do
};
PacketCommander::~PacketCommander() {
// nothing to do
};
void PacketCommander::addMotor(FOCMotor* motor) {
if (numMotors < PACKETCOMMANDER_MAX_MOTORS)
motors[numMotors++] = motor;
};
void PacketCommander::init(PacketIO& io) {
_io = &io;
};
void PacketCommander::run() {
// is a packet available?
*_io >> curr_packet;
if (curr_packet.type != 0x00) {
// new packet arrived - handke ut
commanderror = false;
if (!handlePacket(curr_packet)) {
// TODO error handling
};
lastcommanderror = commanderror;
lastcommandregister = curRegister;
if (commanderror) {
_io->in_sync = false; // TODO flush input buffer, don't set out of sync
//*_io << START_PACKET(PacketType::ALERT, 1) << '?' << END_PACKET;
}
if (! _io->is_complete())
_io->in_sync = false; // TODO flush input buffer
}
};
bool PacketCommander::handlePacket(Packet& packet) {
if (packet.type == PacketType::REGISTER) {
*_io >> curRegister;
handleRegisterPacket(!_io->is_complete(), curRegister);
return true;
}
else if (packet.type == PacketType::SYNC) {
*_io << START_PACKET(PacketType::SYNC, 1);
*_io << (uint8_t)0x01;
*_io << END_PACKET;
// TODO flush output packet
return true;
}
return false;
};
void PacketCommander::handleRegisterPacket(bool write, uint8_t reg) {
if (write) {
bool ok = commsToRegister(reg);
commanderror = commanderror || !ok;
}
if (!write || echo) {
uint8_t size = SimpleFOCRegisters::regs->sizeOfRegister(reg);
if (size > 0) { // sendable register
*_io << START_PACKET(PacketType::RESPONSE, size+1) << reg << Separator('=');
// TODO status?
registerToComms(reg);
*_io << END_PACKET;
// TODO flush packet
}
}
};
bool PacketCommander::commsToRegister(uint8_t reg){
switch (reg) {
case SimpleFOCRegister::REG_MOTOR_ADDRESS:
uint8_t val;
*_io >> val;
if (val >= numMotors)
commanderror = true;
else
curMotor = val;
return true;
default:
return SimpleFOCRegisters::regs->commsToRegister(*_io, reg, motors[curMotor]);
}
};
bool PacketCommander::registerToComms(uint8_t reg){
switch (reg) {
case SimpleFOCRegister::REG_STATUS:
// TODO implement status register
return true;
case SimpleFOCRegister::REG_MOTOR_ADDRESS:
*_io << curMotor;
return true;
case SimpleFOCRegister::REG_NUM_MOTORS:
*_io << numMotors;
return true;
default:
return SimpleFOCRegisters::regs->registerToComms(*_io, reg, motors[curMotor]);
}
};