Clean state, use infomessage data length, fix info module lib code

This commit is contained in:
Midgard 2020-09-09 22:00:59 +02:00
parent 8ecc0433ed
commit 8367b538e1
Signed by: midgard
GPG key ID: 511C112F1331BBB4
4 changed files with 50 additions and 24 deletions

View file

@ -134,11 +134,11 @@ bool receive(struct message *msg) {
break; break;
case OBUS_PAYLDTYPE_INFO: case OBUS_PAYLDTYPE_INFO:
if (receive_frame.can_dlc < 8) { {
Serial.println(F("W Received illegal count msg: payload <8")); uint8_t data_len = receive_frame.can_dlc - 1;
return false; memcpy(msg->infomessage.data, &(receive_frame.data[1]), data_len);
msg->infomessage.len = data_len;
} }
memcpy(msg->infomessage, &(receive_frame.data[1]), OBUS_MSG_LENGTH - 1);
break; break;
default: default:
Serial.println(F("W Couldn't determine payload type")); Serial.println(F("W Couldn't determine payload type"));
@ -190,9 +190,10 @@ void send(struct message *msg) {
break; break;
case OBUS_PAYLDTYPE_INFO: case OBUS_PAYLDTYPE_INFO:
memcpy(&(send_frame.data[1]), msg->infomessage, OBUS_MSG_LENGTH - 1); memcpy(&(send_frame.data[1]), msg->infomessage.data, msg->infomessage.len);
length = 8; length = msg->infomessage.len + 1;
break; break;
default: default:
Serial.print(F("E Unknown payload type ")); Serial.print(F("E Unknown payload type "));
Serial.println(pyld_type); Serial.println(pyld_type);

View file

@ -36,6 +36,8 @@
#define OBUS_PAYLDTYPE_COUNT 2 #define OBUS_PAYLDTYPE_COUNT 2
#define OBUS_PAYLDTYPE_INFO 3 #define OBUS_PAYLDTYPE_INFO 3
#define OBUS_PAYLD_INFO_MAXLEN (OBUS_MSG_LENGTH - 1)
namespace obus_can { namespace obus_can {
struct module { struct module {
@ -49,6 +51,10 @@ struct payld_gamestatus {
uint8_t strikes; uint8_t strikes;
uint8_t max_strikes; uint8_t max_strikes;
}; };
struct payld_infomessage {
uint8_t len;
uint8_t data[OBUS_PAYLD_INFO_MAXLEN];
};
struct message { struct message {
@ -59,7 +65,7 @@ struct message {
struct payld_empty empty; struct payld_empty empty;
struct payld_gamestatus gamestatus; struct payld_gamestatus gamestatus;
uint8_t count; uint8_t count;
uint8_t infomessage[OBUS_MSG_LENGTH - 1]; struct payld_infomessage infomessage;
}; };
}; };
@ -234,10 +240,21 @@ inline void send_m_solved(struct module from) {
send(&msg); send(&msg);
} }
inline void send_i_infomessage(struct module from, uint8_t infomessage[OBUS_MSG_LENGTH - 1]) { /**
* Send an info module "information" OBUS message
*/
inline void send_i_infomessage(struct module from, uint8_t *data, uint8_t data_len) {
assert(from.type == OBUS_TYPE_INFO && from.id != OBUS_CONTROLLER_ID); assert(from.type == OBUS_TYPE_INFO && from.id != OBUS_CONTROLLER_ID);
if (data_len > OBUS_PAYLD_INFO_MAXLEN) {
Serial.println(F("E Info payload too long"));
return;
}
struct message msg = _msg(from, false, OBUS_MSGTYPE_I_INFOMESSAGE); struct message msg = _msg(from, false, OBUS_MSGTYPE_I_INFOMESSAGE);
memcpy(infomessage, msg.infomessage, OBUS_MSG_LENGTH - 1); msg.infomessage.len = data_len;
memset(msg.infomessage.data, 0, OBUS_PAYLD_INFO_MAXLEN);
memcpy(msg.infomessage.data, data, data_len);
send(&msg); send(&msg);
} }

View file

@ -77,21 +77,25 @@ void _setLedBlink(struct color color, uint16_t delay) {
} }
void _resetState() {
strike_count = 0;
active = false;
if (this_module.type == OBUS_TYPE_PUZZLE || this_module.type == OBUS_TYPE_NEEDY) {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
_setLedBlink(COLOR_GREEN, BLINK_DELAY_SLOW);
}
}
void setup(uint8_t type, uint8_t id) { void setup(uint8_t type, uint8_t id) {
this_module.type = type; this_module.type = type;
this_module.id = id; this_module.id = id;
obus_can::init(); obus_can::init();
strike_count = 0; _resetState();
active = true;
if (type == OBUS_TYPE_PUZZLE || type == OBUS_TYPE_NEEDY) {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
_setLedBlink(COLOR_GREEN, BLINK_DELAY_SLOW);
}
} }
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) { bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) {
@ -119,12 +123,14 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
callback_game_start(); callback_game_start();
break; break;
case OBUS_MSGTYPE_C_HELLO: case OBUS_MSGTYPE_C_HELLO:
_resetState();
obus_can::send_m_hello(this_module); obus_can::send_m_hello(this_module);
break; break;
case OBUS_MSGTYPE_C_SOLVED: case OBUS_MSGTYPE_C_SOLVED:
case OBUS_MSGTYPE_C_TIMEOUT: case OBUS_MSGTYPE_C_TIMEOUT:
case OBUS_MSGTYPE_C_STRIKEOUT: case OBUS_MSGTYPE_C_STRIKEOUT:
active = false; active = false;
_setLed(COLOR_OFF);
callback_game_stop(); callback_game_stop();
break; break;
case OBUS_MSGTYPE_C_ACK: case OBUS_MSGTYPE_C_ACK:
@ -148,15 +154,17 @@ bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (
return loopPuzzle(message, callback_game_start, callback_game_stop); return loopPuzzle(message, callback_game_start, callback_game_stop);
} }
bool loopInfo(obus_can::message* message, int (*info_generator)(char*)) { bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*)) {
bool interesting_message = false; bool interesting_message = false;
if (obus_can::receive(message)) { if (obus_can::receive(message)) {
if (message->from.type == OBUS_TYPE_CONTROLLER && message->from.id == 0) { if (message->from.type == OBUS_TYPE_CONTROLLER && message->from.id == 0) {
switch (message->msg_type) { switch (message->msg_type) {
case OBUS_MSGTYPE_C_INFOSTART: case OBUS_MSGTYPE_C_INFOSTART:
char info_message[8]; {
int len = info_generator(info_message); uint8_t info_message[OBUS_PAYLD_INFO_MAXLEN];
obus_can::send_i_infomessage(this_module, info_message); int len = info_generator(info_message);
obus_can::send_i_infomessage(this_module, info_message, len);
}
break; break;
case OBUS_MSGTYPE_C_STATE: case OBUS_MSGTYPE_C_STATE:
interesting_message = true; interesting_message = true;

View file

@ -5,7 +5,7 @@
#include <LiquidCrystal.h> #include <LiquidCrystal.h>
char serial_number[7]; uint8_t serial_number[7];
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
@ -27,7 +27,7 @@ void loop() {
} }
} }
int info_generator(char* buffer) { int info_generator(uint8_t* buffer) {
serial_number[0] = random('A', 'Z'); serial_number[0] = random('A', 'Z');
serial_number[1] = random('A', 'Z'); serial_number[1] = random('A', 'Z');
serial_number[2] = random('A', 'Z'); serial_number[2] = random('A', 'Z');