Change library to use callback functions, add basic hardware

This commit is contained in:
redfast00 2020-08-26 23:35:13 +02:00
parent 920723afed
commit 54a87bb711
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
4 changed files with 58 additions and 5 deletions

View file

@ -2,9 +2,12 @@
#include "obus_can.h"
// Chip select for the CAN module
#define MCP_CS 8
namespace obus_can {
MCP2515 mcp2515(10);
MCP2515 mcp2515(MCP_CS);
bool is_init = false;

View file

@ -1,11 +1,23 @@
#include "obus_can.h"
#include "obus_module.h"
#define RED_LED A4
#define GREEN_LED A5
#define MCP_INT 2
namespace obus_module {
struct obus_can::module this_module;
uint8_t strike_count;
bool running;
bool error;
uint32_t time_stop_strike_led;
void interrupt_can_error() {
error = true;
}
void setup(uint8_t type, uint8_t id) {
this_module.type = type;
@ -14,19 +26,44 @@ void setup(uint8_t type, uint8_t id) {
obus_can::init();
strike_count = 0;
running = false;
error = false;
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
}
void loop() {
bool loop(obus_can::message* message) {
// Check if the message buffer overflowed
if (error) {
// Loop forever while blinking status led orange
bool blink = false;
while (true) {
digitalWrite(RED_LED, blink);
digitalWrite(GREEN_LED, blink);
blink = !blink;
delay(500);
}
}
if (time_stop_strike_led && time_stop_strike_led > millis()) {
digitalWrite(RED_LED, LOW);
}
// TODO receive CAN frame and call callback functions
}
void strike() {
strike_count++;
digitalWrite(RED_LED, HIGH);
time_stop_strike_led = millis() + 1000;
obus_can::send_m_strike(this_module, strike_count);
}
void solve() {
obus_can::send_m_solved(this_module);
digitalWrite(GREEN_LED, HIGH);
running = false;
}
}

View file

@ -4,11 +4,15 @@
#include "Arduino.h"
#include <obus_can.h>
void callback_game_start();
void callback_game_stop();
namespace obus_module {
void setup(uint8_t type, uint8_t id);
void loop();
bool loop(obus_can::message* message);
void strike();

View file

@ -4,7 +4,7 @@
#include <obus_module.h>
void setup() {
Serial.begin(9600);
Serial.begin(115200);
// Choose one
// Puzzle: a module that must be solved
@ -13,7 +13,16 @@ void setup() {
// obusmodule_init(OBUS_TYPE_NEEDY, /* Retrieve ID from MOANA */);
}
obus_can::message message;
void loop() {
obus_module::loop();
bool is_message_valid = obus_module::loop(&message);
}
void callback_game_start() {
}
void callback_game_stop() {
}