Change module loop api, add info module

This commit is contained in:
redfast00 2020-09-09 18:18:55 +02:00
parent d8cb85d725
commit 4e745149e4
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
10 changed files with 130 additions and 46 deletions

View file

@ -32,8 +32,8 @@ void _decode_can_id(uint16_t can_id, struct module *mod, bool *priority) {
assert(mod->type <= 0b11); assert(mod->type <= 0b11);
} }
uint8_t payload_type(uint8_t module_type, uint8_t msg_type) { uint8_t payload_type(uint8_t module_type, uint8_t module_id, uint8_t msg_type) {
if (module_type == OBUS_TYPE_CONTROLLER) { if (module_type == OBUS_TYPE_CONTROLLER && module_type == 0) {
switch (msg_type) { switch (msg_type) {
case OBUS_MSGTYPE_C_ACK: case OBUS_MSGTYPE_C_ACK:
case OBUS_MSGTYPE_C_HELLO: case OBUS_MSGTYPE_C_HELLO:
@ -50,7 +50,10 @@ uint8_t payload_type(uint8_t module_type, uint8_t msg_type) {
return false; return false;
break; break;
} }
}
else if (module_type == OBUS_TYPE_INFO) {
// Info modules can only send 7 bytes of data
return OBUS_PAYLDTYPE_INFO;
// Module messages // Module messages
} else { } else {
switch (msg_type) { switch (msg_type) {
@ -105,7 +108,7 @@ bool receive(struct message *msg) {
_decode_can_id(receive_frame.can_id, &from, &priority); _decode_can_id(receive_frame.can_id, &from, &priority);
// Controller messages // Controller messages
switch (payload_type(from.type, msg_type)) { switch (payload_type(from.type, from.id, msg_type)) {
case OBUS_PAYLDTYPE_EMPTY: case OBUS_PAYLDTYPE_EMPTY:
break; break;
@ -131,6 +134,13 @@ bool receive(struct message *msg) {
msg->count = receive_frame.data[1]; msg->count = receive_frame.data[1];
break; break;
case OBUS_PAYLDTYPE_INFO:
if (receive_frame.can_dlc < 8) {
Serial.println(F("W Received illegal count msg: payload <8"));
return false;
}
memcpy(msg->infomessage, &receive_frame.data[1], OBUS_MSG_LENGTH - 1);
break;
default: default:
Serial.println(F("W Couldn't determine payload type")); Serial.println(F("W Couldn't determine payload type"));
return false; return false;
@ -160,7 +170,7 @@ void send(struct message *msg) {
uint8_t length = 1; uint8_t length = 1;
send_frame.data[0] = msg->msg_type; send_frame.data[0] = msg->msg_type;
uint8_t pyld_type = payload_type(msg->from.type, msg->msg_type); uint8_t pyld_type = payload_type(msg->from.type, msg->from.id, msg->msg_type);
switch (pyld_type) { switch (pyld_type) {
case OBUS_PAYLDTYPE_EMPTY: case OBUS_PAYLDTYPE_EMPTY:
break; break;
@ -180,6 +190,10 @@ void send(struct message *msg) {
length = 2; length = 2;
break; break;
case OBUS_PAYLDTYPE_INFO:
memcpy(&send_frame.data[1], msg->infomessage, OBUS_MSG_LENGTH - 1);
length = 8;
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

@ -23,14 +23,18 @@
#define OBUS_MSGTYPE_C_SOLVED 4 #define OBUS_MSGTYPE_C_SOLVED 4
#define OBUS_MSGTYPE_C_TIMEOUT 5 #define OBUS_MSGTYPE_C_TIMEOUT 5
#define OBUS_MSGTYPE_C_STRIKEOUT 6 #define OBUS_MSGTYPE_C_STRIKEOUT 6
#define OBUS_MSGTYPE_C_INFOSTART 7
#define OBUS_MSGTYPE_M_HELLO 0 #define OBUS_MSGTYPE_M_HELLO 0
#define OBUS_MSGTYPE_M_STRIKE 1 #define OBUS_MSGTYPE_M_STRIKE 1
#define OBUS_MSGTYPE_M_SOLVED 2 #define OBUS_MSGTYPE_M_SOLVED 2
#define OBUS_MSGTYPE_I_INFOMESSAGE 0
#define OBUS_PAYLDTYPE_EMPTY 0 #define OBUS_PAYLDTYPE_EMPTY 0
#define OBUS_PAYLDTYPE_GAMESTATUS 1 #define OBUS_PAYLDTYPE_GAMESTATUS 1
#define OBUS_PAYLDTYPE_COUNT 2 #define OBUS_PAYLDTYPE_COUNT 2
#define OBUS_PAYLDTYPE_INFO 3
namespace obus_can { namespace obus_can {
@ -55,6 +59,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;
}; };
}; };
@ -229,6 +234,13 @@ inline void send_m_solved(struct module from) {
send(&msg); send(&msg);
} }
inline void send_i_infomessage(struct module from, uint8_t infomessage[7]) {
assert(from.type == OBUS_TYPE_INFO && from.id != 0);
struct message msg = _msg(from, false, OBUS_MSGTYPE_I_INFOMESSAGE);
memcpy(infomessage, msg.infomessage, 7);
send(&msg);
}
} }
#endif /* end of include guard: OBUS_CAN_H */ #endif /* end of include guard: OBUS_CAN_H */

View file

@ -73,13 +73,15 @@ void setup(uint8_t type, uint8_t id) {
strike_count = 0; strike_count = 0;
active = true; active = true;
if (type == OBUS_TYPE_PUZZLE || type == OBUS_TYPE_NEEDY) {
pinMode(RED_LED, OUTPUT); pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT);
_setLedBlink(COLOR_GREEN, BLINK_DELAY_SLOW); _setLedBlink(COLOR_GREEN, BLINK_DELAY_SLOW);
}
} }
bool loopPuzzle(obus_can::message* message) { bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) {
// Check if we need to turn the red "strike" LED back off after // Check if we need to turn the red "strike" LED back off after
// turning it on because of a strike // turning it on because of a strike
if (time_stop_strike_led && millis() > time_stop_strike_led) { if (time_stop_strike_led && millis() > time_stop_strike_led) {
@ -102,6 +104,7 @@ bool loopPuzzle(obus_can::message* message) {
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) {
switch (message->msg_type) { switch (message->msg_type) {
case OBUS_MSGTYPE_C_GAMESTART: case OBUS_MSGTYPE_C_GAMESTART:
active = true; active = true;
@ -126,15 +129,37 @@ bool loopPuzzle(obus_can::message* message) {
break; break;
} }
} }
}
_updateLed(); _updateLed();
return interesting_message; return interesting_message;
} }
bool loopNeedy(obus_can::message* message) { bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) {
// For now this is the same function // For now this is the same function
return loopPuzzle(message); return loopPuzzle(message, callback_game_start, callback_game_stop);
}
bool loopInfo(obus_can::message* message, int (*info_generator)(char*)) {
bool interesting_message = false;
if (obus_can::receive(message)) {
if (message->from.type == OBUS_TYPE_CONTROLLER && message->from.id == 0) {
switch (message->msg_type) {
case OBUS_MSGTYPE_C_INFOSTART:
char info_message[8];
int len = info_generator(info_message);
obus_can::send_i_infomessage(this_module, info_message);
break;
case OBUS_MSGTYPE_C_STATE:
interesting_message = true;
break;
default:
break;
}
}
}
return interesting_message;
} }
void strike() { void strike() {

View file

@ -8,17 +8,15 @@
#define OBUS_NEEDY_ID_DEVELOPMENT 255 #define OBUS_NEEDY_ID_DEVELOPMENT 255
#define OBUS_INFO_ID_DEVELOPMENT 255 #define OBUS_INFO_ID_DEVELOPMENT 255
void callback_game_start();
void callback_game_stop();
namespace obus_module { namespace obus_module {
void setup(uint8_t type, uint8_t id); void setup(uint8_t type, uint8_t id);
bool loopPuzzle(obus_can::message* message); bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)());
bool loopNeedy(obus_can::message* message); bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)());
bool loopInfo(obus_can::message* message, int (*info_generator)(char*));
void strike(); void strike();

View file

@ -17,7 +17,7 @@ void setup() {
obus_can::message message; obus_can::message message;
void loop() { void loop() {
bool received = obus_module::loopPuzzle(&message); bool received = obus_module::loopPuzzle(&message, callback_game_start, callback_game_stop);
// TODO handle update frames (not needed for this module, but could be useful as example code) // TODO handle update frames (not needed for this module, but could be useful as example code)
red_button.loop(); red_button.loop();

View file

@ -47,7 +47,7 @@ void setup() {
obus_can::message message; obus_can::message message;
void loop() { void loop() {
bool received = obus_module::loopPuzzle(&message); bool received = obus_module::loopPuzzle(&message, callback_game_start, callback_game_stop);
// TODO handle update frames (not needed for this module, but could be useful as example code) // TODO handle update frames (not needed for this module, but could be useful as example code)
solve_button.loop(); solve_button.loop();
if (solve_button.getCount() > 0) { if (solve_button.getCount() > 0) {

View file

@ -0,0 +1,6 @@
## Testmodule info
This is a simple info module
### Credits
Module developed by redfast00.

View file

@ -0,0 +1,29 @@
// (c) 2020, redfast00
// See the LICENSE file for conditions for copying
#include <obus_module.h>
void setup() {
Serial.begin(115200);
// Choose one
// Puzzle: a module that must be solved
obus_module::setup(OBUS_TYPE_PUZZLE, OBUS_PUZZLE_ID_DEVELOPMENT);
// Needy: a module that periodically requires an action not to get strikes
// obus_module::setup(OBUS_TYPE_NEEDY, OBUS_NEEDY_ID_DEVELOPMENT);
}
obus_can::message message;
void loop() {
bool is_message_valid = obus_module::loop_puzzle(&message, callback_game_start, callback_game_stop);
// bool bool is_message_valid = obus_module::loop_needy(&message);
}
void callback_game_start() {
}
void callback_game_stop() {
}

View file

@ -20,7 +20,7 @@ uint32_t next_activation_time = 0;
uint32_t trigger_time = 0; uint32_t trigger_time = 0;
void loop() { void loop() {
bool is_message_valid = obus_module::loopNeedy(&message); bool is_message_valid = obus_module::loopNeedy(&message, callback_game_start, callback_game_stop);
green_button.loop(); green_button.loop();
// Every second, have a 1/20 chance to trigger the countdown // Every second, have a 1/20 chance to trigger the countdown

View file

@ -16,7 +16,7 @@ void setup() {
obus_can::message message; obus_can::message message;
void loop() { void loop() {
bool is_message_valid = obus_module::loop_puzzle(&message); bool is_message_valid = obus_module::loop_puzzle(&message, callback_game_start, callback_game_stop);
// bool bool is_message_valid = obus_module::loop_needy(&message); // bool bool is_message_valid = obus_module::loop_needy(&message);
} }