Created
January 30, 2018 20:03
-
-
Save insanity54/b3efe0339bc4a155d4da7b65a55a2537 to your computer and use it in GitHub Desktop.
Class accepting another class instance does not have access to vars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Button.h" | |
#include "ButtonManager.h" | |
#include "Phase.h" | |
#include "Sound.h" | |
// pin definitions | |
#define button0Pin 4 | |
#define button1Pin 7 | |
#define buzzerPin 9 | |
Phase phase = Phase(); | |
Button team0Button = Button(0, button0Pin, &phase); | |
Button team1Button = Button(1, button1Pin, &phase); | |
ButtonManager buttonManager = ButtonManager(team0Button, team1Button, &phase); | |
Sound sound = Sound(buzzerPin, &phase); | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(buzzerPin, OUTPUT); | |
} | |
void loop() { | |
team0Button.update(); | |
team1Button.update(); | |
buttonManager.update(); | |
sound.update(); | |
phase.update(); | |
/** | |
* Below is the code in question. | |
* | |
* The code is OK in the main loop. State 2 is observable because the code within | |
* Button::getState() has access to Button::_lastPressTime | |
* | |
* But the issue is that I need to run this code inside ButtonManager.cpp, not the main loop! | |
* This same code inside ButtonManager.cpp runs but there it cannot access Button::_lastPressTime | |
*/ | |
// if (team0Button.getState() == 0) { | |
// digitalWrite(9, HIGH); | |
// delay(10); | |
// digitalWrite(9, LOW); | |
// delay(60); | |
// } | |
// | |
// else if (team0Button.getState() == 1) { | |
// digitalWrite(9, HIGH); | |
// delay(50); | |
// digitalWrite(9, LOW); | |
// delay(60); | |
// } | |
// | |
// else if (team0Button.getState() == 2) { | |
// digitalWrite(9, HIGH); | |
// delay(300); | |
// digitalWrite(9, LOW); | |
// delay(60); | |
// } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(ARDUINO) && ARDUINO >= 100 | |
#include "Arduino.h" | |
#else | |
#include "WProgram.h" | |
#endif | |
#include "Phase.h" | |
/** | |
* PHASES | |
* * Phases are a combination of preparatory programming steps, and game modes. | |
* * Phases determine how the discreet classes (Button.cpp, Domination.cpp, LED.cpp) | |
* react to changes in game state. | |
* | |
* Phase 0-- Test phase | |
* A test sequence runs, lighting up every LED and testing the buzzer. | |
* Allows user to verify that all LED & sound hardware is functioning properly. | |
* | |
* Phase 1-- Hello phase. | |
* D3VICE network syncronization. D3VICE finds an existing game on the XBee network | |
* if one exists. If no game is found, D3VICE switches to standalone mode and enters programming phase. | |
* | |
* Phase 2-- Programming > Game mode | |
* D3VICE buttons act as input for choosing the game mode | |
* | |
* Phase 3-- Programming > Domination > duration | |
* D3VICE buttons act as input for choosing the total accumulated time a team needs to control the point to win | |
* | |
* Phase 4-- Domination > Run | |
* Phase 5-- Domination > Pause | |
* Phase 6-- Domination > Win | |
* | |
*/ | |
Phase::Phase() | |
{ | |
_phase = 0; | |
_isSwitchedLastTick = 1; // starts as 1 so other classes know phase 0 just started | |
_isPhaseAdvanceQueued = 0; | |
} | |
/** | |
* advance | |
* | |
* queues a phase advancement to happen during the next run of Phase::update() | |
*/ | |
void Phase::advance() | |
{ | |
_isPhaseAdvanceQueued = 1; | |
return; | |
} | |
uint8_t Phase::getCurrentPhase() | |
{ | |
return _phase; | |
} | |
bool Phase::getWasSwitchedLastTick() | |
{ | |
return _isSwitchedLastTick; | |
} | |
void Phase::update() | |
{ | |
if (_isSwitchedLastTick == 1) { | |
_isSwitchedLastTick = 0; | |
} | |
if (_isPhaseAdvanceQueued == 1) { | |
_isPhaseAdvanceQueued = 0; | |
_advance(); | |
} | |
} | |
/** | |
* Phase::_advance() | |
* | |
* Do the natural phase advancement. | |
* Do not call Phase::_advance() function from other classes. Instead call Phase::advance() | |
* | |
* It is possible that phase advancments are not in order (ex: from 1 to 2) | |
* It is possible that order may be more like (ex: 1 to 7) | |
* This is because more phases may be added later on which fall outside natural integer progression. | |
* In this function we can code any order we want, for cases in which advance() would be called from other classes. | |
* It may make more sense to add a separate function for explicitly switching to a specific phase. (ex: Phase::goToPhase(20);) | |
* | |
*/ | |
void Phase::_advance() | |
{ | |
_isSwitchedLastTick = 1; | |
if (_phase == 6) { | |
_phase = 0; | |
} | |
else { | |
_phase += 1; | |
} | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef Phase_h | |
#define Phase_h | |
#if defined(ARDUINO) && ARDUINO >= 100 | |
#include "Arduino.h" | |
#else | |
#include "WProgram.h" | |
#endif | |
/** | |
* For a good time https://www.youtube.com/user/thirdphaseofmoon/ | |
*/ | |
class Phase | |
{ | |
public: | |
Phase(); | |
void advance(); | |
uint8_t getCurrentPhase(); | |
bool getWasSwitchedLastTick(); | |
void update(); | |
private: | |
uint8_t _phase; | |
void _advance(); | |
bool _isSwitchedLastTick; | |
bool _isPhaseAdvanceQueued; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(ARDUINO) && ARDUINO >= 100 | |
#include "Arduino.h" | |
#else | |
#include "WProgram.h" | |
#endif | |
#include "Sound.h" | |
#include "Phase.h" | |
Sound::Sound(uint8_t buzzerPin, Phase* phase) | |
{ | |
_buzzerPin = buzzerPin; | |
_phase = phase; | |
} | |
void Sound::update() | |
{ | |
// if the buzzer is buzzing | |
// if the buzzer needs to stop buzzing | |
// stop the buzzer | |
asyncBeep(); | |
// if this is the first tick of a new phase | |
// start beeping | |
if (_phase->getWasSwitchedLastTick() == 1) { | |
asyncBeep(100); | |
} | |
} | |
void Sound::asyncBeep(uint32_t duration) | |
{ | |
// if the function was supplied a duration, set a timer for that duration and start beeping. | |
_asyncBeepStartTime = millis(); | |
_asyncBeepDuration = duration; | |
digitalWrite(_buzzerPin, HIGH); | |
return; | |
} | |
void Sound::asyncBeep() | |
{ | |
// if the function was not supplied a duration, continue beeping if duration has not expired | |
// otherwise, stop beeping | |
if (millis() - _asyncBeepStartTime > _asyncBeepDuration) { | |
digitalWrite(_buzzerPin, LOW); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef Sound_h | |
#define Sound_h | |
#if defined(ARDUINO) && ARDUINO >= 100 | |
#include "Arduino.h" | |
#else | |
#include "WProgram.h" | |
#endif | |
#include "Phase.h" | |
class Sound | |
{ | |
public: | |
Sound(uint8_t buzzerPin, Phase* phase); | |
void asyncMorse(char character); | |
void asyncBeep(uint32_t duration); | |
void asyncBeep(); | |
void update(); | |
private: | |
uint8_t _buzzerPin; | |
uint32_t _asyncBeepStartTime; | |
uint32_t _asyncMorseStartTime; | |
Phase* _phase; | |
bool _isAsyncMorseComplete; | |
bool _isAsyncBeepComplete; | |
uint32_t _morseDitDuration; | |
uint32_t _morseDahDuration; | |
uint32_t _asyncBeepDuration; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment