Add field for amount of puzzle modules connected
This commit is contained in:
parent
07fd7ed1c8
commit
afd3ae67f8
7 changed files with 79 additions and 75 deletions
|
@ -51,7 +51,7 @@ Types for controller:
|
|||
- 2 game start
|
||||
[ X B B B B B B B ]
|
||||
-------- - - -
|
||||
time left ↓ ↓ reserved
|
||||
time left ↓ ↓ #puzzle modules
|
||||
#strikes #max strikes
|
||||
|
||||
- 3 state (every x ms – e.g. in the middle of each second)
|
||||
|
|
|
@ -128,7 +128,7 @@ void blink_error(String message) {
|
|||
}
|
||||
}
|
||||
|
||||
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), 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)) {
|
||||
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.
|
||||
|
@ -150,7 +150,8 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
|
|||
case OBUS_MSGTYPE_C_GAMESTART:
|
||||
active = true;
|
||||
_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;
|
||||
case OBUS_MSGTYPE_C_HELLO:
|
||||
_resetState();
|
||||
|
@ -183,7 +184,7 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
|
|||
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 infomessage[7]), void (*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)(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
|
||||
return loopPuzzle(message, callback_game_start, callback_game_stop, callback_info, callback_state);
|
||||
}
|
||||
|
|
|
@ -15,9 +15,9 @@ void setup(uint8_t type, uint8_t id);
|
|||
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 loopPuzzle(obus_can::message* message, void (*callback_game_start)(), 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 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)(), 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*));
|
||||
|
||||
|
|
|
@ -190,7 +190,7 @@ void initialize_game() {
|
|||
Serial.println(" Game started");
|
||||
|
||||
draw_display(millis(), OBUS_GAME_DURATION_MS);
|
||||
obus_can::send_c_gamestart(this_module, OBUS_GAME_DURATION_MS, strikes, OBUS_MAX_STRIKES, nr_solved_puzzles);
|
||||
obus_can::send_c_gamestart(this_module, OBUS_GAME_DURATION_MS, strikes, OBUS_MAX_STRIKES, nr_connected_puzzles);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -58,8 +58,9 @@ 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() {
|
||||
|
|
|
@ -34,8 +34,9 @@ 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() {
|
||||
|
|
|
@ -27,104 +27,105 @@
|
|||
|
||||
|
||||
uint8_t correct_code[4] = {
|
||||
DATE_MODE_STOP,
|
||||
12,
|
||||
34,
|
||||
56
|
||||
DATE_MODE_STOP,
|
||||
12,
|
||||
34,
|
||||
56
|
||||
};
|
||||
|
||||
ezButton solve_button(DATE_SOLVE_BTN);
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
obus_module::setup(OBUS_TYPE_PUZZLE, 123);
|
||||
solve_button.setDebounceTime(10);
|
||||
solve_button.setCountMode(COUNT_RISING);
|
||||
setup_date_module();
|
||||
Serial.begin(115200);
|
||||
obus_module::setup(OBUS_TYPE_PUZZLE, 123);
|
||||
solve_button.setDebounceTime(10);
|
||||
solve_button.setCountMode(COUNT_RISING);
|
||||
setup_date_module();
|
||||
}
|
||||
|
||||
obus_can::message message;
|
||||
|
||||
void loop() {
|
||||
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) {
|
||||
uint8_t data[4];
|
||||
read_from_date_module(data);
|
||||
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) {
|
||||
uint8_t data[4];
|
||||
read_from_date_module(data);
|
||||
|
||||
if (check_date_code(data)) {
|
||||
Serial.println("SOLVED!");
|
||||
obus_module::solve();
|
||||
} else {
|
||||
Serial.println("STRIKE!");
|
||||
obus_module::strike();
|
||||
}
|
||||
solve_button.resetCount();
|
||||
}
|
||||
if (check_date_code(data)) {
|
||||
Serial.println("SOLVED!");
|
||||
obus_module::solve();
|
||||
} else {
|
||||
Serial.println("STRIKE!");
|
||||
obus_module::strike();
|
||||
}
|
||||
solve_button.resetCount();
|
||||
}
|
||||
}
|
||||
|
||||
void setup_date_module() {
|
||||
pinMode(DATE_CLOCK_PIN, OUTPUT);
|
||||
pinMode(DATE_DATA_PIN, INPUT);
|
||||
pinMode(DATE_READ_PIN, OUTPUT);
|
||||
pinMode(DATE_CLOCK_PIN, OUTPUT);
|
||||
pinMode(DATE_DATA_PIN, INPUT);
|
||||
pinMode(DATE_READ_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
uint8_t read_value_from_date_module(uint8_t bit_order) {
|
||||
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
uint8_t value = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint8_t read_bit = digitalRead(DATE_DATA_PIN);
|
||||
if (bit_order == LSBFIRST)
|
||||
value |= read_bit << i;
|
||||
else
|
||||
value |= read_bit << (7 - i);
|
||||
digitalWrite(DATE_CLOCK_PIN, HIGH);
|
||||
delayMicroseconds(3);
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
}
|
||||
return value;
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
uint8_t value = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint8_t read_bit = digitalRead(DATE_DATA_PIN);
|
||||
if (bit_order == LSBFIRST)
|
||||
value |= read_bit << i;
|
||||
else
|
||||
value |= read_bit << (7 - i);
|
||||
digitalWrite(DATE_CLOCK_PIN, HIGH);
|
||||
delayMicroseconds(3);
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void read_from_date_module(uint8_t* data_out) {
|
||||
digitalWrite(DATE_READ_PIN, HIGH);
|
||||
delayMicroseconds(3);
|
||||
digitalWrite(DATE_READ_PIN, LOW);
|
||||
digitalWrite(DATE_READ_PIN, HIGH);
|
||||
delayMicroseconds(3);
|
||||
digitalWrite(DATE_READ_PIN, LOW);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
data_out[i] = read_value_from_date_module(LSBFIRST);
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
data_out[i] = read_value_from_date_module(LSBFIRST);
|
||||
}
|
||||
|
||||
for (int i = 1; i < 4; i++) {
|
||||
// Convert raw data to human interpretable number
|
||||
data_out[i] = 10*(data_out[i] & 0x0F) + ((data_out[i] & 0xF0) >> 4);
|
||||
}
|
||||
for (int i = 1; i < 4; i++) {
|
||||
// Convert raw data to human interpretable number
|
||||
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(" ");
|
||||
//}
|
||||
//for (int i = 0; i < 4; i++) {
|
||||
// Serial.print(data_out[i]);
|
||||
// Serial.print(" ");
|
||||
//}
|
||||
|
||||
//Serial.println();
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
//Serial.println();
|
||||
digitalWrite(DATE_CLOCK_PIN, LOW);
|
||||
}
|
||||
|
||||
bool check_date_code(uint8_t* code) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (code[i] != correct_code[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (code[i] != correct_code[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void callback_game_start() {
|
||||
// Intentionally emtpy
|
||||
void callback_game_start(uint8_t puzzle_modules_connected) {
|
||||
// Intentionally emtpy
|
||||
(void)puzzle_modules_connected;
|
||||
}
|
||||
|
||||
void callback_game_stop() {
|
||||
// Intentionally empty
|
||||
// Intentionally empty
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue