Merge pull request #14 from ZeusWPI/info-callback

Add callback for getting info
This commit is contained in:
redfast00 2021-01-30 14:26:55 +01:00 committed by GitHub
commit 1125b59a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 14 deletions

View file

@ -1,13 +1,14 @@
#include "obus_can.h"
#include "obus_module.h"
#define RED_LED A4
#define GREEN_LED A5
#define RED_LED 4
#define GREEN_LED 7
#define BLINK_DELAY_SLOW 1000
#define BLINK_DELAY_NORMAL 500
#define BLINK_DELAY_FAST 300
// Not used normally
#define MCP_INT 2
#define COLOR_OFF ((struct color) {false, false})
@ -98,11 +99,11 @@ void setup(uint8_t type, uint8_t id) {
_resetState();
}
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) {
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7])) {
// 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
// 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()) {
bool blink = false;
while (true) {
@ -141,6 +142,10 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
default:
break;
}
} else if (message->from.type == OBUS_TYPE_INFO) {
uint8_t infobuffer[7] = {0};
memcpy(infobuffer, message->infomessage.data, message->infomessage.len);
callback_info(message->from.id, infobuffer);
}
}
@ -149,9 +154,9 @@ bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void
return interesting_message;
}
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)()) {
bool loopNeedy(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t [7])) {
// For now this is the same function
return loopPuzzle(message, callback_game_start, callback_game_stop);
return loopPuzzle(message, callback_game_start, callback_game_stop, callback_info);
}
bool loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*)) {

View file

@ -12,9 +12,9 @@ namespace obus_module {
void setup(uint8_t type, uint8_t id);
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(), void (*callback_game_stop)());
bool loopPuzzle(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)(), void (*callback_game_stop)());
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 loopInfo(obus_can::message* message, int (*info_generator)(uint8_t*));

View file

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

View file

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

View file

@ -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, callback_game_start, callback_game_stop);
bool is_message_valid = obus_module::loopNeedy(&message, callback_game_start, callback_game_stop, callback_info);
green_button.loop();
// Every second, have a 1/20 chance to trigger the countdown
@ -65,3 +65,7 @@ void callback_game_start() {
void callback_game_stop() {
}
void callback_info(uint8_t info_id, uint8_t [7]) {
// Intentionally empty
}

View file

@ -16,8 +16,8 @@ void setup() {
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);
bool is_message_valid = obus_module::loop_puzzle(&message, callback_game_start, callback_game_stop, callback_info);
// bool is_message_valid = obus_module::loop_needy(&message, callback_game_start, callback_game_stop, callback_info);
}
void callback_game_start() {
@ -27,3 +27,7 @@ void callback_game_start() {
void callback_game_stop() {
}
void callback_info(uint8_t info_id, uint8_t [7]) {
}