Fix reorganization

This commit is contained in:
Midgard 2020-08-26 21:13:16 +02:00
parent c1c1096778
commit c226cc8513
Signed by: midgard
GPG key ID: 511C112F1331BBB4
8 changed files with 125 additions and 101 deletions

View file

@ -1,29 +0,0 @@
#include "obus_can.h"
#include "module.h"
struct module this_module;
uint8_t strike_count;
void obusmodule_setup(uint8_t type, uint8_t id) {
this_module.type = type;
this_module.id = id;
obuscan_init();
strike_count = 0;
}
void obusmodule_loop() {
}
void obusmodule_strike() {
strike_count++;
obuscan_send_m_strike(this_module, strike_count);
}
void obusmodule_solve() {
obuscan_send_m_solved(this_module);
}

View file

@ -1,7 +0,0 @@
void obusmodule_setup(uint8_t type, uint8_t id);
void obusmodule_loop();
void obusmodule_strike();
void obusmodule_solve();

View file

@ -2,9 +2,10 @@
#include "obus_can.h" #include "obus_can.h"
namespace obus_can {
MCP2515 mcp2515(10); MCP2515 mcp2515(10);
bool obuscan_is_init = false; bool is_init = false;
uint16_t _encode_can_id(struct module mod, bool priority) { uint16_t _encode_can_id(struct module mod, bool priority) {
@ -28,7 +29,7 @@ void _decode_can_id(uint16_t can_id, struct module *mod, bool *priority) {
assert(mod->type <= 0b11); assert(mod->type <= 0b11);
} }
uint8_t obuscan_payload_type(uint8_t module_type, uint8_t msg_type) { uint8_t payload_type(uint8_t module_type, uint8_t msg_type) {
if (module_type == OBUS_TYPE_CONTROLLER) { if (module_type == OBUS_TYPE_CONTROLLER) {
switch (msg_type) { switch (msg_type) {
case OBUS_MSGTYPE_C_ACK: case OBUS_MSGTYPE_C_ACK:
@ -65,17 +66,17 @@ uint8_t obuscan_payload_type(uint8_t module_type, uint8_t msg_type) {
} }
void obuscan_init() { void init() {
obuscan_is_init = true; is_init = true;
mcp2515.reset(); mcp2515.reset();
mcp2515.setBitrate(CAN_50KBPS); mcp2515.setBitrate(CAN_50KBPS);
mcp2515.setNormalMode(); mcp2515.setNormalMode();
} }
bool obuscan_receive(struct obus_message *msg) { bool receive(struct message *msg) {
if (!obuscan_is_init) { if (!is_init) {
Serial.println(F("E Call obuscan_init first")); Serial.println(F("E Call init first"));
return false; return false;
} }
@ -101,7 +102,7 @@ bool obuscan_receive(struct obus_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 (obuscan_payload_type(from.type, msg_type)) { switch (payload_type(from.type, msg_type)) {
case OBUS_PAYLDTYPE_EMPTY: case OBUS_PAYLDTYPE_EMPTY:
break; break;
@ -136,9 +137,9 @@ bool obuscan_receive(struct obus_message *msg) {
} }
void obuscan_send(struct obus_message *msg) { void send(struct message *msg) {
if (!obuscan_is_init) { if (!is_init) {
Serial.println(F("E Call obuscan_init first")); Serial.println(F("E Call init first"));
return; return;
} }
@ -149,7 +150,7 @@ void obuscan_send(struct obus_message *msg) {
uint8_t length = 1; uint8_t length = 1;
send_frame.data[0] = msg->msg_type; send_frame.data[0] = msg->msg_type;
switch (obuscan_payload_type(msg->from.type, msg->msg_type)) { switch (payload_type(msg->from.type, msg->msg_type)) {
case OBUS_PAYLDTYPE_EMPTY: case OBUS_PAYLDTYPE_EMPTY:
break; break;
@ -178,3 +179,5 @@ void obuscan_send(struct obus_message *msg) {
mcp2515.sendMessage(&send_frame); mcp2515.sendMessage(&send_frame);
} }
}

View file

@ -1,6 +1,7 @@
#ifndef OBUS_CAN_H #ifndef OBUS_CAN_H
#define OBUS_CAN_H #define OBUS_CAN_H
#include "Arduino.h"
#include <assert.h> #include <assert.h>
#define CAN_DOMINANT 0 #define CAN_DOMINANT 0
@ -11,6 +12,7 @@
#define OBUS_MSG_LENGTH 8 // Max 8 to fit in a CAN message #define OBUS_MSG_LENGTH 8 // Max 8 to fit in a CAN message
#define OBUS_TYPE_CONTROLLER 0 #define OBUS_TYPE_CONTROLLER 0
#define OBUS_TYPE_INFO 0
#define OBUS_TYPE_PUZZLE 1 #define OBUS_TYPE_PUZZLE 1
#define OBUS_TYPE_NEEDY 2 #define OBUS_TYPE_NEEDY 2
@ -30,6 +32,8 @@
#define OBUS_PAYLDTYPE_GAMESTATUS 1 #define OBUS_PAYLDTYPE_GAMESTATUS 1
#define OBUS_PAYLDTYPE_COUNT 2 #define OBUS_PAYLDTYPE_COUNT 2
namespace obus_can {
struct module { struct module {
uint8_t type; uint8_t type;
uint8_t id; uint8_t id;
@ -43,7 +47,7 @@ struct payld_gamestatus {
}; };
struct obus_message { struct message {
struct module from; struct module from;
bool priority; bool priority;
uint8_t msg_type; uint8_t msg_type;
@ -63,28 +67,28 @@ struct obus_message {
* *
* @return One of OBUS_PAYLDTYPE_* * @return One of OBUS_PAYLDTYPE_*
*/ */
uint8_t obuscan_payload_type(uint8_t module_type, uint8_t message_type); uint8_t payload_type(uint8_t module_type, uint8_t message_type);
/** /**
* Initialize the CAN controller for OBUS messaging * Initialize the CAN controller for OBUS messaging
*/ */
void obuscan_init(); void init();
/** /**
* Receive a message * Receive a message
* *
* @param msg Pointer to memory where the received message will be wriitten * @param msg Pointer to memory where the received message will be wriitten
* @return true if a message was received, false otherwise * @return true if a message was received, false otherwise
*/ */
bool obuscan_receive(struct obus_message *msg); bool receive(struct message *msg);
/** /**
* Lowlevel interface to send a message, you may want to use one of the helpers, like * Lowlevel interface to send a message, you may want to use one of the helpers, like
* obuscan_send_m_strike * send_m_strike
* *
* @param msg Pointer to a message to send * @param msg Pointer to a message to send
*/ */
void obuscan_send(struct obus_message *msg); void send(struct message *msg);
/** /**
@ -92,9 +96,9 @@ void obuscan_send(struct obus_message *msg);
* *
* Send an OBUS message * Send an OBUS message
*/ */
inline struct obus_message _obuscan_msg(struct module from, bool priority, uint8_t msg_type) { inline struct message _msg(struct module from, bool priority, uint8_t msg_type) {
struct obus_message msg; struct message msg;
msg.from = from; msg.from = from;
msg.priority = priority; msg.priority = priority;
msg.msg_type = msg_type; msg.msg_type = msg_type;
@ -107,15 +111,15 @@ inline struct obus_message _obuscan_msg(struct module from, bool priority, uint8
* *
* Send a controller OBUS message with a gamestatus payload * Send a controller OBUS message with a gamestatus payload
*/ */
inline void _obuscan_send_payld_gamestatus( inline void _send_payld_gamestatus(
struct module from, bool priority, uint8_t msg_type, struct module from, bool priority, uint8_t msg_type,
uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
struct obus_message msg = _obuscan_msg(from, priority, msg_type); struct message msg = _msg(from, priority, msg_type);
msg.gamestatus.time_left = time_left; msg.gamestatus.time_left = time_left;
msg.gamestatus.strikes = strikes; msg.gamestatus.strikes = strikes;
msg.gamestatus.max_strikes = max_strikes; msg.gamestatus.max_strikes = max_strikes;
obuscan_send(&msg); send(&msg);
} }
@ -123,74 +127,74 @@ inline void _obuscan_send_payld_gamestatus(
/** /**
* Send a controller "ACK" OBUS message * Send a controller "ACK" OBUS message
*/ */
inline void obuscan_send_c_ack(struct module from) { inline void send_c_ack(struct module from) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
struct obus_message msg = _obuscan_msg(from, false, OBUS_MSGTYPE_C_ACK); struct message msg = _msg(from, false, OBUS_MSGTYPE_C_ACK);
obuscan_send(&msg); send(&msg);
} }
/** /**
* Send a controller "hello" OBUS message * Send a controller "hello" OBUS message
*/ */
inline void obuscan_send_c_hello(struct module from) { inline void send_c_hello(struct module from) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
struct obus_message msg = _obuscan_msg(from, false, OBUS_MSGTYPE_C_HELLO); struct message msg = _msg(from, false, OBUS_MSGTYPE_C_HELLO);
obuscan_send(&msg); send(&msg);
} }
/** /**
* Send a controller "game start" OBUS message * Send a controller "game start" OBUS message
*/ */
inline void obuscan_send_c_gamestart( inline void send_c_gamestart(
struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
_obuscan_send_payld_gamestatus( _send_payld_gamestatus(
from, false, OBUS_MSGTYPE_C_GAMESTART, time_left, strikes, max_strikes); from, false, OBUS_MSGTYPE_C_GAMESTART, time_left, strikes, max_strikes);
} }
/** /**
* Send a controller "state" OBUS message * Send a controller "state" OBUS message
*/ */
inline void obuscan_send_c_state( inline void send_c_state(
struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
_obuscan_send_payld_gamestatus( _send_payld_gamestatus(
from, false, OBUS_MSGTYPE_C_STATE, time_left, strikes, max_strikes); from, false, OBUS_MSGTYPE_C_STATE, time_left, strikes, max_strikes);
} }
/** /**
* Send a controller "solved" OBUS message * Send a controller "solved" OBUS message
*/ */
inline void obuscan_send_c_solved( inline void send_c_solved(
struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
_obuscan_send_payld_gamestatus( _send_payld_gamestatus(
from, false, OBUS_MSGTYPE_C_SOLVED, time_left, strikes, max_strikes); from, false, OBUS_MSGTYPE_C_SOLVED, time_left, strikes, max_strikes);
} }
/** /**
* Send a controller "timeout" OBUS message * Send a controller "timeout" OBUS message
*/ */
inline void obuscan_send_c_timeout( inline void send_c_timeout(
struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
_obuscan_send_payld_gamestatus( _send_payld_gamestatus(
from, false, OBUS_MSGTYPE_C_TIMEOUT, time_left, strikes, max_strikes); from, false, OBUS_MSGTYPE_C_TIMEOUT, time_left, strikes, max_strikes);
} }
/** /**
* Send a controller "strikeout" OBUS message * Send a controller "strikeout" OBUS message
*/ */
inline void obuscan_send_c_strikeout( inline void send_c_strikeout(
struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) { struct module from, uint32_t time_left, uint8_t strikes, uint8_t max_strikes) {
assert(from.type == OBUS_TYPE_CONTROLLER); assert(from.type == OBUS_TYPE_CONTROLLER);
_obuscan_send_payld_gamestatus( _send_payld_gamestatus(
from, false, OBUS_MSGTYPE_C_STRIKEOUT, time_left, strikes, max_strikes); from, false, OBUS_MSGTYPE_C_STRIKEOUT, time_left, strikes, max_strikes);
} }
@ -198,29 +202,31 @@ inline void obuscan_send_c_strikeout(
/** /**
* Send a module "hello" OBUS message * Send a module "hello" OBUS message
*/ */
inline void obuscan_send_m_hello(struct module from) { inline void send_m_hello(struct module from) {
assert(from.type != OBUS_TYPE_CONTROLLER); assert(from.type != OBUS_TYPE_CONTROLLER);
struct obus_message msg = _obuscan_msg(from, false, OBUS_MSGTYPE_M_HELLO); struct message msg = _msg(from, false, OBUS_MSGTYPE_M_HELLO);
obuscan_send(&msg); send(&msg);
} }
/** /**
* Send a module "strike" OBUS message * Send a module "strike" OBUS message
*/ */
inline void obuscan_send_m_strike(struct module from, uint8_t count) { inline void send_m_strike(struct module from, uint8_t count) {
assert(from.type != OBUS_TYPE_CONTROLLER); assert(from.type != OBUS_TYPE_CONTROLLER);
struct obus_message msg = _obuscan_msg(from, false, OBUS_MSGTYPE_M_STRIKE); struct message msg = _msg(from, false, OBUS_MSGTYPE_M_STRIKE);
msg.count = count; msg.count = count;
obuscan_send(&msg); send(&msg);
} }
/** /**
* Send a module "solved" OBUS message * Send a module "solved" OBUS message
*/ */
inline void obuscan_send_m_solved(struct module from) { inline void send_m_solved(struct module from) {
assert(from.type != OBUS_TYPE_CONTROLLER); assert(from.type != OBUS_TYPE_CONTROLLER);
struct obus_message msg = _obuscan_msg(from, false, OBUS_MSGTYPE_M_STRIKE); struct message msg = _msg(from, false, OBUS_MSGTYPE_M_STRIKE);
obuscan_send(&msg); send(&msg);
}
} }
#endif /* end of include guard: OBUS_CAN_H */ #endif /* end of include guard: OBUS_CAN_H */

32
lib/obus_module.cpp Normal file
View file

@ -0,0 +1,32 @@
#include "obus_can.h"
#include "obus_module.h"
namespace obus_module {
struct obus_can::module this_module;
uint8_t strike_count;
void setup(uint8_t type, uint8_t id) {
this_module.type = type;
this_module.id = id;
obus_can::init();
strike_count = 0;
}
void loop() {
}
void strike() {
strike_count++;
obus_can::send_m_strike(this_module, strike_count);
}
void solve() {
obus_can::send_m_solved(this_module);
}
}

19
lib/obus_module.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef OBUS_MODULE_H
#define OBUS_MODULE_H
#include "Arduino.h"
#include <obus_can.h>
namespace obus_module {
void setup(uint8_t type, uint8_t id);
void loop();
void strike();
void solve();
}
#endif /* end of include guard: OBUS_MODULE_H */

View file

@ -20,7 +20,7 @@
uint8_t state = STATE_INACTIVE; uint8_t state = STATE_INACTIVE;
struct module connected_modules_ids[OBUS_MAX_MODULES]; struct obus_can::module connected_modules_ids[OBUS_MAX_MODULES];
uint8_t nr_connected_modules; uint8_t nr_connected_modules;
uint8_t strikes; uint8_t strikes;
@ -34,7 +34,7 @@ uint32_t hello_round_start;
uint32_t game_start; uint32_t game_start;
uint32_t last_update; uint32_t last_update;
struct module this_module = { struct obus_can::module this_module = {
.type = OBUS_TYPE_CONTROLLER, .type = OBUS_TYPE_CONTROLLER,
.id = OBUS_CONTROLLER_ID .id = OBUS_CONTROLLER_ID
}; };
@ -42,7 +42,7 @@ struct module this_module = {
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
obuscan_init(); obus_can::init();
state = STATE_INACTIVE; state = STATE_INACTIVE;
} }
@ -82,13 +82,13 @@ void start_hello() {
unsolved_puzzles[i] = 0; unsolved_puzzles[i] = 0;
} }
obuscan_send_c_hello(this_module); obus_can::send_c_hello(this_module);
Serial.println(F(" Start of discovery round")); Serial.println(F(" Start of discovery round"));
} }
uint16_t full_module_id(struct module mod) { uint16_t full_module_id(struct obus_can::module mod) {
return \ return \
((uint16_t) mod.type << 8) | \ ((uint16_t) mod.type << 8) | \
(uint16_t) mod.id; (uint16_t) mod.id;
@ -96,10 +96,10 @@ uint16_t full_module_id(struct module mod) {
void receive_hello() { void receive_hello() {
struct obus_message msg; struct obus_can::message msg;
uint32_t current_time = millis(); uint32_t current_time = millis();
if (obuscan_receive(&msg)) { if (obus_can::receive(&msg)) {
if (msg.msg_type == OBUS_MSGTYPE_M_HELLO) { if (msg.msg_type == OBUS_MSGTYPE_M_HELLO) {
Serial.print(" Registered module "); Serial.print(" Registered module ");
Serial.println(full_module_id(msg.from)); Serial.println(full_module_id(msg.from));
@ -113,7 +113,7 @@ void receive_hello() {
} }
} }
obuscan_send_c_ack(this_module); obus_can::send_c_ack(this_module);
Serial.println(" ACK"); Serial.println(" ACK");
} }
} else if (current_time - hello_round_start > OBUS_DISC_DURATION_MS) { } else if (current_time - hello_round_start > OBUS_DISC_DURATION_MS) {
@ -132,14 +132,14 @@ void initialize_game() {
Serial.println(" Game started"); Serial.println(" Game started");
obuscan_send_c_gamestart(this_module, OBUS_GAME_DURATION_MS, strikes, OBUS_MAX_STRIKES); obus_can::send_c_gamestart(this_module, OBUS_GAME_DURATION_MS, strikes, OBUS_MAX_STRIKES);
} }
void receive_module_update() { void receive_module_update() {
struct obus_message msg; struct obus_can::message msg;
if (obuscan_receive(&msg)) { if (obus_can::receive(&msg)) {
switch (msg.msg_type) { switch (msg.msg_type) {
case OBUS_MSGTYPE_M_STRIKE: case OBUS_MSGTYPE_M_STRIKE:
@ -172,25 +172,25 @@ void game_loop() {
if (check_solved()) { if (check_solved()) {
Serial.println(" Game solved"); Serial.println(" Game solved");
obuscan_send_c_solved(this_module, time_left, strikes, OBUS_MAX_STRIKES); obus_can::send_c_solved(this_module, time_left, strikes, OBUS_MAX_STRIKES);
state = STATE_INACTIVE; state = STATE_INACTIVE;
return; return;
} }
if (time_left == 0) { if (time_left == 0) {
Serial.println(" Time's up"); Serial.println(" Time's up");
obuscan_send_c_timeout(this_module, time_left, strikes, OBUS_MAX_STRIKES); obus_can::send_c_timeout(this_module, time_left, strikes, OBUS_MAX_STRIKES);
state = STATE_INACTIVE; state = STATE_INACTIVE;
return; return;
} }
if (strikes >= OBUS_MAX_STRIKES) { if (strikes >= OBUS_MAX_STRIKES) {
Serial.println(" Strikeout"); Serial.println(" Strikeout");
obuscan_send_c_strikeout(this_module, time_left, strikes, OBUS_MAX_STRIKES); obus_can::send_c_strikeout(this_module, time_left, strikes, OBUS_MAX_STRIKES);
state = STATE_INACTIVE; state = STATE_INACTIVE;
return; return;
} }
if (last_update + OBUS_UPDATE_INTERVAL <= current_time) { if (last_update + OBUS_UPDATE_INTERVAL <= current_time) {
obuscan_send_c_state(this_module, time_left, strikes, OBUS_MAX_STRIKES); obus_can::send_c_state(this_module, time_left, strikes, OBUS_MAX_STRIKES);
last_update = current_time; last_update = current_time;
} }
} }

View file

@ -8,12 +8,12 @@ void setup() {
// Choose one // Choose one
// Puzzle: a module that must be solved // Puzzle: a module that must be solved
obusmodule_init(OBUS_TYPE_PUZZLE, /* Retrieve ID from MOANA */); obus_module::setup(OBUS_TYPE_PUZZLE, /* Retrieve ID from MOANA */);
// Needy: a module that periodically requires an action not to get strikes // Needy: a module that periodically requires an action not to get strikes
// obusmodule_init(OBUS_TYPE_NEEDY, /* Retrieve ID from MOANA */); // obusmodule_init(OBUS_TYPE_NEEDY, /* Retrieve ID from MOANA */);
} }
void loop() { void loop() {
obusmodule_loop(); obus_module::loop();
} }