Add needy module, clean up code

This commit is contained in:
redfast00 2020-08-27 05:30:22 +02:00
parent 908095d572
commit ac2c8129a9
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
6 changed files with 102 additions and 10 deletions

View file

@ -11,7 +11,7 @@ namespace obus_module {
struct obus_can::module this_module;
uint8_t strike_count;
bool running;
bool active;
uint32_t time_stop_strike_led;
void setup(uint8_t type, uint8_t id) {
@ -21,14 +21,14 @@ void setup(uint8_t type, uint8_t id) {
obus_can::init();
strike_count = 0;
running = false;
active = false;
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
}
bool loop(obus_can::message* message) {
bool loopPuzzle(obus_can::message* message) {
// Check if we need to turn the red "strike" LED back off after
// turning it on because of a strike
if (time_stop_strike_led && time_stop_strike_led > millis()) {
@ -37,7 +37,7 @@ bool loop(obus_can::message* message) {
}
// 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
// We will need to fork our CAN library for this, because the needed functions
// are private
if (obus_can::is_error_condition()) {
bool blink = false;
@ -51,7 +51,7 @@ bool loop(obus_can::message* message) {
if (obus_can::receive(message)) {
switch(message->msg_type) {
case OBUS_MSGTYPE_C_GAMESTART:
running = true;
active = true;
callback_game_start();
break;
case OBUS_MSGTYPE_C_HELLO:
@ -60,7 +60,7 @@ bool loop(obus_can::message* message) {
case OBUS_MSGTYPE_C_SOLVED:
case OBUS_MSGTYPE_C_TIMEOUT:
case OBUS_MSGTYPE_C_STRIKEOUT:
running = false;
active = false;
callback_game_stop();
break;
case OBUS_MSGTYPE_C_ACK:
@ -70,10 +70,17 @@ bool loop(obus_can::message* message) {
}
return false;
}
}
bool loopNeedy(obus_can::message* message) {
// For now this is the same function
return loopPuzzle(message);
}
void strike() {
if (!active) {
return;
}
strike_count++;
digitalWrite(RED_LED, HIGH);
time_stop_strike_led = millis() + 1000;
@ -81,9 +88,16 @@ void strike() {
}
void solve() {
if (!active) {
return;
}
obus_can::send_m_solved(this_module);
digitalWrite(GREEN_LED, HIGH);
running = false;
active = false;
}
bool is_active() {
return active;
}
}

View file

@ -12,12 +12,16 @@ namespace obus_module {
void setup(uint8_t type, uint8_t id);
bool loop(obus_can::message* message);
bool loopPuzzle(obus_can::message* message);
bool loopNeedy(obus_can::message* message);
void strike();
void solve();
bool is_active();
}
#endif /* end of include guard: OBUS_MODULE_H */

View file

@ -18,7 +18,7 @@ void setup() {
obus_can::message message;
void loop() {
bool received = obus_module::loop(&message);
bool received = obus_module::loopPuzzle(&message);
// TODO handle update frames (not needed for this module, but could be useful as example code)
red_button.loop();

View file

@ -0,0 +1,6 @@
## Testmodule needy buttons
If the module starts making a sound, press the button.
### Credits
Module developed by redfast00.

View file

@ -0,0 +1,68 @@
// (c) 2020, redfast00
// See the LICENSE file for conditions for copying
#include <obus_module.h>
#include <ezButton.h>
#define SPEAKER_PIN 10
ezButton green_button(6);
void setup() {
Serial.begin(115200);
// WARNING: do not use 255 for your module
obus_module::setup(OBUS_TYPE_NEEDY, 255);
green_button.setDebounceTime(100);
}
obus_can::message message;
uint32_t next_activation_time = 0;
uint32_t trigger_time = 0;
void loop() {
bool is_message_valid = obus_module::loopNeedy(&message);
green_button.loop();
// Every second, have a 1/20 chance to trigger the countdown
if (obus_module::is_active() && !trigger_time && (millis() > next_activation_time)) {
next_activation_time = millis() + 1000;
if (random(20) == 0) {
trigger_time = millis() + 30000;
}
}
// Strike if time runs out
if (trigger_time && millis() > trigger_time) {
obus_module::strike();
trigger_time = 0;
}
// If the button is pressed, reset countdown if countdown is running, else strike
if (green_button.getCount() > 0) {
green_button.resetCount();
if (trigger_time) {
trigger_time = 0;
} else {
obus_module::strike();
}
}
// Play the appropriate sound
if (trigger_time && millis() > trigger_time - 15000) {
tone(SPEAKER_PIN, 440);
}
else if (trigger_time) {
tone(SPEAKER_PIN, 449);
} else {
noTone(SPEAKER_PIN);
}
}
void callback_game_start() {
}
void callback_game_stop() {
}

View file

@ -10,7 +10,7 @@ void setup() {
// Puzzle: a module that must be solved
obus_module::setup(OBUS_TYPE_PUZZLE, /* Retrieve ID from MOANA */);
// Needy: a module that periodically requires an action not to get strikes
// obusmodule_init(OBUS_TYPE_NEEDY, /* Retrieve ID from MOANA */);
// obus_module::setup(OBUS_TYPE_NEEDY, /* Retrieve ID from MOANA */);
}
obus_can::message message;