diff --git a/lib/obus_can.cpp b/lib/obus_can.cpp index 0deb09a..066aff5 100644 --- a/lib/obus_can.cpp +++ b/lib/obus_can.cpp @@ -32,8 +32,8 @@ void _decode_can_id(uint16_t can_id, struct module *mod, bool *priority) { assert(mod->type <= 0b11); } -uint8_t payload_type(uint8_t module_type, uint8_t msg_type) { - if (module_type == OBUS_TYPE_CONTROLLER) { +uint8_t payload_type(uint8_t module_type, uint8_t module_id, uint8_t msg_type) { + if (module_type == OBUS_TYPE_CONTROLLER && module_type == 0) { switch (msg_type) { case OBUS_MSGTYPE_C_ACK: case OBUS_MSGTYPE_C_HELLO: @@ -50,7 +50,10 @@ uint8_t payload_type(uint8_t module_type, uint8_t msg_type) { return false; break; } - + } + else if (module_type == OBUS_TYPE_INFO) { + // Info modules can only send 7 bytes of data + return OBUS_PAYLDTYPE_INFO; // Module messages } else { switch (msg_type) { @@ -105,7 +108,7 @@ bool receive(struct message *msg) { _decode_can_id(receive_frame.can_id, &from, &priority); // Controller messages - switch (payload_type(from.type, msg_type)) { + switch (payload_type(from.type, from.id, msg_type)) { case OBUS_PAYLDTYPE_EMPTY: break; @@ -131,6 +134,13 @@ bool receive(struct message *msg) { msg->count = receive_frame.data[1]; 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: Serial.println(F("W Couldn't determine payload type")); return false; @@ -160,7 +170,7 @@ void send(struct message *msg) { uint8_t length = 1; 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) { case OBUS_PAYLDTYPE_EMPTY: break; @@ -180,6 +190,10 @@ void send(struct message *msg) { length = 2; break; + case OBUS_PAYLDTYPE_INFO: + memcpy(&send_frame.data[1], msg->infomessage, OBUS_MSG_LENGTH - 1); + length = 8; + break; default: Serial.print(F("E Unknown payload type ")); Serial.println(pyld_type); diff --git a/lib/obus_can.h b/lib/obus_can.h index 0830e69..4c05639 100644 --- a/lib/obus_can.h +++ b/lib/obus_can.h @@ -23,14 +23,18 @@ #define OBUS_MSGTYPE_C_SOLVED 4 #define OBUS_MSGTYPE_C_TIMEOUT 5 #define OBUS_MSGTYPE_C_STRIKEOUT 6 +#define OBUS_MSGTYPE_C_INFOSTART 7 #define OBUS_MSGTYPE_M_HELLO 0 #define OBUS_MSGTYPE_M_STRIKE 1 #define OBUS_MSGTYPE_M_SOLVED 2 +#define OBUS_MSGTYPE_I_INFOMESSAGE 0 + #define OBUS_PAYLDTYPE_EMPTY 0 #define OBUS_PAYLDTYPE_GAMESTATUS 1 #define OBUS_PAYLDTYPE_COUNT 2 +#define OBUS_PAYLDTYPE_INFO 3 namespace obus_can { @@ -55,6 +59,7 @@ struct message { struct payld_empty empty; struct payld_gamestatus gamestatus; uint8_t count; + uint8_t infomessage; }; }; @@ -229,6 +234,13 @@ inline void send_m_solved(struct module from) { 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 */ diff --git a/lib/obus_module.cpp b/lib/obus_module.cpp index 25a293d..96a4468 100644 --- a/lib/obus_module.cpp +++ b/lib/obus_module.cpp @@ -73,13 +73,15 @@ void setup(uint8_t type, uint8_t id) { strike_count = 0; active = true; - pinMode(RED_LED, OUTPUT); - pinMode(GREEN_LED, OUTPUT); + if (type == OBUS_TYPE_PUZZLE || type == OBUS_TYPE_NEEDY) { + pinMode(RED_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 // turning it on because of a strike if (time_stop_strike_led && millis() > time_stop_strike_led) { @@ -102,28 +104,30 @@ bool loopPuzzle(obus_can::message* message) { bool interesting_message = false; if (obus_can::receive(message)) { - switch (message->msg_type) { - case OBUS_MSGTYPE_C_GAMESTART: - active = true; - _setLed(COLOR_OFF); - callback_game_start(); - break; - case OBUS_MSGTYPE_C_HELLO: - obus_can::send_m_hello(this_module); - break; - case OBUS_MSGTYPE_C_SOLVED: - case OBUS_MSGTYPE_C_TIMEOUT: - case OBUS_MSGTYPE_C_STRIKEOUT: - active = false; - callback_game_stop(); - break; - case OBUS_MSGTYPE_C_ACK: - break; - case OBUS_MSGTYPE_C_STATE: - interesting_message = true; - break; - default: - break; + if (message->from.type == OBUS_TYPE_CONTROLLER && message->from.id == 0) { + switch (message->msg_type) { + case OBUS_MSGTYPE_C_GAMESTART: + active = true; + _setLed(COLOR_OFF); + callback_game_start(); + break; + case OBUS_MSGTYPE_C_HELLO: + obus_can::send_m_hello(this_module); + break; + case OBUS_MSGTYPE_C_SOLVED: + case OBUS_MSGTYPE_C_TIMEOUT: + case OBUS_MSGTYPE_C_STRIKEOUT: + active = false; + callback_game_stop(); + break; + case OBUS_MSGTYPE_C_ACK: + break; + case OBUS_MSGTYPE_C_STATE: + interesting_message = true; + break; + default: + break; + } } } @@ -132,9 +136,30 @@ bool loopPuzzle(obus_can::message* 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 - 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() { diff --git a/lib/obus_module.h b/lib/obus_module.h index 8c585e1..9a283e9 100644 --- a/lib/obus_module.h +++ b/lib/obus_module.h @@ -8,17 +8,15 @@ #define OBUS_NEEDY_ID_DEVELOPMENT 255 #define OBUS_INFO_ID_DEVELOPMENT 255 -void callback_game_start(); - -void callback_game_stop(); - namespace obus_module { 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(); diff --git a/src/modules/testmodule_buttons/testmodule_buttons.ino b/src/modules/testmodule_buttons/testmodule_buttons.ino index 19223ad..312e1cc 100644 --- a/src/modules/testmodule_buttons/testmodule_buttons.ino +++ b/src/modules/testmodule_buttons/testmodule_buttons.ino @@ -17,7 +17,7 @@ void setup() { obus_can::message message; 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) red_button.loop(); diff --git a/src/modules/testmodule_date/testmodule_date.ino b/src/modules/testmodule_date/testmodule_date.ino index 1f166ab..14cec63 100644 --- a/src/modules/testmodule_date/testmodule_date.ino +++ b/src/modules/testmodule_date/testmodule_date.ino @@ -30,7 +30,7 @@ uint8_t correct_code[4] = { DATE_MODE_STOP, 12, 34, - 56 + 56 }; ezButton solve_button(DATE_SOLVE_BTN); @@ -47,7 +47,7 @@ void setup() { obus_can::message message; 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) solve_button.loop(); if (solve_button.getCount() > 0) { @@ -102,12 +102,12 @@ void read_from_date_module(uint8_t* data_out) { data_out[i] = 10*(data_out[i] & 0x0F) + ((data_out[i] & 0xF0) >> 4); } - + //for (int i = 0; i < 4; i++) { // Serial.print(data_out[i]); // Serial.print(" "); //} - + //Serial.println(); digitalWrite(DATE_CLOCK_PIN, LOW); } diff --git a/src/modules/testmodule_info/doc/index.md b/src/modules/testmodule_info/doc/index.md new file mode 100644 index 0000000..83532e5 --- /dev/null +++ b/src/modules/testmodule_info/doc/index.md @@ -0,0 +1,6 @@ +## Testmodule info + +This is a simple info module + +### Credits +Module developed by redfast00. diff --git a/src/modules/testmodule_info/testmodule_info.ino b/src/modules/testmodule_info/testmodule_info.ino new file mode 100644 index 0000000..5adb827 --- /dev/null +++ b/src/modules/testmodule_info/testmodule_info.ino @@ -0,0 +1,29 @@ +// (c) 2020, redfast00 +// See the LICENSE file for conditions for copying + +#include + +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() { + +} diff --git a/src/modules/testmodule_needy_buttons/testmodule_needy_buttons.ino b/src/modules/testmodule_needy_buttons/testmodule_needy_buttons.ino index a58e648..cb1002d 100644 --- a/src/modules/testmodule_needy_buttons/testmodule_needy_buttons.ino +++ b/src/modules/testmodule_needy_buttons/testmodule_needy_buttons.ino @@ -20,7 +20,7 @@ uint32_t next_activation_time = 0; uint32_t trigger_time = 0; 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(); // Every second, have a 1/20 chance to trigger the countdown diff --git a/src/template_module/main.ino b/src/template_module/main.ino index 3b05aa5..83601f4 100644 --- a/src/template_module/main.ino +++ b/src/template_module/main.ino @@ -16,7 +16,7 @@ void setup() { obus_can::message message; 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); }