obus/lib/obus_can.cpp

184 lines
4 KiB
C++
Raw Normal View History

2020-08-20 17:47:32 +02:00
#include <mcp2515.h>
2020-08-20 21:51:33 +02:00
#include "obus_can.h"
2020-08-20 17:47:32 +02:00
2020-08-26 21:13:16 +02:00
namespace obus_can {
2020-08-20 17:47:32 +02:00
MCP2515 mcp2515(10);
2020-08-26 21:13:16 +02:00
bool is_init = false;
2020-08-20 17:47:32 +02:00
uint16_t _encode_can_id(struct module mod, bool priority) {
assert(mod.type <= 0b11);
/* b bb bbbbbbbb
* type module ID
* priority bit
*/
return \
((uint16_t) (priority ? CAN_DOMINANT : CAN_RECESSIVE) << 10) | \
((uint16_t) mod.type << 8) | \
(uint16_t) mod.id;
}
void _decode_can_id(uint16_t can_id, struct module *mod, bool *priority) {
*priority = ((can_id >> 10) & 1) == CAN_DOMINANT;
mod->type = (can_id >> 8) & 0b11;
mod->id = can_id & 0b11111111;
assert(mod->type <= 0b11);
}
2020-08-26 21:13:16 +02:00
uint8_t payload_type(uint8_t module_type, uint8_t msg_type) {
2020-08-20 21:13:19 +02:00
if (module_type == OBUS_TYPE_CONTROLLER) {
switch (msg_type) {
case OBUS_MSGTYPE_C_ACK:
case OBUS_MSGTYPE_C_HELLO:
return OBUS_PAYLDTYPE_EMPTY;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
case OBUS_MSGTYPE_C_GAMESTART:
case OBUS_MSGTYPE_C_STATE:
case OBUS_MSGTYPE_C_SOLVED:
case OBUS_MSGTYPE_C_TIMEOUT:
case OBUS_MSGTYPE_C_STRIKEOUT:
return OBUS_PAYLDTYPE_GAMESTATUS;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
default:
return false;
break;
}
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
// Module messages
} else {
switch (msg_type) {
case OBUS_MSGTYPE_M_STRIKE:
return OBUS_PAYLDTYPE_COUNT;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
case OBUS_MSGTYPE_M_HELLO:
case OBUS_MSGTYPE_M_SOLVED:
return OBUS_PAYLDTYPE_EMPTY;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
default:
return -1;
break;
}
2020-08-20 17:47:32 +02:00
}
2020-08-20 21:13:19 +02:00
}
2020-08-20 17:47:32 +02:00
2020-08-26 21:13:16 +02:00
void init() {
is_init = true;
2020-08-20 21:13:19 +02:00
mcp2515.reset();
mcp2515.setBitrate(CAN_50KBPS);
mcp2515.setNormalMode();
2020-08-20 17:47:32 +02:00
}
2020-08-26 21:13:16 +02:00
bool receive(struct message *msg) {
if (!is_init) {
Serial.println(F("E Call init first"));
2020-08-20 17:47:32 +02:00
return false;
}
struct can_frame receive_frame;
memset(&receive_frame.data, 0, CAN_MAX_DLEN);
MCP2515::ERROR status = mcp2515.readMessage(&receive_frame);
if (status != MCP2515::ERROR_OK) {
return false;
}
// Always at least OBUS message type required
if (receive_frame.can_dlc < 1) {
Serial.println(F("W Received illegal msg: payload <1"));
return false;
}
uint8_t msg_type = receive_frame.data[0];
2020-08-20 21:13:19 +02:00
struct module from;
bool priority;
_decode_can_id(receive_frame.can_id, &from, &priority);
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
// Controller messages
2020-08-26 21:13:16 +02:00
switch (payload_type(from.type, msg_type)) {
2020-08-20 17:47:32 +02:00
case OBUS_PAYLDTYPE_EMPTY:
break;
case OBUS_PAYLDTYPE_GAMESTATUS:
if (receive_frame.can_dlc < 7) {
Serial.println(F("W Received illegal gamestatus msg: payload <7"));
return false;
}
msg->gamestatus.time_left =
((uint32_t) receive_frame.data[1] << 0x18) |
((uint32_t) receive_frame.data[2] << 0x10) |
((uint32_t) receive_frame.data[3] << 0x08) |
((uint32_t) receive_frame.data[4]);
msg->gamestatus.strikes = receive_frame.data[5];
msg->gamestatus.max_strikes = receive_frame.data[6];
break;
case OBUS_PAYLDTYPE_COUNT:
msg->count = receive_frame.data[1];
2020-08-20 17:47:32 +02:00
break;
default:
2020-08-20 21:13:19 +02:00
Serial.println(F("W Couldn't determine payload type"));
return false;
2020-08-20 17:47:32 +02:00
}
2020-08-20 21:13:19 +02:00
msg->from = from;
msg->priority = priority;
2020-08-20 17:47:32 +02:00
msg->msg_type = msg_type;
return true;
}
2020-08-26 21:13:16 +02:00
void send(struct message *msg) {
if (!is_init) {
Serial.println(F("E Call init first"));
2020-08-20 21:13:19 +02:00
return;
}
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
struct can_frame send_frame;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
memset(&send_frame.data, 0, CAN_MAX_DLEN);
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
uint8_t length = 1;
send_frame.data[0] = msg->msg_type;
2020-08-20 17:47:32 +02:00
2020-08-26 21:13:16 +02:00
switch (payload_type(msg->from.type, msg->msg_type)) {
2020-08-20 21:13:19 +02:00
case OBUS_PAYLDTYPE_EMPTY:
break;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
case OBUS_PAYLDTYPE_GAMESTATUS:
send_frame.data[1] = (uint8_t) ((msg->gamestatus.time_left & 0xFF000000) >> 0x18);
send_frame.data[2] = (uint8_t) ((msg->gamestatus.time_left & 0x00FF0000) >> 0x10);
send_frame.data[3] = (uint8_t) ((msg->gamestatus.time_left & 0x0000FF00) >> 0x08);
send_frame.data[4] = (uint8_t) (msg->gamestatus.time_left & 0x000000FF);
send_frame.data[5] = msg->gamestatus.strikes;
send_frame.data[6] = msg->gamestatus.max_strikes;
length = 7;
break;
2020-08-20 17:47:32 +02:00
case OBUS_PAYLDTYPE_COUNT:
send_frame.data[1] = msg->count;
length = 2;
break;
2020-08-20 21:13:19 +02:00
default:
Serial.println(F("Unknown payload type"));
return;
}
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
send_frame.can_id = _encode_can_id(msg->from, msg->priority);
send_frame.can_dlc = length;
2020-08-20 17:47:32 +02:00
2020-08-20 21:13:19 +02:00
mcp2515.sendMessage(&send_frame);
2020-08-20 17:47:32 +02:00
}
2020-08-26 21:13:16 +02:00
}