Merge pull request #22 from ZeusWPI/separate-function-calls
Separate function calls
This commit is contained in:
commit
82f4405f41
|
@ -50,8 +50,9 @@ class Message:
|
||||||
timeleft = self.payload[1] << 0x18 | self.payload[2] << 0x10 | self.payload[3] << 0x08 | self.payload[4]
|
timeleft = self.payload[1] << 0x18 | self.payload[2] << 0x10 | self.payload[3] << 0x08 | self.payload[4]
|
||||||
strikes = self.payload[5]
|
strikes = self.payload[5]
|
||||||
max_strikes = self.payload[6]
|
max_strikes = self.payload[6]
|
||||||
|
solved_puzzle_modules = self.payload[7]
|
||||||
|
|
||||||
return f'{timeleft/1000:3.2f} {strikes:02}/{max_strikes:02}'
|
return f'{timeleft/1000:3.2f} {strikes:02}/{max_strikes:02} [{solved_puzzle_modules:02}]'
|
||||||
|
|
||||||
def parse_message(self):
|
def parse_message(self):
|
||||||
sender_type = self.sender_type()
|
sender_type = self.sender_type()
|
||||||
|
|
|
@ -51,19 +51,19 @@ Types for controller:
|
||||||
- 2 game start
|
- 2 game start
|
||||||
[ X B B B B B B B ]
|
[ X B B B B B B B ]
|
||||||
-------- - - -
|
-------- - - -
|
||||||
time left ↓ ↓ reserved
|
time left ↓ ↓ #puzzle modules
|
||||||
#strikes #max strikes
|
#strikes #max strikes
|
||||||
|
|
||||||
- 3 state (every x ms – e.g. in the middle of each second)
|
- 3 state (every x ms – e.g. in the middle of each second)
|
||||||
[ X B B B B B B B ]
|
[ X B B B B B B B ]
|
||||||
-------- - - -
|
-------- - - -
|
||||||
time left ↓ ↓ reserved
|
time left ↓ ↓ #solved puzzle modules
|
||||||
#strikes #max strikes
|
#strikes #max strikes
|
||||||
|
|
||||||
- 4-6 solved, timeout, strikeout
|
- 4-6 solved, timeout, strikeout
|
||||||
[ X B B B B B B B ]
|
[ X B B B B B B B ]
|
||||||
-------- - - -
|
-------- - - -
|
||||||
end time ↓ ↓ reserved
|
end time ↓ ↓ #solved puzzle modules
|
||||||
#strikes #max strikes
|
#strikes #max strikes
|
||||||
|
|
||||||
- 7 info start
|
- 7 info start
|
||||||
|
|
|
@ -114,13 +114,14 @@ bool receive(struct message *msg) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OBUS_PAYLDTYPE_GAMESTATUS:
|
case OBUS_PAYLDTYPE_GAMESTATUS:
|
||||||
if (receive_frame.can_dlc < 7) {
|
if (receive_frame.can_dlc < 8) {
|
||||||
Serial.println(F("W Received illegal gamestatus msg: payload <7"));
|
Serial.println(F("W Received illegal gamestatus msg: payload <8"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
msg->gamestatus.time_left = unpack_4b_into_u32(&(receive_frame.data[1]));
|
msg->gamestatus.time_left = unpack_4b_into_u32(&(receive_frame.data[1]));
|
||||||
msg->gamestatus.strikes = receive_frame.data[5];
|
msg->gamestatus.strikes = receive_frame.data[5];
|
||||||
msg->gamestatus.max_strikes = receive_frame.data[6];
|
msg->gamestatus.max_strikes = receive_frame.data[6];
|
||||||
|
msg->gamestatus.puzzle_modules_solved = receive_frame.data[7];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OBUS_PAYLDTYPE_COUNT:
|
case OBUS_PAYLDTYPE_COUNT:
|
||||||
|
@ -186,7 +187,8 @@ void send(struct message *msg) {
|
||||||
pack_u32_into_4b(&(send_frame.data[1]), msg->gamestatus.time_left);
|
pack_u32_into_4b(&(send_frame.data[1]), msg->gamestatus.time_left);
|
||||||
send_frame.data[5] = msg->gamestatus.strikes;
|
send_frame.data[5] = msg->gamestatus.strikes;
|
||||||
send_frame.data[6] = msg->gamestatus.max_strikes;
|
send_frame.data[6] = msg->gamestatus.max_strikes;
|
||||||
length = 7;
|
send_frame.data[7] = msg->gamestatus.puzzle_modules_solved;
|
||||||
|
length = 8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OBUS_PAYLDTYPE_COUNT:
|
case OBUS_PAYLDTYPE_COUNT:
|
||||||
|
|
|
@ -51,6 +51,7 @@ struct payld_gamestatus {
|
||||||
uint32_t time_left;
|
uint32_t time_left;
|
||||||
uint8_t strikes;
|
uint8_t strikes;
|
||||||
uint8_t max_strikes;
|
uint8_t max_strikes;
|
||||||
|
uint8_t puzzle_modules_solved;
|
||||||
};
|
};
|
||||||
struct payld_infomessage {
|
struct payld_infomessage {
|
||||||
uint8_t len;
|
uint8_t len;
|
||||||
|
@ -128,12 +129,13 @@ inline struct message _msg(struct module from, bool priority, uint8_t msg_type)
|
||||||
*/
|
*/
|
||||||
inline void _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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
struct message msg = _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;
|
||||||
|
msg.gamestatus.puzzle_modules_solved = puzzle_modules_solved;
|
||||||
send(&msg);
|
send(&msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,44 +165,44 @@ inline void send_c_hello(struct module from) {
|
||||||
* Send a controller "game start" OBUS message
|
* Send a controller "game start" OBUS message
|
||||||
*/
|
*/
|
||||||
inline void 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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
assert(from.type == OBUS_TYPE_CONTROLLER);
|
assert(from.type == OBUS_TYPE_CONTROLLER);
|
||||||
_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, puzzle_modules_solved);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a controller "state" OBUS message
|
* Send a controller "state" OBUS message
|
||||||
*/
|
*/
|
||||||
inline void 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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
assert(from.type == OBUS_TYPE_CONTROLLER);
|
assert(from.type == OBUS_TYPE_CONTROLLER);
|
||||||
_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, puzzle_modules_solved);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a controller "solved" OBUS message
|
* Send a controller "solved" OBUS message
|
||||||
*/
|
*/
|
||||||
inline void 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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
assert(from.type == OBUS_TYPE_CONTROLLER);
|
assert(from.type == OBUS_TYPE_CONTROLLER);
|
||||||
_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, puzzle_modules_solved);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a controller "timeout" OBUS message
|
* Send a controller "timeout" OBUS message
|
||||||
*/
|
*/
|
||||||
inline void 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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
assert(from.type == OBUS_TYPE_CONTROLLER);
|
assert(from.type == OBUS_TYPE_CONTROLLER);
|
||||||
_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, puzzle_modules_solved);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -216,11 +218,11 @@ inline void send_c_infostart(struct module from) {
|
||||||
* Send a controller "strikeout" OBUS message
|
* Send a controller "strikeout" OBUS message
|
||||||
*/
|
*/
|
||||||
inline void 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, uint8_t puzzle_modules_solved) {
|
||||||
|
|
||||||
assert(from.type == OBUS_TYPE_CONTROLLER);
|
assert(from.type == OBUS_TYPE_CONTROLLER);
|
||||||
_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, puzzle_modules_solved);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#define BLINK_DELAY_NORMAL 500
|
#define BLINK_DELAY_NORMAL 500
|
||||||
#define BLINK_DELAY_FAST 300
|
#define BLINK_DELAY_FAST 300
|
||||||
|
|
||||||
|
#define MAX_TIME_BETWEEN_CALLS 100
|
||||||
|
|
||||||
// Not used normally
|
// Not used normally
|
||||||
#define MCP_INT 2
|
#define MCP_INT 2
|
||||||
|
|
||||||
|
@ -21,6 +23,7 @@ namespace obus_module {
|
||||||
struct obus_can::module this_module;
|
struct obus_can::module this_module;
|
||||||
uint8_t strike_count;
|
uint8_t strike_count;
|
||||||
bool active;
|
bool active;
|
||||||
|
uint32_t next_loop_call_deadline;
|
||||||
|
|
||||||
// Current LED status
|
// Current LED status
|
||||||
struct color { bool red; bool green; };
|
struct color { bool red; bool green; };
|
||||||
|
@ -81,6 +84,7 @@ void _setLedBlink(struct color color, uint16_t delay) {
|
||||||
void _resetState() {
|
void _resetState() {
|
||||||
strike_count = 0;
|
strike_count = 0;
|
||||||
active = false;
|
active = false;
|
||||||
|
next_loop_call_deadline = 0;
|
||||||
|
|
||||||
if (this_module.type == OBUS_TYPE_PUZZLE || this_module.type == OBUS_TYPE_NEEDY) {
|
if (this_module.type == OBUS_TYPE_PUZZLE || this_module.type == OBUS_TYPE_NEEDY) {
|
||||||
pinMode(RED_LED, OUTPUT);
|
pinMode(RED_LED, OUTPUT);
|
||||||
|
@ -99,29 +103,55 @@ void setup(uint8_t type, uint8_t id) {
|
||||||
_resetState();
|
_resetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7])) {
|
void empty_callback_info(uint8_t info_id, uint8_t infomessage[7]) {
|
||||||
// TODO this can be more efficient by only enabling error interrupts and
|
// Mark arguments as not used
|
||||||
// reacting to the interrupt instead of checking if the flag is set in a loop
|
(void)info_id;
|
||||||
// We will need to fork our CAN library for this, because the needed functions are private.
|
(void)infomessage;
|
||||||
// Also, we can't do this by default, because the INT pin is normally not connected to the board
|
}
|
||||||
if (obus_can::is_error_condition()) {
|
|
||||||
|
void empty_callback_state(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved) {
|
||||||
|
// Mark arguments as not used
|
||||||
|
(void)time_left;
|
||||||
|
(void)strikes;
|
||||||
|
(void)max_strikes;
|
||||||
|
(void)puzzle_modules_solved;
|
||||||
|
}
|
||||||
|
|
||||||
|
void blink_error(String message) {
|
||||||
bool blink = false;
|
bool blink = false;
|
||||||
while (true) {
|
while (true) {
|
||||||
digitalWrite(RED_LED, blink);
|
digitalWrite(RED_LED, blink);
|
||||||
digitalWrite(GREEN_LED, blink);
|
digitalWrite(GREEN_LED, blink);
|
||||||
blink = !blink;
|
blink = !blink;
|
||||||
delay(BLINK_DELAY_NORMAL);
|
delay(BLINK_DELAY_NORMAL);
|
||||||
|
Serial.println(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool interesting_message = false;
|
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(uint8_t puzzle_modules), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]), void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved)) {
|
||||||
|
// TODO this can be more efficient by only enabling error interrupts and
|
||||||
|
// reacting to the interrupt instead of checking if the flag is set in a loop
|
||||||
|
// We will need to fork our CAN library for this, because the needed functions are private.
|
||||||
|
// Also, we can't do this by default, because the INT pin is normally not connected to the board
|
||||||
|
if (obus_can::is_error_condition()) {
|
||||||
|
blink_error(F("E CAN error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next_loop_call_deadline != 0 && millis() > next_loop_call_deadline) {
|
||||||
|
blink_error(F("E missed deadline"));
|
||||||
|
}
|
||||||
|
next_loop_call_deadline = millis() + MAX_TIME_BETWEEN_CALLS;
|
||||||
|
|
||||||
|
bool received_message = false;
|
||||||
if (obus_can::receive(message)) {
|
if (obus_can::receive(message)) {
|
||||||
|
received_message = true;
|
||||||
if (is_from_controller(message->from)) {
|
if (is_from_controller(message->from)) {
|
||||||
switch (message->msg_type) {
|
switch (message->msg_type) {
|
||||||
case OBUS_MSGTYPE_C_GAMESTART:
|
case OBUS_MSGTYPE_C_GAMESTART:
|
||||||
active = true;
|
active = true;
|
||||||
_setLed(COLOR_YELLOW);
|
_setLed(COLOR_YELLOW);
|
||||||
callback_game_start();
|
// The field is named puzzle_modules_solved, but it actually contains the amount of puzzle modules
|
||||||
|
callback_game_start(message->gamestatus.puzzle_modules_solved);
|
||||||
break;
|
break;
|
||||||
case OBUS_MSGTYPE_C_HELLO:
|
case OBUS_MSGTYPE_C_HELLO:
|
||||||
_resetState();
|
_resetState();
|
||||||
|
@ -137,7 +167,7 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
|
||||||
case OBUS_MSGTYPE_C_ACK:
|
case OBUS_MSGTYPE_C_ACK:
|
||||||
break;
|
break;
|
||||||
case OBUS_MSGTYPE_C_STATE:
|
case OBUS_MSGTYPE_C_STATE:
|
||||||
interesting_message = true;
|
callback_state(message->gamestatus.time_left, message->gamestatus.strikes, message->gamestatus.max_strikes, message->gamestatus.puzzle_modules_solved);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -151,12 +181,12 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
|
||||||
|
|
||||||
_ledLoop();
|
_ledLoop();
|
||||||
|
|
||||||
return interesting_message;
|
return received_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7])) {
|
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(uint8_t puzzle_modules), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]), void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved)) {
|
||||||
// For now this is the same function
|
// For now this is the same function
|
||||||
return loopPuzzle(message, callback_game_start, callback_game_stop, callback_info);
|
return loopPuzzle(message, callback_game_start, callback_game_stop, callback_info, callback_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*)) {
|
bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*)) {
|
||||||
|
|
|
@ -12,9 +12,12 @@ 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, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7]));
|
void empty_callback_info(uint8_t info_id, uint8_t infomessage[7]);
|
||||||
|
void empty_callback_state(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved);
|
||||||
|
|
||||||
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7]));
|
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(uint8_t puzzle_modules), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]) = empty_callback_info, void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved) = empty_callback_state);
|
||||||
|
|
||||||
|
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(uint8_t puzzle_modules), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]) = empty_callback_info, void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_solved) = empty_callback_state);
|
||||||
|
|
||||||
bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*));
|
bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*));
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ uint8_t shiftInFixed(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
|
||||||
else
|
else
|
||||||
value |= digitalRead(dataPin) << (7 - i);
|
value |= digitalRead(dataPin) << (7 - i);
|
||||||
digitalWrite(clockPin, HIGH);
|
digitalWrite(clockPin, HIGH);
|
||||||
delay(1);
|
delayMicroseconds(3);
|
||||||
digitalWrite(clockPin, LOW);
|
digitalWrite(clockPin, LOW);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
@ -50,7 +50,7 @@ void loop() // run over and over
|
||||||
{
|
{
|
||||||
// read data in
|
// read data in
|
||||||
digitalWrite(READ_PIN, HIGH);
|
digitalWrite(READ_PIN, HIGH);
|
||||||
delay(200);
|
delayMicroseconds(3);
|
||||||
digitalWrite(READ_PIN, LOW);
|
digitalWrite(READ_PIN, LOW);
|
||||||
|
|
||||||
// shift data bit by bit
|
// shift data bit by bit
|
||||||
|
|
|
@ -28,6 +28,7 @@ uint8_t state = STATE_INACTIVE;
|
||||||
struct obus_can::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 nr_connected_puzzles;
|
uint8_t nr_connected_puzzles;
|
||||||
|
uint8_t nr_solved_puzzles;
|
||||||
uint8_t strikes;
|
uint8_t strikes;
|
||||||
|
|
||||||
// Bitvector for checking if game is solved or not
|
// Bitvector for checking if game is solved or not
|
||||||
|
@ -86,6 +87,9 @@ void add_puzzle_to_bit_vector(uint8_t module_id) {
|
||||||
void solve_puzzle_in_bit_vector(uint8_t module_id) {
|
void solve_puzzle_in_bit_vector(uint8_t module_id) {
|
||||||
uint8_t byte_index = module_id >> 3;
|
uint8_t byte_index = module_id >> 3;
|
||||||
uint8_t bit_index = module_id & 0x07;
|
uint8_t bit_index = module_id & 0x07;
|
||||||
|
if (unsolved_puzzles[byte_index] & (0x1 << bit_index)) {
|
||||||
|
nr_solved_puzzles++;
|
||||||
|
}
|
||||||
unsolved_puzzles[byte_index] &= ~(0x1 << bit_index);
|
unsolved_puzzles[byte_index] &= ~(0x1 << bit_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,6 +180,7 @@ void receive_hello() {
|
||||||
|
|
||||||
void initialize_game() {
|
void initialize_game() {
|
||||||
strikes = 0;
|
strikes = 0;
|
||||||
|
nr_solved_puzzles = 0;
|
||||||
game_start = millis();
|
game_start = millis();
|
||||||
|
|
||||||
last_draw = 0;
|
last_draw = 0;
|
||||||
|
@ -185,7 +190,7 @@ void initialize_game() {
|
||||||
Serial.println(" Game started");
|
Serial.println(" Game started");
|
||||||
|
|
||||||
draw_display(millis(), OBUS_GAME_DURATION_MS);
|
draw_display(millis(), OBUS_GAME_DURATION_MS);
|
||||||
obus_can::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, nr_connected_puzzles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -241,14 +246,14 @@ void game_loop() {
|
||||||
|
|
||||||
if (check_solved()) {
|
if (check_solved()) {
|
||||||
Serial.println(" Game solved");
|
Serial.println(" Game solved");
|
||||||
obus_can::send_c_solved(this_module, time_left, strikes, OBUS_MAX_STRIKES);
|
obus_can::send_c_solved(this_module, time_left, strikes, OBUS_MAX_STRIKES, nr_solved_puzzles);
|
||||||
state = STATE_GAMEOVER;
|
state = STATE_GAMEOVER;
|
||||||
tm.displayText("dISArmEd");
|
tm.displayText("dISArmEd");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (time_left == 0) {
|
if (time_left == 0) {
|
||||||
Serial.println(" Time's up");
|
Serial.println(" Time's up");
|
||||||
obus_can::send_c_timeout(this_module, time_left, strikes, OBUS_MAX_STRIKES);
|
obus_can::send_c_timeout(this_module, time_left, strikes, OBUS_MAX_STRIKES, nr_solved_puzzles);
|
||||||
state = STATE_GAMEOVER;
|
state = STATE_GAMEOVER;
|
||||||
tm.displayText(" boo t");
|
tm.displayText(" boo t");
|
||||||
// m
|
// m
|
||||||
|
@ -258,7 +263,7 @@ void game_loop() {
|
||||||
}
|
}
|
||||||
if (strikes >= OBUS_MAX_STRIKES) {
|
if (strikes >= OBUS_MAX_STRIKES) {
|
||||||
Serial.println(" Strikeout");
|
Serial.println(" Strikeout");
|
||||||
obus_can::send_c_strikeout(this_module, time_left, strikes, OBUS_MAX_STRIKES);
|
obus_can::send_c_strikeout(this_module, time_left, strikes, OBUS_MAX_STRIKES, nr_solved_puzzles);
|
||||||
state = STATE_GAMEOVER;
|
state = STATE_GAMEOVER;
|
||||||
tm.displayText(" boo S");
|
tm.displayText(" boo S");
|
||||||
// m
|
// m
|
||||||
|
@ -270,7 +275,7 @@ void game_loop() {
|
||||||
draw_display(current_time, time_left);
|
draw_display(current_time, time_left);
|
||||||
|
|
||||||
if (last_update + OBUS_UPDATE_INTERVAL <= current_time) {
|
if (last_update + OBUS_UPDATE_INTERVAL <= current_time) {
|
||||||
obus_can::send_c_state(this_module, time_left, strikes, OBUS_MAX_STRIKES);
|
obus_can::send_c_state(this_module, time_left, strikes, OBUS_MAX_STRIKES, nr_solved_puzzles);
|
||||||
last_update = current_time;
|
last_update = current_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, callback_game_start, callback_game_stop, callback_info);
|
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
|
||||||
|
@ -58,14 +58,11 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_start() {
|
void callback_game_start(uint8_t puzzle_modules_connected) {
|
||||||
|
// Intentionally emtpy
|
||||||
|
(void)puzzle_modules_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_stop() {
|
void callback_game_stop() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_info(uint8_t info_id, uint8_t [7]) {
|
|
||||||
// Intentionally empty
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ void setup() {
|
||||||
obus_can::message message;
|
obus_can::message message;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
bool received = obus_module::loopPuzzle(&message, callback_game_start, callback_game_stop, callback_info);
|
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();
|
||||||
|
@ -34,14 +34,11 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_start() {
|
void callback_game_start(uint8_t puzzle_modules_connected) {
|
||||||
// Intentionally emtpy
|
// Intentionally emtpy
|
||||||
|
(void)puzzle_modules_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_stop() {
|
void callback_game_stop() {
|
||||||
// Intentionally empty
|
// Intentionally empty
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_info(uint8_t info_id, uint8_t [7]) {
|
|
||||||
// Intentionally empty
|
|
||||||
}
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ void setup() {
|
||||||
obus_can::message message;
|
obus_can::message message;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
bool received = obus_module::loopPuzzle(&message, callback_game_start, callback_game_stop, callback_info);
|
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) {
|
||||||
|
@ -82,7 +82,7 @@ uint8_t read_value_from_date_module(uint8_t bit_order) {
|
||||||
else
|
else
|
||||||
value |= read_bit << (7 - i);
|
value |= read_bit << (7 - i);
|
||||||
digitalWrite(DATE_CLOCK_PIN, HIGH);
|
digitalWrite(DATE_CLOCK_PIN, HIGH);
|
||||||
delay(1);
|
delayMicroseconds(3);
|
||||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
@ -90,7 +90,7 @@ uint8_t read_value_from_date_module(uint8_t bit_order) {
|
||||||
|
|
||||||
void read_from_date_module(uint8_t* data_out) {
|
void read_from_date_module(uint8_t* data_out) {
|
||||||
digitalWrite(DATE_READ_PIN, HIGH);
|
digitalWrite(DATE_READ_PIN, HIGH);
|
||||||
delay(200);
|
delayMicroseconds(3);
|
||||||
digitalWrite(DATE_READ_PIN, LOW);
|
digitalWrite(DATE_READ_PIN, LOW);
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
|
@ -121,14 +121,11 @@ bool check_date_code(uint8_t* code) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_start() {
|
void callback_game_start(uint8_t puzzle_modules_connected) {
|
||||||
// Intentionally emtpy
|
// Intentionally emtpy
|
||||||
|
(void)puzzle_modules_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_stop() {
|
void callback_game_stop() {
|
||||||
// Intentionally empty
|
// Intentionally empty
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_info(uint8_t info_id, uint8_t [7]) {
|
|
||||||
// Intentionally empty
|
|
||||||
}
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ void setup() {
|
||||||
obus_can::message message;
|
obus_can::message message;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
bool is_message_valid = obus_module::loop_puzzle(&message, callback_game_start, callback_game_stop, callback_info);
|
bool is_message_valid = obus_module::loopPuzzle(&message, callback_game_start, callback_game_stop);
|
||||||
// bool is_message_valid = obus_module::loop_needy(&message, callback_game_start, callback_game_stop, callback_info);
|
// bool is_message_valid = obus_module::loopNeedy(&message, callback_game_start, callback_game_stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_game_start() {
|
void callback_game_start() {
|
||||||
|
@ -27,7 +27,3 @@ void callback_game_start() {
|
||||||
void callback_game_stop() {
|
void callback_game_stop() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback_info(uint8_t info_id, uint8_t [7]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue